regedit: Null-terminate incomplete REG_EXPAND_SZ and REG_MULTI_SZ Unicode data.
[wine.git] / server / device.c
blob192395dcc7fba03d15a5a8f46dcad8451093e34d
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 int device_file_read( struct fd *fd, struct async *async, file_pos_t pos );
178 static int device_file_write( struct fd *fd, struct async *async, file_pos_t pos );
179 static int device_file_flush( struct fd *fd, struct async *async );
180 static int device_file_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
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 int queue_irp( struct device_file *file, const irp_params_t *params, struct async *async )
467 struct irp_call *irp = create_irp( file, params, async );
468 if (!irp) return 0;
470 fd_queue_async( file->fd, async, ASYNC_TYPE_WAIT );
471 irp->async = (struct async *)grab_object( async );
472 add_irp_to_queue( file, irp, current );
473 release_object( irp );
474 set_error( STATUS_PENDING );
475 return 1;
478 static enum server_fd_type device_file_get_fd_type( struct fd *fd )
480 return FD_TYPE_DEVICE;
483 static int device_file_read( struct fd *fd, struct async *async, file_pos_t pos )
485 struct device_file *file = get_fd_user( fd );
486 irp_params_t params;
488 memset( &params, 0, sizeof(params) );
489 params.read.major = IRP_MJ_READ;
490 params.read.key = 0;
491 params.read.pos = pos;
492 params.read.file = file->user_ptr;
493 return queue_irp( file, &params, async );
496 static int device_file_write( struct fd *fd, struct async *async, file_pos_t pos )
498 struct device_file *file = get_fd_user( fd );
499 irp_params_t params;
501 memset( &params, 0, sizeof(params) );
502 params.write.major = IRP_MJ_WRITE;
503 params.write.key = 0;
504 params.write.pos = pos;
505 params.write.file = file->user_ptr;
506 return queue_irp( file, &params, async );
509 static int device_file_flush( struct fd *fd, struct async *async )
511 struct device_file *file = get_fd_user( fd );
512 irp_params_t params;
514 memset( &params, 0, sizeof(params) );
515 params.flush.major = IRP_MJ_FLUSH_BUFFERS;
516 params.flush.file = file->user_ptr;
517 return queue_irp( file, &params, NULL );
520 static int device_file_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
522 struct device_file *file = get_fd_user( fd );
523 irp_params_t params;
525 memset( &params, 0, sizeof(params) );
526 params.ioctl.major = IRP_MJ_DEVICE_CONTROL;
527 params.ioctl.code = code;
528 params.ioctl.file = file->user_ptr;
529 return queue_irp( file, &params, async );
532 static struct device *create_device( struct object *root, const struct unicode_str *name,
533 struct device_manager *manager, unsigned int attr )
535 struct device *device;
537 if ((device = create_named_object( root, &device_ops, name, attr, NULL )))
539 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
541 /* initialize it if it didn't already exist */
542 device->unix_path = NULL;
543 device->manager = manager;
544 list_add_tail( &manager->devices, &device->entry );
545 list_init( &device->files );
548 return device;
551 struct object *create_unix_device( struct object *root, const struct unicode_str *name,
552 const char *unix_path )
554 struct device *device;
556 if ((device = create_named_object( root, &device_ops, name, 0, NULL )))
558 device->unix_path = strdup( unix_path );
559 device->manager = NULL; /* no manager, requests go straight to the Unix device */
560 list_init( &device->files );
562 return &device->obj;
566 /* terminate requests when the underlying device is deleted */
567 static void delete_file( struct device_file *file )
569 struct irp_call *irp, *next;
571 /* terminate all pending requests */
572 LIST_FOR_EACH_ENTRY_SAFE( irp, next, &file->requests, struct irp_call, dev_entry )
574 list_remove( &irp->mgr_entry );
575 set_irp_result( irp, STATUS_FILE_DELETED, NULL, 0, 0 );
579 static void delete_device( struct device *device )
581 struct device_file *file, *next;
583 if (!device->manager) return; /* already deleted */
585 LIST_FOR_EACH_ENTRY_SAFE( file, next, &device->files, struct device_file, entry )
586 delete_file( file );
588 unlink_named_object( &device->obj );
589 list_remove( &device->entry );
590 device->manager = NULL;
594 static void device_manager_dump( struct object *obj, int verbose )
596 fprintf( stderr, "Device manager\n" );
599 static int device_manager_signaled( struct object *obj, struct wait_queue_entry *entry )
601 struct device_manager *manager = (struct device_manager *)obj;
603 return !list_empty( &manager->requests );
606 static void device_manager_destroy( struct object *obj )
608 struct device_manager *manager = (struct device_manager *)obj;
609 struct list *ptr;
611 while ((ptr = list_head( &manager->devices )))
613 struct device *device = LIST_ENTRY( ptr, struct device, entry );
614 delete_device( device );
618 static struct device_manager *create_device_manager(void)
620 struct device_manager *manager;
622 if ((manager = alloc_object( &device_manager_ops )))
624 list_init( &manager->devices );
625 list_init( &manager->requests );
627 return manager;
631 /* create a device manager */
632 DECL_HANDLER(create_device_manager)
634 struct device_manager *manager = create_device_manager();
636 if (manager)
638 reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
639 release_object( manager );
644 /* create a device */
645 DECL_HANDLER(create_device)
647 struct device *device;
648 struct unicode_str name = get_req_unicode_str();
649 struct device_manager *manager;
650 struct object *root = NULL;
652 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
653 0, &device_manager_ops )))
654 return;
656 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir )))
658 release_object( manager );
659 return;
662 if ((device = create_device( root, &name, manager, req->attributes )))
664 device->user_ptr = req->user_ptr;
665 reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
666 release_object( device );
669 if (root) release_object( root );
670 release_object( manager );
674 /* delete a device */
675 DECL_HANDLER(delete_device)
677 struct device *device;
679 if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
681 delete_device( device );
682 release_object( device );
687 /* retrieve the next pending device irp request */
688 DECL_HANDLER(get_next_device_request)
690 struct irp_call *irp;
691 struct device_manager *manager;
692 struct list *ptr;
693 struct iosb *iosb;
695 reply->params.major = IRP_MJ_MAXIMUM_FUNCTION + 1;
697 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
698 0, &device_manager_ops )))
699 return;
701 if (req->prev)
703 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->prev, 0, &irp_call_ops )))
705 set_irp_result( irp, req->status, NULL, 0, 0 );
706 close_handle( current->process, req->prev ); /* avoid an extra round-trip for close */
707 release_object( irp );
709 clear_error();
712 if ((ptr = list_head( &manager->requests )))
714 irp = LIST_ENTRY( ptr, struct irp_call, mgr_entry );
715 if (irp->thread)
717 reply->client_pid = get_process_id( irp->thread->process );
718 reply->client_tid = get_thread_id( irp->thread );
720 reply->params = irp->params;
721 iosb = irp->iosb;
722 reply->in_size = iosb->in_size;
723 reply->out_size = iosb->out_size;
724 if (iosb->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
725 else if ((reply->next = alloc_handle( current->process, irp, 0, 0 )))
727 set_reply_data_ptr( iosb->in_data, iosb->in_size );
728 iosb->in_data = NULL;
729 iosb->in_size = 0;
730 list_remove( &irp->mgr_entry );
731 list_init( &irp->mgr_entry );
734 else set_error( STATUS_PENDING );
736 release_object( manager );
740 /* store results of an async irp */
741 DECL_HANDLER(set_irp_result)
743 struct irp_call *irp;
745 if ((irp = (struct irp_call *)get_handle_obj( current->process, req->handle, 0, &irp_call_ops )))
747 if (irp->file) set_file_user_ptr( irp->file, req->file_ptr );
748 set_irp_result( irp, req->status, get_req_data(), get_req_data_size(), req->size );
749 close_handle( current->process, req->handle ); /* avoid an extra round-trip for close */
750 release_object( irp );