wmiutils: Use --prefer-native instead of DLL_WINE_PREATTACH.
[wine/zf.git] / server / device.c
blob6400751e33984157be7ecb1df41f321accf125c7
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"
23 #include "wine/rbtree.h"
25 #include <assert.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
31 #include "ntstatus.h"
32 #define WIN32_NO_STATUS
33 #include "windef.h"
34 #include "winternl.h"
35 #include "ddk/wdm.h"
37 #include "object.h"
38 #include "file.h"
39 #include "handle.h"
40 #include "request.h"
41 #include "process.h"
43 /* IRP object */
45 struct irp_call
47 struct object obj; /* object header */
48 struct list dev_entry; /* entry in device queue */
49 struct list mgr_entry; /* entry in manager queue */
50 struct device_file *file; /* file containing this irp */
51 struct thread *thread; /* thread that queued the irp */
52 struct async *async; /* pending async op */
53 irp_params_t params; /* irp parameters */
54 struct iosb *iosb; /* I/O status block */
55 int canceled; /* the call was canceled */
56 client_ptr_t user_ptr; /* client side pointer */
59 static void irp_call_dump( struct object *obj, int verbose );
60 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry );
61 static void irp_call_destroy( struct object *obj );
63 static const struct object_ops irp_call_ops =
65 sizeof(struct irp_call), /* size */
66 &no_type, /* type */
67 irp_call_dump, /* dump */
68 add_queue, /* add_queue */
69 remove_queue, /* remove_queue */
70 irp_call_signaled, /* signaled */
71 no_satisfied, /* satisfied */
72 no_signal, /* signal */
73 no_get_fd, /* get_fd */
74 default_map_access, /* map_access */
75 default_get_sd, /* get_sd */
76 default_set_sd, /* set_sd */
77 no_get_full_name, /* get_full_name */
78 no_lookup_name, /* lookup_name */
79 no_link_name, /* link_name */
80 NULL, /* unlink_name */
81 no_open_file, /* open_file */
82 no_kernel_obj_list, /* get_kernel_obj_list */
83 no_close_handle, /* close_handle */
84 irp_call_destroy /* destroy */
88 /* device manager (a list of devices managed by the same client process) */
90 struct device_manager
92 struct object obj; /* object header */
93 struct list devices; /* list of devices */
94 struct list requests; /* list of pending irps across all devices */
95 struct irp_call *current_call; /* call currently executed on client side */
96 struct wine_rb_tree kernel_objects; /* map of objects that have client side pointer associated */
99 static void device_manager_dump( struct object *obj, int verbose );
100 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry );
101 static void device_manager_destroy( struct object *obj );
103 static const struct object_ops device_manager_ops =
105 sizeof(struct device_manager), /* size */
106 &no_type, /* type */
107 device_manager_dump, /* dump */
108 add_queue, /* add_queue */
109 remove_queue, /* remove_queue */
110 device_manager_signaled, /* signaled */
111 no_satisfied, /* satisfied */
112 no_signal, /* signal */
113 no_get_fd, /* get_fd */
114 default_map_access, /* map_access */
115 default_get_sd, /* get_sd */
116 default_set_sd, /* set_sd */
117 no_get_full_name, /* get_full_name */
118 no_lookup_name, /* lookup_name */
119 no_link_name, /* link_name */
120 NULL, /* unlink_name */
121 no_open_file, /* open_file */
122 no_kernel_obj_list, /* get_kernel_obj_list */
123 no_close_handle, /* close_handle */
124 device_manager_destroy /* destroy */
128 /* device (a single device object) */
130 static const WCHAR device_name[] = {'D','e','v','i','c','e'};
132 struct type_descr device_type =
134 { device_name, sizeof(device_name) }, /* name */
135 FILE_ALL_ACCESS, /* valid_access */
136 { /* mapping */
137 FILE_GENERIC_READ,
138 FILE_GENERIC_WRITE,
139 FILE_GENERIC_EXECUTE,
140 FILE_ALL_ACCESS
144 struct device
146 struct object obj; /* object header */
147 struct device_manager *manager; /* manager for this device (or NULL if deleted) */
148 char *unix_path; /* path to unix device if any */
149 struct list kernel_object; /* list of kernel object pointers */
150 struct list entry; /* entry in device manager list */
151 struct list files; /* list of open files */
154 static void device_dump( struct object *obj, int verbose );
155 static void device_destroy( struct object *obj );
156 static struct object *device_open_file( struct object *obj, unsigned int access,
157 unsigned int sharing, unsigned int options );
158 static struct list *device_get_kernel_obj_list( struct object *obj );
160 static const struct object_ops device_ops =
162 sizeof(struct device), /* size */
163 &device_type, /* type */
164 device_dump, /* dump */
165 no_add_queue, /* add_queue */
166 NULL, /* remove_queue */
167 NULL, /* signaled */
168 no_satisfied, /* satisfied */
169 no_signal, /* signal */
170 no_get_fd, /* get_fd */
171 default_map_access, /* map_access */
172 default_get_sd, /* get_sd */
173 default_set_sd, /* set_sd */
174 default_get_full_name, /* get_full_name */
175 no_lookup_name, /* lookup_name */
176 directory_link_name, /* link_name */
177 default_unlink_name, /* unlink_name */
178 device_open_file, /* open_file */
179 device_get_kernel_obj_list, /* get_kernel_obj_list */
180 no_close_handle, /* close_handle */
181 device_destroy /* destroy */
185 /* device file (an open file handle to a device) */
187 struct device_file
189 struct object obj; /* object header */
190 struct device *device; /* device for this file */
191 struct fd *fd; /* file descriptor for irp */
192 struct list kernel_object; /* list of kernel object pointers */
193 int closed; /* closed file flag */
194 struct list entry; /* entry in device list */
195 struct list requests; /* list of pending irp requests */
198 static void device_file_dump( struct object *obj, int verbose );
199 static struct fd *device_file_get_fd( struct object *obj );
200 static WCHAR *device_file_get_full_name( struct object *obj, data_size_t *len );
201 static struct list *device_file_get_kernel_obj_list( struct object *obj );
202 static int device_file_close_handle( struct object *obj, struct process *process, obj_handle_t handle );
203 static void device_file_destroy( struct object *obj );
204 static enum server_fd_type device_file_get_fd_type( struct fd *fd );
205 static int device_file_read( struct fd *fd, struct async *async, file_pos_t pos );
206 static int device_file_write( struct fd *fd, struct async *async, file_pos_t pos );
207 static int device_file_flush( struct fd *fd, struct async *async );
208 static int device_file_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
209 static void device_file_reselect_async( struct fd *fd, struct async_queue *queue );
210 static int device_file_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class );
212 static const struct object_ops device_file_ops =
214 sizeof(struct device_file), /* size */
215 &file_type, /* type */
216 device_file_dump, /* dump */
217 add_queue, /* add_queue */
218 remove_queue, /* remove_queue */
219 default_fd_signaled, /* signaled */
220 no_satisfied, /* satisfied */
221 no_signal, /* signal */
222 device_file_get_fd, /* get_fd */
223 default_map_access, /* map_access */
224 default_get_sd, /* get_sd */
225 default_set_sd, /* set_sd */
226 device_file_get_full_name, /* get_full_name */
227 no_lookup_name, /* lookup_name */
228 no_link_name, /* link_name */
229 NULL, /* unlink_name */
230 no_open_file, /* open_file */
231 device_file_get_kernel_obj_list, /* get_kernel_obj_list */
232 device_file_close_handle, /* close_handle */
233 device_file_destroy /* destroy */
236 static const struct fd_ops device_file_fd_ops =
238 default_fd_get_poll_events, /* get_poll_events */
239 default_poll_event, /* poll_event */
240 device_file_get_fd_type, /* get_fd_type */
241 device_file_read, /* read */
242 device_file_write, /* write */
243 device_file_flush, /* flush */
244 default_fd_get_file_info, /* get_file_info */
245 device_file_get_volume_info, /* get_volume_info */
246 device_file_ioctl, /* ioctl */
247 default_fd_queue_async, /* queue_async */
248 device_file_reselect_async /* reselect_async */
252 struct list *no_kernel_obj_list( struct object *obj )
254 return NULL;
257 struct kernel_object
259 struct device_manager *manager;
260 client_ptr_t user_ptr;
261 struct object *object;
262 int owned;
263 struct list list_entry;
264 struct wine_rb_entry rb_entry;
267 static int compare_kernel_object( const void *k, const struct wine_rb_entry *entry )
269 struct kernel_object *ptr = WINE_RB_ENTRY_VALUE( entry, struct kernel_object, rb_entry );
270 return memcmp( k, &ptr->user_ptr, sizeof(client_ptr_t) );
273 static struct kernel_object *kernel_object_from_obj( struct device_manager *manager, struct object *obj )
275 struct kernel_object *kernel_object;
276 struct list *list;
278 if (!(list = obj->ops->get_kernel_obj_list( obj ))) return NULL;
279 LIST_FOR_EACH_ENTRY( kernel_object, list, struct kernel_object, list_entry )
281 if (kernel_object->manager != manager) continue;
282 return kernel_object;
284 return NULL;
287 static client_ptr_t get_kernel_object_ptr( struct device_manager *manager, struct object *obj )
289 struct kernel_object *kernel_object = kernel_object_from_obj( manager, obj );
290 return kernel_object ? kernel_object->user_ptr : 0;
293 static struct kernel_object *set_kernel_object( struct device_manager *manager, struct object *obj, client_ptr_t user_ptr )
295 struct kernel_object *kernel_object;
296 struct list *list;
298 if (!(list = obj->ops->get_kernel_obj_list( obj ))) return NULL;
300 if (!(kernel_object = malloc( sizeof(*kernel_object) ))) return NULL;
301 kernel_object->manager = manager;
302 kernel_object->user_ptr = user_ptr;
303 kernel_object->object = obj;
304 kernel_object->owned = 0;
306 if (wine_rb_put( &manager->kernel_objects, &user_ptr, &kernel_object->rb_entry ))
308 /* kernel_object pointer already set */
309 free( kernel_object );
310 return NULL;
313 list_add_head( list, &kernel_object->list_entry );
314 return kernel_object;
317 static struct kernel_object *kernel_object_from_ptr( struct device_manager *manager, client_ptr_t client_ptr )
319 struct wine_rb_entry *entry = wine_rb_get( &manager->kernel_objects, &client_ptr );
320 return entry ? WINE_RB_ENTRY_VALUE( entry, struct kernel_object, rb_entry ) : NULL;
323 static void grab_kernel_object( struct kernel_object *ptr )
325 if (!ptr->owned)
327 grab_object( ptr->object );
328 ptr->owned = 1;
332 static void irp_call_dump( struct object *obj, int verbose )
334 struct irp_call *irp = (struct irp_call *)obj;
335 fprintf( stderr, "IRP call file=%p\n", irp->file );
338 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry )
340 struct irp_call *irp = (struct irp_call *)obj;
342 return !irp->file; /* file is cleared once the irp has completed */
345 static void irp_call_destroy( struct object *obj )
347 struct irp_call *irp = (struct irp_call *)obj;
349 if (irp->async)
351 async_terminate( irp->async, STATUS_CANCELLED );
352 release_object( irp->async );
354 if (irp->iosb) release_object( irp->iosb );
355 if (irp->file) release_object( irp->file );
356 if (irp->thread) release_object( irp->thread );
359 static struct irp_call *create_irp( struct device_file *file, const irp_params_t *params, struct async *async )
361 struct irp_call *irp;
363 if (file && !file->device->manager) /* it has been deleted */
365 set_error( STATUS_FILE_DELETED );
366 return NULL;
369 if ((irp = alloc_object( &irp_call_ops )))
371 irp->file = file ? (struct device_file *)grab_object( file ) : NULL;
372 irp->thread = NULL;
373 irp->async = NULL;
374 irp->params = *params;
375 irp->iosb = NULL;
376 irp->canceled = 0;
377 irp->user_ptr = 0;
379 if (async) irp->iosb = async_get_iosb( async );
380 if (!irp->iosb && !(irp->iosb = create_iosb( NULL, 0, 0 )))
382 release_object( irp );
383 irp = NULL;
386 return irp;
389 static void set_irp_result( struct irp_call *irp, unsigned int status,
390 const void *out_data, data_size_t out_size, data_size_t result )
392 struct device_file *file = irp->file;
393 struct iosb *iosb = irp->iosb;
395 if (!file) return; /* already finished */
397 /* FIXME: handle the STATUS_PENDING case */
398 iosb->status = status;
399 iosb->result = result;
400 iosb->out_size = min( iosb->out_size, out_size );
401 if (iosb->out_size && !(iosb->out_data = memdup( out_data, iosb->out_size )))
402 iosb->out_size = 0;
404 /* remove it from the device queue */
405 list_remove( &irp->dev_entry );
406 irp->file = NULL;
407 if (irp->async)
409 if (result) status = STATUS_ALERTED;
410 async_terminate( irp->async, status );
411 release_object( irp->async );
412 irp->async = NULL;
414 wake_up( &irp->obj, 0 );
416 release_object( irp ); /* no longer on the device queue */
417 release_object( file );
421 static void device_dump( struct object *obj, int verbose )
423 fputs( "Device\n", stderr );
426 static void device_destroy( struct object *obj )
428 struct device *device = (struct device *)obj;
430 assert( list_empty( &device->files ));
432 free( device->unix_path );
433 if (device->manager) list_remove( &device->entry );
436 static void add_irp_to_queue( struct device_manager *manager, struct irp_call *irp, struct thread *thread )
438 grab_object( irp ); /* grab reference for queued irp */
439 irp->thread = thread ? (struct thread *)grab_object( thread ) : NULL;
440 if (irp->file) list_add_tail( &irp->file->requests, &irp->dev_entry );
441 list_add_tail( &manager->requests, &irp->mgr_entry );
442 if (list_head( &manager->requests ) == &irp->mgr_entry) wake_up( &manager->obj, 0 ); /* first one */
445 static struct object *device_open_file( struct object *obj, unsigned int access,
446 unsigned int sharing, unsigned int options )
448 struct device *device = (struct device *)obj;
449 struct device_file *file;
450 struct unicode_str nt_name;
452 if (!(file = alloc_object( &device_file_ops ))) return NULL;
454 file->device = (struct device *)grab_object( device );
455 file->closed = 0;
456 list_init( &file->kernel_object );
457 list_init( &file->requests );
458 list_add_tail( &device->files, &file->entry );
459 if (device->unix_path)
461 mode_t mode = 0666;
462 access = file->obj.ops->map_access( &file->obj, access );
463 nt_name.str = device->obj.ops->get_full_name( &device->obj, &nt_name.len );
464 file->fd = open_fd( NULL, device->unix_path, nt_name, O_NONBLOCK | O_LARGEFILE,
465 &mode, access, sharing, options );
466 if (file->fd) set_fd_user( file->fd, &device_file_fd_ops, &file->obj );
468 else file->fd = alloc_pseudo_fd( &device_file_fd_ops, &file->obj, options );
470 if (!file->fd)
472 release_object( file );
473 return NULL;
476 allow_fd_caching( file->fd );
478 if (device->manager)
480 struct irp_call *irp;
481 irp_params_t params;
483 memset( &params, 0, sizeof(params) );
484 params.create.type = IRP_CALL_CREATE;
485 params.create.access = access;
486 params.create.sharing = sharing;
487 params.create.options = options;
488 params.create.device = get_kernel_object_ptr( device->manager, &device->obj );
490 if ((irp = create_irp( file, &params, NULL )))
492 add_irp_to_queue( device->manager, irp, current );
493 release_object( irp );
496 return &file->obj;
499 static struct list *device_get_kernel_obj_list( struct object *obj )
501 struct device *device = (struct device *)obj;
502 return &device->kernel_object;
505 static void device_file_dump( struct object *obj, int verbose )
507 struct device_file *file = (struct device_file *)obj;
509 fprintf( stderr, "File on device %p\n", file->device );
512 static struct fd *device_file_get_fd( struct object *obj )
514 struct device_file *file = (struct device_file *)obj;
516 return (struct fd *)grab_object( file->fd );
519 static WCHAR *device_file_get_full_name( struct object *obj, data_size_t *len )
521 struct device_file *file = (struct device_file *)obj;
522 return file->device->obj.ops->get_full_name( &file->device->obj, len );
525 static struct list *device_file_get_kernel_obj_list( struct object *obj )
527 struct device_file *file = (struct device_file *)obj;
528 return &file->kernel_object;
531 static int device_file_close_handle( struct object *obj, struct process *process, obj_handle_t handle )
533 struct device_file *file = (struct device_file *)obj;
535 if (!file->closed && file->device->manager && obj->handle_count == 1) /* last handle */
537 struct irp_call *irp;
538 irp_params_t params;
540 file->closed = 1;
541 memset( &params, 0, sizeof(params) );
542 params.close.type = IRP_CALL_CLOSE;
544 if ((irp = create_irp( file, &params, NULL )))
546 add_irp_to_queue( file->device->manager, irp, current );
547 release_object( irp );
550 return 1;
553 static void device_file_destroy( struct object *obj )
555 struct device_file *file = (struct device_file *)obj;
556 struct irp_call *irp, *next;
558 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
560 list_remove( &irp->dev_entry );
561 release_object( irp ); /* no longer on the device queue */
563 if (file->fd) release_object( file->fd );
564 list_remove( &file->entry );
565 release_object( file->device );
568 static int fill_irp_params( struct device_manager *manager, struct irp_call *irp, irp_params_t *params )
570 switch (irp->params.type)
572 case IRP_CALL_NONE:
573 case IRP_CALL_FREE:
574 case IRP_CALL_CANCEL:
575 break;
576 case IRP_CALL_CREATE:
577 irp->params.create.file = alloc_handle( current->process, irp->file,
578 irp->params.create.access, 0 );
579 if (!irp->params.create.file) return 0;
580 break;
581 case IRP_CALL_CLOSE:
582 irp->params.close.file = get_kernel_object_ptr( manager, &irp->file->obj );
583 break;
584 case IRP_CALL_READ:
585 irp->params.read.file = get_kernel_object_ptr( manager, &irp->file->obj );
586 irp->params.read.out_size = irp->iosb->out_size;
587 break;
588 case IRP_CALL_WRITE:
589 irp->params.write.file = get_kernel_object_ptr( manager, &irp->file->obj );
590 break;
591 case IRP_CALL_FLUSH:
592 irp->params.flush.file = get_kernel_object_ptr( manager, &irp->file->obj );
593 break;
594 case IRP_CALL_IOCTL:
595 irp->params.ioctl.file = get_kernel_object_ptr( manager, &irp->file->obj );
596 irp->params.ioctl.out_size = irp->iosb->out_size;
597 break;
598 case IRP_CALL_VOLUME:
599 irp->params.volume.file = get_kernel_object_ptr( manager, &irp->file->obj );
600 irp->params.volume.out_size = irp->iosb->out_size;
601 break;
604 *params = irp->params;
605 return 1;
608 static void free_irp_params( struct irp_call *irp )
610 switch (irp->params.type)
612 case IRP_CALL_CREATE:
613 close_handle( current->process, irp->params.create.file );
614 break;
615 default:
616 break;
620 /* queue an irp to the device */
621 static int queue_irp( struct device_file *file, const irp_params_t *params, struct async *async )
623 struct irp_call *irp = create_irp( file, params, async );
624 if (!irp) return 0;
626 fd_queue_async( file->fd, async, ASYNC_TYPE_WAIT );
627 irp->async = (struct async *)grab_object( async );
628 add_irp_to_queue( file->device->manager, irp, current );
629 release_object( irp );
630 set_error( STATUS_PENDING );
631 return 0;
634 static enum server_fd_type device_file_get_fd_type( struct fd *fd )
636 return FD_TYPE_DEVICE;
639 static int device_file_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class )
641 struct device_file *file = get_fd_user( fd );
642 irp_params_t params;
644 memset( &params, 0, sizeof(params) );
645 params.volume.type = IRP_CALL_VOLUME;
646 params.volume.info_class = info_class;
647 return queue_irp( file, &params, async );
650 static int device_file_read( struct fd *fd, struct async *async, file_pos_t pos )
652 struct device_file *file = get_fd_user( fd );
653 irp_params_t params;
655 memset( &params, 0, sizeof(params) );
656 params.read.type = IRP_CALL_READ;
657 params.read.key = 0;
658 params.read.pos = pos;
659 return queue_irp( file, &params, async );
662 static int device_file_write( struct fd *fd, struct async *async, file_pos_t pos )
664 struct device_file *file = get_fd_user( fd );
665 irp_params_t params;
667 memset( &params, 0, sizeof(params) );
668 params.write.type = IRP_CALL_WRITE;
669 params.write.key = 0;
670 params.write.pos = pos;
671 return queue_irp( file, &params, async );
674 static int device_file_flush( struct fd *fd, struct async *async )
676 struct device_file *file = get_fd_user( fd );
677 irp_params_t params;
679 memset( &params, 0, sizeof(params) );
680 params.flush.type = IRP_CALL_FLUSH;
681 return queue_irp( file, &params, async );
684 static int device_file_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
686 struct device_file *file = get_fd_user( fd );
687 irp_params_t params;
689 memset( &params, 0, sizeof(params) );
690 params.ioctl.type = IRP_CALL_IOCTL;
691 params.ioctl.code = code;
692 return queue_irp( file, &params, async );
695 static void cancel_irp_call( struct irp_call *irp )
697 struct irp_call *cancel_irp;
698 irp_params_t params;
700 irp->canceled = 1;
701 if (!irp->user_ptr || !irp->file || !irp->file->device->manager) return;
703 memset( &params, 0, sizeof(params) );
704 params.cancel.type = IRP_CALL_CANCEL;
705 params.cancel.irp = irp->user_ptr;
707 if ((cancel_irp = create_irp( NULL, &params, NULL )))
709 add_irp_to_queue( irp->file->device->manager, cancel_irp, NULL );
710 release_object( cancel_irp );
713 set_irp_result( irp, STATUS_CANCELLED, NULL, 0, 0 );
716 static void device_file_reselect_async( struct fd *fd, struct async_queue *queue )
718 struct device_file *file = get_fd_user( fd );
719 struct irp_call *irp;
721 LIST_FOR_EACH_ENTRY( irp, &file->requests, struct irp_call, dev_entry )
722 if (irp->iosb->status != STATUS_PENDING)
724 cancel_irp_call( irp );
725 return;
729 static struct device *create_device( struct object *root, const struct unicode_str *name,
730 struct device_manager *manager )
732 struct device *device;
734 if ((device = create_named_object( root, &device_ops, name, 0, NULL )))
736 device->unix_path = NULL;
737 device->manager = manager;
738 grab_object( device );
739 list_add_tail( &manager->devices, &device->entry );
740 list_init( &device->kernel_object );
741 list_init( &device->files );
743 return device;
746 struct object *create_unix_device( struct object *root, const struct unicode_str *name,
747 unsigned int attr, const struct security_descriptor *sd,
748 const char *unix_path )
750 struct device *device;
752 if ((device = create_named_object( root, &device_ops, name, attr, sd )))
754 device->unix_path = strdup( unix_path );
755 device->manager = NULL; /* no manager, requests go straight to the Unix device */
756 list_init( &device->kernel_object );
757 list_init( &device->files );
759 return &device->obj;
763 /* terminate requests when the underlying device is deleted */
764 static void delete_file( struct device_file *file )
766 struct irp_call *irp, *next;
768 /* the pending requests may be the only thing holding a reference to the file */
769 grab_object( file );
771 /* terminate all pending requests */
772 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
774 list_remove( &irp->mgr_entry );
775 set_irp_result( irp, STATUS_FILE_DELETED, NULL, 0, 0 );
778 release_object( file );
781 static void delete_device( struct device *device )
783 struct device_file *file, *next;
785 if (!device->manager) return; /* already deleted */
787 LIST_FOR_EACH_ENTRY_SAFE( file, next, &device->files, struct device_file, entry )
788 delete_file( file );
790 unlink_named_object( &device->obj );
791 list_remove( &device->entry );
792 device->manager = NULL;
793 release_object( device );
797 static void device_manager_dump( struct object *obj, int verbose )
799 fprintf( stderr, "Device manager\n" );
802 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry )
804 struct device_manager *manager = (struct device_manager *)obj;
806 return !list_empty( &manager->requests );
809 static void device_manager_destroy( struct object *obj )
811 struct device_manager *manager = (struct device_manager *)obj;
812 struct kernel_object *kernel_object;
813 struct list *ptr;
815 if (manager->current_call)
817 release_object( manager->current_call );
818 manager->current_call = NULL;
821 while (manager->kernel_objects.root)
823 kernel_object = WINE_RB_ENTRY_VALUE( manager->kernel_objects.root, struct kernel_object, rb_entry );
824 wine_rb_remove( &manager->kernel_objects, &kernel_object->rb_entry );
825 list_remove( &kernel_object->list_entry );
826 if (kernel_object->owned) release_object( kernel_object->object );
827 free( kernel_object );
830 while ((ptr = list_head( &manager->devices )))
832 struct device *device = LIST_ENTRY( ptr, struct device, entry );
833 delete_device( device );
836 while ((ptr = list_head( &manager->requests )))
838 struct irp_call *irp = LIST_ENTRY( ptr, struct irp_call, mgr_entry );
839 list_remove( &irp->mgr_entry );
840 assert( !irp->file && !irp->async );
841 release_object( irp );
845 static struct device_manager *create_device_manager(void)
847 struct device_manager *manager;
849 if ((manager = alloc_object( &device_manager_ops )))
851 manager->current_call = NULL;
852 list_init( &manager->devices );
853 list_init( &manager->requests );
854 wine_rb_init( &manager->kernel_objects, compare_kernel_object );
856 return manager;
859 void free_kernel_objects( struct object *obj )
861 struct list *ptr, *list;
863 if (!(list = obj->ops->get_kernel_obj_list( obj ))) return;
865 while ((ptr = list_head( list )))
867 struct kernel_object *kernel_object = LIST_ENTRY( ptr, struct kernel_object, list_entry );
868 struct irp_call *irp;
869 irp_params_t params;
871 assert( !kernel_object->owned );
873 memset( &params, 0, sizeof(params) );
874 params.free.type = IRP_CALL_FREE;
875 params.free.obj = kernel_object->user_ptr;
877 if ((irp = create_irp( NULL, &params, NULL )))
879 add_irp_to_queue( kernel_object->manager, irp, NULL );
880 release_object( irp );
883 list_remove( &kernel_object->list_entry );
884 wine_rb_remove( &kernel_object->manager->kernel_objects, &kernel_object->rb_entry );
885 free( kernel_object );
890 /* create a device manager */
891 DECL_HANDLER(create_device_manager)
893 struct device_manager *manager = create_device_manager();
895 if (manager)
897 reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
898 release_object( manager );
903 /* create a device */
904 DECL_HANDLER(create_device)
906 struct device *device;
907 struct unicode_str name = get_req_unicode_str();
908 struct device_manager *manager;
909 struct object *root = NULL;
911 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
912 0, &device_manager_ops )))
913 return;
915 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir )))
917 release_object( manager );
918 return;
921 if ((device = create_device( root, &name, manager )))
923 struct kernel_object *ptr = set_kernel_object( manager, &device->obj, req->user_ptr );
924 if (ptr)
925 grab_kernel_object( ptr );
926 else
927 set_error( STATUS_NO_MEMORY );
928 release_object( device );
931 if (root) release_object( root );
932 release_object( manager );
936 /* delete a device */
937 DECL_HANDLER(delete_device)
939 struct device_manager *manager;
940 struct kernel_object *ref;
941 struct device *device;
943 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
944 0, &device_manager_ops )))
945 return;
947 if ((ref = kernel_object_from_ptr( manager, req->device )) && ref->object->ops == &device_ops)
949 device = (struct device *)grab_object( ref->object );
950 delete_device( device );
951 release_object( device );
953 else set_error( STATUS_INVALID_HANDLE );
955 release_object( manager );
959 /* retrieve the next pending device irp request */
960 DECL_HANDLER(get_next_device_request)
962 struct irp_call *irp;
963 struct device_manager *manager;
964 struct list *ptr;
965 struct iosb *iosb;
967 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
968 0, &device_manager_ops )))
969 return;
971 if (req->prev) close_handle( current->process, req->prev ); /* avoid an extra round-trip for close */
973 /* process result of previous call */
974 if (manager->current_call)
976 irp = manager->current_call;
977 irp->user_ptr = req->user_ptr;
979 if (req->status)
980 set_irp_result( irp, req->status, NULL, 0, 0 );
981 if (irp->canceled)
982 /* if it was canceled during dispatch, we couldn't queue cancel call without client pointer,
983 * so we need to do it now */
984 cancel_irp_call( irp );
985 else if (irp->async)
986 set_async_pending( irp->async, irp->file && is_fd_overlapped( irp->file->fd ) );
988 free_irp_params( irp );
989 release_object( irp );
990 manager->current_call = NULL;
993 clear_error();
995 if ((ptr = list_head( &manager->requests )))
997 struct thread *thread;
999 irp = LIST_ENTRY( ptr, struct irp_call, mgr_entry );
1001 thread = irp->thread ? irp->thread : current;
1002 reply->client_thread = get_kernel_object_ptr( manager, &thread->obj );
1003 reply->client_tid = get_thread_id( thread );
1005 iosb = irp->iosb;
1006 reply->in_size = iosb->in_size;
1007 if (iosb->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
1008 else if (!irp->file || (reply->next = alloc_handle( current->process, irp, 0, 0 )))
1010 if (fill_irp_params( manager, irp, &reply->params ))
1012 set_reply_data_ptr( iosb->in_data, iosb->in_size );
1013 iosb->in_data = NULL;
1014 iosb->in_size = 0;
1015 list_remove( &irp->mgr_entry );
1016 list_init( &irp->mgr_entry );
1017 /* we already own the object if it's only on manager queue */
1018 if (irp->file) grab_object( irp );
1019 manager->current_call = irp;
1021 else close_handle( current->process, reply->next );
1024 else set_error( STATUS_PENDING );
1026 release_object( manager );
1030 /* store results of an async irp */
1031 DECL_HANDLER(set_irp_result)
1033 struct irp_call *irp;
1035 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->handle, 0, &irp_call_ops )))
1037 if (!irp->canceled)
1038 set_irp_result( irp, req->status, get_req_data(), get_req_data_size(), req->size );
1039 else if(irp->user_ptr) /* cancel already queued */
1040 set_error( STATUS_MORE_PROCESSING_REQUIRED );
1041 else /* we may be still dispatching the IRP. don't bother queuing cancel if it's already complete */
1042 irp->canceled = 0;
1043 close_handle( current->process, req->handle ); /* avoid an extra round-trip for close */
1044 release_object( irp );
1049 /* get kernel pointer from server object */
1050 DECL_HANDLER(get_kernel_object_ptr)
1052 struct device_manager *manager;
1053 struct object *object = NULL;
1055 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
1056 0, &device_manager_ops )))
1057 return;
1059 if ((object = get_handle_obj( current->process, req->handle, 0, NULL )))
1061 reply->user_ptr = get_kernel_object_ptr( manager, object );
1062 release_object( object );
1065 release_object( manager );
1069 /* associate kernel pointer with server object */
1070 DECL_HANDLER(set_kernel_object_ptr)
1072 struct device_manager *manager;
1073 struct object *object = NULL;
1075 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
1076 0, &device_manager_ops )))
1077 return;
1079 if (!(object = get_handle_obj( current->process, req->handle, 0, NULL )))
1081 release_object( manager );
1082 return;
1085 if (!set_kernel_object( manager, object, req->user_ptr ))
1086 set_error( STATUS_INVALID_HANDLE );
1088 release_object( object );
1089 release_object( manager );
1093 /* grab server object reference from kernel object pointer */
1094 DECL_HANDLER(grab_kernel_object)
1096 struct device_manager *manager;
1097 struct kernel_object *ref;
1099 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
1100 0, &device_manager_ops )))
1101 return;
1103 if ((ref = kernel_object_from_ptr( manager, req->user_ptr )) && !ref->owned)
1104 grab_kernel_object( ref );
1105 else
1106 set_error( STATUS_INVALID_HANDLE );
1108 release_object( manager );
1112 /* release server object reference from kernel object pointer */
1113 DECL_HANDLER(release_kernel_object)
1115 struct device_manager *manager;
1116 struct kernel_object *ref;
1118 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
1119 0, &device_manager_ops )))
1120 return;
1122 if ((ref = kernel_object_from_ptr( manager, req->user_ptr )) && ref->owned)
1124 ref->owned = 0;
1125 release_object( ref->object );
1127 else set_error( STATUS_INVALID_HANDLE );
1129 release_object( manager );
1133 /* get handle from kernel object pointer */
1134 DECL_HANDLER(get_kernel_object_handle)
1136 struct device_manager *manager;
1137 struct kernel_object *ref;
1139 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
1140 0, &device_manager_ops )))
1141 return;
1143 if ((ref = kernel_object_from_ptr( manager, req->user_ptr )))
1144 reply->handle = alloc_handle( current->process, ref->object, req->access, 0 );
1145 else
1146 set_error( STATUS_INVALID_HANDLE );
1148 release_object( manager );