Release 1.5.29.
[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     int                 write_fd;
60     unsigned int        max_msgsize;
61     timeout_t           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 struct object *mailslot_open_file( struct object *obj, unsigned int access,
70                                           unsigned int sharing, unsigned int options );
71 static void mailslot_destroy( struct object * );
72
73 static const struct object_ops mailslot_ops =
74 {
75     sizeof(struct mailslot),   /* size */
76     mailslot_dump,             /* dump */
77     no_get_type,               /* get_type */
78     add_queue,                 /* add_queue */
79     remove_queue,              /* remove_queue */
80     default_fd_signaled,       /* signaled */
81     no_satisfied,              /* satisfied */
82     no_signal,                 /* signal */
83     mailslot_get_fd,           /* get_fd */
84     mailslot_map_access,       /* map_access */
85     default_get_sd,            /* get_sd */
86     default_set_sd,            /* set_sd */
87     no_lookup_name,            /* lookup_name */
88     mailslot_open_file,        /* open_file */
89     fd_close_handle,           /* close_handle */
90     mailslot_destroy           /* destroy */
91 };
92
93 static enum server_fd_type mailslot_get_fd_type( struct fd *fd );
94 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
95
96 static const struct fd_ops mailslot_fd_ops =
97 {
98     default_fd_get_poll_events, /* get_poll_events */
99     default_poll_event,         /* poll_event */
100     no_flush,                   /* flush */
101     mailslot_get_fd_type,       /* get_fd_type */
102     default_fd_ioctl,           /* ioctl */
103     mailslot_queue_async,       /* queue_async */
104     default_fd_reselect_async,  /* reselect_async */
105     default_fd_cancel_async     /* cancel_async */
106 };
107
108
109 struct mail_writer
110 {
111     struct object         obj;
112     struct fd            *fd;
113     struct mailslot      *mailslot;
114     struct list           entry;
115     unsigned int          access;
116     unsigned int          sharing;
117 };
118
119 static void mail_writer_dump( struct object *obj, int verbose );
120 static struct fd *mail_writer_get_fd( struct object *obj );
121 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
122 static void mail_writer_destroy( struct object *obj);
123
124 static const struct object_ops mail_writer_ops =
125 {
126     sizeof(struct mail_writer), /* size */
127     mail_writer_dump,           /* dump */
128     no_get_type,                /* get_type */
129     no_add_queue,               /* add_queue */
130     NULL,                       /* remove_queue */
131     NULL,                       /* signaled */
132     NULL,                       /* satisfied */
133     no_signal,                  /* signal */
134     mail_writer_get_fd,         /* get_fd */
135     mail_writer_map_access,     /* map_access */
136     default_get_sd,             /* get_sd */
137     default_set_sd,             /* set_sd */
138     no_lookup_name,             /* lookup_name */
139     no_open_file,               /* open_file */
140     fd_close_handle,            /* close_handle */
141     mail_writer_destroy         /* destroy */
142 };
143
144 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
145
146 static const struct fd_ops mail_writer_fd_ops =
147 {
148     default_fd_get_poll_events,  /* get_poll_events */
149     default_poll_event,          /* poll_event */
150     no_flush,                    /* flush */
151     mail_writer_get_fd_type,     /* get_fd_type */
152     default_fd_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 struct mailslot_device
160 {
161     struct object       obj;         /* object header */
162     struct fd          *fd;          /* pseudo-fd for ioctls */
163     struct namespace   *mailslots;   /* mailslot namespace */
164 };
165
166 static void mailslot_device_dump( struct object *obj, int verbose );
167 static struct object_type *mailslot_device_get_type( struct object *obj );
168 static struct fd *mailslot_device_get_fd( struct object *obj );
169 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
170                                                    unsigned int attr );
171 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
172                                                  unsigned int sharing, unsigned int options );
173 static void mailslot_device_destroy( struct object *obj );
174 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
175
176 static const struct object_ops mailslot_device_ops =
177 {
178     sizeof(struct mailslot_device), /* size */
179     mailslot_device_dump,           /* dump */
180     mailslot_device_get_type,       /* get_type */
181     no_add_queue,                   /* add_queue */
182     NULL,                           /* remove_queue */
183     NULL,                           /* signaled */
184     no_satisfied,                   /* satisfied */
185     no_signal,                      /* signal */
186     mailslot_device_get_fd,         /* get_fd */
187     no_map_access,                  /* map_access */
188     default_get_sd,                 /* get_sd */
189     default_set_sd,                 /* set_sd */
190     mailslot_device_lookup_name,    /* lookup_name */
191     mailslot_device_open_file,      /* open_file */
192     fd_close_handle,                /* close_handle */
193     mailslot_device_destroy         /* destroy */
194 };
195
196 static const struct fd_ops mailslot_device_fd_ops =
197 {
198     default_fd_get_poll_events,     /* get_poll_events */
199     default_poll_event,             /* poll_event */
200     no_flush,                       /* flush */
201     mailslot_device_get_fd_type,    /* get_fd_type */
202     default_fd_ioctl,               /* ioctl */
203     default_fd_queue_async,         /* queue_async */
204     default_fd_reselect_async,      /* reselect_async */
205     default_fd_cancel_async         /* cancel_async */
206 };
207
208 static void mailslot_destroy( struct object *obj)
209 {
210     struct mailslot *mailslot = (struct mailslot *) obj;
211
212     assert( mailslot->fd );
213
214     if (mailslot->write_fd != -1)
215     {
216         shutdown( mailslot->write_fd, SHUT_RDWR );
217         close( mailslot->write_fd );
218     }
219     release_object( mailslot->fd );
220 }
221
222 static void mailslot_dump( struct object *obj, int verbose )
223 {
224     struct mailslot *mailslot = (struct mailslot *) obj;
225
226     assert( obj->ops == &mailslot_ops );
227     fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
228              mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
229 }
230
231 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
232 {
233     return FD_TYPE_MAILSLOT;
234 }
235
236 static struct fd *mailslot_get_fd( struct object *obj )
237 {
238     struct mailslot *mailslot = (struct mailslot *) obj;
239
240     return (struct fd *)grab_object( mailslot->fd );
241 }
242
243 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
244 {
245     /* mailslots can only be read */
246     if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
247     if (access & GENERIC_ALL)     access |= FILE_GENERIC_READ;
248     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
249 }
250
251 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
252                                           unsigned int sharing, unsigned int options )
253 {
254     struct mailslot *mailslot = (struct mailslot *)obj;
255     struct mail_writer *writer;
256     int unix_fd;
257
258     if (!(sharing & FILE_SHARE_READ))
259     {
260         set_error( STATUS_SHARING_VIOLATION );
261         return NULL;
262     }
263
264     if (!list_empty( &mailslot->writers ))
265     {
266         /* Readers and writers cannot be mixed.
267          * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
268          */
269         writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
270
271         if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
272            !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
273         {
274             set_error( STATUS_SHARING_VIOLATION );
275             return NULL;
276         }
277     }
278
279     if ((unix_fd = dup( mailslot->write_fd )) == -1)
280     {
281         file_set_error();
282         return NULL;
283     }
284     if (!(writer = alloc_object( &mail_writer_ops )))
285     {
286         close( unix_fd );
287         return NULL;
288     }
289     grab_object( mailslot );
290     writer->mailslot = mailslot;
291     writer->access   = mail_writer_map_access( &writer->obj, access );
292     writer->sharing  = sharing;
293     list_add_head( &mailslot->writers, &writer->entry );
294
295     if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
296     {
297         release_object( writer );
298         return NULL;
299     }
300     allow_fd_caching( writer->fd );
301     return &writer->obj;
302 }
303
304 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
305 {
306     struct mailslot *mailslot = get_fd_user( fd );
307     struct async *async;
308
309     assert(mailslot->obj.ops == &mailslot_ops);
310
311     if ((async = fd_queue_async( fd, data, type )))
312     {
313         async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
314                            STATUS_IO_TIMEOUT );
315         release_object( async );
316         set_error( STATUS_PENDING );
317     }
318 }
319
320 static void mailslot_device_dump( struct object *obj, int verbose )
321 {
322     assert( obj->ops == &mailslot_device_ops );
323     fprintf( stderr, "Mail slot device\n" );
324 }
325
326 static struct object_type *mailslot_device_get_type( struct object *obj )
327 {
328     static const WCHAR name[] = {'D','e','v','i','c','e'};
329     static const struct unicode_str str = { name, sizeof(name) };
330     return get_object_type( &str );
331 }
332
333 static struct fd *mailslot_device_get_fd( struct object *obj )
334 {
335     struct mailslot_device *device = (struct mailslot_device *)obj;
336     return (struct fd *)grab_object( device->fd );
337 }
338
339 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
340                                                    unsigned int attr )
341 {
342     struct mailslot_device *device = (struct mailslot_device*)obj;
343     struct object *found;
344
345     assert( obj->ops == &mailslot_device_ops );
346
347     if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
348         name->len = 0;
349
350     return found;
351 }
352
353 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
354                                                  unsigned int sharing, unsigned int options )
355 {
356     return grab_object( obj );
357 }
358
359 static void mailslot_device_destroy( struct object *obj )
360 {
361     struct mailslot_device *device = (struct mailslot_device*)obj;
362     assert( obj->ops == &mailslot_device_ops );
363     if (device->fd) release_object( device->fd );
364     free( device->mailslots );
365 }
366
367 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
368 {
369     return FD_TYPE_DEVICE;
370 }
371
372 void create_mailslot_device( struct directory *root, const struct unicode_str *name )
373 {
374     struct mailslot_device *dev;
375
376     if ((dev = create_named_object_dir( root, name, 0, &mailslot_device_ops )) &&
377         get_error() != STATUS_OBJECT_NAME_EXISTS)
378     {
379         dev->mailslots = NULL;
380         if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj, 0 )) ||
381             !(dev->mailslots = create_namespace( 7 )))
382         {
383             release_object( dev );
384             dev = NULL;
385         }
386     }
387     if (dev) make_object_static( &dev->obj );
388 }
389
390 static struct mailslot *create_mailslot( struct directory *root,
391                                          const struct unicode_str *name, unsigned int attr,
392                                          int max_msgsize, timeout_t read_timeout )
393 {
394     struct object *obj;
395     struct unicode_str new_name;
396     struct mailslot_device *dev;
397     struct mailslot *mailslot;
398     int fds[2];
399
400     if (!name || !name->len) return alloc_object( &mailslot_ops );
401
402     if (!(obj = find_object_dir( root, name, attr, &new_name )))
403     {
404         set_error( STATUS_OBJECT_NAME_INVALID );
405         return NULL;
406     }
407
408     if (!new_name.len)
409     {
410         if (attr & OBJ_OPENIF && obj->ops == &mailslot_ops)
411             /* it already exists - there can only be one mailslot to read from */
412             set_error( STATUS_OBJECT_NAME_EXISTS );
413         else if (attr & OBJ_OPENIF)
414             set_error( STATUS_OBJECT_TYPE_MISMATCH );
415         else
416             set_error( STATUS_OBJECT_NAME_COLLISION );
417         release_object( obj );
418         return NULL;
419     }
420
421     if (obj->ops != &mailslot_device_ops)
422     {
423         set_error( STATUS_OBJECT_NAME_INVALID );
424         release_object( obj );
425         return NULL;
426     }
427
428     dev = (struct mailslot_device *)obj;
429     mailslot = create_object( dev->mailslots, &mailslot_ops, &new_name, NULL );
430     release_object( dev );
431
432     if (!mailslot) return NULL;
433
434     mailslot->fd = NULL;
435     mailslot->write_fd = -1;
436     mailslot->max_msgsize = max_msgsize;
437     mailslot->read_timeout = read_timeout;
438     list_init( &mailslot->writers );
439
440     if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
441     {
442         fcntl( fds[0], F_SETFL, O_NONBLOCK );
443         fcntl( fds[1], F_SETFL, O_NONBLOCK );
444         shutdown( fds[0], SHUT_RD );
445         mailslot->write_fd = fds[0];
446         if ((mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
447                                                  FILE_SYNCHRONOUS_IO_NONALERT )))
448         {
449             allow_fd_caching( mailslot->fd );
450             return mailslot;
451         }
452     }
453     else file_set_error();
454
455     release_object( mailslot );
456     return NULL;
457 }
458
459 static void mail_writer_dump( struct object *obj, int verbose )
460 {
461     fprintf( stderr, "Mailslot writer\n" );
462 }
463
464 static void mail_writer_destroy( struct object *obj)
465 {
466     struct mail_writer *writer = (struct mail_writer *) obj;
467
468     if (writer->fd) release_object( writer->fd );
469     list_remove( &writer->entry );
470     release_object( writer->mailslot );
471 }
472
473 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
474 {
475     return FD_TYPE_MAILSLOT;
476 }
477
478 static struct fd *mail_writer_get_fd( struct object *obj )
479 {
480     struct mail_writer *writer = (struct mail_writer *) obj;
481     return (struct fd *)grab_object( writer->fd );
482 }
483
484 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
485 {
486     /* mailslot writers can only get write access */
487     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
488     if (access & GENERIC_ALL)     access |= FILE_GENERIC_WRITE;
489     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
490 }
491
492 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
493                                           unsigned int access )
494 {
495     return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
496 }
497
498
499 /* create a mailslot */
500 DECL_HANDLER(create_mailslot)
501 {
502     struct mailslot *mailslot;
503     struct unicode_str name;
504     struct directory *root = NULL;
505
506     reply->handle = 0;
507     get_req_unicode_str( &name );
508     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
509         return;
510
511     if ((mailslot = create_mailslot( root, &name, req->attributes, req->max_msgsize,
512                                      req->read_timeout )))
513     {
514         reply->handle = alloc_handle( current->process, mailslot, req->access, req->attributes );
515         release_object( mailslot );
516     }
517
518     if (root) release_object( root );
519 }
520
521
522 /* set mailslot information */
523 DECL_HANDLER(set_mailslot_info)
524 {
525     struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
526
527     if (mailslot)
528     {
529         if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
530             mailslot->read_timeout = req->read_timeout;
531         reply->max_msgsize = mailslot->max_msgsize;
532         reply->read_timeout = mailslot->read_timeout;
533         release_object( mailslot );
534     }
535 }