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