Made the registry cache more general.
[wine/dcerpc.git] / server / mailslot.c
blob69b9feaf7bc636269c76fa2e38c8b458cc71f917
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"
47 #include "file.h"
48 #include "handle.h"
49 #include "thread.h"
50 #include "request.h"
52 struct mailslot
54 struct object obj;
55 struct fd *fd;
56 struct fd *write_fd;
57 unsigned int max_msgsize;
58 unsigned int read_timeout;
59 struct list writers;
60 struct list read_q;
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_poll_events( struct fd * );
83 static void mailslot_poll_event( struct fd *, int );
84 static int mailslot_get_info( struct fd * );
85 static void mailslot_queue_async( struct fd *, void*, void*, void*, int, int );
86 static void mailslot_cancel_async( struct fd * );
88 static const struct fd_ops mailslot_fd_ops =
90 mailslot_get_poll_events, /* get_poll_events */
91 mailslot_poll_event, /* poll_event */
92 no_flush, /* flush */
93 mailslot_get_info, /* get_file_info */
94 mailslot_queue_async, /* queue_async */
95 mailslot_cancel_async /* cancel_async */
98 struct mail_writer
100 struct object obj;
101 struct mailslot *mailslot;
102 struct list entry;
103 int access;
104 int sharing;
107 static void mail_writer_dump( struct object *obj, int verbose );
108 static struct fd *mail_writer_get_fd( struct object *obj );
109 static void mail_writer_destroy( struct object *obj);
111 static const struct object_ops mail_writer_ops =
113 sizeof(struct mail_writer), /* size */
114 mail_writer_dump, /* dump */
115 no_add_queue, /* add_queue */
116 NULL, /* remove_queue */
117 NULL, /* signaled */
118 NULL, /* satisfied */
119 no_signal, /* signal */
120 mail_writer_get_fd, /* get_fd */
121 no_close_handle, /* close_handle */
122 mail_writer_destroy /* destroy */
125 static int mail_writer_get_info( struct fd *fd );
127 static const struct fd_ops mail_writer_fd_ops =
129 NULL, /* get_poll_events */
130 NULL, /* poll_event */
131 no_flush, /* flush */
132 mail_writer_get_info, /* get_file_info */
133 no_queue_async, /* queue_async */
134 NULL /* cancel_async */
137 static void mailslot_destroy( struct object *obj)
139 struct mailslot *mailslot = (struct mailslot *) obj;
141 assert( mailslot->fd );
142 assert( mailslot->write_fd );
144 async_terminate_queue( &mailslot->read_q, STATUS_CANCELLED );
146 release_object( mailslot->fd );
147 release_object( mailslot->write_fd );
150 static void mailslot_dump( struct object *obj, int verbose )
152 struct mailslot *mailslot = (struct mailslot *) obj;
154 assert( obj->ops == &mailslot_ops );
155 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%d\n",
156 mailslot->max_msgsize, mailslot->read_timeout );
159 static int mailslot_message_count(struct mailslot *mailslot)
161 struct pollfd pfd;
163 /* poll the socket to see if there's any messages */
164 pfd.fd = get_unix_fd( mailslot->fd );
165 pfd.events = POLLIN;
166 pfd.revents = 0;
167 return (poll( &pfd, 1, 0 ) == 1) ? 1 : 0;
170 static int mailslot_next_msg_size( struct mailslot *mailslot )
172 int size, fd;
174 size = 0;
175 fd = get_unix_fd( mailslot->fd );
176 ioctl( fd, FIONREAD, &size );
177 return size;
180 static int mailslot_get_info( struct fd *fd )
182 struct mailslot *mailslot = get_fd_user( fd );
183 assert( mailslot->obj.ops == &mailslot_ops );
184 return FD_FLAG_TIMEOUT | FD_FLAG_AVAILABLE;
187 static struct fd *mailslot_get_fd( struct object *obj )
189 struct mailslot *mailslot = (struct mailslot *) obj;
191 return (struct fd *)grab_object( mailslot->fd );
194 static int mailslot_get_poll_events( struct fd *fd )
196 struct mailslot *mailslot = get_fd_user( fd );
197 int events = 0;
198 assert( mailslot->obj.ops == &mailslot_ops );
200 if (!list_empty( &mailslot->read_q ))
201 events |= POLLIN;
203 return events;
206 static void mailslot_poll_event( struct fd *fd, int event )
208 struct mailslot *mailslot = get_fd_user( fd );
210 if (!list_empty( &mailslot->read_q ) && (POLLIN & event))
211 async_terminate_head( &mailslot->read_q, STATUS_ALERTED );
213 set_fd_events( fd, mailslot_get_poll_events(fd) );
216 static void mailslot_queue_async( struct fd *fd, void *apc, void *user,
217 void *iosb, int type, int count )
219 struct mailslot *mailslot = get_fd_user( fd );
220 int events, *ptimeout = NULL;
222 assert(mailslot->obj.ops == &mailslot_ops);
224 if (type != ASYNC_TYPE_READ)
226 set_error(STATUS_INVALID_PARAMETER);
227 return;
230 if (list_empty( &mailslot->writers ) ||
231 !mailslot_message_count( mailslot ))
233 set_error(STATUS_IO_TIMEOUT);
234 return;
237 if (mailslot->read_timeout != MAILSLOT_WAIT_FOREVER)
238 ptimeout = &mailslot->read_timeout;
240 if (!create_async( current, ptimeout, &mailslot->read_q, apc, user, iosb ))
241 return;
243 /* Check if the new pending request can be served immediately */
244 events = check_fd_events( fd, mailslot_get_poll_events( fd ) );
245 if (events)
247 mailslot_poll_event( fd, events );
248 return;
251 set_fd_events( fd, mailslot_get_poll_events( fd ));
254 static void mailslot_cancel_async( struct fd *fd )
256 struct mailslot *mailslot = get_fd_user( fd );
258 assert(mailslot->obj.ops == &mailslot_ops);
259 async_terminate_queue( &mailslot->read_q, STATUS_CANCELLED );
262 static struct mailslot *create_mailslot( const WCHAR *name, size_t len, int max_msgsize,
263 int read_timeout )
265 struct mailslot *mailslot;
266 int fds[2];
267 static const WCHAR slot[] = {'m','a','i','l','s','l','o','t','\\',0};
269 if (( len <= strlenW( slot )) || strncmpiW( slot, name, strlenW( slot ) ))
271 set_error( STATUS_OBJECT_NAME_INVALID );
272 return NULL;
275 mailslot = create_named_object( sync_namespace, &mailslot_ops, name, len );
276 if (!mailslot)
277 return NULL;
279 /* it already exists - there can only be one mailslot to read from */
280 if (get_error() == STATUS_OBJECT_NAME_COLLISION)
282 release_object( mailslot );
283 return NULL;
286 mailslot->fd = NULL;
287 mailslot->write_fd = NULL;
288 mailslot->max_msgsize = max_msgsize;
289 mailslot->read_timeout = read_timeout;
290 list_init( &mailslot->writers );
291 list_init( &mailslot->read_q );
293 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
295 fcntl( fds[0], F_SETFL, O_NONBLOCK );
296 fcntl( fds[1], F_SETFL, O_NONBLOCK );
297 mailslot->fd = create_anonymous_fd( &mailslot_fd_ops,
298 fds[1], &mailslot->obj );
299 mailslot->write_fd = create_anonymous_fd( &mail_writer_fd_ops,
300 fds[0], &mailslot->obj );
301 if (mailslot->fd && mailslot->write_fd) return mailslot;
303 else file_set_error();
305 release_object( mailslot );
306 return NULL;
309 static struct mailslot *open_mailslot( const WCHAR *name, size_t len )
311 struct object *obj;
313 obj = find_object( sync_namespace, name, len );
314 if (obj)
316 if (obj->ops == &mailslot_ops)
317 return (struct mailslot *)obj;
318 release_object( obj );
319 set_error( STATUS_OBJECT_TYPE_MISMATCH );
321 else
322 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
324 return NULL;
327 static void mail_writer_dump( struct object *obj, int verbose )
329 fprintf( stderr, "Mailslot writer\n" );
332 static void mail_writer_destroy( struct object *obj)
334 struct mail_writer *writer = (struct mail_writer *) obj;
336 list_remove( &writer->entry );
337 release_object( writer->mailslot );
340 static int mail_writer_get_info( struct fd *fd )
342 return 0;
345 static struct fd *mail_writer_get_fd( struct object *obj )
347 struct mail_writer *writer = (struct mail_writer *) obj;
349 return (struct fd *)grab_object( writer->mailslot->write_fd );
353 * Readers and writers cannot be mixed.
354 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
356 static struct mail_writer *create_mail_writer( struct mailslot *mailslot, unsigned int access,
357 unsigned int sharing )
359 struct mail_writer *writer;
361 if (!list_empty( &mailslot->writers ))
363 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
365 if (((access & GENERIC_WRITE) || (writer->access & GENERIC_WRITE)) &&
366 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
368 set_error( STATUS_SHARING_VIOLATION );
369 return NULL;
373 writer = alloc_object( &mail_writer_ops );
374 if (!writer)
375 return NULL;
377 grab_object( mailslot );
378 writer->mailslot = mailslot;
379 writer->access = access;
380 writer->sharing = sharing;
382 list_add_head( &mailslot->writers, &writer->entry );
384 return writer;
387 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
388 unsigned int access )
390 struct object *obj;
391 obj = get_handle_obj( process, handle, access, &mailslot_ops );
392 return (struct mailslot *) obj;
396 /* create a mailslot */
397 DECL_HANDLER(create_mailslot)
399 struct mailslot *mailslot;
401 reply->handle = 0;
402 mailslot = create_mailslot( get_req_data(), get_req_data_size(),
403 req->max_msgsize, req->read_timeout );
404 if (mailslot)
406 reply->handle = alloc_handle( current->process, mailslot,
407 GENERIC_READ, req->inherit );
408 release_object( mailslot );
413 /* open an existing mailslot */
414 DECL_HANDLER(open_mailslot)
416 struct mailslot *mailslot;
418 reply->handle = 0;
420 if (!(req->sharing & FILE_SHARE_READ))
422 set_error( STATUS_SHARING_VIOLATION );
423 return;
426 mailslot = open_mailslot( get_req_data(), get_req_data_size() );
427 if (mailslot)
429 struct mail_writer *writer;
431 writer = create_mail_writer( mailslot, req->access, req->sharing );
432 if (writer)
434 reply->handle = alloc_handle( current->process, writer,
435 req->access, req->inherit );
436 release_object( writer );
438 release_object( mailslot );
440 else
441 set_error( STATUS_NO_SUCH_FILE );
445 /* set mailslot information */
446 DECL_HANDLER(set_mailslot_info)
448 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
450 if (mailslot)
452 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
453 mailslot->read_timeout = req->read_timeout;
454 reply->max_msgsize = mailslot->max_msgsize;
455 reply->read_timeout = mailslot->read_timeout;
456 reply->msg_count = mailslot_message_count(mailslot);
458 /* get the size of the next message */
459 if (reply->msg_count)
460 reply->next_msgsize = mailslot_next_msg_size(mailslot);
461 else
462 reply->next_msgsize = MAILSLOT_NO_MESSAGE;
464 release_object( mailslot );