ntdll: Fix read_directory_getattrlist() to get the name of a symlink rather than...
[wine.git] / server / device.c
blob6afa00497d446186d414f0224ce43b92dc1d6189
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 <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
30 #include "ddk/wdm.h"
32 #include "object.h"
33 #include "file.h"
34 #include "handle.h"
35 #include "request.h"
36 #include "process.h"
38 /* IRP object */
40 struct irp_call
42 struct object obj; /* object header */
43 struct list dev_entry; /* entry in device queue */
44 struct list mgr_entry; /* entry in manager queue */
45 struct device_file *file; /* file containing this irp */
46 struct thread *thread; /* thread that queued the irp */
47 client_ptr_t user_arg; /* user arg used to identify the request */
48 struct async *async; /* pending async op */
49 unsigned int status; /* resulting status (or STATUS_PENDING) */
50 irp_params_t params; /* irp parameters */
51 data_size_t result; /* size of result (input or output depending on the type) */
52 data_size_t in_size; /* size of input data */
53 void *in_data; /* input data */
54 data_size_t out_size; /* size of output data */
55 void *out_data; /* output data */
58 static void irp_call_dump( struct object *obj, int verbose );
59 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry );
60 static void irp_call_destroy( struct object *obj );
62 static const struct object_ops irp_call_ops =
64 sizeof(struct irp_call), /* size */
65 irp_call_dump, /* dump */
66 no_get_type, /* get_type */
67 add_queue, /* add_queue */
68 remove_queue, /* remove_queue */
69 irp_call_signaled, /* signaled */
70 no_satisfied, /* satisfied */
71 no_signal, /* signal */
72 no_get_fd, /* get_fd */
73 no_map_access, /* map_access */
74 default_get_sd, /* get_sd */
75 default_set_sd, /* set_sd */
76 no_lookup_name, /* lookup_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_open_file, /* open_file */
112 no_close_handle, /* close_handle */
113 device_manager_destroy /* destroy */
117 /* device (a single device object) */
119 struct device
121 struct object obj; /* object header */
122 struct device_manager *manager; /* manager for this device (or NULL if deleted) */
123 client_ptr_t user_ptr; /* opaque ptr for client side */
124 struct list entry; /* entry in device manager list */
125 struct list files; /* list of open files */
128 static void device_dump( struct object *obj, int verbose );
129 static struct object_type *device_get_type( struct object *obj );
130 static void device_destroy( struct object *obj );
131 static struct object *device_open_file( struct object *obj, unsigned int access,
132 unsigned int sharing, unsigned int options );
134 static const struct object_ops device_ops =
136 sizeof(struct device), /* size */
137 device_dump, /* dump */
138 device_get_type, /* get_type */
139 no_add_queue, /* add_queue */
140 NULL, /* remove_queue */
141 NULL, /* signaled */
142 no_satisfied, /* satisfied */
143 no_signal, /* signal */
144 no_get_fd, /* get_fd */
145 default_fd_map_access, /* map_access */
146 default_get_sd, /* get_sd */
147 default_set_sd, /* set_sd */
148 no_lookup_name, /* lookup_name */
149 device_open_file, /* open_file */
150 no_close_handle, /* close_handle */
151 device_destroy /* destroy */
155 /* device file (an open file handle to a device) */
157 struct device_file
159 struct object obj; /* object header */
160 struct device *device; /* device for this file */
161 struct fd *fd; /* file descriptor for irp */
162 struct list entry; /* entry in device list */
163 struct list requests; /* list of pending irp requests */
166 static void device_file_dump( struct object *obj, int verbose );
167 static struct fd *device_file_get_fd( struct object *obj );
168 static void device_file_destroy( struct object *obj );
169 static enum server_fd_type device_file_get_fd_type( struct fd *fd );
170 static obj_handle_t device_file_read( struct fd *fd, const async_data_t *async_data, int blocking,
171 file_pos_t pos );
172 static obj_handle_t device_file_write( struct fd *fd, const async_data_t *async_data, int blocking,
173 file_pos_t pos, data_size_t *written );
174 static obj_handle_t device_file_flush( struct fd *fd, const async_data_t *async_data, int blocking );
175 static obj_handle_t device_file_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
176 int blocking );
178 static const struct object_ops device_file_ops =
180 sizeof(struct device_file), /* size */
181 device_file_dump, /* dump */
182 no_get_type, /* get_type */
183 add_queue, /* add_queue */
184 remove_queue, /* remove_queue */
185 default_fd_signaled, /* signaled */
186 no_satisfied, /* satisfied */
187 no_signal, /* signal */
188 device_file_get_fd, /* get_fd */
189 default_fd_map_access, /* map_access */
190 default_get_sd, /* get_sd */
191 default_set_sd, /* set_sd */
192 no_lookup_name, /* lookup_name */
193 no_open_file, /* open_file */
194 no_close_handle, /* close_handle */
195 device_file_destroy /* destroy */
198 static const struct fd_ops device_file_fd_ops =
200 default_fd_get_poll_events, /* get_poll_events */
201 default_poll_event, /* poll_event */
202 device_file_get_fd_type, /* get_fd_type */
203 device_file_read, /* read */
204 device_file_write, /* write */
205 device_file_flush, /* flush */
206 device_file_ioctl, /* ioctl */
207 default_fd_queue_async, /* queue_async */
208 default_fd_reselect_async, /* reselect_async */
209 default_fd_cancel_async /* cancel_async */
213 static void irp_call_dump( struct object *obj, int verbose )
215 struct irp_call *irp = (struct irp_call *)obj;
216 fprintf( stderr, "IRP call file=%p\n", irp->file );
219 static int irp_call_signaled( struct object *obj, struct wait_queue_entry *entry )
221 struct irp_call *irp = (struct irp_call *)obj;
223 return !irp->file; /* file is cleared once the irp has completed */
226 static void irp_call_destroy( struct object *obj )
228 struct irp_call *irp = (struct irp_call *)obj;
230 free( irp->in_data );
231 free( irp->out_data );
232 if (irp->async)
234 async_terminate( irp->async, STATUS_CANCELLED );
235 release_object( irp->async );
237 if (irp->file) release_object( irp->file );
238 release_object( irp->thread );
241 static struct irp_call *create_irp( struct device_file *file, const irp_params_t *params,
242 const void *in_data, data_size_t in_size, data_size_t out_size )
244 struct irp_call *irp;
246 if (!file->device->manager) /* it has been deleted */
248 set_error( STATUS_FILE_DELETED );
249 return NULL;
252 if ((irp = alloc_object( &irp_call_ops )))
254 irp->file = (struct device_file *)grab_object( file );
255 irp->async = NULL;
256 irp->params = *params;
257 irp->status = STATUS_PENDING;
258 irp->result = 0;
259 irp->in_size = in_size;
260 irp->in_data = NULL;
261 irp->out_size = out_size;
262 irp->out_data = NULL;
264 if (irp->in_size && !(irp->in_data = memdup( in_data, in_size )))
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;
278 if (!file) return; /* already finished */
280 /* FIXME: handle the STATUS_PENDING case */
281 irp->status = status;
282 irp->result = result;
283 irp->out_size = min( irp->out_size, out_size );
284 if (irp->out_size && !(irp->out_data = memdup( out_data, irp->out_size )))
285 irp->out_size = 0;
286 release_object( file );
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 if (status != STATUS_ALERTED)
299 /* remove it from the device queue */
300 /* (for STATUS_ALERTED this will be done in get_irp_result) */
301 list_remove( &irp->dev_entry );
302 release_object( irp ); /* no longer on the device queue */
307 static void device_dump( struct object *obj, int verbose )
309 struct device *device = (struct device *)obj;
311 fprintf( stderr, "Device " );
312 dump_object_name( &device->obj );
313 fputc( '\n', stderr );
316 static struct object_type *device_get_type( struct object *obj )
318 static const WCHAR name[] = {'D','e','v','i','c','e'};
319 static const struct unicode_str str = { name, sizeof(name) };
320 return get_object_type( &str );
323 static void device_destroy( struct object *obj )
325 struct device *device = (struct device *)obj;
327 assert( list_empty( &device->files ));
329 if (device->manager) list_remove( &device->entry );
332 static struct object *device_open_file( struct object *obj, unsigned int access,
333 unsigned int sharing, unsigned int options )
335 struct device *device = (struct device *)obj;
336 struct device_file *file;
338 if ((file = alloc_object( &device_file_ops )))
340 file->device = (struct device *)grab_object( device );
341 list_init( &file->requests );
342 list_add_tail( &device->files, &file->entry );
343 if (!(file->fd = alloc_pseudo_fd( &device_file_fd_ops, &file->obj, 0 )))
345 release_object( file );
346 file = NULL;
349 return &file->obj;
352 static void device_file_dump( struct object *obj, int verbose )
354 struct device_file *file = (struct device_file *)obj;
356 fprintf( stderr, "File on device %p\n", file->device );
359 static struct fd *device_file_get_fd( struct object *obj )
361 struct device_file *file = (struct device_file *)obj;
363 return (struct fd *)grab_object( file->fd );
366 static void device_file_destroy( struct object *obj )
368 struct device_file *file = (struct device_file *)obj;
369 struct irp_call *irp, *next;
371 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
373 list_remove( &irp->dev_entry );
374 release_object( irp ); /* no longer on the device queue */
376 if (file->fd) release_object( file->fd );
377 list_remove( &file->entry );
378 release_object( file->device );
381 static struct irp_call *find_irp_call( struct device_file *file, struct thread *thread,
382 client_ptr_t user_arg )
384 struct irp_call *irp;
386 LIST_FOR_EACH_ENTRY( irp, &file->requests, struct irp_call, dev_entry )
387 if (irp->thread == thread && irp->user_arg == user_arg) return irp;
389 set_error( STATUS_INVALID_PARAMETER );
390 return NULL;
393 /* queue an irp to the device */
394 static obj_handle_t queue_irp( struct device_file *file, struct irp_call *irp,
395 const async_data_t *async_data, int blocking )
397 obj_handle_t handle = 0;
398 struct device_manager *manager = file->device->manager;
400 assert( manager );
402 if (blocking && !(handle = alloc_handle( current->process, irp, SYNCHRONIZE, 0 ))) return 0;
404 if (!(irp->async = fd_queue_async( file->fd, async_data, ASYNC_TYPE_WAIT )))
406 if (handle) close_handle( current->process, handle );
407 return 0;
409 irp->thread = (struct thread *)grab_object( current );
410 irp->user_arg = async_data->arg;
411 grab_object( irp ); /* grab reference for queued irp */
413 list_add_tail( &file->requests, &irp->dev_entry );
414 list_add_tail( &manager->requests, &irp->mgr_entry );
415 if (list_head( &manager->requests ) == &irp->mgr_entry) wake_up( &manager->obj, 0 ); /* first one */
416 set_error( STATUS_PENDING );
417 return handle;
420 static enum server_fd_type device_file_get_fd_type( struct fd *fd )
422 return FD_TYPE_DEVICE;
425 static obj_handle_t device_file_read( struct fd *fd, const async_data_t *async_data, int blocking,
426 file_pos_t pos )
428 struct device_file *file = get_fd_user( fd );
429 struct irp_call *irp;
430 obj_handle_t handle;
431 irp_params_t params;
433 params.major = IRP_MJ_READ;
434 params.read.key = 0;
435 params.read.pos = pos;
437 irp = create_irp( file, &params, NULL, 0, get_reply_max_size() );
438 if (!irp) return 0;
440 handle = queue_irp( file, irp, async_data, blocking );
441 release_object( irp );
442 return handle;
445 static obj_handle_t device_file_write( struct fd *fd, const async_data_t *async_data, int blocking,
446 file_pos_t pos, data_size_t *written )
448 struct device_file *file = get_fd_user( fd );
449 struct irp_call *irp;
450 obj_handle_t handle;
451 irp_params_t params;
453 params.major = IRP_MJ_WRITE;
454 params.write.key = 0;
455 params.write.pos = pos;
457 irp = create_irp( file, &params, get_req_data(), get_req_data_size(), 0 );
458 if (!irp) return 0;
460 handle = queue_irp( file, irp, async_data, blocking );
461 release_object( irp );
462 return handle;
465 static obj_handle_t device_file_flush( struct fd *fd, const async_data_t *async_data, int blocking )
467 struct device_file *file = get_fd_user( fd );
468 struct irp_call *irp;
469 obj_handle_t handle;
470 irp_params_t params;
472 params.major = IRP_MJ_FLUSH_BUFFERS;
474 irp = create_irp( file, &params, NULL, 0, 0 );
475 if (!irp) return 0;
477 handle = queue_irp( file, irp, async_data, blocking );
478 release_object( irp );
479 return handle;
482 static obj_handle_t device_file_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
483 int blocking )
485 struct device_file *file = get_fd_user( fd );
486 struct irp_call *irp;
487 obj_handle_t handle;
488 irp_params_t params;
490 params.major = IRP_MJ_DEVICE_CONTROL;
491 params.ioctl.code = code;
493 irp = create_irp( file, &params, get_req_data(), get_req_data_size(),
494 get_reply_max_size() );
495 if (!irp) return 0;
497 handle = queue_irp( file, irp, async_data, blocking );
498 release_object( irp );
499 return handle;
502 static struct device *create_device( struct directory *root, const struct unicode_str *name,
503 struct device_manager *manager, unsigned int attr )
505 struct device *device;
507 if ((device = create_named_object_dir( root, name, attr, &device_ops )))
509 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
511 /* initialize it if it didn't already exist */
512 device->manager = manager;
513 list_add_tail( &manager->devices, &device->entry );
514 list_init( &device->files );
517 return device;
520 /* terminate requests when the underlying device is deleted */
521 static void delete_file( struct device_file *file )
523 struct irp_call *irp, *next;
525 /* terminate all pending requests */
526 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
528 list_remove( &irp->mgr_entry );
529 set_irp_result( irp, STATUS_FILE_DELETED, NULL, 0, 0 );
533 static void delete_device( struct device *device )
535 struct device_file *file, *next;
537 if (!device->manager) return; /* already deleted */
539 LIST_FOR_EACH_ENTRY_SAFE( file, next, &device->files, struct device_file, entry )
540 delete_file( file );
542 unlink_named_object( &device->obj );
543 list_remove( &device->entry );
544 device->manager = NULL;
548 static void device_manager_dump( struct object *obj, int verbose )
550 fprintf( stderr, "Device manager\n" );
553 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry )
555 struct device_manager *manager = (struct device_manager *)obj;
557 return !list_empty( &manager->requests );
560 static void device_manager_destroy( struct object *obj )
562 struct device_manager *manager = (struct device_manager *)obj;
563 struct list *ptr;
565 while ((ptr = list_head( &manager->devices )))
567 struct device *device = LIST_ENTRY( ptr, struct device, entry );
568 delete_device( device );
572 static struct device_manager *create_device_manager(void)
574 struct device_manager *manager;
576 if ((manager = alloc_object( &device_manager_ops )))
578 list_init( &manager->devices );
579 list_init( &manager->requests );
581 return manager;
585 /* create a device manager */
586 DECL_HANDLER(create_device_manager)
588 struct device_manager *manager = create_device_manager();
590 if (manager)
592 reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
593 release_object( manager );
598 /* create a device */
599 DECL_HANDLER(create_device)
601 struct device *device;
602 struct unicode_str name;
603 struct device_manager *manager;
604 struct directory *root = NULL;
606 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
607 0, &device_manager_ops )))
608 return;
610 get_req_unicode_str( &name );
611 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
613 release_object( manager );
614 return;
617 if ((device = create_device( root, &name, manager, req->attributes )))
619 device->user_ptr = req->user_ptr;
620 reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
621 release_object( device );
624 if (root) release_object( root );
625 release_object( manager );
629 /* delete a device */
630 DECL_HANDLER(delete_device)
632 struct device *device;
634 if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
636 delete_device( device );
637 release_object( device );
642 /* retrieve the next pending device irp request */
643 DECL_HANDLER(get_next_device_request)
645 struct irp_call *irp;
646 struct device_manager *manager;
647 struct list *ptr;
649 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
650 0, &device_manager_ops )))
651 return;
653 if (req->prev)
655 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->prev, 0, &irp_call_ops )))
657 set_irp_result( irp, req->status, NULL, 0, 0 );
658 close_handle( current->process, req->prev ); /* avoid an extra round-trip for close */
659 release_object( irp );
661 clear_error();
664 if ((ptr = list_head( &manager->requests )))
666 irp = LIST_ENTRY( ptr, struct irp_call, mgr_entry );
667 reply->params = irp->params;
668 reply->user_ptr = irp->file->device->user_ptr;
669 reply->client_pid = get_process_id( irp->thread->process );
670 reply->client_tid = get_thread_id( irp->thread );
671 reply->in_size = irp->in_size;
672 reply->out_size = irp->out_size;
673 if (irp->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
674 else if ((reply->next = alloc_handle( current->process, irp, 0, 0 )))
676 set_reply_data_ptr( irp->in_data, irp->in_size );
677 irp->in_data = NULL;
678 irp->in_size = 0;
679 list_remove( &irp->mgr_entry );
680 list_init( &irp->mgr_entry );
683 else set_error( STATUS_PENDING );
685 release_object( manager );
689 /* store results of an async irp */
690 DECL_HANDLER(set_irp_result)
692 struct irp_call *irp;
693 struct device_manager *manager;
695 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
696 0, &device_manager_ops )))
697 return;
699 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->handle, 0, &irp_call_ops )))
701 set_irp_result( irp, req->status, get_req_data(), get_req_data_size(), req->size );
702 close_handle( current->process, req->handle ); /* avoid an extra round-trip for close */
703 release_object( irp );
705 release_object( manager );
709 /* retrieve results of an async irp */
710 DECL_HANDLER(get_irp_result)
712 struct device_file *file;
713 struct irp_call *irp;
715 if (!(file = (struct device_file *)get_handle_obj( current->process, req->handle,
716 0, &device_file_ops )))
717 return;
719 if ((irp = find_irp_call( file, current, req->user_arg )))
721 if (irp->out_data)
723 data_size_t size = min( irp->out_size, get_reply_max_size() );
724 if (size)
726 set_reply_data_ptr( irp->out_data, size );
727 irp->out_data = NULL;
730 reply->size = irp->result;
731 set_error( irp->status );
732 list_remove( &irp->dev_entry );
733 release_object( irp ); /* no longer on the device queue */
735 release_object( file );