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 void mailslot_device_destroy( struct object *obj );
158 static enum server_fd_type mailslot_device_get_file_info( struct fd *fd, int *flags );
160 static const struct object_ops mailslot_device_ops =
162 sizeof(struct mailslot_device), /* size */
163 mailslot_device_dump, /* dump */
164 no_add_queue, /* add_queue */
165 NULL, /* remove_queue */
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 */
177 static const struct fd_ops mailslot_device_fd_ops =
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 */
187 static void mailslot_destroy( struct object *obj)
189 struct mailslot *mailslot = (struct mailslot *) obj;
191 assert( mailslot->fd );
192 assert( mailslot->write_fd );
194 release_object( mailslot->fd );
195 release_object( mailslot->write_fd );
198 static void mailslot_dump( struct object *obj, int verbose )
200 struct mailslot *mailslot = (struct mailslot *) obj;
202 assert( obj->ops == &mailslot_ops );
203 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%d\n",
204 mailslot->max_msgsize, mailslot->read_timeout );
207 static int mailslot_message_count(struct mailslot *mailslot)
211 /* poll the socket to see if there's any messages */
212 pfd.fd = get_unix_fd( mailslot->fd );
215 return (poll( &pfd, 1, 0 ) == 1) ? 1 : 0;
218 static enum server_fd_type mailslot_get_info( struct fd *fd, int *flags )
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;
226 static struct fd *mailslot_get_fd( struct object *obj )
228 struct mailslot *mailslot = (struct mailslot *) obj;
230 return (struct fd *)grab_object( mailslot->fd );
233 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
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);
242 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
244 struct mailslot *mailslot = get_fd_user( fd );
246 assert(mailslot->obj.ops == &mailslot_ops);
248 if (type != ASYNC_TYPE_READ)
250 set_error(STATUS_INVALID_PARAMETER);
254 if (list_empty( &mailslot->writers ) ||
255 !mailslot_message_count( mailslot ))
257 set_error(STATUS_IO_TIMEOUT);
261 if (mailslot->read_timeout != -1)
263 struct timeval when = current_time;
264 add_timeout( &when, mailslot->read_timeout );
265 fd_queue_async_timeout( fd, data, type, count, &when );
267 else fd_queue_async_timeout( fd, data, type, count, NULL );
270 static void mailslot_device_dump( struct object *obj, int verbose )
272 assert( obj->ops == &mailslot_device_ops );
273 fprintf( stderr, "Mail slot device\n" );
276 static struct fd *mailslot_device_get_fd( struct object *obj )
278 struct mailslot_device *device = (struct mailslot_device *)obj;
279 return (struct fd *)grab_object( device->fd );
282 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
285 struct mailslot_device *device = (struct mailslot_device*)obj;
286 struct object *found;
288 assert( obj->ops == &mailslot_device_ops );
290 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
296 static void mailslot_device_destroy( struct object *obj )
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 );
304 static enum server_fd_type mailslot_device_get_file_info( struct fd *fd, int *flags )
307 return FD_TYPE_DEVICE;
310 void create_mailslot_device( struct directory *root, const struct unicode_str *name )
312 struct mailslot_device *dev;
314 if ((dev = create_named_object_dir( root, name, 0, &mailslot_device_ops )) &&
315 get_error() != STATUS_OBJECT_NAME_EXISTS)
317 dev->mailslots = NULL;
318 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj )) ||
319 !(dev->mailslots = create_namespace( 7 )))
321 release_object( dev );
325 if (dev) make_object_static( &dev->obj );
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 )
333 struct unicode_str new_name;
334 struct mailslot_device *dev;
335 struct mailslot *mailslot;
338 if (!name || !name->len) return alloc_object( &mailslot_ops );
340 if (!(obj = find_object_dir( root, name, attr, &new_name ))) return NULL;
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 );
350 set_error( STATUS_OBJECT_NAME_COLLISION );
351 release_object( obj );
355 if (obj->ops != &mailslot_device_ops)
357 set_error( STATUS_OBJECT_TYPE_MISMATCH );
358 release_object( obj );
362 dev = (struct mailslot_device *)obj;
363 mailslot = create_object( dev->mailslots, &mailslot_ops, &new_name, NULL );
364 release_object( dev );
366 if (!mailslot) return NULL;
369 mailslot->write_fd = NULL;
370 mailslot->max_msgsize = max_msgsize;
371 mailslot->read_timeout = read_timeout;
372 list_init( &mailslot->writers );
374 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
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;
384 else file_set_error();
386 release_object( mailslot );
390 static void mail_writer_dump( struct object *obj, int verbose )
392 fprintf( stderr, "Mailslot writer\n" );
395 static void mail_writer_destroy( struct object *obj)
397 struct mail_writer *writer = (struct mail_writer *) obj;
399 list_remove( &writer->entry );
400 release_object( writer->mailslot );
403 static enum server_fd_type mail_writer_get_info( struct fd *fd, int *flags )
406 return FD_TYPE_MAILSLOT;
409 static struct fd *mail_writer_get_fd( struct object *obj )
411 struct mail_writer *writer = (struct mail_writer *) obj;
413 return (struct fd *)grab_object( writer->mailslot->write_fd );
416 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
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);
426 * Readers and writers cannot be mixed.
427 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
429 static struct mail_writer *create_mail_writer( struct mailslot *mailslot, unsigned int access,
430 unsigned int sharing )
432 struct mail_writer *writer;
434 if (!list_empty( &mailslot->writers ))
436 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
438 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
439 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
441 set_error( STATUS_SHARING_VIOLATION );
446 writer = alloc_object( &mail_writer_ops );
450 grab_object( mailslot );
451 writer->mailslot = mailslot;
452 writer->access = mail_writer_map_access( &writer->obj, access );
453 writer->sharing = sharing;
455 list_add_head( &mailslot->writers, &writer->entry );
460 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
461 unsigned int access )
463 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
467 /* create a mailslot */
468 DECL_HANDLER(create_mailslot)
470 struct mailslot *mailslot;
471 struct unicode_str name;
472 struct directory *root = NULL;
475 get_req_unicode_str( &name );
476 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
479 if ((mailslot = create_mailslot( root, &name, req->attributes, req->max_msgsize,
480 req->read_timeout )))
482 reply->handle = alloc_handle( current->process, mailslot, req->access, req->attributes );
483 release_object( mailslot );
486 if (root) release_object( root );
490 /* open an existing mailslot */
491 DECL_HANDLER(open_mailslot)
493 struct mailslot *mailslot;
494 struct unicode_str name;
495 struct directory *root = NULL;
498 get_req_unicode_str( &name );
500 if (!(req->sharing & FILE_SHARE_READ))
502 set_error( STATUS_SHARING_VIOLATION );
506 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
508 mailslot = open_object_dir( root, &name, req->attributes, &mailslot_ops );
509 if (root) release_object( root );
513 struct mail_writer *writer;
515 writer = create_mail_writer( mailslot, req->access, req->sharing );
518 reply->handle = alloc_handle( current->process, writer, req->access, req->attributes );
519 release_object( writer );
521 release_object( mailslot );
524 set_error( STATUS_NO_SUCH_FILE );
528 /* set mailslot information */
529 DECL_HANDLER(set_mailslot_info)
531 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
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 );