push 150847dede558b39567907bd3738046f0ea247b2
[wine/hacks.git] / server / mailslot.c
blobfd50080a17cfe6f100fc2928857927429f68e1bc
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "wine/unicode.h"
29 #include <assert.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
40 #endif
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
43 #endif
44 #ifdef HAVE_SYS_FILIO_H
45 #include <sys/filio.h>
46 #endif
47 #include "windef.h"
48 #include "winternl.h"
50 #include "file.h"
51 #include "handle.h"
52 #include "thread.h"
53 #include "request.h"
55 struct mailslot
57 struct object obj;
58 struct fd *fd;
59 int write_fd;
60 unsigned int max_msgsize;
61 timeout_t read_timeout;
62 struct list writers;
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 struct object *mailslot_open_file( struct object *obj, unsigned int access,
70 unsigned int sharing, unsigned int options );
71 static void mailslot_destroy( struct object * );
73 static const struct object_ops mailslot_ops =
75 sizeof(struct mailslot), /* size */
76 mailslot_dump, /* dump */
77 add_queue, /* add_queue */
78 remove_queue, /* remove_queue */
79 default_fd_signaled, /* signaled */
80 no_satisfied, /* satisfied */
81 no_signal, /* signal */
82 mailslot_get_fd, /* get_fd */
83 mailslot_map_access, /* map_access */
84 no_lookup_name, /* lookup_name */
85 mailslot_open_file, /* open_file */
86 fd_close_handle, /* close_handle */
87 mailslot_destroy /* destroy */
90 static enum server_fd_type mailslot_get_fd_type( struct fd *fd );
91 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
93 static const struct fd_ops mailslot_fd_ops =
95 default_fd_get_poll_events, /* get_poll_events */
96 default_poll_event, /* poll_event */
97 no_flush, /* flush */
98 mailslot_get_fd_type, /* get_fd_type */
99 default_fd_ioctl, /* ioctl */
100 mailslot_queue_async, /* queue_async */
101 default_fd_reselect_async, /* reselect_async */
102 default_fd_cancel_async /* cancel_async */
106 struct mail_writer
108 struct object obj;
109 struct fd *fd;
110 struct mailslot *mailslot;
111 struct list entry;
112 unsigned int access;
113 unsigned int sharing;
116 static void mail_writer_dump( struct object *obj, int verbose );
117 static struct fd *mail_writer_get_fd( struct object *obj );
118 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
119 static void mail_writer_destroy( struct object *obj);
121 static const struct object_ops mail_writer_ops =
123 sizeof(struct mail_writer), /* size */
124 mail_writer_dump, /* dump */
125 no_add_queue, /* add_queue */
126 NULL, /* remove_queue */
127 NULL, /* signaled */
128 NULL, /* satisfied */
129 no_signal, /* signal */
130 mail_writer_get_fd, /* get_fd */
131 mail_writer_map_access, /* map_access */
132 no_lookup_name, /* lookup_name */
133 no_open_file, /* open_file */
134 fd_close_handle, /* close_handle */
135 mail_writer_destroy /* destroy */
138 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
140 static const struct fd_ops mail_writer_fd_ops =
142 default_fd_get_poll_events, /* get_poll_events */
143 default_poll_event, /* poll_event */
144 no_flush, /* flush */
145 mail_writer_get_fd_type, /* get_fd_type */
146 default_fd_ioctl, /* ioctl */
147 default_fd_queue_async, /* queue_async */
148 default_fd_reselect_async, /* reselect_async */
149 default_fd_cancel_async /* cancel_async */
153 struct mailslot_device
155 struct object obj; /* object header */
156 struct fd *fd; /* pseudo-fd for ioctls */
157 struct namespace *mailslots; /* mailslot namespace */
160 static void mailslot_device_dump( struct object *obj, int verbose );
161 static struct fd *mailslot_device_get_fd( struct object *obj );
162 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
163 unsigned int attr );
164 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
165 unsigned int sharing, unsigned int options );
166 static void mailslot_device_destroy( struct object *obj );
167 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
169 static const struct object_ops mailslot_device_ops =
171 sizeof(struct mailslot_device), /* size */
172 mailslot_device_dump, /* dump */
173 no_add_queue, /* add_queue */
174 NULL, /* remove_queue */
175 NULL, /* signaled */
176 no_satisfied, /* satisfied */
177 no_signal, /* signal */
178 mailslot_device_get_fd, /* get_fd */
179 no_map_access, /* map_access */
180 mailslot_device_lookup_name, /* lookup_name */
181 mailslot_device_open_file, /* open_file */
182 fd_close_handle, /* close_handle */
183 mailslot_device_destroy /* destroy */
186 static const struct fd_ops mailslot_device_fd_ops =
188 default_fd_get_poll_events, /* get_poll_events */
189 default_poll_event, /* poll_event */
190 no_flush, /* flush */
191 mailslot_device_get_fd_type, /* get_fd_type */
192 default_fd_ioctl, /* ioctl */
193 default_fd_queue_async, /* queue_async */
194 default_fd_reselect_async, /* reselect_async */
195 default_fd_cancel_async /* cancel_async */
198 static void mailslot_destroy( struct object *obj)
200 struct mailslot *mailslot = (struct mailslot *) obj;
202 assert( mailslot->fd );
204 if (mailslot->write_fd != -1)
206 shutdown( mailslot->write_fd, SHUT_RDWR );
207 close( mailslot->write_fd );
209 release_object( mailslot->fd );
212 static void mailslot_dump( struct object *obj, int verbose )
214 struct mailslot *mailslot = (struct mailslot *) obj;
216 assert( obj->ops == &mailslot_ops );
217 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
218 mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
221 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
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 /* mailslots can only be read */
236 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
237 if (access & GENERIC_ALL) access |= FILE_GENERIC_READ;
238 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
241 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
242 unsigned int sharing, unsigned int options )
244 struct mailslot *mailslot = (struct mailslot *)obj;
245 struct mail_writer *writer;
246 int unix_fd;
248 if (!(sharing & FILE_SHARE_READ))
250 set_error( STATUS_SHARING_VIOLATION );
251 return NULL;
254 if (!list_empty( &mailslot->writers ))
256 /* Readers and writers cannot be mixed.
257 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
259 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
261 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
262 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
264 set_error( STATUS_SHARING_VIOLATION );
265 return NULL;
269 if ((unix_fd = dup( mailslot->write_fd )) == -1)
271 file_set_error();
272 return NULL;
274 if (!(writer = alloc_object( &mail_writer_ops )))
276 close( unix_fd );
277 return NULL;
279 grab_object( mailslot );
280 writer->mailslot = mailslot;
281 writer->access = mail_writer_map_access( &writer->obj, access );
282 writer->sharing = sharing;
283 list_add_head( &mailslot->writers, &writer->entry );
285 if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
287 release_object( writer );
288 return NULL;
290 return &writer->obj;
293 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
295 struct mailslot *mailslot = get_fd_user( fd );
296 struct async *async;
298 assert(mailslot->obj.ops == &mailslot_ops);
300 if ((async = fd_queue_async( fd, data, type, count )))
302 async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
303 STATUS_IO_TIMEOUT );
304 release_object( async );
305 set_error( STATUS_PENDING );
309 static void mailslot_device_dump( struct object *obj, int verbose )
311 assert( obj->ops == &mailslot_device_ops );
312 fprintf( stderr, "Mail slot device\n" );
315 static struct fd *mailslot_device_get_fd( struct object *obj )
317 struct mailslot_device *device = (struct mailslot_device *)obj;
318 return (struct fd *)grab_object( device->fd );
321 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
322 unsigned int attr )
324 struct mailslot_device *device = (struct mailslot_device*)obj;
325 struct object *found;
327 assert( obj->ops == &mailslot_device_ops );
329 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
330 name->len = 0;
332 return found;
335 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
336 unsigned int sharing, unsigned int options )
338 return grab_object( obj );
341 static void mailslot_device_destroy( struct object *obj )
343 struct mailslot_device *device = (struct mailslot_device*)obj;
344 assert( obj->ops == &mailslot_device_ops );
345 if (device->fd) release_object( device->fd );
346 free( device->mailslots );
349 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
351 return FD_TYPE_DEVICE;
354 void create_mailslot_device( struct directory *root, const struct unicode_str *name )
356 struct mailslot_device *dev;
358 if ((dev = create_named_object_dir( root, name, 0, &mailslot_device_ops )) &&
359 get_error() != STATUS_OBJECT_NAME_EXISTS)
361 dev->mailslots = NULL;
362 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj, 0 )) ||
363 !(dev->mailslots = create_namespace( 7 )))
365 release_object( dev );
366 dev = NULL;
369 if (dev) make_object_static( &dev->obj );
372 static struct mailslot *create_mailslot( struct directory *root,
373 const struct unicode_str *name, unsigned int attr,
374 int max_msgsize, timeout_t read_timeout )
376 struct object *obj;
377 struct unicode_str new_name;
378 struct mailslot_device *dev;
379 struct mailslot *mailslot;
380 int fds[2];
382 if (!name || !name->len) return alloc_object( &mailslot_ops );
384 if (!(obj = find_object_dir( root, name, attr, &new_name )))
386 set_error( STATUS_OBJECT_NAME_INVALID );
387 return NULL;
390 if (!new_name.len)
392 if (attr & OBJ_OPENIF && obj->ops == &mailslot_ops)
393 /* it already exists - there can only be one mailslot to read from */
394 set_error( STATUS_OBJECT_NAME_EXISTS );
395 else if (attr & OBJ_OPENIF)
396 set_error( STATUS_OBJECT_TYPE_MISMATCH );
397 else
398 set_error( STATUS_OBJECT_NAME_COLLISION );
399 release_object( obj );
400 return NULL;
403 if (obj->ops != &mailslot_device_ops)
405 set_error( STATUS_OBJECT_NAME_INVALID );
406 release_object( obj );
407 return NULL;
410 dev = (struct mailslot_device *)obj;
411 mailslot = create_object( dev->mailslots, &mailslot_ops, &new_name, NULL );
412 release_object( dev );
414 if (!mailslot) return NULL;
416 mailslot->fd = NULL;
417 mailslot->write_fd = -1;
418 mailslot->max_msgsize = max_msgsize;
419 mailslot->read_timeout = read_timeout;
420 list_init( &mailslot->writers );
422 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
424 fcntl( fds[0], F_SETFL, O_NONBLOCK );
425 fcntl( fds[1], F_SETFL, O_NONBLOCK );
426 shutdown( fds[0], SHUT_RD );
427 mailslot->write_fd = fds[0];
428 mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
429 FILE_SYNCHRONOUS_IO_NONALERT );
430 if (mailslot->fd) return mailslot;
432 else file_set_error();
434 release_object( mailslot );
435 return NULL;
438 static void mail_writer_dump( struct object *obj, int verbose )
440 fprintf( stderr, "Mailslot writer\n" );
443 static void mail_writer_destroy( struct object *obj)
445 struct mail_writer *writer = (struct mail_writer *) obj;
447 if (writer->fd) release_object( writer->fd );
448 list_remove( &writer->entry );
449 release_object( writer->mailslot );
452 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
454 return FD_TYPE_MAILSLOT;
457 static struct fd *mail_writer_get_fd( struct object *obj )
459 struct mail_writer *writer = (struct mail_writer *) obj;
460 return (struct fd *)grab_object( writer->fd );
463 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
465 /* mailslot writers can only get write access */
466 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
467 if (access & GENERIC_ALL) access |= FILE_GENERIC_WRITE;
468 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
471 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
472 unsigned int access )
474 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
478 /* create a mailslot */
479 DECL_HANDLER(create_mailslot)
481 struct mailslot *mailslot;
482 struct unicode_str name;
483 struct directory *root = NULL;
485 reply->handle = 0;
486 get_req_unicode_str( &name );
487 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
488 return;
490 if ((mailslot = create_mailslot( root, &name, req->attributes, req->max_msgsize,
491 req->read_timeout )))
493 reply->handle = alloc_handle( current->process, mailslot, req->access, req->attributes );
494 release_object( mailslot );
497 if (root) release_object( root );
501 /* set mailslot information */
502 DECL_HANDLER(set_mailslot_info)
504 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
506 if (mailslot)
508 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
509 mailslot->read_timeout = req->read_timeout;
510 reply->max_msgsize = mailslot->max_msgsize;
511 reply->read_timeout = mailslot->read_timeout;
512 release_object( mailslot );