2 * Server-side mailslot management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2005 Mike McCormack
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.
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.
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
24 #include "wine/port.h"
26 #define WIN32_NO_STATUS
27 #include "wine/unicode.h"
36 #include <sys/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
44 #ifdef HAVE_SYS_FILIO_H
45 #include <sys/filio.h>
60 unsigned int max_msgsize;
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 * );
71 static const struct object_ops mailslot_ops =
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 fd_close_handle, /* close_handle */
84 mailslot_destroy /* destroy */
87 static enum server_fd_type mailslot_get_info( struct fd *fd, int *flags );
88 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
90 static const struct fd_ops mailslot_fd_ops =
92 default_fd_get_poll_events, /* get_poll_events */
93 default_poll_event, /* poll_event */
95 mailslot_get_info, /* get_file_info */
96 mailslot_queue_async, /* queue_async */
97 default_fd_cancel_async /* cancel_async */
104 struct mailslot *mailslot;
107 unsigned int sharing;
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);
115 static const struct object_ops mail_writer_ops =
117 sizeof(struct mail_writer), /* size */
118 mail_writer_dump, /* dump */
119 no_add_queue, /* add_queue */
120 NULL, /* remove_queue */
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 fd_close_handle, /* close_handle */
128 mail_writer_destroy /* destroy */
131 static enum server_fd_type mail_writer_get_info( struct fd *fd, int *flags );
133 static const struct fd_ops mail_writer_fd_ops =
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 */
144 struct mailslot_device
146 struct object obj; /* object header */
147 struct fd *fd; /* pseudo-fd for ioctls */
148 struct namespace *mailslots; /* mailslot namespace */
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,
155 static void mailslot_device_destroy( struct object *obj );
156 static enum server_fd_type mailslot_device_get_file_info( struct fd *fd, int *flags );
158 static const struct object_ops mailslot_device_ops =
160 sizeof(struct mailslot_device), /* size */
161 mailslot_device_dump, /* dump */
162 no_add_queue, /* add_queue */
163 NULL, /* remove_queue */
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 fd_close_handle, /* close_handle */
171 mailslot_device_destroy /* destroy */
174 static const struct fd_ops mailslot_device_fd_ops =
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 */
184 static void mailslot_destroy( struct object *obj)
186 struct mailslot *mailslot = (struct mailslot *) obj;
188 assert( mailslot->fd );
189 assert( mailslot->write_fd );
191 release_object( mailslot->fd );
192 release_object( mailslot->write_fd );
195 static void mailslot_dump( struct object *obj, int verbose )
197 struct mailslot *mailslot = (struct mailslot *) obj;
199 assert( obj->ops == &mailslot_ops );
200 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%d\n",
201 mailslot->max_msgsize, mailslot->read_timeout );
204 static int mailslot_message_count(struct mailslot *mailslot)
208 /* poll the socket to see if there's any messages */
209 pfd.fd = get_unix_fd( mailslot->fd );
212 return (poll( &pfd, 1, 0 ) == 1) ? 1 : 0;
215 static enum server_fd_type mailslot_get_info( struct fd *fd, int *flags )
217 struct mailslot *mailslot = get_fd_user( fd );
218 assert( mailslot->obj.ops == &mailslot_ops );
219 *flags = FD_FLAG_TIMEOUT | FD_FLAG_AVAILABLE;
220 return FD_TYPE_MAILSLOT;
223 static struct fd *mailslot_get_fd( struct object *obj )
225 struct mailslot *mailslot = (struct mailslot *) obj;
227 return (struct fd *)grab_object( mailslot->fd );
230 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
232 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
233 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
234 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
235 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
236 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
239 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
241 struct mailslot *mailslot = get_fd_user( fd );
243 assert(mailslot->obj.ops == &mailslot_ops);
245 if (type != ASYNC_TYPE_READ)
247 set_error(STATUS_INVALID_PARAMETER);
251 if (list_empty( &mailslot->writers ) ||
252 !mailslot_message_count( mailslot ))
254 set_error(STATUS_IO_TIMEOUT);
258 if (mailslot->read_timeout != -1)
260 struct timeval when = current_time;
261 add_timeout( &when, mailslot->read_timeout );
262 fd_queue_async_timeout( fd, data, type, count, &when );
264 else fd_queue_async_timeout( fd, data, type, count, NULL );
267 static void mailslot_device_dump( struct object *obj, int verbose )
269 assert( obj->ops == &mailslot_device_ops );
270 fprintf( stderr, "Mail slot device\n" );
273 static struct fd *mailslot_device_get_fd( struct object *obj )
275 struct mailslot_device *device = (struct mailslot_device *)obj;
276 return (struct fd *)grab_object( device->fd );
279 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
282 struct mailslot_device *device = (struct mailslot_device*)obj;
283 struct object *found;
285 assert( obj->ops == &mailslot_device_ops );
287 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
293 static void mailslot_device_destroy( struct object *obj )
295 struct mailslot_device *device = (struct mailslot_device*)obj;
296 assert( obj->ops == &mailslot_device_ops );
297 if (device->fd) release_object( device->fd );
298 free( device->mailslots );
301 static enum server_fd_type mailslot_device_get_file_info( struct fd *fd, int *flags )
304 return FD_TYPE_DEVICE;
307 void create_mailslot_device( struct directory *root, const struct unicode_str *name )
309 struct mailslot_device *dev;
311 if ((dev = create_named_object_dir( root, name, 0, &mailslot_device_ops )) &&
312 get_error() != STATUS_OBJECT_NAME_EXISTS)
314 dev->mailslots = NULL;
315 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj )) ||
316 !(dev->mailslots = create_namespace( 7 )))
318 release_object( dev );
322 if (dev) make_object_static( &dev->obj );
325 static struct mailslot *create_mailslot( struct directory *root,
326 const struct unicode_str *name, unsigned int attr,
327 int max_msgsize, int read_timeout )
330 struct unicode_str new_name;
331 struct mailslot_device *dev;
332 struct mailslot *mailslot;
335 if (!name || !name->len) return alloc_object( &mailslot_ops );
337 if (!(obj = find_object_dir( root, name, attr, &new_name ))) return NULL;
341 if (attr & OBJ_OPENIF && obj->ops == &mailslot_ops)
342 /* it already exists - there can only be one mailslot to read from */
343 set_error( STATUS_OBJECT_NAME_EXISTS );
344 else if (attr & OBJ_OPENIF)
345 set_error( STATUS_OBJECT_TYPE_MISMATCH );
347 set_error( STATUS_OBJECT_NAME_COLLISION );
348 release_object( obj );
352 if (obj->ops != &mailslot_device_ops)
354 set_error( STATUS_OBJECT_TYPE_MISMATCH );
355 release_object( obj );
359 dev = (struct mailslot_device *)obj;
360 mailslot = create_object( dev->mailslots, &mailslot_ops, &new_name, NULL );
361 release_object( dev );
363 if (!mailslot) return NULL;
366 mailslot->write_fd = NULL;
367 mailslot->max_msgsize = max_msgsize;
368 mailslot->read_timeout = read_timeout;
369 list_init( &mailslot->writers );
371 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
373 fcntl( fds[0], F_SETFL, O_NONBLOCK );
374 fcntl( fds[1], F_SETFL, O_NONBLOCK );
375 mailslot->fd = create_anonymous_fd( &mailslot_fd_ops,
376 fds[1], &mailslot->obj );
377 mailslot->write_fd = create_anonymous_fd( &mail_writer_fd_ops,
378 fds[0], &mailslot->obj );
379 if (mailslot->fd && mailslot->write_fd) return mailslot;
381 else file_set_error();
383 release_object( mailslot );
387 static void mail_writer_dump( struct object *obj, int verbose )
389 fprintf( stderr, "Mailslot writer\n" );
392 static void mail_writer_destroy( struct object *obj)
394 struct mail_writer *writer = (struct mail_writer *) obj;
396 list_remove( &writer->entry );
397 release_object( writer->mailslot );
400 static enum server_fd_type mail_writer_get_info( struct fd *fd, int *flags )
403 return FD_TYPE_MAILSLOT;
406 static struct fd *mail_writer_get_fd( struct object *obj )
408 struct mail_writer *writer = (struct mail_writer *) obj;
410 return (struct fd *)grab_object( writer->mailslot->write_fd );
413 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
415 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
416 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
417 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
418 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
419 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
423 * Readers and writers cannot be mixed.
424 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
426 static struct mail_writer *create_mail_writer( struct mailslot *mailslot, unsigned int access,
427 unsigned int sharing )
429 struct mail_writer *writer;
431 if (!list_empty( &mailslot->writers ))
433 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
435 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
436 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
438 set_error( STATUS_SHARING_VIOLATION );
443 writer = alloc_object( &mail_writer_ops );
447 grab_object( mailslot );
448 writer->mailslot = mailslot;
449 writer->access = mail_writer_map_access( &writer->obj, access );
450 writer->sharing = sharing;
452 list_add_head( &mailslot->writers, &writer->entry );
457 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
458 unsigned int access )
460 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
464 /* create a mailslot */
465 DECL_HANDLER(create_mailslot)
467 struct mailslot *mailslot;
468 struct unicode_str name;
469 struct directory *root = NULL;
472 get_req_unicode_str( &name );
473 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
476 if ((mailslot = create_mailslot( root, &name, req->attributes, req->max_msgsize,
477 req->read_timeout )))
479 reply->handle = alloc_handle( current->process, mailslot, req->access, req->attributes );
480 release_object( mailslot );
483 if (root) release_object( root );
487 /* open an existing mailslot */
488 DECL_HANDLER(open_mailslot)
490 struct mailslot *mailslot;
491 struct unicode_str name;
492 struct directory *root = NULL;
495 get_req_unicode_str( &name );
497 if (!(req->sharing & FILE_SHARE_READ))
499 set_error( STATUS_SHARING_VIOLATION );
503 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
505 mailslot = open_object_dir( root, &name, req->attributes, &mailslot_ops );
506 if (root) release_object( root );
510 struct mail_writer *writer;
512 writer = create_mail_writer( mailslot, req->access, req->sharing );
515 reply->handle = alloc_handle( current->process, writer, req->access, req->attributes );
516 release_object( writer );
518 release_object( mailslot );
521 set_error( STATUS_NO_SUCH_FILE );
525 /* set mailslot information */
526 DECL_HANDLER(set_mailslot_info)
528 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
532 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
533 mailslot->read_timeout = req->read_timeout;
534 reply->max_msgsize = mailslot->max_msgsize;
535 reply->read_timeout = mailslot->read_timeout;
536 release_object( mailslot );