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