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 no_open_file, /* open_file */
84 fd_close_handle, /* close_handle */
85 mailslot_destroy /* destroy */
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 );
91 static const struct fd_ops mailslot_fd_ops =
93 default_fd_get_poll_events, /* get_poll_events */
94 default_poll_event, /* poll_event */
96 mailslot_get_info, /* get_file_info */
97 mailslot_queue_async, /* queue_async */
98 default_fd_cancel_async /* cancel_async */
105 struct mailslot *mailslot;
108 unsigned int sharing;
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);
116 static const struct object_ops mail_writer_ops =
118 sizeof(struct mail_writer), /* size */
119 mail_writer_dump, /* dump */
120 no_add_queue, /* add_queue */
121 NULL, /* remove_queue */
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 */
133 static enum server_fd_type mail_writer_get_info( struct fd *fd, int *flags );
135 static const struct fd_ops mail_writer_fd_ops =
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 */
146 struct mailslot_device
148 struct object obj; /* object header */
149 struct fd *fd; /* pseudo-fd for ioctls */
150 struct namespace *mailslots; /* mailslot namespace */
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,
157 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
158 unsigned int sharing, unsigned int options );
159 static void mailslot_device_destroy( struct object *obj );
160 static enum server_fd_type mailslot_device_get_file_info( struct fd *fd, int *flags );
162 static const struct object_ops mailslot_device_ops =
164 sizeof(struct mailslot_device), /* size */
165 mailslot_device_dump, /* dump */
166 no_add_queue, /* add_queue */
167 NULL, /* remove_queue */
169 no_satisfied, /* satisfied */
170 no_signal, /* signal */
171 mailslot_device_get_fd, /* get_fd */
172 no_map_access, /* map_access */
173 mailslot_device_lookup_name, /* lookup_name */
174 mailslot_device_open_file, /* open_file */
175 fd_close_handle, /* close_handle */
176 mailslot_device_destroy /* destroy */
179 static const struct fd_ops mailslot_device_fd_ops =
181 default_fd_get_poll_events, /* get_poll_events */
182 default_poll_event, /* poll_event */
183 no_flush, /* flush */
184 mailslot_device_get_file_info, /* get_file_info */
185 default_fd_queue_async, /* queue_async */
186 default_fd_cancel_async /* cancel_async */
189 static void mailslot_destroy( struct object *obj)
191 struct mailslot *mailslot = (struct mailslot *) obj;
193 assert( mailslot->fd );
194 assert( mailslot->write_fd );
196 release_object( mailslot->fd );
197 release_object( mailslot->write_fd );
200 static void mailslot_dump( struct object *obj, int verbose )
202 struct mailslot *mailslot = (struct mailslot *) obj;
204 assert( obj->ops == &mailslot_ops );
205 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%d\n",
206 mailslot->max_msgsize, mailslot->read_timeout );
209 static int mailslot_message_count(struct mailslot *mailslot)
213 /* poll the socket to see if there's any messages */
214 pfd.fd = get_unix_fd( mailslot->fd );
217 return (poll( &pfd, 1, 0 ) == 1) ? 1 : 0;
220 static enum server_fd_type mailslot_get_info( struct fd *fd, int *flags )
222 struct mailslot *mailslot = get_fd_user( fd );
223 assert( mailslot->obj.ops == &mailslot_ops );
224 *flags = FD_FLAG_TIMEOUT | FD_FLAG_AVAILABLE;
225 return FD_TYPE_MAILSLOT;
228 static struct fd *mailslot_get_fd( struct object *obj )
230 struct mailslot *mailslot = (struct mailslot *) obj;
232 return (struct fd *)grab_object( mailslot->fd );
235 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
237 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
238 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
239 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
240 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
241 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
244 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
246 struct mailslot *mailslot = get_fd_user( fd );
248 assert(mailslot->obj.ops == &mailslot_ops);
250 if (type != ASYNC_TYPE_READ)
252 set_error(STATUS_INVALID_PARAMETER);
256 if (list_empty( &mailslot->writers ) ||
257 !mailslot_message_count( mailslot ))
259 set_error(STATUS_IO_TIMEOUT);
263 if (mailslot->read_timeout != -1)
265 struct timeval when = current_time;
266 add_timeout( &when, mailslot->read_timeout );
267 fd_queue_async_timeout( fd, data, type, count, &when );
269 else fd_queue_async_timeout( fd, data, type, count, NULL );
272 static void mailslot_device_dump( struct object *obj, int verbose )
274 assert( obj->ops == &mailslot_device_ops );
275 fprintf( stderr, "Mail slot device\n" );
278 static struct fd *mailslot_device_get_fd( struct object *obj )
280 struct mailslot_device *device = (struct mailslot_device *)obj;
281 return (struct fd *)grab_object( device->fd );
284 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
287 struct mailslot_device *device = (struct mailslot_device*)obj;
288 struct object *found;
290 assert( obj->ops == &mailslot_device_ops );
292 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
298 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
299 unsigned int sharing, unsigned int options )
301 return grab_object( obj );
304 static void mailslot_device_destroy( struct object *obj )
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 free( device->mailslots );
312 static enum server_fd_type mailslot_device_get_file_info( struct fd *fd, int *flags )
315 return FD_TYPE_DEVICE;
318 void create_mailslot_device( struct directory *root, const struct unicode_str *name )
320 struct mailslot_device *dev;
322 if ((dev = create_named_object_dir( root, name, 0, &mailslot_device_ops )) &&
323 get_error() != STATUS_OBJECT_NAME_EXISTS)
325 dev->mailslots = NULL;
326 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj )) ||
327 !(dev->mailslots = create_namespace( 7 )))
329 release_object( dev );
333 if (dev) make_object_static( &dev->obj );
336 static struct mailslot *create_mailslot( struct directory *root,
337 const struct unicode_str *name, unsigned int attr,
338 int max_msgsize, int read_timeout )
341 struct unicode_str new_name;
342 struct mailslot_device *dev;
343 struct mailslot *mailslot;
346 if (!name || !name->len) return alloc_object( &mailslot_ops );
348 if (!(obj = find_object_dir( root, name, attr, &new_name )))
350 set_error( STATUS_OBJECT_NAME_INVALID );
356 if (attr & OBJ_OPENIF && obj->ops == &mailslot_ops)
357 /* it already exists - there can only be one mailslot to read from */
358 set_error( STATUS_OBJECT_NAME_EXISTS );
359 else if (attr & OBJ_OPENIF)
360 set_error( STATUS_OBJECT_TYPE_MISMATCH );
362 set_error( STATUS_OBJECT_NAME_COLLISION );
363 release_object( obj );
367 if (obj->ops != &mailslot_device_ops)
369 set_error( STATUS_OBJECT_NAME_INVALID );
370 release_object( obj );
374 dev = (struct mailslot_device *)obj;
375 mailslot = create_object( dev->mailslots, &mailslot_ops, &new_name, NULL );
376 release_object( dev );
378 if (!mailslot) return NULL;
381 mailslot->write_fd = NULL;
382 mailslot->max_msgsize = max_msgsize;
383 mailslot->read_timeout = read_timeout;
384 list_init( &mailslot->writers );
386 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
388 fcntl( fds[0], F_SETFL, O_NONBLOCK );
389 fcntl( fds[1], F_SETFL, O_NONBLOCK );
390 mailslot->fd = create_anonymous_fd( &mailslot_fd_ops,
391 fds[1], &mailslot->obj );
392 mailslot->write_fd = create_anonymous_fd( &mail_writer_fd_ops,
393 fds[0], &mailslot->obj );
394 if (mailslot->fd && mailslot->write_fd) return mailslot;
396 else file_set_error();
398 release_object( mailslot );
402 static void mail_writer_dump( struct object *obj, int verbose )
404 fprintf( stderr, "Mailslot writer\n" );
407 static void mail_writer_destroy( struct object *obj)
409 struct mail_writer *writer = (struct mail_writer *) obj;
411 list_remove( &writer->entry );
412 release_object( writer->mailslot );
415 static enum server_fd_type mail_writer_get_info( struct fd *fd, int *flags )
418 return FD_TYPE_MAILSLOT;
421 static struct fd *mail_writer_get_fd( struct object *obj )
423 struct mail_writer *writer = (struct mail_writer *) obj;
425 return (struct fd *)grab_object( writer->mailslot->write_fd );
428 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
430 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
431 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
432 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
433 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
434 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
438 * Readers and writers cannot be mixed.
439 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
441 static struct mail_writer *create_mail_writer( struct mailslot *mailslot, unsigned int access,
442 unsigned int sharing )
444 struct mail_writer *writer;
446 if (!list_empty( &mailslot->writers ))
448 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
450 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
451 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
453 set_error( STATUS_SHARING_VIOLATION );
458 writer = alloc_object( &mail_writer_ops );
462 grab_object( mailslot );
463 writer->mailslot = mailslot;
464 writer->access = mail_writer_map_access( &writer->obj, access );
465 writer->sharing = sharing;
467 list_add_head( &mailslot->writers, &writer->entry );
472 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
473 unsigned int access )
475 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
479 /* create a mailslot */
480 DECL_HANDLER(create_mailslot)
482 struct mailslot *mailslot;
483 struct unicode_str name;
484 struct directory *root = NULL;
487 get_req_unicode_str( &name );
488 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
491 if ((mailslot = create_mailslot( root, &name, req->attributes, req->max_msgsize,
492 req->read_timeout )))
494 reply->handle = alloc_handle( current->process, mailslot, req->access, req->attributes );
495 release_object( mailslot );
498 if (root) release_object( root );
502 /* open an existing mailslot */
503 DECL_HANDLER(open_mailslot)
505 struct mailslot *mailslot;
506 struct unicode_str name;
507 struct directory *root = NULL;
510 get_req_unicode_str( &name );
512 if (!(req->sharing & FILE_SHARE_READ))
514 set_error( STATUS_SHARING_VIOLATION );
518 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
520 mailslot = open_object_dir( root, &name, req->attributes, &mailslot_ops );
521 if (root) release_object( root );
525 struct mail_writer *writer;
527 writer = create_mail_writer( mailslot, req->access, req->sharing );
530 reply->handle = alloc_handle( current->process, writer, req->access, req->attributes );
531 release_object( writer );
533 release_object( mailslot );
536 set_error( STATUS_NO_SUCH_FILE );
540 /* set mailslot information */
541 DECL_HANDLER(set_mailslot_info)
543 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
547 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
548 mailslot->read_timeout = req->read_timeout;
549 reply->max_msgsize = mailslot->max_msgsize;
550 reply->read_timeout = mailslot->read_timeout;
551 release_object( mailslot );