d3d11: Make extracting input signature more robust.
[wine.git] / server / device.c
blob8c46a69fc65bf3140bc7673f120bce92f49faf55
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 struct async *async; /* pending async op */
52 irp_params_t params; /* irp parameters */
53 struct iosb *iosb; /* I/O status block */
56 static void irp_call_dump( struct object *obj, int verbose );
57 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry );
58 static void irp_call_destroy( struct object *obj );
60 static const struct object_ops irp_call_ops =
62 sizeof(struct irp_call), /* size */
63 irp_call_dump, /* dump */
64 no_get_type, /* get_type */
65 add_queue, /* add_queue */
66 remove_queue, /* remove_queue */
67 irp_call_signaled, /* signaled */
68 no_satisfied, /* satisfied */
69 no_signal, /* signal */
70 no_get_fd, /* get_fd */
71 no_map_access, /* map_access */
72 default_get_sd, /* get_sd */
73 default_set_sd, /* set_sd */
74 no_lookup_name, /* lookup_name */
75 no_link_name, /* link_name */
76 NULL, /* unlink_name */
77 no_open_file, /* open_file */
78 no_close_handle, /* close_handle */
79 irp_call_destroy /* destroy */
83 /* device manager (a list of devices managed by the same client process) */
85 struct device_manager
87 struct object obj; /* object header */
88 struct list devices; /* list of devices */
89 struct list requests; /* list of pending irps across all devices */
92 static void device_manager_dump( struct object *obj, int verbose );
93 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry );
94 static void device_manager_destroy( struct object *obj );
96 static const struct object_ops device_manager_ops =
98 sizeof(struct device_manager), /* size */
99 device_manager_dump, /* dump */
100 no_get_type, /* get_type */
101 add_queue, /* add_queue */
102 remove_queue, /* remove_queue */
103 device_manager_signaled, /* signaled */
104 no_satisfied, /* satisfied */
105 no_signal, /* signal */
106 no_get_fd, /* get_fd */
107 no_map_access, /* map_access */
108 default_get_sd, /* get_sd */
109 default_set_sd, /* set_sd */
110 no_lookup_name, /* lookup_name */
111 no_link_name, /* link_name */
112 NULL, /* unlink_name */
113 no_open_file, /* open_file */
114 no_close_handle, /* close_handle */
115 device_manager_destroy /* destroy */
119 /* device (a single device object) */
121 struct device
123 struct object obj; /* object header */
124 struct device_manager *manager; /* manager for this device (or NULL if deleted) */
125 char *unix_path; /* path to unix device if any */
126 client_ptr_t user_ptr; /* opaque ptr for client side */
127 struct list entry; /* entry in device manager list */
128 struct list files; /* list of open files */
131 static void device_dump( struct object *obj, int verbose );
132 static struct object_type *device_get_type( struct object *obj );
133 static void device_destroy( struct object *obj );
134 static struct object *device_open_file( struct object *obj, unsigned int access,
135 unsigned int sharing, unsigned int options );
137 static const struct object_ops device_ops =
139 sizeof(struct device), /* size */
140 device_dump, /* dump */
141 device_get_type, /* get_type */
142 no_add_queue, /* add_queue */
143 NULL, /* remove_queue */
144 NULL, /* signaled */
145 no_satisfied, /* satisfied */
146 no_signal, /* signal */
147 no_get_fd, /* get_fd */
148 default_fd_map_access, /* map_access */
149 default_get_sd, /* get_sd */
150 default_set_sd, /* set_sd */
151 no_lookup_name, /* lookup_name */
152 directory_link_name, /* link_name */
153 default_unlink_name, /* unlink_name */
154 device_open_file, /* open_file */
155 no_close_handle, /* close_handle */
156 device_destroy /* destroy */
160 /* device file (an open file handle to a device) */
162 struct device_file
164 struct object obj; /* object header */
165 struct device *device; /* device for this file */
166 struct fd *fd; /* file descriptor for irp */
167 client_ptr_t user_ptr; /* opaque ptr for client side */
168 struct list entry; /* entry in device list */
169 struct list requests; /* list of pending irp requests */
172 static void device_file_dump( struct object *obj, int verbose );
173 static struct fd *device_file_get_fd( struct object *obj );
174 static int device_file_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
175 static void device_file_destroy( struct object *obj );
176 static enum server_fd_type device_file_get_fd_type( struct fd *fd );
177 static obj_handle_t device_file_read( struct fd *fd, struct async *async, int blocking, file_pos_t pos );
178 static obj_handle_t device_file_write( struct fd *fd, struct async *async, int blocking, file_pos_t pos );
179 static obj_handle_t device_file_flush( struct fd *fd, struct async *async, int blocking );
180 static obj_handle_t device_file_ioctl( struct fd *fd, ioctl_code_t code, struct async *async, int blocking );
182 static const struct object_ops device_file_ops =
184 sizeof(struct device_file), /* size */
185 device_file_dump, /* dump */
186 no_get_type, /* get_type */
187 add_queue, /* add_queue */
188 remove_queue, /* remove_queue */
189 default_fd_signaled, /* signaled */
190 no_satisfied, /* satisfied */
191 no_signal, /* signal */
192 device_file_get_fd, /* get_fd */
193 default_fd_map_access, /* map_access */
194 default_get_sd, /* get_sd */
195 default_set_sd, /* set_sd */
196 no_lookup_name, /* lookup_name */
197 no_link_name, /* link_name */
198 NULL, /* unlink_name */
199 no_open_file, /* open_file */
200 device_file_close_handle, /* close_handle */
201 device_file_destroy /* destroy */
204 static const struct fd_ops device_file_fd_ops =
206 default_fd_get_poll_events, /* get_poll_events */
207 default_poll_event, /* poll_event */
208 device_file_get_fd_type, /* get_fd_type */
209 device_file_read, /* read */
210 device_file_write, /* write */
211 device_file_flush, /* flush */
212 device_file_ioctl, /* ioctl */
213 default_fd_queue_async, /* queue_async */
214 default_fd_reselect_async /* reselect_async */
218 static void irp_call_dump( struct object *obj, int verbose )
220 struct irp_call *irp = (struct irp_call *)obj;
221 fprintf( stderr, "IRP call file=%p\n", irp->file );
224 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry )
226 struct irp_call *irp = (struct irp_call *)obj;
228 return !irp->file; /* file is cleared once the irp has completed */
231 static void irp_call_destroy( struct object *obj )
233 struct irp_call *irp = (struct irp_call *)obj;
235 if (irp->async)
237 async_terminate( irp->async, STATUS_CANCELLED );
238 release_object( irp->async );
240 if (irp->iosb) release_object( irp->iosb );
241 if (irp->file) release_object( irp->file );
242 if (irp->thread) release_object( irp->thread );
245 static struct irp_call *create_irp( struct device_file *file, const irp_params_t *params, struct async *async )
247 struct irp_call *irp;
249 if (!file->device->manager) /* it has been deleted */
251 set_error( STATUS_FILE_DELETED );
252 return NULL;
255 if ((irp = alloc_object( &irp_call_ops )))
257 irp->file = (struct device_file *)grab_object( file );
258 irp->thread = NULL;
259 irp->async = NULL;
260 irp->params = *params;
261 irp->iosb = NULL;
263 if (async) irp->iosb = async_get_iosb( async );
264 if (!irp->iosb && !(irp->iosb = create_iosb( NULL, 0, 0 )))
266 release_object( irp );
267 irp = NULL;
270 return irp;
273 static void set_irp_result( struct irp_call *irp, unsigned int status,
274 const void *out_data, data_size_t out_size, data_size_t result )
276 struct device_file *file = irp->file;
277 struct iosb *iosb = irp->iosb;
279 if (!file) return; /* already finished */
281 /* FIXME: handle the STATUS_PENDING case */
282 iosb->status = status;
283 iosb->result = result;
284 iosb->out_size = min( iosb->out_size, out_size );
285 if (iosb->out_size && !(iosb->out_data = memdup( out_data, iosb->out_size )))
286 iosb->out_size = 0;
287 irp->file = NULL;
288 if (irp->async)
290 if (result) status = STATUS_ALERTED;
291 async_terminate( irp->async, status );
292 release_object( irp->async );
293 irp->async = NULL;
295 wake_up( &irp->obj, 0 );
297 /* remove it from the device queue */
298 list_remove( &irp->dev_entry );
299 release_object( irp ); /* no longer on the device queue */
300 release_object( file );
304 static void device_dump( struct object *obj, int verbose )
306 fputs( "Device\n", stderr );
309 static struct object_type *device_get_type( struct object *obj )
311 static const WCHAR name[] = {'D','e','v','i','c','e'};
312 static const struct unicode_str str = { name, sizeof(name) };
313 return get_object_type( &str );
316 static void device_destroy( struct object *obj )
318 struct device *device = (struct device *)obj;
320 assert( list_empty( &device->files ));
322 free( device->unix_path );
323 if (device->manager) list_remove( &device->entry );
326 static void add_irp_to_queue( struct device_file *file, struct irp_call *irp, struct thread *thread )
328 struct device_manager *manager = file->device->manager;
330 assert( manager );
332 grab_object( irp ); /* grab reference for queued irp */
333 irp->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
334 list_add_tail( &file->requests, &irp->dev_entry );
335 list_add_tail( &manager->requests, &irp->mgr_entry );
336 if (list_head( &manager->requests ) == &irp->mgr_entry) wake_up( &manager->obj, 0 ); /* first one */
339 static struct object *device_open_file( struct object *obj, unsigned int access,
340 unsigned int sharing, unsigned int options )
342 struct device *device = (struct device *)obj;
343 struct device_file *file;
345 if (!(file = alloc_object( &device_file_ops ))) return NULL;
347 file->device = (struct device *)grab_object( device );
348 file->user_ptr = 0;
349 list_init( &file->requests );
350 list_add_tail( &device->files, &file->entry );
351 if (device->unix_path)
353 mode_t mode = 0666;
354 access = file->obj.ops->map_access( &file->obj, access );
355 file->fd = open_fd( NULL, device->unix_path, O_NONBLOCK | O_LARGEFILE,
356 &mode, access, sharing, options );
357 if (file->fd) set_fd_user( file->fd, &device_file_fd_ops, &file->obj );
359 else file->fd = alloc_pseudo_fd( &device_file_fd_ops, &file->obj, 0 );
361 if (!file->fd)
363 release_object( file );
364 return NULL;
367 allow_fd_caching( file->fd );
369 if (device->manager)
371 struct irp_call *irp;
372 irp_params_t params;
374 memset( &params, 0, sizeof(params) );
375 params.create.major = IRP_MJ_CREATE;
376 params.create.access = access;
377 params.create.sharing = sharing;
378 params.create.options = options;
379 params.create.device = file->device->user_ptr;
381 if ((irp = create_irp( file, &params, NULL )))
383 add_irp_to_queue( file, irp, NULL );
384 release_object( irp );
387 return &file->obj;
390 static void device_file_dump( struct object *obj, int verbose )
392 struct device_file *file = (struct device_file *)obj;
394 fprintf( stderr, "File on device %p\n", file->device );
397 static struct fd *device_file_get_fd( struct object *obj )
399 struct device_file *file = (struct device_file *)obj;
401 return (struct fd *)grab_object( file->fd );
404 static int device_file_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
406 struct device_file *file = (struct device_file *)obj;
408 if (file->device->manager && obj->handle_count == 1) /* last handle */
410 struct irp_call *irp;
411 irp_params_t params;
413 memset( &params, 0, sizeof(params) );
414 params.close.major = IRP_MJ_CLOSE;
415 params.close.file = file->user_ptr;
417 if ((irp = create_irp( file, &params, NULL )))
419 add_irp_to_queue( file, irp, NULL );
420 release_object( irp );
423 return 1;
426 static void device_file_destroy( struct object *obj )
428 struct device_file *file = (struct device_file *)obj;
429 struct irp_call *irp, *next;
431 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
433 list_remove( &irp->dev_entry );
434 release_object( irp ); /* no longer on the device queue */
436 if (file->fd) release_object( file->fd );
437 list_remove( &file->entry );
438 release_object( file->device );
441 static void set_file_user_ptr( struct device_file *file, client_ptr_t ptr )
443 struct irp_call *irp;
445 if (file->user_ptr == ptr) return; /* nothing to do */
447 file->user_ptr = ptr;
449 /* update already queued irps */
451 LIST_FOR_EACH_ENTRY( irp, &file->requests, struct irp_call, dev_entry )
453 switch (irp->params.major)
455 case IRP_MJ_CLOSE: irp->params.close.file = ptr; break;
456 case IRP_MJ_READ: irp->params.read.file = ptr; break;
457 case IRP_MJ_WRITE: irp->params.write.file = ptr; break;
458 case IRP_MJ_FLUSH_BUFFERS: irp->params.flush.file = ptr; break;
459 case IRP_MJ_DEVICE_CONTROL: irp->params.ioctl.file = ptr; break;
464 /* queue an irp to the device */
465 static obj_handle_t queue_irp( struct device_file *file, struct irp_call *irp,
466 struct async *async, int blocking )
468 obj_handle_t handle = 0;
470 if (blocking && !(handle = alloc_handle( current->process, irp, SYNCHRONIZE, 0 ))) return 0;
472 if (!fd_queue_async( file->fd, async, ASYNC_TYPE_WAIT ))
474 if (handle) close_handle( current->process, handle );
475 return 0;
477 irp->async = (struct async *)grab_object( async );
478 add_irp_to_queue( file, irp, current );
479 set_error( STATUS_PENDING );
480 return handle;
483 static enum server_fd_type device_file_get_fd_type( struct fd *fd )
485 return FD_TYPE_DEVICE;
488 static obj_handle_t device_file_read( struct fd *fd, struct async *async, int blocking, file_pos_t pos )
490 struct device_file *file = get_fd_user( fd );
491 struct irp_call *irp;
492 obj_handle_t handle;
493 irp_params_t params;
495 memset( &params, 0, sizeof(params) );
496 params.read.major = IRP_MJ_READ;
497 params.read.key = 0;
498 params.read.pos = pos;
499 params.read.file = file->user_ptr;
501 irp = create_irp( file, &params, async );
502 if (!irp) return 0;
504 handle = queue_irp( file, irp, async, blocking );
505 release_object( irp );
506 return handle;
509 static obj_handle_t device_file_write( struct fd *fd, struct async *async, int blocking, file_pos_t pos )
511 struct device_file *file = get_fd_user( fd );
512 struct irp_call *irp;
513 obj_handle_t handle;
514 irp_params_t params;
516 memset( &params, 0, sizeof(params) );
517 params.write.major = IRP_MJ_WRITE;
518 params.write.key = 0;
519 params.write.pos = pos;
520 params.write.file = file->user_ptr;
522 irp = create_irp( file, &params, async );
523 if (!irp) return 0;
525 handle = queue_irp( file, irp, async, blocking );
526 release_object( irp );
527 return handle;
530 static obj_handle_t device_file_flush( struct fd *fd, struct async *async, int blocking )
532 struct device_file *file = get_fd_user( fd );
533 struct irp_call *irp;
534 obj_handle_t handle;
535 irp_params_t params;
537 memset( &params, 0, sizeof(params) );
538 params.flush.major = IRP_MJ_FLUSH_BUFFERS;
539 params.flush.file = file->user_ptr;
541 irp = create_irp( file, &params, NULL );
542 if (!irp) return 0;
544 handle = queue_irp( file, irp, async, blocking );
545 release_object( irp );
546 return handle;
549 static obj_handle_t device_file_ioctl( struct fd *fd, ioctl_code_t code, struct async *async,
550 int blocking )
552 struct device_file *file = get_fd_user( fd );
553 struct irp_call *irp;
554 obj_handle_t handle;
555 irp_params_t params;
557 memset( &params, 0, sizeof(params) );
558 params.ioctl.major = IRP_MJ_DEVICE_CONTROL;
559 params.ioctl.code = code;
560 params.ioctl.file = file->user_ptr;
562 irp = create_irp( file, &params, async );
563 if (!irp) return 0;
565 handle = queue_irp( file, irp, async, blocking );
566 release_object( irp );
567 return handle;
570 static struct device *create_device( struct object *root, const struct unicode_str *name,
571 struct device_manager *manager, unsigned int attr )
573 struct device *device;
575 if ((device = create_named_object( root, &device_ops, name, attr, NULL )))
577 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
579 /* initialize it if it didn't already exist */
580 device->unix_path = NULL;
581 device->manager = manager;
582 list_add_tail( &manager->devices, &device->entry );
583 list_init( &device->files );
586 return device;
589 struct object *create_unix_device( struct object *root, const struct unicode_str *name,
590 const char *unix_path )
592 struct device *device;
594 if ((device = create_named_object( root, &device_ops, name, 0, NULL )))
596 device->unix_path = strdup( unix_path );
597 device->manager = NULL; /* no manager, requests go straight to the Unix device */
598 list_init( &device->files );
600 return &device->obj;
604 /* terminate requests when the underlying device is deleted */
605 static void delete_file( struct device_file *file )
607 struct irp_call *irp, *next;
609 /* terminate all pending requests */
610 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
612 list_remove( &irp->mgr_entry );
613 set_irp_result( irp, STATUS_FILE_DELETED, NULL, 0, 0 );
617 static void delete_device( struct device *device )
619 struct device_file *file, *next;
621 if (!device->manager) return; /* already deleted */
623 LIST_FOR_EACH_ENTRY_SAFE( file, next, &device->files, struct device_file, entry )
624 delete_file( file );
626 unlink_named_object( &device->obj );
627 list_remove( &device->entry );
628 device->manager = NULL;
632 static void device_manager_dump( struct object *obj, int verbose )
634 fprintf( stderr, "Device manager\n" );
637 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry )
639 struct device_manager *manager = (struct device_manager *)obj;
641 return !list_empty( &manager->requests );
644 static void device_manager_destroy( struct object *obj )
646 struct device_manager *manager = (struct device_manager *)obj;
647 struct list *ptr;
649 while ((ptr = list_head( &manager->devices )))
651 struct device *device = LIST_ENTRY( ptr, struct device, entry );
652 delete_device( device );
656 static struct device_manager *create_device_manager(void)
658 struct device_manager *manager;
660 if ((manager = alloc_object( &device_manager_ops )))
662 list_init( &manager->devices );
663 list_init( &manager->requests );
665 return manager;
669 /* create a device manager */
670 DECL_HANDLER(create_device_manager)
672 struct device_manager *manager = create_device_manager();
674 if (manager)
676 reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
677 release_object( manager );
682 /* create a device */
683 DECL_HANDLER(create_device)
685 struct device *device;
686 struct unicode_str name = get_req_unicode_str();
687 struct device_manager *manager;
688 struct object *root = NULL;
690 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
691 0, &device_manager_ops )))
692 return;
694 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir )))
696 release_object( manager );
697 return;
700 if ((device = create_device( root, &name, manager, req->attributes )))
702 device->user_ptr = req->user_ptr;
703 reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
704 release_object( device );
707 if (root) release_object( root );
708 release_object( manager );
712 /* delete a device */
713 DECL_HANDLER(delete_device)
715 struct device *device;
717 if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
719 delete_device( device );
720 release_object( device );
725 /* retrieve the next pending device irp request */
726 DECL_HANDLER(get_next_device_request)
728 struct irp_call *irp;
729 struct device_manager *manager;
730 struct list *ptr;
731 struct iosb *iosb;
733 reply->params.major = IRP_MJ_MAXIMUM_FUNCTION + 1;
735 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
736 0, &device_manager_ops )))
737 return;
739 if (req->prev)
741 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->prev, 0, &irp_call_ops )))
743 set_irp_result( irp, req->status, NULL, 0, 0 );
744 close_handle( current->process, req->prev ); /* avoid an extra round-trip for close */
745 release_object( irp );
747 clear_error();
750 if ((ptr = list_head( &manager->requests )))
752 irp = LIST_ENTRY( ptr, struct irp_call, mgr_entry );
753 if (irp->thread)
755 reply->client_pid = get_process_id( irp->thread->process );
756 reply->client_tid = get_thread_id( irp->thread );
758 reply->params = irp->params;
759 iosb = irp->iosb;
760 reply->in_size = iosb->in_size;
761 reply->out_size = iosb->out_size;
762 if (iosb->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
763 else if ((reply->next = alloc_handle( current->process, irp, 0, 0 )))
765 set_reply_data_ptr( iosb->in_data, iosb->in_size );
766 iosb->in_data = NULL;
767 iosb->in_size = 0;
768 list_remove( &irp->mgr_entry );
769 list_init( &irp->mgr_entry );
772 else set_error( STATUS_PENDING );
774 release_object( manager );
778 /* store results of an async irp */
779 DECL_HANDLER(set_irp_result)
781 struct irp_call *irp;
783 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->handle, 0, &irp_call_ops )))
785 if (irp->file) set_file_user_ptr( irp->file, req->file_ptr );
786 set_irp_result( irp, req->status, get_req_data(), get_req_data_size(), req->size );
787 close_handle( current->process, req->handle ); /* avoid an extra round-trip for close */
788 release_object( irp );