server: Make the create_async function take an absolute timeout.
[wine] / server / mailslot.c
1 /*
2  * Server-side mailslot management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  * Copyright (C) 2005 Mike McCormack
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "wine/unicode.h"
28
29 #include <assert.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
40 #endif
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
43 #endif
44 #ifdef HAVE_SYS_FILIO_H
45 #include <sys/filio.h>
46 #endif
47 #include "windef.h"
48 #include "winternl.h"
49
50 #include "file.h"
51 #include "handle.h"
52 #include "thread.h"
53 #include "request.h"
54
55 struct mailslot
56 {
57     struct object       obj;
58     struct fd          *fd;
59     struct fd          *write_fd;
60     unsigned int        max_msgsize;
61     int                 read_timeout;
62     struct list         writers;
63 };
64
65 /* mailslot functions */
66 static void mailslot_dump( struct object*, int );
67 static struct fd *mailslot_get_fd( struct object * );
68 static unsigned int mailslot_map_access( struct object *obj, unsigned int access );
69 static void mailslot_destroy( struct object * );
70
71 static const struct object_ops mailslot_ops =
72 {
73     sizeof(struct mailslot),   /* size */
74     mailslot_dump,             /* dump */
75     default_fd_add_queue,      /* add_queue */
76     default_fd_remove_queue,   /* remove_queue */
77     default_fd_signaled,       /* signaled */
78     no_satisfied,              /* satisfied */
79     no_signal,                 /* signal */
80     mailslot_get_fd,           /* get_fd */
81     mailslot_map_access,       /* map_access */
82     no_lookup_name,            /* lookup_name */
83     no_close_handle,           /* close_handle */
84     mailslot_destroy           /* destroy */
85 };
86
87 static int mailslot_get_info( struct fd * );
88 static void mailslot_queue_async( struct fd *, void*, void*, void*, int, int );
89
90 static const struct fd_ops mailslot_fd_ops =
91 {
92     default_fd_get_poll_events, /* get_poll_events */
93     default_poll_event,         /* poll_event */
94     no_flush,                   /* flush */
95     mailslot_get_info,          /* get_file_info */
96     mailslot_queue_async,       /* queue_async */
97     default_fd_cancel_async     /* cancel_async */
98 };
99
100
101 struct mail_writer
102 {
103     struct object         obj;
104     struct mailslot      *mailslot;
105     struct list           entry;
106     unsigned int          access;
107     unsigned int          sharing;
108 };
109
110 static void mail_writer_dump( struct object *obj, int verbose );
111 static struct fd *mail_writer_get_fd( struct object *obj );
112 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
113 static void mail_writer_destroy( struct object *obj);
114
115 static const struct object_ops mail_writer_ops =
116 {
117     sizeof(struct mail_writer), /* size */
118     mail_writer_dump,           /* dump */
119     no_add_queue,               /* add_queue */
120     NULL,                       /* remove_queue */
121     NULL,                       /* signaled */
122     NULL,                       /* satisfied */
123     no_signal,                  /* signal */
124     mail_writer_get_fd,         /* get_fd */
125     mail_writer_map_access,     /* map_access */
126     no_lookup_name,             /* lookup_name */
127     no_close_handle,            /* close_handle */
128     mail_writer_destroy         /* destroy */
129 };
130
131 static int mail_writer_get_info( struct fd *fd );
132
133 static const struct fd_ops mail_writer_fd_ops =
134 {
135     NULL,                        /* get_poll_events */
136     NULL,                        /* poll_event */
137     no_flush,                    /* flush */
138     mail_writer_get_info,        /* get_file_info */
139     no_queue_async,              /* queue_async */
140     NULL                         /* cancel_async */
141 };
142
143
144 struct mailslot_device
145 {
146     struct object       obj;         /* object header */
147     struct fd          *fd;          /* pseudo-fd for ioctls */
148     struct namespace   *mailslots;   /* mailslot namespace */
149 };
150
151 static void mailslot_device_dump( struct object *obj, int verbose );
152 static struct fd *mailslot_device_get_fd( struct object *obj );
153 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
154                                                    unsigned int attr );
155 static void mailslot_device_destroy( struct object *obj );
156 static int mailslot_device_get_file_info( struct fd *fd );
157
158 static const struct object_ops mailslot_device_ops =
159 {
160     sizeof(struct mailslot_device), /* size */
161     mailslot_device_dump,           /* dump */
162     no_add_queue,                   /* add_queue */
163     NULL,                           /* remove_queue */
164     NULL,                           /* signaled */
165     no_satisfied,                   /* satisfied */
166     no_signal,                      /* signal */
167     mailslot_device_get_fd,         /* get_fd */
168     no_map_access,                  /* map_access */
169     mailslot_device_lookup_name,    /* lookup_name */
170     no_close_handle,                /* close_handle */
171     mailslot_device_destroy         /* destroy */
172 };
173
174 static const struct fd_ops mailslot_device_fd_ops =
175 {
176     default_fd_get_poll_events,     /* get_poll_events */
177     default_poll_event,             /* poll_event */
178     no_flush,                       /* flush */
179     mailslot_device_get_file_info,  /* get_file_info */
180     default_fd_queue_async,         /* queue_async */
181     default_fd_cancel_async         /* cancel_async */
182 };
183
184 static void mailslot_destroy( struct object *obj)
185 {
186     struct mailslot *mailslot = (struct mailslot *) obj;
187
188     assert( mailslot->fd );
189     assert( mailslot->write_fd );
190
191     release_object( mailslot->fd );
192     release_object( mailslot->write_fd );
193 }
194
195 static void mailslot_dump( struct object *obj, int verbose )
196 {
197     struct mailslot *mailslot = (struct mailslot *) obj;
198
199     assert( obj->ops == &mailslot_ops );
200     fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%d\n",
201              mailslot->max_msgsize, mailslot->read_timeout );
202 }
203
204 static int mailslot_message_count(struct mailslot *mailslot)
205 {
206     struct pollfd pfd;
207
208     /* poll the socket to see if there's any messages */
209     pfd.fd = get_unix_fd( mailslot->fd );
210     pfd.events = POLLIN;
211     pfd.revents = 0;
212     return (poll( &pfd, 1, 0 ) == 1) ? 1 : 0;
213 }
214
215 static int mailslot_next_msg_size( struct mailslot *mailslot )
216 {
217     int size, fd;
218
219     size = 0;
220     fd = get_unix_fd( mailslot->fd );
221     ioctl( fd, FIONREAD, &size );
222     return size;
223 }
224
225 static int mailslot_get_info( struct fd *fd )
226 {
227     struct mailslot *mailslot = get_fd_user( fd );
228     assert( mailslot->obj.ops == &mailslot_ops );
229     return FD_FLAG_TIMEOUT | FD_FLAG_AVAILABLE;
230 }
231
232 static struct fd *mailslot_get_fd( struct object *obj )
233 {
234     struct mailslot *mailslot = (struct mailslot *) obj;
235
236     return (struct fd *)grab_object( mailslot->fd );
237 }
238
239 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
240 {
241     if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
242     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
243     if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
244     if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
245     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
246 }
247
248 static void mailslot_queue_async( struct fd *fd, void *apc, void *user,
249                                   void *iosb, int type, int count )
250 {
251     struct mailslot *mailslot = get_fd_user( fd );
252
253     assert(mailslot->obj.ops == &mailslot_ops);
254
255     if (type != ASYNC_TYPE_READ)
256     {
257         set_error(STATUS_INVALID_PARAMETER);
258         return;
259     }
260
261     if (list_empty( &mailslot->writers ) ||
262         !mailslot_message_count( mailslot ))
263     {
264         set_error(STATUS_IO_TIMEOUT);
265         return;
266     }
267
268     if (mailslot->read_timeout != -1)
269     {
270         struct timeval when;
271         gettimeofday( &when, NULL );
272         add_timeout( &when, mailslot->read_timeout );
273         fd_queue_async_timeout( fd, apc, user, iosb, type, count, &when );
274     }
275     else fd_queue_async_timeout( fd, apc, user, iosb, type, count, NULL );
276 }
277
278 static void mailslot_device_dump( struct object *obj, int verbose )
279 {
280     assert( obj->ops == &mailslot_device_ops );
281     fprintf( stderr, "Mail slot device\n" );
282 }
283
284 static struct fd *mailslot_device_get_fd( struct object *obj )
285 {
286     struct mailslot_device *device = (struct mailslot_device *)obj;
287     return (struct fd *)grab_object( device->fd );
288 }
289
290 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
291                                                    unsigned int attr )
292 {
293     struct mailslot_device *device = (struct mailslot_device*)obj;
294     struct object *found;
295
296     assert( obj->ops == &mailslot_device_ops );
297
298     if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
299         name->len = 0;
300
301     return found;
302 }
303
304 static void mailslot_device_destroy( struct object *obj )
305 {
306     struct mailslot_device *device = (struct mailslot_device*)obj;
307     assert( obj->ops == &mailslot_device_ops );
308     if (device->fd) release_object( device->fd );
309     if (device->mailslots) free( device->mailslots );
310 }
311
312 static int mailslot_device_get_file_info( struct fd *fd )
313 {
314     return 0;
315 }
316
317 void create_mailslot_device( struct directory *root, const struct unicode_str *name )
318 {
319     struct mailslot_device *dev;
320
321     if ((dev = create_named_object_dir( root, name, 0, &mailslot_device_ops )) &&
322         get_error() != STATUS_OBJECT_NAME_EXISTS)
323     {
324         dev->mailslots = NULL;
325         if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj )) ||
326             !(dev->mailslots = create_namespace( 7 )))
327         {
328             release_object( dev );
329             dev = NULL;
330         }
331     }
332     if (dev) make_object_static( &dev->obj );
333 }
334
335 static struct mailslot *create_mailslot( struct directory *root,
336                                          const struct unicode_str *name, unsigned int attr,
337                                          int max_msgsize, int read_timeout )
338 {
339     struct object *obj;
340     struct unicode_str new_name;
341     struct mailslot_device *dev;
342     struct mailslot *mailslot;
343     int fds[2];
344
345     if (!name || !name->len) return alloc_object( &mailslot_ops );
346
347     if (!(obj = find_object_dir( root, name, attr, &new_name ))) return NULL;
348
349     if (!new_name.len)
350     {
351         if (attr & OBJ_OPENIF && obj->ops == &mailslot_ops)
352             /* it already exists - there can only be one mailslot to read from */
353             set_error( STATUS_OBJECT_NAME_EXISTS );
354         else if (attr & OBJ_OPENIF)
355             set_error( STATUS_OBJECT_TYPE_MISMATCH );
356         else
357             set_error( STATUS_OBJECT_NAME_COLLISION );
358         release_object( obj );
359         return NULL;
360     }
361
362     if (obj->ops != &mailslot_device_ops)
363     {
364         set_error( STATUS_OBJECT_TYPE_MISMATCH );
365         release_object( obj );
366         return NULL;
367     }
368
369     dev = (struct mailslot_device *)obj;
370     mailslot = create_object( dev->mailslots, &mailslot_ops, &new_name, NULL );
371     release_object( dev );
372
373     if (!mailslot) return NULL;
374
375     mailslot->fd = NULL;
376     mailslot->write_fd = NULL;
377     mailslot->max_msgsize = max_msgsize;
378     mailslot->read_timeout = read_timeout;
379     list_init( &mailslot->writers );
380
381     if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
382     {
383         fcntl( fds[0], F_SETFL, O_NONBLOCK );
384         fcntl( fds[1], F_SETFL, O_NONBLOCK );
385         mailslot->fd = create_anonymous_fd( &mailslot_fd_ops,
386                                 fds[1], &mailslot->obj );
387         mailslot->write_fd = create_anonymous_fd( &mail_writer_fd_ops,
388                                 fds[0], &mailslot->obj );
389         if (mailslot->fd && mailslot->write_fd) return mailslot;
390     }
391     else file_set_error();
392
393     release_object( mailslot );
394     return NULL;
395 }
396
397 static void mail_writer_dump( struct object *obj, int verbose )
398 {
399     fprintf( stderr, "Mailslot writer\n" );
400 }
401
402 static void mail_writer_destroy( struct object *obj)
403 {
404     struct mail_writer *writer = (struct mail_writer *) obj;
405
406     list_remove( &writer->entry );
407     release_object( writer->mailslot );
408 }
409
410 static int mail_writer_get_info( struct fd *fd )
411 {
412     return 0;
413 }
414
415 static struct fd *mail_writer_get_fd( struct object *obj )
416 {
417     struct mail_writer *writer = (struct mail_writer *) obj;
418
419     return (struct fd *)grab_object( writer->mailslot->write_fd );
420 }
421
422 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
423 {
424     if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
425     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
426     if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
427     if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
428     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
429 }
430
431 /*
432  * Readers and writers cannot be mixed.
433  * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
434  */
435 static struct mail_writer *create_mail_writer( struct mailslot *mailslot, unsigned int access,
436                                                unsigned int sharing )
437 {
438     struct mail_writer *writer;
439
440     if (!list_empty( &mailslot->writers ))
441     {
442         writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
443
444         if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
445            !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
446         {
447             set_error( STATUS_SHARING_VIOLATION );
448             return NULL;
449         }
450     }
451
452     writer = alloc_object( &mail_writer_ops );
453     if (!writer)
454         return NULL;
455
456     grab_object( mailslot );
457     writer->mailslot = mailslot;
458     writer->access   = mail_writer_map_access( &writer->obj, access );
459     writer->sharing  = sharing;
460
461     list_add_head( &mailslot->writers, &writer->entry );
462
463     return writer;
464 }
465
466 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
467                                           unsigned int access )
468 {
469     return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
470 }
471
472
473 /* create a mailslot */
474 DECL_HANDLER(create_mailslot)
475 {
476     struct mailslot *mailslot;
477     struct unicode_str name;
478     struct directory *root = NULL;
479
480     reply->handle = 0;
481     get_req_unicode_str( &name );
482     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
483         return;
484
485     if ((mailslot = create_mailslot( root, &name, req->attributes, req->max_msgsize,
486                                      req->read_timeout )))
487     {
488         reply->handle = alloc_handle( current->process, mailslot, req->access, req->attributes );
489         release_object( mailslot );
490     }
491
492     if (root) release_object( root );
493 }
494
495
496 /* open an existing mailslot */
497 DECL_HANDLER(open_mailslot)
498 {
499     struct mailslot *mailslot;
500     struct unicode_str name;
501     struct directory *root = NULL;
502
503     reply->handle = 0;
504     get_req_unicode_str( &name );
505
506     if (!(req->sharing & FILE_SHARE_READ))
507     {
508         set_error( STATUS_SHARING_VIOLATION );
509         return;
510     }
511
512     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
513         return;
514     mailslot = open_object_dir( root, &name, req->attributes, &mailslot_ops );
515     if (root) release_object( root );
516
517     if (mailslot)
518     {
519         struct mail_writer *writer;
520
521         writer = create_mail_writer( mailslot, req->access, req->sharing );
522         if (writer)
523         {
524             reply->handle = alloc_handle( current->process, writer, req->access, req->attributes );
525             release_object( writer );
526         }
527         release_object( mailslot );
528     }
529     else
530         set_error( STATUS_NO_SUCH_FILE );
531 }
532
533
534 /* set mailslot information */
535 DECL_HANDLER(set_mailslot_info)
536 {
537     struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
538
539     if (mailslot)
540     {
541         if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
542             mailslot->read_timeout = req->read_timeout;
543         reply->max_msgsize = mailslot->max_msgsize;
544         reply->read_timeout = mailslot->read_timeout;
545         reply->msg_count = mailslot_message_count(mailslot);
546
547         /* get the size of the next message */
548         if (reply->msg_count)
549             reply->next_msgsize = mailslot_next_msg_size(mailslot);
550         else
551             reply->next_msgsize = MAILSLOT_NO_MESSAGE;
552
553         release_object( mailslot );
554     }
555 }