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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "wine/port.h"
25 #include "wine/unicode.h"
34 #include <sys/types.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
39 #ifdef HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
42 #ifdef HAVE_SYS_FILIO_H
43 #include <sys/filio.h>
58 unsigned int max_msgsize;
59 unsigned int read_timeout;
63 /* mailslot functions */
64 static void mailslot_dump( struct object*, int );
65 static struct fd *mailslot_get_fd( struct object * );
66 static void mailslot_destroy( struct object * );
68 static const struct object_ops mailslot_ops =
70 sizeof(struct mailslot), /* size */
71 mailslot_dump, /* dump */
72 default_fd_add_queue, /* add_queue */
73 default_fd_remove_queue, /* remove_queue */
74 default_fd_signaled, /* signaled */
75 no_satisfied, /* satisfied */
76 no_signal, /* signal */
77 mailslot_get_fd, /* get_fd */
78 no_close_handle, /* close_handle */
79 mailslot_destroy /* destroy */
82 static int mailslot_get_info( struct fd * );
83 static void mailslot_queue_async( struct fd *, void*, void*, void*, int, int );
85 static const struct fd_ops mailslot_fd_ops =
87 default_fd_get_poll_events, /* get_poll_events */
88 default_poll_event, /* poll_event */
90 mailslot_get_info, /* get_file_info */
91 mailslot_queue_async, /* queue_async */
92 default_fd_cancel_async /* cancel_async */
98 struct mailslot *mailslot;
104 static void mail_writer_dump( struct object *obj, int verbose );
105 static struct fd *mail_writer_get_fd( struct object *obj );
106 static void mail_writer_destroy( struct object *obj);
108 static const struct object_ops mail_writer_ops =
110 sizeof(struct mail_writer), /* size */
111 mail_writer_dump, /* dump */
112 no_add_queue, /* add_queue */
113 NULL, /* remove_queue */
115 NULL, /* satisfied */
116 no_signal, /* signal */
117 mail_writer_get_fd, /* get_fd */
118 no_close_handle, /* close_handle */
119 mail_writer_destroy /* destroy */
122 static int mail_writer_get_info( struct fd *fd );
124 static const struct fd_ops mail_writer_fd_ops =
126 NULL, /* get_poll_events */
127 NULL, /* poll_event */
128 no_flush, /* flush */
129 mail_writer_get_info, /* get_file_info */
130 no_queue_async, /* queue_async */
131 NULL /* cancel_async */
134 static void mailslot_destroy( struct object *obj)
136 struct mailslot *mailslot = (struct mailslot *) obj;
138 assert( mailslot->fd );
139 assert( mailslot->write_fd );
141 release_object( mailslot->fd );
142 release_object( mailslot->write_fd );
145 static void mailslot_dump( struct object *obj, int verbose )
147 struct mailslot *mailslot = (struct mailslot *) obj;
149 assert( obj->ops == &mailslot_ops );
150 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%d\n",
151 mailslot->max_msgsize, mailslot->read_timeout );
154 static int mailslot_message_count(struct mailslot *mailslot)
158 /* poll the socket to see if there's any messages */
159 pfd.fd = get_unix_fd( mailslot->fd );
162 return (poll( &pfd, 1, 0 ) == 1) ? 1 : 0;
165 static int mailslot_next_msg_size( struct mailslot *mailslot )
170 fd = get_unix_fd( mailslot->fd );
171 ioctl( fd, FIONREAD, &size );
175 static int mailslot_get_info( struct fd *fd )
177 struct mailslot *mailslot = get_fd_user( fd );
178 assert( mailslot->obj.ops == &mailslot_ops );
179 return FD_FLAG_TIMEOUT | FD_FLAG_AVAILABLE;
182 static struct fd *mailslot_get_fd( struct object *obj )
184 struct mailslot *mailslot = (struct mailslot *) obj;
186 return (struct fd *)grab_object( mailslot->fd );
189 static void mailslot_queue_async( struct fd *fd, void *apc, void *user,
190 void *iosb, int type, int count )
192 struct mailslot *mailslot = get_fd_user( fd );
195 assert(mailslot->obj.ops == &mailslot_ops);
197 if (type != ASYNC_TYPE_READ)
199 set_error(STATUS_INVALID_PARAMETER);
203 if (list_empty( &mailslot->writers ) ||
204 !mailslot_message_count( mailslot ))
206 set_error(STATUS_IO_TIMEOUT);
210 if (mailslot->read_timeout != MAILSLOT_WAIT_FOREVER)
211 timeout = &mailslot->read_timeout;
213 fd_queue_async_timeout( fd, apc, user, iosb, type, count, timeout );
216 static struct mailslot *create_mailslot( const struct unicode_str *name, unsigned int attr,
217 int max_msgsize, int read_timeout )
219 struct mailslot *mailslot;
221 static const WCHAR slot[] = {'m','a','i','l','s','l','o','t','\\'};
223 if ((name->len <= sizeof(slot)) || strncmpiW( slot, name->str, sizeof(slot)/sizeof(WCHAR) ))
225 set_error( STATUS_OBJECT_NAME_INVALID );
229 mailslot = create_named_object( sync_namespace, &mailslot_ops, name, attr );
233 /* it already exists - there can only be one mailslot to read from */
234 if (get_error() == STATUS_OBJECT_NAME_COLLISION)
236 release_object( mailslot );
241 mailslot->write_fd = NULL;
242 mailslot->max_msgsize = max_msgsize;
243 mailslot->read_timeout = read_timeout;
244 list_init( &mailslot->writers );
246 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
248 fcntl( fds[0], F_SETFL, O_NONBLOCK );
249 fcntl( fds[1], F_SETFL, O_NONBLOCK );
250 mailslot->fd = create_anonymous_fd( &mailslot_fd_ops,
251 fds[1], &mailslot->obj );
252 mailslot->write_fd = create_anonymous_fd( &mail_writer_fd_ops,
253 fds[0], &mailslot->obj );
254 if (mailslot->fd && mailslot->write_fd) return mailslot;
256 else file_set_error();
258 release_object( mailslot );
262 static struct mailslot *open_mailslot( const struct unicode_str *name, unsigned int attr )
266 obj = find_object( sync_namespace, name, attr );
269 if (obj->ops == &mailslot_ops)
270 return (struct mailslot *)obj;
271 release_object( obj );
272 set_error( STATUS_OBJECT_TYPE_MISMATCH );
275 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
280 static void mail_writer_dump( struct object *obj, int verbose )
282 fprintf( stderr, "Mailslot writer\n" );
285 static void mail_writer_destroy( struct object *obj)
287 struct mail_writer *writer = (struct mail_writer *) obj;
289 list_remove( &writer->entry );
290 release_object( writer->mailslot );
293 static int mail_writer_get_info( struct fd *fd )
298 static struct fd *mail_writer_get_fd( struct object *obj )
300 struct mail_writer *writer = (struct mail_writer *) obj;
302 return (struct fd *)grab_object( writer->mailslot->write_fd );
306 * Readers and writers cannot be mixed.
307 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
309 static struct mail_writer *create_mail_writer( struct mailslot *mailslot, unsigned int access,
310 unsigned int sharing )
312 struct mail_writer *writer;
314 if (!list_empty( &mailslot->writers ))
316 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
318 if (((access & GENERIC_WRITE) || (writer->access & GENERIC_WRITE)) &&
319 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
321 set_error( STATUS_SHARING_VIOLATION );
326 writer = alloc_object( &mail_writer_ops );
330 grab_object( mailslot );
331 writer->mailslot = mailslot;
332 writer->access = access;
333 writer->sharing = sharing;
335 list_add_head( &mailslot->writers, &writer->entry );
340 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
341 unsigned int access )
344 obj = get_handle_obj( process, handle, access, &mailslot_ops );
345 return (struct mailslot *) obj;
349 /* create a mailslot */
350 DECL_HANDLER(create_mailslot)
352 struct mailslot *mailslot;
353 struct unicode_str name;
356 get_req_unicode_str( &name );
357 mailslot = create_mailslot( &name, req->attributes, req->max_msgsize, req->read_timeout );
360 reply->handle = alloc_handle( current->process, mailslot,
361 req->access, req->attributes & OBJ_INHERIT );
362 release_object( mailslot );
367 /* open an existing mailslot */
368 DECL_HANDLER(open_mailslot)
370 struct mailslot *mailslot;
371 struct unicode_str name;
374 get_req_unicode_str( &name );
376 if (!(req->sharing & FILE_SHARE_READ))
378 set_error( STATUS_SHARING_VIOLATION );
382 mailslot = open_mailslot( &name, req->attributes );
385 struct mail_writer *writer;
387 writer = create_mail_writer( mailslot, req->access, req->sharing );
390 reply->handle = alloc_handle( current->process, writer,
391 req->access, req->attributes & OBJ_INHERIT );
392 release_object( writer );
394 release_object( mailslot );
397 set_error( STATUS_NO_SUCH_FILE );
401 /* set mailslot information */
402 DECL_HANDLER(set_mailslot_info)
404 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
408 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
409 mailslot->read_timeout = req->read_timeout;
410 reply->max_msgsize = mailslot->max_msgsize;
411 reply->read_timeout = mailslot->read_timeout;
412 reply->msg_count = mailslot_message_count(mailslot);
414 /* get the size of the next message */
415 if (reply->msg_count)
416 reply->next_msgsize = mailslot_next_msg_size(mailslot);
418 reply->next_msgsize = MAILSLOT_NO_MESSAGE;
420 release_object( mailslot );