- use interfaces rather than internal functions
[wine/multimedia.git] / server / mailslot.c
blobc4e46eb44ce679f89505b3c2f3536d07dbb08bb4
1 /*
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
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wine/unicode.h"
27 #include <assert.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
38 #endif
39 #ifdef HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
41 #endif
42 #ifdef HAVE_SYS_FILIO_H
43 #include <sys/filio.h>
44 #endif
45 #include "windef.h"
46 #include "winbase.h"
48 #include "file.h"
49 #include "handle.h"
50 #include "thread.h"
51 #include "request.h"
53 struct mailslot
55 struct object obj;
56 struct fd *fd;
57 struct fd *write_fd;
58 unsigned int max_msgsize;
59 unsigned int read_timeout;
60 struct list writers;
61 struct list read_q;
64 /* mailslot functions */
65 static void mailslot_dump( struct object*, int );
66 static struct fd *mailslot_get_fd( struct object * );
67 static void mailslot_destroy( struct object * );
69 static const struct object_ops mailslot_ops =
71 sizeof(struct mailslot), /* size */
72 mailslot_dump, /* dump */
73 default_fd_add_queue, /* add_queue */
74 default_fd_remove_queue, /* remove_queue */
75 default_fd_signaled, /* signaled */
76 no_satisfied, /* satisfied */
77 no_signal, /* signal */
78 mailslot_get_fd, /* get_fd */
79 no_close_handle, /* close_handle */
80 mailslot_destroy /* destroy */
83 static int mailslot_get_poll_events( struct fd * );
84 static void mailslot_poll_event( struct fd *, int );
85 static int mailslot_get_info( struct fd * );
86 static void mailslot_queue_async( struct fd *, void*, void*, void*, int, int );
87 static void mailslot_cancel_async( struct fd * );
89 static const struct fd_ops mailslot_fd_ops =
91 mailslot_get_poll_events, /* get_poll_events */
92 mailslot_poll_event, /* poll_event */
93 no_flush, /* flush */
94 mailslot_get_info, /* get_file_info */
95 mailslot_queue_async, /* queue_async */
96 mailslot_cancel_async /* cancel_async */
99 struct mail_writer
101 struct object obj;
102 struct mailslot *mailslot;
103 struct list entry;
104 int access;
105 int sharing;
108 static void mail_writer_dump( struct object *obj, int verbose );
109 static struct fd *mail_writer_get_fd( struct object *obj );
110 static void mail_writer_destroy( struct object *obj);
112 static const struct object_ops mail_writer_ops =
114 sizeof(struct mail_writer), /* size */
115 mail_writer_dump, /* dump */
116 no_add_queue, /* add_queue */
117 NULL, /* remove_queue */
118 NULL, /* signaled */
119 NULL, /* satisfied */
120 no_signal, /* signal */
121 mail_writer_get_fd, /* get_fd */
122 no_close_handle, /* close_handle */
123 mail_writer_destroy /* destroy */
126 static int mail_writer_get_info( struct fd *fd );
128 static const struct fd_ops mail_writer_fd_ops =
130 NULL, /* get_poll_events */
131 NULL, /* poll_event */
132 no_flush, /* flush */
133 mail_writer_get_info, /* get_file_info */
134 no_queue_async, /* queue_async */
135 NULL /* cancel_async */
138 static void mailslot_destroy( struct object *obj)
140 struct mailslot *mailslot = (struct mailslot *) obj;
142 assert( mailslot->fd );
143 assert( mailslot->write_fd );
145 async_terminate_queue( &mailslot->read_q, STATUS_CANCELLED );
147 release_object( mailslot->fd );
148 release_object( mailslot->write_fd );
151 static void mailslot_dump( struct object *obj, int verbose )
153 struct mailslot *mailslot = (struct mailslot *) obj;
155 assert( obj->ops == &mailslot_ops );
156 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%d\n",
157 mailslot->max_msgsize, mailslot->read_timeout );
160 static int mailslot_message_count(struct mailslot *mailslot)
162 struct pollfd pfd;
164 /* poll the socket to see if there's any messages */
165 pfd.fd = get_unix_fd( mailslot->fd );
166 pfd.events = POLLIN;
167 pfd.revents = 0;
168 return (poll( &pfd, 1, 0 ) == 1) ? 1 : 0;
171 static int mailslot_next_msg_size( struct mailslot *mailslot )
173 int size, fd;
175 size = 0;
176 fd = get_unix_fd( mailslot->fd );
177 ioctl( fd, FIONREAD, &size );
178 return size;
181 static int mailslot_get_info( struct fd *fd )
183 struct mailslot *mailslot = get_fd_user( fd );
184 assert( mailslot->obj.ops == &mailslot_ops );
185 return FD_FLAG_TIMEOUT | FD_FLAG_AVAILABLE;
188 static struct fd *mailslot_get_fd( struct object *obj )
190 struct mailslot *mailslot = (struct mailslot *) obj;
192 return (struct fd *)grab_object( mailslot->fd );
195 static int mailslot_get_poll_events( struct fd *fd )
197 struct mailslot *mailslot = get_fd_user( fd );
198 int events = 0;
199 assert( mailslot->obj.ops == &mailslot_ops );
201 if (!list_empty( &mailslot->read_q ))
202 events |= POLLIN;
204 return events;
207 static void mailslot_poll_event( struct fd *fd, int event )
209 struct mailslot *mailslot = get_fd_user( fd );
211 if (!list_empty( &mailslot->read_q ) && (POLLIN & event))
212 async_terminate_head( &mailslot->read_q, STATUS_ALERTED );
214 set_fd_events( fd, mailslot_get_poll_events(fd) );
217 static void mailslot_queue_async( struct fd *fd, void *apc, void *user,
218 void *iosb, int type, int count )
220 struct mailslot *mailslot = get_fd_user( fd );
221 int events, *ptimeout = NULL;
223 assert(mailslot->obj.ops == &mailslot_ops);
225 if (type != ASYNC_TYPE_READ)
227 set_error(STATUS_INVALID_PARAMETER);
228 return;
231 if (list_empty( &mailslot->writers ) ||
232 !mailslot_message_count( mailslot ))
234 set_error(STATUS_IO_TIMEOUT);
235 return;
238 if (mailslot->read_timeout != MAILSLOT_WAIT_FOREVER)
239 ptimeout = &mailslot->read_timeout;
241 if (!create_async( current, ptimeout, &mailslot->read_q, apc, user, iosb ))
242 return;
244 /* Check if the new pending request can be served immediately */
245 events = check_fd_events( fd, mailslot_get_poll_events( fd ) );
246 if (events)
248 mailslot_poll_event( fd, events );
249 return;
252 set_fd_events( fd, mailslot_get_poll_events( fd ));
255 static void mailslot_cancel_async( struct fd *fd )
257 struct mailslot *mailslot = get_fd_user( fd );
259 assert(mailslot->obj.ops == &mailslot_ops);
260 async_terminate_queue( &mailslot->read_q, STATUS_CANCELLED );
263 static struct mailslot *create_mailslot( const WCHAR *name, size_t len, int max_msgsize,
264 int read_timeout )
266 struct mailslot *mailslot;
267 int fds[2];
268 static const WCHAR slot[] = {'m','a','i','l','s','l','o','t','\\',0};
270 if (( len <= strlenW( slot )) || strncmpiW( slot, name, strlenW( slot ) ))
272 set_error( STATUS_OBJECT_NAME_INVALID );
273 return NULL;
276 mailslot = create_named_object( sync_namespace, &mailslot_ops, name, len );
277 if (!mailslot)
278 return NULL;
280 /* it already exists - there can only be one mailslot to read from */
281 if (get_error() == STATUS_OBJECT_NAME_COLLISION)
283 release_object( mailslot );
284 return NULL;
287 mailslot->fd = NULL;
288 mailslot->write_fd = NULL;
289 mailslot->max_msgsize = max_msgsize;
290 mailslot->read_timeout = read_timeout;
291 list_init( &mailslot->writers );
292 list_init( &mailslot->read_q );
294 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
296 fcntl( fds[0], F_SETFL, O_NONBLOCK );
297 fcntl( fds[1], F_SETFL, O_NONBLOCK );
298 mailslot->fd = create_anonymous_fd( &mailslot_fd_ops,
299 fds[1], &mailslot->obj );
300 mailslot->write_fd = create_anonymous_fd( &mail_writer_fd_ops,
301 fds[0], &mailslot->obj );
302 if (mailslot->fd && mailslot->write_fd) return mailslot;
304 else file_set_error();
306 release_object( mailslot );
307 return NULL;
310 static struct mailslot *open_mailslot( const WCHAR *name, size_t len )
312 struct object *obj;
314 obj = find_object( sync_namespace, name, len );
315 if (obj)
317 if (obj->ops == &mailslot_ops)
318 return (struct mailslot *)obj;
319 release_object( obj );
320 set_error( STATUS_OBJECT_TYPE_MISMATCH );
322 else
323 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
325 return NULL;
328 static void mail_writer_dump( struct object *obj, int verbose )
330 fprintf( stderr, "Mailslot writer\n" );
333 static void mail_writer_destroy( struct object *obj)
335 struct mail_writer *writer = (struct mail_writer *) obj;
337 list_remove( &writer->entry );
338 release_object( writer->mailslot );
341 static int mail_writer_get_info( struct fd *fd )
343 return 0;
346 static struct fd *mail_writer_get_fd( struct object *obj )
348 struct mail_writer *writer = (struct mail_writer *) obj;
350 return (struct fd *)grab_object( writer->mailslot->write_fd );
354 * Readers and writers cannot be mixed.
355 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
357 static struct mail_writer *create_mail_writer( struct mailslot *mailslot, unsigned int access,
358 unsigned int sharing )
360 struct mail_writer *writer;
362 if (!list_empty( &mailslot->writers ))
364 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
366 if (((access & GENERIC_WRITE) || (writer->access & GENERIC_WRITE)) &&
367 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
369 set_error( STATUS_SHARING_VIOLATION );
370 return NULL;
374 writer = alloc_object( &mail_writer_ops );
375 if (!writer)
376 return NULL;
378 grab_object( mailslot );
379 writer->mailslot = mailslot;
380 writer->access = access;
381 writer->sharing = sharing;
383 list_add_head( &mailslot->writers, &writer->entry );
385 return writer;
388 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
389 unsigned int access )
391 struct object *obj;
392 obj = get_handle_obj( process, handle, access, &mailslot_ops );
393 return (struct mailslot *) obj;
397 /* create a mailslot */
398 DECL_HANDLER(create_mailslot)
400 struct mailslot *mailslot;
402 reply->handle = 0;
403 mailslot = create_mailslot( get_req_data(), get_req_data_size(),
404 req->max_msgsize, req->read_timeout );
405 if (mailslot)
407 reply->handle = alloc_handle( current->process, mailslot,
408 GENERIC_READ, req->inherit );
409 release_object( mailslot );
414 /* open an existing mailslot */
415 DECL_HANDLER(open_mailslot)
417 struct mailslot *mailslot;
419 reply->handle = 0;
421 if (!(req->sharing & FILE_SHARE_READ))
423 set_error( STATUS_SHARING_VIOLATION );
424 return;
427 mailslot = open_mailslot( get_req_data(), get_req_data_size() );
428 if (mailslot)
430 struct mail_writer *writer;
432 writer = create_mail_writer( mailslot, req->access, req->sharing );
433 if (writer)
435 reply->handle = alloc_handle( current->process, writer,
436 req->access, req->inherit );
437 release_object( writer );
439 release_object( mailslot );
441 else
442 set_error( STATUS_NO_SUCH_FILE );
446 /* set mailslot information */
447 DECL_HANDLER(set_mailslot_info)
449 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
451 if (mailslot)
453 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
454 mailslot->read_timeout = req->read_timeout;
455 reply->max_msgsize = mailslot->max_msgsize;
456 reply->read_timeout = mailslot->read_timeout;
457 reply->msg_count = mailslot_message_count(mailslot);
459 /* get the size of the next message */
460 if (reply->msg_count)
461 reply->next_msgsize = mailslot_next_msg_size(mailslot);
462 else
463 reply->next_msgsize = MAILSLOT_NO_MESSAGE;
465 release_object( mailslot );