winemac: Make some operations on Cocoa views asynchronous.
[wine.git] / server / device.c
bloba7dce3bb78dd9f9341c9ac64f75547a823e44bd2
1 /*
2 * Server-side device support
4 * Copyright (C) 2007 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winternl.h"
34 #include "ddk/wdm.h"
36 #include "object.h"
37 #include "file.h"
38 #include "handle.h"
39 #include "request.h"
40 #include "process.h"
42 /* IRP object */
44 struct irp_call
46 struct object obj; /* object header */
47 struct list dev_entry; /* entry in device queue */
48 struct list mgr_entry; /* entry in manager queue */
49 struct device_file *file; /* file containing this irp */
50 struct thread *thread; /* thread that queued the irp */
51 client_ptr_t user_arg; /* user arg used to identify the request */
52 struct async *async; /* pending async op */
53 irp_params_t params; /* irp parameters */
54 struct iosb *iosb; /* I/O status block */
57 static void irp_call_dump( struct object *obj, int verbose );
58 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry );
59 static void irp_call_destroy( struct object *obj );
61 static const struct object_ops irp_call_ops =
63 sizeof(struct irp_call), /* size */
64 irp_call_dump, /* dump */
65 no_get_type, /* get_type */
66 add_queue, /* add_queue */
67 remove_queue, /* remove_queue */
68 irp_call_signaled, /* signaled */
69 no_satisfied, /* satisfied */
70 no_signal, /* signal */
71 no_get_fd, /* get_fd */
72 no_map_access, /* map_access */
73 default_get_sd, /* get_sd */
74 default_set_sd, /* set_sd */
75 no_lookup_name, /* lookup_name */
76 no_link_name, /* link_name */
77 NULL, /* unlink_name */
78 no_open_file, /* open_file */
79 no_close_handle, /* close_handle */
80 irp_call_destroy /* destroy */
84 /* device manager (a list of devices managed by the same client process) */
86 struct device_manager
88 struct object obj; /* object header */
89 struct list devices; /* list of devices */
90 struct list requests; /* list of pending irps across all devices */
93 static void device_manager_dump( struct object *obj, int verbose );
94 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry );
95 static void device_manager_destroy( struct object *obj );
97 static const struct object_ops device_manager_ops =
99 sizeof(struct device_manager), /* size */
100 device_manager_dump, /* dump */
101 no_get_type, /* get_type */
102 add_queue, /* add_queue */
103 remove_queue, /* remove_queue */
104 device_manager_signaled, /* signaled */
105 no_satisfied, /* satisfied */
106 no_signal, /* signal */
107 no_get_fd, /* get_fd */
108 no_map_access, /* map_access */
109 default_get_sd, /* get_sd */
110 default_set_sd, /* set_sd */
111 no_lookup_name, /* lookup_name */
112 no_link_name, /* link_name */
113 NULL, /* unlink_name */
114 no_open_file, /* open_file */
115 no_close_handle, /* close_handle */
116 device_manager_destroy /* destroy */
120 /* device (a single device object) */
122 struct device
124 struct object obj; /* object header */
125 struct device_manager *manager; /* manager for this device (or NULL if deleted) */
126 char *unix_path; /* path to unix device if any */
127 client_ptr_t user_ptr; /* opaque ptr for client side */
128 struct list entry; /* entry in device manager list */
129 struct list files; /* list of open files */
132 static void device_dump( struct object *obj, int verbose );
133 static struct object_type *device_get_type( struct object *obj );
134 static void device_destroy( struct object *obj );
135 static struct object *device_open_file( struct object *obj, unsigned int access,
136 unsigned int sharing, unsigned int options );
138 static const struct object_ops device_ops =
140 sizeof(struct device), /* size */
141 device_dump, /* dump */
142 device_get_type, /* get_type */
143 no_add_queue, /* add_queue */
144 NULL, /* remove_queue */
145 NULL, /* signaled */
146 no_satisfied, /* satisfied */
147 no_signal, /* signal */
148 no_get_fd, /* get_fd */
149 default_fd_map_access, /* map_access */
150 default_get_sd, /* get_sd */
151 default_set_sd, /* set_sd */
152 no_lookup_name, /* lookup_name */
153 directory_link_name, /* link_name */
154 default_unlink_name, /* unlink_name */
155 device_open_file, /* open_file */
156 no_close_handle, /* close_handle */
157 device_destroy /* destroy */
161 /* device file (an open file handle to a device) */
163 struct device_file
165 struct object obj; /* object header */
166 struct device *device; /* device for this file */
167 struct fd *fd; /* file descriptor for irp */
168 client_ptr_t user_ptr; /* opaque ptr for client side */
169 struct list entry; /* entry in device list */
170 struct list requests; /* list of pending irp requests */
173 static void device_file_dump( struct object *obj, int verbose );
174 static struct fd *device_file_get_fd( struct object *obj );
175 static int device_file_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
176 static void device_file_destroy( struct object *obj );
177 static enum server_fd_type device_file_get_fd_type( struct fd *fd );
178 static obj_handle_t device_file_read( struct fd *fd, const async_data_t *async_data, int blocking,
179 file_pos_t pos, struct iosb *iosb );
180 static obj_handle_t device_file_write( struct fd *fd, const async_data_t *async_data, int blocking,
181 file_pos_t pos, struct iosb *iosb );
182 static obj_handle_t device_file_flush( struct fd *fd, const async_data_t *async_data, int blocking );
183 static obj_handle_t device_file_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
184 int blocking );
186 static const struct object_ops device_file_ops =
188 sizeof(struct device_file), /* size */
189 device_file_dump, /* dump */
190 no_get_type, /* get_type */
191 add_queue, /* add_queue */
192 remove_queue, /* remove_queue */
193 default_fd_signaled, /* signaled */
194 no_satisfied, /* satisfied */
195 no_signal, /* signal */
196 device_file_get_fd, /* get_fd */
197 default_fd_map_access, /* map_access */
198 default_get_sd, /* get_sd */
199 default_set_sd, /* set_sd */
200 no_lookup_name, /* lookup_name */
201 no_link_name, /* link_name */
202 NULL, /* unlink_name */
203 no_open_file, /* open_file */
204 device_file_close_handle, /* close_handle */
205 device_file_destroy /* destroy */
208 static const struct fd_ops device_file_fd_ops =
210 default_fd_get_poll_events, /* get_poll_events */
211 default_poll_event, /* poll_event */
212 device_file_get_fd_type, /* get_fd_type */
213 device_file_read, /* read */
214 device_file_write, /* write */
215 device_file_flush, /* flush */
216 device_file_ioctl, /* ioctl */
217 default_fd_queue_async, /* queue_async */
218 default_fd_reselect_async /* reselect_async */
222 static void irp_call_dump( struct object *obj, int verbose )
224 struct irp_call *irp = (struct irp_call *)obj;
225 fprintf( stderr, "IRP call file=%p\n", irp->file );
228 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry )
230 struct irp_call *irp = (struct irp_call *)obj;
232 return !irp->file; /* file is cleared once the irp has completed */
235 static void irp_call_destroy( struct object *obj )
237 struct irp_call *irp = (struct irp_call *)obj;
239 if (irp->async)
241 async_terminate( irp->async, STATUS_CANCELLED );
242 release_object( irp->async );
244 if (irp->iosb) release_object( irp->iosb );
245 if (irp->file) release_object( irp->file );
246 if (irp->thread) release_object( irp->thread );
249 static struct irp_call *create_irp( struct device_file *file, const irp_params_t *params, struct iosb *iosb )
251 struct irp_call *irp;
253 if (!file->device->manager) /* it has been deleted */
255 set_error( STATUS_FILE_DELETED );
256 return NULL;
259 if ((irp = alloc_object( &irp_call_ops )))
261 irp->file = (struct device_file *)grab_object( file );
262 irp->thread = NULL;
263 irp->async = NULL;
264 irp->params = *params;
266 if (iosb)
268 irp->iosb = (struct iosb *)grab_object( iosb );
270 else if (!(irp->iosb = create_iosb( NULL, 0, 0 )))
272 release_object( irp );
273 irp = NULL;
276 return irp;
279 static void set_irp_result( struct irp_call *irp, unsigned int status,
280 const void *out_data, data_size_t out_size, data_size_t result )
282 struct device_file *file = irp->file;
283 struct iosb *iosb = irp->iosb;
285 if (!file) return; /* already finished */
287 /* FIXME: handle the STATUS_PENDING case */
288 iosb->status = status;
289 iosb->result = result;
290 iosb->out_size = min( iosb->out_size, out_size );
291 if (iosb->out_size && !(iosb->out_data = memdup( out_data, iosb->out_size )))
292 iosb->out_size = 0;
293 irp->file = NULL;
294 if (irp->async)
296 if (result) status = STATUS_ALERTED;
297 async_terminate( irp->async, status );
298 release_object( irp->async );
299 irp->async = NULL;
301 wake_up( &irp->obj, 0 );
303 /* remove it from the device queue */
304 list_remove( &irp->dev_entry );
305 release_object( irp ); /* no longer on the device queue */
306 release_object( file );
310 static void device_dump( struct object *obj, int verbose )
312 fputs( "Device\n", stderr );
315 static struct object_type *device_get_type( struct object *obj )
317 static const WCHAR name[] = {'D','e','v','i','c','e'};
318 static const struct unicode_str str = { name, sizeof(name) };
319 return get_object_type( &str );
322 static void device_destroy( struct object *obj )
324 struct device *device = (struct device *)obj;
326 assert( list_empty( &device->files ));
328 free( device->unix_path );
329 if (device->manager) list_remove( &device->entry );
332 static void add_irp_to_queue( struct device_file *file, struct irp_call *irp, struct thread *thread )
334 struct device_manager *manager = file->device->manager;
336 assert( manager );
338 grab_object( irp ); /* grab reference for queued irp */
339 irp->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
340 list_add_tail( &file->requests, &irp->dev_entry );
341 list_add_tail( &manager->requests, &irp->mgr_entry );
342 if (list_head( &manager->requests ) == &irp->mgr_entry) wake_up( &manager->obj, 0 ); /* first one */
345 static struct object *device_open_file( struct object *obj, unsigned int access,
346 unsigned int sharing, unsigned int options )
348 struct device *device = (struct device *)obj;
349 struct device_file *file;
351 if (!(file = alloc_object( &device_file_ops ))) return NULL;
353 file->device = (struct device *)grab_object( device );
354 file->user_ptr = 0;
355 list_init( &file->requests );
356 list_add_tail( &device->files, &file->entry );
357 if (device->unix_path)
359 mode_t mode = 0666;
360 access = file->obj.ops->map_access( &file->obj, access );
361 file->fd = open_fd( NULL, device->unix_path, O_NONBLOCK | O_LARGEFILE,
362 &mode, access, sharing, options );
363 if (file->fd) set_fd_user( file->fd, &device_file_fd_ops, &file->obj );
365 else file->fd = alloc_pseudo_fd( &device_file_fd_ops, &file->obj, 0 );
367 if (!file->fd)
369 release_object( file );
370 return NULL;
373 allow_fd_caching( file->fd );
375 if (device->manager)
377 struct irp_call *irp;
378 irp_params_t params;
380 memset( &params, 0, sizeof(params) );
381 params.create.major = IRP_MJ_CREATE;
382 params.create.access = access;
383 params.create.sharing = sharing;
384 params.create.options = options;
385 params.create.device = file->device->user_ptr;
387 if ((irp = create_irp( file, &params, NULL )))
389 add_irp_to_queue( file, irp, NULL );
390 release_object( irp );
393 return &file->obj;
396 static void device_file_dump( struct object *obj, int verbose )
398 struct device_file *file = (struct device_file *)obj;
400 fprintf( stderr, "File on device %p\n", file->device );
403 static struct fd *device_file_get_fd( struct object *obj )
405 struct device_file *file = (struct device_file *)obj;
407 return (struct fd *)grab_object( file->fd );
410 static int device_file_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
412 struct device_file *file = (struct device_file *)obj;
414 if (file->device->manager && obj->handle_count == 1) /* last handle */
416 struct irp_call *irp;
417 irp_params_t params;
419 memset( &params, 0, sizeof(params) );
420 params.close.major = IRP_MJ_CLOSE;
421 params.close.file = file->user_ptr;
423 if ((irp = create_irp( file, &params, NULL )))
425 add_irp_to_queue( file, irp, NULL );
426 release_object( irp );
429 return 1;
432 static void device_file_destroy( struct object *obj )
434 struct device_file *file = (struct device_file *)obj;
435 struct irp_call *irp, *next;
437 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
439 list_remove( &irp->dev_entry );
440 release_object( irp ); /* no longer on the device queue */
442 if (file->fd) release_object( file->fd );
443 list_remove( &file->entry );
444 release_object( file->device );
447 static void set_file_user_ptr( struct device_file *file, client_ptr_t ptr )
449 struct irp_call *irp;
451 if (file->user_ptr == ptr) return; /* nothing to do */
453 file->user_ptr = ptr;
455 /* update already queued irps */
457 LIST_FOR_EACH_ENTRY( irp, &file->requests, struct irp_call, dev_entry )
459 switch (irp->params.major)
461 case IRP_MJ_CLOSE: irp->params.close.file = ptr; break;
462 case IRP_MJ_READ: irp->params.read.file = ptr; break;
463 case IRP_MJ_WRITE: irp->params.write.file = ptr; break;
464 case IRP_MJ_FLUSH_BUFFERS: irp->params.flush.file = ptr; break;
465 case IRP_MJ_DEVICE_CONTROL: irp->params.ioctl.file = ptr; break;
470 /* queue an irp to the device */
471 static obj_handle_t queue_irp( struct device_file *file, struct irp_call *irp,
472 const async_data_t *async_data, int blocking )
474 obj_handle_t handle = 0;
476 if (blocking && !(handle = alloc_handle( current->process, irp, SYNCHRONIZE, 0 ))) return 0;
478 if (!(irp->async = fd_queue_async( file->fd, async_data, irp->iosb, ASYNC_TYPE_WAIT )))
480 if (handle) close_handle( current->process, handle );
481 return 0;
483 irp->user_arg = async_data->arg;
484 add_irp_to_queue( file, irp, current );
485 set_error( STATUS_PENDING );
486 return handle;
489 static enum server_fd_type device_file_get_fd_type( struct fd *fd )
491 return FD_TYPE_DEVICE;
494 static obj_handle_t device_file_read( struct fd *fd, const async_data_t *async_data, int blocking,
495 file_pos_t pos, struct iosb *iosb )
497 struct device_file *file = get_fd_user( fd );
498 struct irp_call *irp;
499 obj_handle_t handle;
500 irp_params_t params;
502 memset( &params, 0, sizeof(params) );
503 params.read.major = IRP_MJ_READ;
504 params.read.key = 0;
505 params.read.pos = pos;
506 params.read.file = file->user_ptr;
508 irp = create_irp( file, &params, iosb );
509 if (!irp) return 0;
511 handle = queue_irp( file, irp, async_data, blocking );
512 release_object( irp );
513 return handle;
516 static obj_handle_t device_file_write( struct fd *fd, const async_data_t *async_data, int blocking,
517 file_pos_t pos, struct iosb *iosb )
519 struct device_file *file = get_fd_user( fd );
520 struct irp_call *irp;
521 obj_handle_t handle;
522 irp_params_t params;
524 memset( &params, 0, sizeof(params) );
525 params.write.major = IRP_MJ_WRITE;
526 params.write.key = 0;
527 params.write.pos = pos;
528 params.write.file = file->user_ptr;
530 irp = create_irp( file, &params, iosb );
531 if (!irp) return 0;
533 handle = queue_irp( file, irp, async_data, blocking );
534 release_object( irp );
535 return handle;
538 static obj_handle_t device_file_flush( struct fd *fd, const async_data_t *async_data, int blocking )
540 struct device_file *file = get_fd_user( fd );
541 struct irp_call *irp;
542 obj_handle_t handle;
543 irp_params_t params;
545 memset( &params, 0, sizeof(params) );
546 params.flush.major = IRP_MJ_FLUSH_BUFFERS;
547 params.flush.file = file->user_ptr;
549 irp = create_irp( file, &params, NULL );
550 if (!irp) return 0;
552 handle = queue_irp( file, irp, async_data, blocking );
553 release_object( irp );
554 return handle;
557 static obj_handle_t device_file_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
558 int blocking )
560 struct device_file *file = get_fd_user( fd );
561 struct irp_call *irp;
562 obj_handle_t handle;
563 irp_params_t params;
564 struct iosb *iosb;
566 memset( &params, 0, sizeof(params) );
567 params.ioctl.major = IRP_MJ_DEVICE_CONTROL;
568 params.ioctl.code = code;
569 params.ioctl.file = file->user_ptr;
571 iosb = create_iosb( get_req_data(), get_req_data_size(), get_reply_max_size() );
572 if (!iosb) return 0;
573 irp = create_irp( file, &params, iosb );
574 release_object(iosb);
575 if (!irp) return 0;
577 handle = queue_irp( file, irp, async_data, blocking );
578 release_object( irp );
579 return handle;
582 static struct device *create_device( struct object *root, const struct unicode_str *name,
583 struct device_manager *manager, unsigned int attr )
585 struct device *device;
587 if ((device = create_named_object( root, &device_ops, name, attr, NULL )))
589 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
591 /* initialize it if it didn't already exist */
592 device->unix_path = NULL;
593 device->manager = manager;
594 list_add_tail( &manager->devices, &device->entry );
595 list_init( &device->files );
598 return device;
601 struct object *create_unix_device( struct object *root, const struct unicode_str *name,
602 const char *unix_path )
604 struct device *device;
606 if ((device = create_named_object( root, &device_ops, name, 0, NULL )))
608 device->unix_path = strdup( unix_path );
609 device->manager = NULL; /* no manager, requests go straight to the Unix device */
610 list_init( &device->files );
612 return &device->obj;
616 /* terminate requests when the underlying device is deleted */
617 static void delete_file( struct device_file *file )
619 struct irp_call *irp, *next;
621 /* terminate all pending requests */
622 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
624 list_remove( &irp->mgr_entry );
625 set_irp_result( irp, STATUS_FILE_DELETED, NULL, 0, 0 );
629 static void delete_device( struct device *device )
631 struct device_file *file, *next;
633 if (!device->manager) return; /* already deleted */
635 LIST_FOR_EACH_ENTRY_SAFE( file, next, &device->files, struct device_file, entry )
636 delete_file( file );
638 unlink_named_object( &device->obj );
639 list_remove( &device->entry );
640 device->manager = NULL;
644 static void device_manager_dump( struct object *obj, int verbose )
646 fprintf( stderr, "Device manager\n" );
649 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry )
651 struct device_manager *manager = (struct device_manager *)obj;
653 return !list_empty( &manager->requests );
656 static void device_manager_destroy( struct object *obj )
658 struct device_manager *manager = (struct device_manager *)obj;
659 struct list *ptr;
661 while ((ptr = list_head( &manager->devices )))
663 struct device *device = LIST_ENTRY( ptr, struct device, entry );
664 delete_device( device );
668 static struct device_manager *create_device_manager(void)
670 struct device_manager *manager;
672 if ((manager = alloc_object( &device_manager_ops )))
674 list_init( &manager->devices );
675 list_init( &manager->requests );
677 return manager;
681 /* create a device manager */
682 DECL_HANDLER(create_device_manager)
684 struct device_manager *manager = create_device_manager();
686 if (manager)
688 reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
689 release_object( manager );
694 /* create a device */
695 DECL_HANDLER(create_device)
697 struct device *device;
698 struct unicode_str name = get_req_unicode_str();
699 struct device_manager *manager;
700 struct object *root = NULL;
702 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
703 0, &device_manager_ops )))
704 return;
706 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir )))
708 release_object( manager );
709 return;
712 if ((device = create_device( root, &name, manager, req->attributes )))
714 device->user_ptr = req->user_ptr;
715 reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
716 release_object( device );
719 if (root) release_object( root );
720 release_object( manager );
724 /* delete a device */
725 DECL_HANDLER(delete_device)
727 struct device *device;
729 if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
731 delete_device( device );
732 release_object( device );
737 /* retrieve the next pending device irp request */
738 DECL_HANDLER(get_next_device_request)
740 struct irp_call *irp;
741 struct device_manager *manager;
742 struct list *ptr;
743 struct iosb *iosb;
745 reply->params.major = IRP_MJ_MAXIMUM_FUNCTION + 1;
747 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
748 0, &device_manager_ops )))
749 return;
751 if (req->prev)
753 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->prev, 0, &irp_call_ops )))
755 set_irp_result( irp, req->status, NULL, 0, 0 );
756 close_handle( current->process, req->prev ); /* avoid an extra round-trip for close */
757 release_object( irp );
759 clear_error();
762 if ((ptr = list_head( &manager->requests )))
764 irp = LIST_ENTRY( ptr, struct irp_call, mgr_entry );
765 if (irp->thread)
767 reply->client_pid = get_process_id( irp->thread->process );
768 reply->client_tid = get_thread_id( irp->thread );
770 reply->params = irp->params;
771 iosb = irp->iosb;
772 reply->in_size = iosb->in_size;
773 reply->out_size = iosb->out_size;
774 if (iosb->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
775 else if ((reply->next = alloc_handle( current->process, irp, 0, 0 )))
777 set_reply_data_ptr( iosb->in_data, iosb->in_size );
778 iosb->in_data = NULL;
779 iosb->in_size = 0;
780 list_remove( &irp->mgr_entry );
781 list_init( &irp->mgr_entry );
784 else set_error( STATUS_PENDING );
786 release_object( manager );
790 /* store results of an async irp */
791 DECL_HANDLER(set_irp_result)
793 struct irp_call *irp;
795 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->handle, 0, &irp_call_ops )))
797 if (irp->file) set_file_user_ptr( irp->file, req->file_ptr );
798 set_irp_result( irp, req->status, get_req_data(), get_req_data_size(), req->size );
799 close_handle( current->process, req->handle ); /* avoid an extra round-trip for close */
800 release_object( irp );