inetcomm: Add IStream_CopyTo to the sub-stream implementation.
[wine.git] / server / device.c
blobabf0ac54170b3a1e96244d023ddb3c3cff129373
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"
31 #include "object.h"
32 #include "file.h"
33 #include "handle.h"
34 #include "request.h"
36 struct ioctl_call
38 struct object obj; /* object header */
39 struct list dev_entry; /* entry in device queue */
40 struct list mgr_entry; /* entry in manager queue */
41 struct device *device; /* device containing this ioctl */
42 struct thread *thread; /* thread that queued the ioctl */
43 void *user_arg; /* user arg used to identify the request */
44 struct async *async; /* pending async op */
45 ioctl_code_t code; /* ioctl code */
46 unsigned int status; /* resulting status (or STATUS_PENDING) */
47 data_size_t in_size; /* size of input data */
48 void *in_data; /* input data */
49 data_size_t out_size; /* size of output data */
50 void *out_data; /* output data */
53 static void ioctl_call_dump( struct object *obj, int verbose );
54 static int ioctl_call_signaled( struct object *obj, struct thread *thread );
55 static void ioctl_call_destroy( struct object *obj );
57 static const struct object_ops ioctl_call_ops =
59 sizeof(struct ioctl_call), /* size */
60 ioctl_call_dump, /* dump */
61 no_get_type, /* get_type */
62 add_queue, /* add_queue */
63 remove_queue, /* remove_queue */
64 ioctl_call_signaled, /* signaled */
65 no_satisfied, /* satisfied */
66 no_signal, /* signal */
67 no_get_fd, /* get_fd */
68 no_map_access, /* map_access */
69 default_get_sd, /* get_sd */
70 default_set_sd, /* set_sd */
71 no_lookup_name, /* lookup_name */
72 no_open_file, /* open_file */
73 no_close_handle, /* close_handle */
74 ioctl_call_destroy /* destroy */
78 struct device_manager
80 struct object obj; /* object header */
81 struct list devices; /* list of devices */
82 struct list requests; /* list of pending ioctls across all devices */
85 static void device_manager_dump( struct object *obj, int verbose );
86 static int device_manager_signaled( struct object *obj, struct thread *thread );
87 static void device_manager_destroy( struct object *obj );
89 static const struct object_ops device_manager_ops =
91 sizeof(struct device_manager), /* size */
92 device_manager_dump, /* dump */
93 no_get_type, /* get_type */
94 add_queue, /* add_queue */
95 remove_queue, /* remove_queue */
96 device_manager_signaled, /* signaled */
97 no_satisfied, /* satisfied */
98 no_signal, /* signal */
99 no_get_fd, /* get_fd */
100 no_map_access, /* map_access */
101 default_get_sd, /* get_sd */
102 default_set_sd, /* set_sd */
103 no_lookup_name, /* lookup_name */
104 no_open_file, /* open_file */
105 no_close_handle, /* close_handle */
106 device_manager_destroy /* destroy */
110 struct device
112 struct object obj; /* object header */
113 struct device_manager *manager; /* manager for this device (or NULL if deleted) */
114 struct fd *fd; /* file descriptor for ioctl */
115 void *user_ptr; /* opaque ptr for client side */
116 struct list entry; /* entry in device manager list */
117 struct list requests; /* list of pending ioctl requests */
120 static void device_dump( struct object *obj, int verbose );
121 static struct object_type *device_get_type( struct object *obj );
122 static struct fd *device_get_fd( struct object *obj );
123 static void device_destroy( struct object *obj );
124 static struct object *device_open_file( struct object *obj, unsigned int access,
125 unsigned int sharing, unsigned int options );
126 static enum server_fd_type device_get_fd_type( struct fd *fd );
127 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
128 const void *data, data_size_t size );
130 static const struct object_ops device_ops =
132 sizeof(struct device), /* size */
133 device_dump, /* dump */
134 device_get_type, /* get_type */
135 no_add_queue, /* add_queue */
136 NULL, /* remove_queue */
137 NULL, /* signaled */
138 no_satisfied, /* satisfied */
139 no_signal, /* signal */
140 device_get_fd, /* get_fd */
141 default_fd_map_access, /* map_access */
142 default_get_sd, /* get_sd */
143 default_set_sd, /* set_sd */
144 no_lookup_name, /* lookup_name */
145 device_open_file, /* open_file */
146 no_close_handle, /* close_handle */
147 device_destroy /* destroy */
150 static const struct fd_ops device_fd_ops =
152 default_fd_get_poll_events, /* get_poll_events */
153 default_poll_event, /* poll_event */
154 no_flush, /* flush */
155 device_get_fd_type, /* get_fd_type */
156 device_ioctl, /* ioctl */
157 default_fd_queue_async, /* queue_async */
158 default_fd_reselect_async, /* reselect_async */
159 default_fd_cancel_async /* cancel_async */
163 static void ioctl_call_dump( struct object *obj, int verbose )
165 struct ioctl_call *ioctl = (struct ioctl_call *)obj;
166 fprintf( stderr, "Ioctl call code=%08x device=%p\n", ioctl->code, ioctl->device );
169 static int ioctl_call_signaled( struct object *obj, struct thread *thread )
171 struct ioctl_call *ioctl = (struct ioctl_call *)obj;
173 return !ioctl->device; /* device is cleared once the ioctl has completed */
176 static void ioctl_call_destroy( struct object *obj )
178 struct ioctl_call *ioctl = (struct ioctl_call *)obj;
180 free( ioctl->in_data );
181 free( ioctl->out_data );
182 if (ioctl->async)
184 async_terminate( ioctl->async, STATUS_CANCELLED );
185 release_object( ioctl->async );
187 if (ioctl->device) release_object( ioctl->device );
188 release_object( ioctl->thread );
191 static struct ioctl_call *create_ioctl( struct device *device, ioctl_code_t code,
192 const void *in_data, data_size_t in_size,
193 data_size_t out_size )
195 struct ioctl_call *ioctl;
197 if ((ioctl = alloc_object( &ioctl_call_ops )))
199 ioctl->device = (struct device *)grab_object( device );
200 ioctl->code = code;
201 ioctl->async = NULL;
202 ioctl->status = STATUS_PENDING;
203 ioctl->in_size = in_size;
204 ioctl->in_data = NULL;
205 ioctl->out_size = out_size;
206 ioctl->out_data = NULL;
208 if (ioctl->in_size && !(ioctl->in_data = memdup( in_data, in_size )))
210 release_object( ioctl );
211 ioctl = NULL;
214 return ioctl;
217 static void set_ioctl_result( struct ioctl_call *ioctl, unsigned int status,
218 const void *out_data, data_size_t out_size )
220 struct device *device = ioctl->device;
222 if (!device) return; /* already finished */
224 /* FIXME: handle the STATUS_PENDING case */
225 ioctl->status = status;
226 ioctl->out_size = min( ioctl->out_size, out_size );
227 if (ioctl->out_size && !(ioctl->out_data = memdup( out_data, ioctl->out_size )))
228 ioctl->out_size = 0;
229 release_object( device );
230 ioctl->device = NULL;
231 if (ioctl->async)
233 if (ioctl->out_size) status = STATUS_ALERTED;
234 async_terminate( ioctl->async, status );
235 release_object( ioctl->async );
236 ioctl->async = NULL;
238 wake_up( &ioctl->obj, 0 );
240 if (status != STATUS_ALERTED)
242 /* remove it from the device queue */
243 /* (for STATUS_ALERTED this will be done in get_ioctl_result) */
244 list_remove( &ioctl->dev_entry );
245 release_object( ioctl ); /* no longer on the device queue */
250 static void device_dump( struct object *obj, int verbose )
252 struct device *device = (struct device *)obj;
254 fprintf( stderr, "Device " );
255 dump_object_name( &device->obj );
256 fputc( '\n', stderr );
259 static struct object_type *device_get_type( struct object *obj )
261 static const WCHAR name[] = {'D','e','v','i','c','e'};
262 static const struct unicode_str str = { name, sizeof(name) };
263 return get_object_type( &str );
266 static struct fd *device_get_fd( struct object *obj )
268 struct device *device = (struct device *)obj;
270 return (struct fd *)grab_object( device->fd );
273 static void device_destroy( struct object *obj )
275 struct device *device = (struct device *)obj;
276 struct ioctl_call *ioctl, *next;
278 LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &device->requests, struct ioctl_call, dev_entry )
280 list_remove( &ioctl->dev_entry );
281 release_object( ioctl ); /* no longer on the device queue */
283 if (device->fd) release_object( device->fd );
284 if (device->manager) list_remove( &device->entry );
287 static struct object *device_open_file( struct object *obj, unsigned int access,
288 unsigned int sharing, unsigned int options )
290 return grab_object( obj );
293 static enum server_fd_type device_get_fd_type( struct fd *fd )
295 return FD_TYPE_DEVICE;
298 static struct ioctl_call *find_ioctl_call( struct device *device, struct thread *thread,
299 void *user_arg )
301 struct ioctl_call *ioctl;
303 LIST_FOR_EACH_ENTRY( ioctl, &device->requests, struct ioctl_call, dev_entry )
304 if (ioctl->thread == thread && ioctl->user_arg == user_arg) return ioctl;
306 set_error( STATUS_INVALID_PARAMETER );
307 return NULL;
310 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
311 const void *data, data_size_t size )
313 struct device *device = get_fd_user( fd );
314 struct ioctl_call *ioctl;
315 obj_handle_t handle;
317 if (!device->manager) /* it has been deleted */
319 set_error( STATUS_FILE_DELETED );
320 return 0;
323 if (!(ioctl = create_ioctl( device, code, data, size, get_reply_max_size() )))
324 return 0;
326 ioctl->thread = (struct thread *)grab_object( current );
327 ioctl->user_arg = async_data->arg;
329 if (!(handle = alloc_handle( current->process, ioctl, SYNCHRONIZE, 0 )))
331 release_object( ioctl );
332 return 0;
335 if (!(ioctl->async = fd_queue_async( device->fd, async_data, ASYNC_TYPE_WAIT, 0 )))
337 close_handle( current->process, handle );
338 release_object( ioctl );
339 return 0;
341 list_add_tail( &device->requests, &ioctl->dev_entry );
342 list_add_tail( &device->manager->requests, &ioctl->mgr_entry );
343 if (list_head( &device->manager->requests ) == &ioctl->mgr_entry) /* first one */
344 wake_up( &device->manager->obj, 0 );
345 /* don't release ioctl since it is now queued in the device */
346 set_error( STATUS_PENDING );
347 return handle;
350 static struct device *create_device( struct directory *root, const struct unicode_str *name,
351 struct device_manager *manager, unsigned int attr )
353 struct device *device;
355 if ((device = create_named_object_dir( root, name, attr, &device_ops )))
357 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
359 /* initialize it if it didn't already exist */
360 device->manager = manager;
361 list_add_tail( &manager->devices, &device->entry );
362 list_init( &device->requests );
363 if (!(device->fd = alloc_pseudo_fd( &device_fd_ops, &device->obj, 0 )))
365 release_object( device );
366 device = NULL;
370 return device;
373 static void delete_device( struct device *device )
375 struct ioctl_call *ioctl, *next;
377 if (!device->manager) return; /* already deleted */
379 /* terminate all pending requests */
380 LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &device->requests, struct ioctl_call, dev_entry )
382 list_remove( &ioctl->mgr_entry );
383 set_ioctl_result( ioctl, STATUS_FILE_DELETED, NULL, 0 );
385 unlink_named_object( &device->obj );
386 list_remove( &device->entry );
387 device->manager = NULL;
391 static void device_manager_dump( struct object *obj, int verbose )
393 fprintf( stderr, "Device manager\n" );
396 static int device_manager_signaled( struct object *obj, struct thread *thread )
398 struct device_manager *manager = (struct device_manager *)obj;
400 return !list_empty( &manager->requests );
403 static void device_manager_destroy( struct object *obj )
405 struct device_manager *manager = (struct device_manager *)obj;
406 struct list *ptr;
408 while ((ptr = list_head( &manager->devices )))
410 struct device *device = LIST_ENTRY( ptr, struct device, entry );
411 delete_device( device );
415 static struct device_manager *create_device_manager(void)
417 struct device_manager *manager;
419 if ((manager = alloc_object( &device_manager_ops )))
421 list_init( &manager->devices );
422 list_init( &manager->requests );
424 return manager;
428 /* create a device manager */
429 DECL_HANDLER(create_device_manager)
431 struct device_manager *manager = create_device_manager();
433 if (manager)
435 reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
436 release_object( manager );
441 /* create a device */
442 DECL_HANDLER(create_device)
444 struct device *device;
445 struct unicode_str name;
446 struct device_manager *manager;
447 struct directory *root = NULL;
449 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
450 0, &device_manager_ops )))
451 return;
453 get_req_unicode_str( &name );
454 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
456 release_object( manager );
457 return;
460 if ((device = create_device( root, &name, manager, req->attributes )))
462 device->user_ptr = req->user_ptr;
463 reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
464 release_object( device );
467 if (root) release_object( root );
468 release_object( manager );
472 /* delete a device */
473 DECL_HANDLER(delete_device)
475 struct device *device;
477 if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
479 delete_device( device );
480 release_object( device );
485 /* retrieve the next pending device ioctl request */
486 DECL_HANDLER(get_next_device_request)
488 struct ioctl_call *ioctl;
489 struct device_manager *manager;
490 struct list *ptr;
492 if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
493 0, &device_manager_ops )))
494 return;
496 if (req->prev)
498 if ((ioctl = (struct ioctl_call *)get_handle_obj( current->process, req->prev,
499 0, &ioctl_call_ops )))
501 set_ioctl_result( ioctl, req->status, get_req_data(), get_req_data_size() );
502 close_handle( current->process, req->prev ); /* avoid an extra round-trip for close */
503 release_object( ioctl );
505 clear_error();
508 if ((ptr = list_head( &manager->requests )))
510 ioctl = LIST_ENTRY( ptr, struct ioctl_call, mgr_entry );
511 reply->code = ioctl->code;
512 reply->user_ptr = ioctl->device->user_ptr;
513 reply->in_size = ioctl->in_size;
514 reply->out_size = ioctl->out_size;
515 if (ioctl->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
516 else if ((reply->next = alloc_handle( current->process, ioctl, 0, 0 )))
518 set_reply_data_ptr( ioctl->in_data, ioctl->in_size );
519 ioctl->in_data = NULL;
520 ioctl->in_size = 0;
521 list_remove( &ioctl->mgr_entry );
522 list_init( &ioctl->mgr_entry );
525 else set_error( STATUS_PENDING );
527 release_object( manager );
531 /* retrieve results of an async ioctl */
532 DECL_HANDLER(get_ioctl_result)
534 struct device *device;
535 struct ioctl_call *ioctl;
537 if (!(device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
538 return;
540 if ((ioctl = find_ioctl_call( device, current, req->user_arg )))
542 if (ioctl->out_data)
544 data_size_t size = min( ioctl->out_size, get_reply_max_size() );
545 if (size)
547 set_reply_data_ptr( ioctl->out_data, size );
548 ioctl->out_data = NULL;
551 set_error( ioctl->status );
552 list_remove( &ioctl->dev_entry );
553 release_object( ioctl ); /* no longer on the device queue */
555 release_object( device );