user32: Fix returned value of LB_DIR.
[wine] / server / device.c
1 /*
2  * Server-side device support
3  *
4  * Copyright (C) 2007 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <stdarg.h>
25
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winternl.h"
30
31 #include "object.h"
32 #include "file.h"
33 #include "handle.h"
34 #include "request.h"
35
36 struct ioctl_call
37 {
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 */
51 };
52
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 );
56
57 static const struct object_ops ioctl_call_ops =
58 {
59     sizeof(struct ioctl_call),        /* size */
60     ioctl_call_dump,                  /* dump */
61     add_queue,                        /* add_queue */
62     remove_queue,                     /* remove_queue */
63     ioctl_call_signaled,              /* signaled */
64     no_satisfied,                     /* satisfied */
65     no_signal,                        /* signal */
66     no_get_fd,                        /* get_fd */
67     no_map_access,                    /* map_access */
68     default_get_sd,                   /* get_sd */
69     default_set_sd,                   /* set_sd */
70     no_lookup_name,                   /* lookup_name */
71     no_open_file,                     /* open_file */
72     no_close_handle,                  /* close_handle */
73     ioctl_call_destroy                /* destroy */
74 };
75
76
77 struct device_manager
78 {
79     struct object          obj;           /* object header */
80     struct list            devices;       /* list of devices */
81     struct list            requests;      /* list of pending ioctls across all devices */
82 };
83
84 static void device_manager_dump( struct object *obj, int verbose );
85 static int device_manager_signaled( struct object *obj, struct thread *thread );
86 static void device_manager_destroy( struct object *obj );
87
88 static const struct object_ops device_manager_ops =
89 {
90     sizeof(struct device_manager),    /* size */
91     device_manager_dump,              /* dump */
92     add_queue,                        /* add_queue */
93     remove_queue,                     /* remove_queue */
94     device_manager_signaled,          /* signaled */
95     no_satisfied,                     /* satisfied */
96     no_signal,                        /* signal */
97     no_get_fd,                        /* get_fd */
98     no_map_access,                    /* map_access */
99     default_get_sd,                   /* get_sd */
100     default_set_sd,                   /* set_sd */
101     no_lookup_name,                   /* lookup_name */
102     no_open_file,                     /* open_file */
103     no_close_handle,                  /* close_handle */
104     device_manager_destroy            /* destroy */
105 };
106
107
108 struct device
109 {
110     struct object          obj;           /* object header */
111     struct device_manager *manager;       /* manager for this device (or NULL if deleted) */
112     struct fd             *fd;            /* file descriptor for ioctl */
113     void                  *user_ptr;      /* opaque ptr for client side */
114     struct list            entry;         /* entry in device manager list */
115     struct list            requests;      /* list of pending ioctl requests */
116 };
117
118 static void device_dump( struct object *obj, int verbose );
119 static struct fd *device_get_fd( struct object *obj );
120 static void device_destroy( struct object *obj );
121 static struct object *device_open_file( struct object *obj, unsigned int access,
122                                         unsigned int sharing, unsigned int options );
123 static enum server_fd_type device_get_fd_type( struct fd *fd );
124 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
125                                   const void *data, data_size_t size );
126
127 static const struct object_ops device_ops =
128 {
129     sizeof(struct device),            /* size */
130     device_dump,                      /* dump */
131     no_add_queue,                     /* add_queue */
132     NULL,                             /* remove_queue */
133     NULL,                             /* signaled */
134     no_satisfied,                     /* satisfied */
135     no_signal,                        /* signal */
136     device_get_fd,                    /* get_fd */
137     default_fd_map_access,            /* map_access */
138     default_get_sd,                   /* get_sd */
139     default_set_sd,                   /* set_sd */
140     no_lookup_name,                   /* lookup_name */
141     device_open_file,                 /* open_file */
142     no_close_handle,                  /* close_handle */
143     device_destroy                    /* destroy */
144 };
145
146 static const struct fd_ops device_fd_ops =
147 {
148     default_fd_get_poll_events,       /* get_poll_events */
149     default_poll_event,               /* poll_event */
150     no_flush,                         /* flush */
151     device_get_fd_type,               /* get_fd_type */
152     device_ioctl,                     /* ioctl */
153     default_fd_queue_async,           /* queue_async */
154     default_fd_reselect_async,        /* reselect_async */
155     default_fd_cancel_async           /* cancel_async */
156 };
157
158
159 static void ioctl_call_dump( struct object *obj, int verbose )
160 {
161     struct ioctl_call *ioctl = (struct ioctl_call *)obj;
162     fprintf( stderr, "Ioctl call code=%08x device=%p\n", ioctl->code, ioctl->device );
163 }
164
165 static int ioctl_call_signaled( struct object *obj, struct thread *thread )
166 {
167     struct ioctl_call *ioctl = (struct ioctl_call *)obj;
168
169     return !ioctl->device;  /* device is cleared once the ioctl has completed */
170 }
171
172 static void ioctl_call_destroy( struct object *obj )
173 {
174     struct ioctl_call *ioctl = (struct ioctl_call *)obj;
175
176     free( ioctl->in_data );
177     free( ioctl->out_data );
178     if (ioctl->async)
179     {
180         async_terminate( ioctl->async, STATUS_CANCELLED );
181         release_object( ioctl->async );
182     }
183     if (ioctl->device) release_object( ioctl->device );
184     release_object( ioctl->thread );
185 }
186
187 static struct ioctl_call *create_ioctl( struct device *device, ioctl_code_t code,
188                                         const void *in_data, data_size_t in_size,
189                                         data_size_t out_size )
190 {
191     struct ioctl_call *ioctl;
192
193     if ((ioctl = alloc_object( &ioctl_call_ops )))
194     {
195         ioctl->device   = (struct device *)grab_object( device );
196         ioctl->code     = code;
197         ioctl->async    = NULL;
198         ioctl->status   = STATUS_PENDING;
199         ioctl->in_size  = in_size;
200         ioctl->in_data  = NULL;
201         ioctl->out_size = out_size;
202         ioctl->out_data = NULL;
203
204         if (ioctl->in_size && !(ioctl->in_data = memdup( in_data, in_size )))
205         {
206             release_object( ioctl );
207             ioctl = NULL;
208         }
209     }
210     return ioctl;
211 }
212
213 static void set_ioctl_result( struct ioctl_call *ioctl, unsigned int status,
214                               const void *out_data, data_size_t out_size )
215 {
216     struct device *device = ioctl->device;
217
218     if (!device) return;  /* already finished */
219
220     /* FIXME: handle the STATUS_PENDING case */
221     ioctl->status = status;
222     ioctl->out_size = min( ioctl->out_size, out_size );
223     if (ioctl->out_size && !(ioctl->out_data = memdup( out_data, ioctl->out_size )))
224         ioctl->out_size = 0;
225     release_object( device );
226     ioctl->device = NULL;
227     if (ioctl->async)
228     {
229         if (ioctl->out_size) status = STATUS_ALERTED;
230         async_terminate( ioctl->async, status );
231         release_object( ioctl->async );
232         ioctl->async = NULL;
233     }
234     wake_up( &ioctl->obj, 0 );
235
236     if (status != STATUS_ALERTED)
237     {
238         /* remove it from the device queue */
239         /* (for STATUS_ALERTED this will be done in get_ioctl_result) */
240         list_remove( &ioctl->dev_entry );
241         release_object( ioctl );  /* no longer on the device queue */
242     }
243 }
244
245
246 static void device_dump( struct object *obj, int verbose )
247 {
248     struct device *device = (struct device *)obj;
249
250     fprintf( stderr, "Device " );
251     dump_object_name( &device->obj );
252     fputc( '\n', stderr );
253 }
254
255 static struct fd *device_get_fd( struct object *obj )
256 {
257     struct device *device = (struct device *)obj;
258
259     return (struct fd *)grab_object( device->fd );
260 }
261
262 static void device_destroy( struct object *obj )
263 {
264     struct device *device = (struct device *)obj;
265     struct ioctl_call *ioctl, *next;
266
267     LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &device->requests, struct ioctl_call, dev_entry )
268     {
269         list_remove( &ioctl->dev_entry );
270         release_object( ioctl );  /* no longer on the device queue */
271     }
272     if (device->fd) release_object( device->fd );
273     if (device->manager) list_remove( &device->entry );
274 }
275
276 static struct object *device_open_file( struct object *obj, unsigned int access,
277                                         unsigned int sharing, unsigned int options )
278 {
279     return grab_object( obj );
280 }
281
282 static enum server_fd_type device_get_fd_type( struct fd *fd )
283 {
284     return FD_TYPE_DEVICE;
285 }
286
287 static struct ioctl_call *find_ioctl_call( struct device *device, struct thread *thread,
288                                            void *user_arg )
289 {
290     struct ioctl_call *ioctl;
291
292     LIST_FOR_EACH_ENTRY( ioctl, &device->requests, struct ioctl_call, dev_entry )
293         if (ioctl->thread == thread && ioctl->user_arg == user_arg) return ioctl;
294
295     set_error( STATUS_INVALID_PARAMETER );
296     return NULL;
297 }
298
299 static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
300                                   const void *data, data_size_t size )
301 {
302     struct device *device = get_fd_user( fd );
303     struct ioctl_call *ioctl;
304     obj_handle_t handle;
305
306     if (!device->manager)  /* it has been deleted */
307     {
308         set_error( STATUS_FILE_DELETED );
309         return 0;
310     }
311
312     if (!(ioctl = create_ioctl( device, code, data, size, get_reply_max_size() )))
313         return 0;
314
315     ioctl->thread   = (struct thread *)grab_object( current );
316     ioctl->user_arg = async_data->arg;
317
318     if (!(handle = alloc_handle( current->process, ioctl, SYNCHRONIZE, 0 )))
319     {
320         release_object( ioctl );
321         return 0;
322     }
323
324     if (!(ioctl->async = fd_queue_async( device->fd, async_data, ASYNC_TYPE_WAIT, 0 )))
325     {
326         close_handle( current->process, handle );
327         release_object( ioctl );
328         return 0;
329     }
330     list_add_tail( &device->requests, &ioctl->dev_entry );
331     list_add_tail( &device->manager->requests, &ioctl->mgr_entry );
332     if (list_head( &device->manager->requests ) == &ioctl->mgr_entry)  /* first one */
333         wake_up( &device->manager->obj, 0 );
334     /* don't release ioctl since it is now queued in the device */
335     set_error( STATUS_PENDING );
336     return handle;
337 }
338
339 static struct device *create_device( struct directory *root, const struct unicode_str *name,
340                                      struct device_manager *manager, unsigned int attr )
341 {
342     struct device *device;
343
344     if ((device = create_named_object_dir( root, name, attr, &device_ops )))
345     {
346         if (get_error() != STATUS_OBJECT_NAME_EXISTS)
347         {
348             /* initialize it if it didn't already exist */
349             device->manager = manager;
350             list_add_tail( &manager->devices, &device->entry );
351             list_init( &device->requests );
352             if (!(device->fd = alloc_pseudo_fd( &device_fd_ops, &device->obj, 0 )))
353             {
354                 release_object( device );
355                 device = NULL;
356             }
357         }
358     }
359     return device;
360 }
361
362 static void delete_device( struct device *device )
363 {
364     struct ioctl_call *ioctl, *next;
365
366     if (!device->manager) return;  /* already deleted */
367
368     /* terminate all pending requests */
369     LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &device->requests, struct ioctl_call, dev_entry )
370     {
371         list_remove( &ioctl->mgr_entry );
372         set_ioctl_result( ioctl, STATUS_FILE_DELETED, NULL, 0 );
373     }
374     unlink_named_object( &device->obj );
375     list_remove( &device->entry );
376     device->manager = NULL;
377 }
378
379
380 static void device_manager_dump( struct object *obj, int verbose )
381 {
382     fprintf( stderr, "Device manager\n" );
383 }
384
385 static int device_manager_signaled( struct object *obj, struct thread *thread )
386 {
387     struct device_manager *manager = (struct device_manager *)obj;
388
389     return !list_empty( &manager->requests );
390 }
391
392 static void device_manager_destroy( struct object *obj )
393 {
394     struct device_manager *manager = (struct device_manager *)obj;
395     struct list *ptr;
396
397     while ((ptr = list_head( &manager->devices )))
398     {
399         struct device *device = LIST_ENTRY( ptr, struct device, entry );
400         delete_device( device );
401     }
402 }
403
404 static struct device_manager *create_device_manager(void)
405 {
406     struct device_manager *manager;
407
408     if ((manager = alloc_object( &device_manager_ops )))
409     {
410         list_init( &manager->devices );
411         list_init( &manager->requests );
412     }
413     return manager;
414 }
415
416
417 /* create a device manager */
418 DECL_HANDLER(create_device_manager)
419 {
420     struct device_manager *manager = create_device_manager();
421
422     if (manager)
423     {
424         reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
425         release_object( manager );
426     }
427 }
428
429
430 /* create a device */
431 DECL_HANDLER(create_device)
432 {
433     struct device *device;
434     struct unicode_str name;
435     struct device_manager *manager;
436     struct directory *root = NULL;
437
438     if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
439                                                              0, &device_manager_ops )))
440         return;
441
442     get_req_unicode_str( &name );
443     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
444     {
445         release_object( manager );
446         return;
447     }
448
449     if ((device = create_device( root, &name, manager, req->attributes )))
450     {
451         device->user_ptr = req->user_ptr;
452         reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
453         release_object( device );
454     }
455
456     if (root) release_object( root );
457     release_object( manager );
458 }
459
460
461 /* delete a device */
462 DECL_HANDLER(delete_device)
463 {
464     struct device *device;
465
466     if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
467     {
468         delete_device( device );
469         release_object( device );
470     }
471 }
472
473
474 /* retrieve the next pending device ioctl request */
475 DECL_HANDLER(get_next_device_request)
476 {
477     struct ioctl_call *ioctl;
478     struct device_manager *manager;
479     struct list *ptr;
480
481     if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
482                                                              0, &device_manager_ops )))
483         return;
484
485     if (req->prev)
486     {
487         if ((ioctl = (struct ioctl_call *)get_handle_obj( current->process, req->prev,
488                                                           0, &ioctl_call_ops )))
489         {
490             set_ioctl_result( ioctl, req->status, get_req_data(), get_req_data_size() );
491             close_handle( current->process, req->prev );  /* avoid an extra round-trip for close */
492             release_object( ioctl );
493         }
494         clear_error();
495     }
496
497     if ((ptr = list_head( &manager->requests )))
498     {
499         ioctl = LIST_ENTRY( ptr, struct ioctl_call, mgr_entry );
500         reply->code = ioctl->code;
501         reply->user_ptr = ioctl->device->user_ptr;
502         reply->in_size = ioctl->in_size;
503         reply->out_size = ioctl->out_size;
504         if (ioctl->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
505         else if ((reply->next = alloc_handle( current->process, ioctl, 0, 0 )))
506         {
507             set_reply_data_ptr( ioctl->in_data, ioctl->in_size );
508             ioctl->in_data = NULL;
509             ioctl->in_size = 0;
510             list_remove( &ioctl->mgr_entry );
511             list_init( &ioctl->mgr_entry );
512         }
513     }
514     else set_error( STATUS_PENDING );
515
516     release_object( manager );
517 }
518
519
520 /* retrieve results of an async ioctl */
521 DECL_HANDLER(get_ioctl_result)
522 {
523     struct device *device;
524     struct ioctl_call *ioctl;
525
526     if (!(device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
527         return;
528
529     if ((ioctl = find_ioctl_call( device, current, req->user_arg )))
530     {
531         if (ioctl->out_data)
532         {
533             data_size_t size = min( ioctl->out_size, get_reply_max_size() );
534             if (size)
535             {
536                 set_reply_data_ptr( ioctl->out_data, size );
537                 ioctl->out_data = NULL;
538             }
539         }
540         set_error( ioctl->status );
541         list_remove( &ioctl->dev_entry );
542         release_object( ioctl );  /* no longer on the device queue */
543     }
544     release_object( device );
545 }