quartz/tests: Fix test failures on Windows XP without upgraded DirectX.
[wine.git] / server / mailslot.c
blob9fafedbd2046e8ed5c13c9938f61226d184cd324
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 int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent );
70 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
71 unsigned int sharing, unsigned int options );
72 static void mailslot_destroy( struct object * );
74 static const struct object_ops mailslot_ops =
76 sizeof(struct mailslot), /* size */
77 mailslot_dump, /* dump */
78 no_get_type, /* get_type */
79 add_queue, /* add_queue */
80 remove_queue, /* remove_queue */
81 default_fd_signaled, /* signaled */
82 no_satisfied, /* satisfied */
83 no_signal, /* signal */
84 mailslot_get_fd, /* get_fd */
85 mailslot_map_access, /* map_access */
86 default_get_sd, /* get_sd */
87 default_set_sd, /* set_sd */
88 no_lookup_name, /* lookup_name */
89 mailslot_link_name, /* link_name */
90 default_unlink_name, /* unlink_name */
91 mailslot_open_file, /* open_file */
92 fd_close_handle, /* close_handle */
93 mailslot_destroy /* destroy */
96 static enum server_fd_type mailslot_get_fd_type( struct fd *fd );
97 static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count );
99 static const struct fd_ops mailslot_fd_ops =
101 default_fd_get_poll_events, /* get_poll_events */
102 default_poll_event, /* poll_event */
103 mailslot_get_fd_type, /* get_fd_type */
104 no_fd_read, /* read */
105 no_fd_write, /* write */
106 no_fd_flush, /* flush */
107 no_fd_get_volume_info, /* get_volume_info */
108 default_fd_ioctl, /* ioctl */
109 mailslot_queue_async, /* queue_async */
110 default_fd_reselect_async /* reselect_async */
114 struct mail_writer
116 struct object obj;
117 struct fd *fd;
118 struct mailslot *mailslot;
119 struct list entry;
120 unsigned int access;
121 unsigned int sharing;
124 static void mail_writer_dump( struct object *obj, int verbose );
125 static struct fd *mail_writer_get_fd( struct object *obj );
126 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
127 static void mail_writer_destroy( struct object *obj);
129 static const struct object_ops mail_writer_ops =
131 sizeof(struct mail_writer), /* size */
132 mail_writer_dump, /* dump */
133 no_get_type, /* get_type */
134 no_add_queue, /* add_queue */
135 NULL, /* remove_queue */
136 NULL, /* signaled */
137 NULL, /* satisfied */
138 no_signal, /* signal */
139 mail_writer_get_fd, /* get_fd */
140 mail_writer_map_access, /* map_access */
141 default_get_sd, /* get_sd */
142 default_set_sd, /* set_sd */
143 no_lookup_name, /* lookup_name */
144 no_link_name, /* link_name */
145 NULL, /* unlink_name */
146 no_open_file, /* open_file */
147 fd_close_handle, /* close_handle */
148 mail_writer_destroy /* destroy */
151 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
153 static const struct fd_ops mail_writer_fd_ops =
155 default_fd_get_poll_events, /* get_poll_events */
156 default_poll_event, /* poll_event */
157 mail_writer_get_fd_type, /* get_fd_type */
158 no_fd_read, /* read */
159 no_fd_write, /* write */
160 no_fd_flush, /* flush */
161 no_fd_get_volume_info, /* get_volume_info */
162 default_fd_ioctl, /* ioctl */
163 default_fd_queue_async, /* queue_async */
164 default_fd_reselect_async /* reselect_async */
168 struct mailslot_device
170 struct object obj; /* object header */
171 struct fd *fd; /* pseudo-fd for ioctls */
172 struct namespace *mailslots; /* mailslot namespace */
175 static void mailslot_device_dump( struct object *obj, int verbose );
176 static struct object_type *mailslot_device_get_type( struct object *obj );
177 static struct fd *mailslot_device_get_fd( struct object *obj );
178 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
179 unsigned int attr );
180 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
181 unsigned int sharing, unsigned int options );
182 static void mailslot_device_destroy( struct object *obj );
183 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
185 static const struct object_ops mailslot_device_ops =
187 sizeof(struct mailslot_device), /* size */
188 mailslot_device_dump, /* dump */
189 mailslot_device_get_type, /* get_type */
190 no_add_queue, /* add_queue */
191 NULL, /* remove_queue */
192 NULL, /* signaled */
193 no_satisfied, /* satisfied */
194 no_signal, /* signal */
195 mailslot_device_get_fd, /* get_fd */
196 no_map_access, /* map_access */
197 default_get_sd, /* get_sd */
198 default_set_sd, /* set_sd */
199 mailslot_device_lookup_name, /* lookup_name */
200 directory_link_name, /* link_name */
201 default_unlink_name, /* unlink_name */
202 mailslot_device_open_file, /* open_file */
203 fd_close_handle, /* close_handle */
204 mailslot_device_destroy /* destroy */
207 static const struct fd_ops mailslot_device_fd_ops =
209 default_fd_get_poll_events, /* get_poll_events */
210 default_poll_event, /* poll_event */
211 mailslot_device_get_fd_type, /* get_fd_type */
212 no_fd_read, /* read */
213 no_fd_write, /* write */
214 no_fd_flush, /* flush */
215 no_fd_get_volume_info, /* get_volume_info */
216 default_fd_ioctl, /* ioctl */
217 default_fd_queue_async, /* queue_async */
218 default_fd_reselect_async /* reselect_async */
221 static void mailslot_destroy( struct object *obj)
223 struct mailslot *mailslot = (struct mailslot *) obj;
225 assert( mailslot->fd );
227 if (mailslot->write_fd != -1)
229 shutdown( mailslot->write_fd, SHUT_RDWR );
230 close( mailslot->write_fd );
232 release_object( mailslot->fd );
235 static void mailslot_dump( struct object *obj, int verbose )
237 struct mailslot *mailslot = (struct mailslot *) obj;
239 assert( obj->ops == &mailslot_ops );
240 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
241 mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
244 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
246 return FD_TYPE_MAILSLOT;
249 static struct fd *mailslot_get_fd( struct object *obj )
251 struct mailslot *mailslot = (struct mailslot *) obj;
253 return (struct fd *)grab_object( mailslot->fd );
256 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
258 /* mailslots can only be read */
259 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
260 if (access & GENERIC_ALL) access |= FILE_GENERIC_READ;
261 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
264 static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent )
266 struct mailslot_device *dev = (struct mailslot_device *)parent;
268 if (parent->ops != &mailslot_device_ops)
270 set_error( STATUS_OBJECT_NAME_INVALID );
271 return 0;
273 namespace_add( dev->mailslots, name );
274 name->parent = grab_object( parent );
275 return 1;
278 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
279 unsigned int sharing, unsigned int options )
281 struct mailslot *mailslot = (struct mailslot *)obj;
282 struct mail_writer *writer;
283 int unix_fd;
285 if (!(sharing & FILE_SHARE_READ))
287 set_error( STATUS_SHARING_VIOLATION );
288 return NULL;
291 if (!list_empty( &mailslot->writers ))
293 /* Readers and writers cannot be mixed.
294 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
296 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
298 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
299 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
301 set_error( STATUS_SHARING_VIOLATION );
302 return NULL;
306 if ((unix_fd = dup( mailslot->write_fd )) == -1)
308 file_set_error();
309 return NULL;
311 if (!(writer = alloc_object( &mail_writer_ops )))
313 close( unix_fd );
314 return NULL;
316 grab_object( mailslot );
317 writer->mailslot = mailslot;
318 writer->access = mail_writer_map_access( &writer->obj, access );
319 writer->sharing = sharing;
320 list_add_head( &mailslot->writers, &writer->entry );
322 if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
324 release_object( writer );
325 return NULL;
327 allow_fd_caching( writer->fd );
328 return &writer->obj;
331 static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count )
333 struct mailslot *mailslot = get_fd_user( fd );
335 assert(mailslot->obj.ops == &mailslot_ops);
337 fd_queue_async( fd, async, type );
338 async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
339 STATUS_IO_TIMEOUT );
340 set_error( STATUS_PENDING );
343 static void mailslot_device_dump( struct object *obj, int verbose )
345 fputs( "Mailslot device\n", stderr );
348 static struct object_type *mailslot_device_get_type( struct object *obj )
350 static const WCHAR name[] = {'D','e','v','i','c','e'};
351 static const struct unicode_str str = { name, sizeof(name) };
352 return get_object_type( &str );
355 static struct fd *mailslot_device_get_fd( struct object *obj )
357 struct mailslot_device *device = (struct mailslot_device *)obj;
358 return (struct fd *)grab_object( device->fd );
361 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
362 unsigned int attr )
364 struct mailslot_device *device = (struct mailslot_device*)obj;
365 struct object *found;
367 assert( obj->ops == &mailslot_device_ops );
369 if (!name) return NULL; /* open the device itself */
371 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
372 name->len = 0;
374 return found;
377 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
378 unsigned int sharing, unsigned int options )
380 return grab_object( obj );
383 static void mailslot_device_destroy( struct object *obj )
385 struct mailslot_device *device = (struct mailslot_device*)obj;
386 assert( obj->ops == &mailslot_device_ops );
387 if (device->fd) release_object( device->fd );
388 free( device->mailslots );
391 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
393 return FD_TYPE_DEVICE;
396 struct object *create_mailslot_device( struct object *root, const struct unicode_str *name )
398 struct mailslot_device *dev;
400 if ((dev = create_named_object( root, &mailslot_device_ops, name, 0, NULL )) &&
401 get_error() != STATUS_OBJECT_NAME_EXISTS)
403 dev->mailslots = NULL;
404 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj, 0 )) ||
405 !(dev->mailslots = create_namespace( 7 )))
407 release_object( dev );
408 dev = NULL;
411 return &dev->obj;
414 static struct mailslot *create_mailslot( struct object *root,
415 const struct unicode_str *name, unsigned int attr,
416 int max_msgsize, timeout_t read_timeout,
417 const struct security_descriptor *sd )
419 struct mailslot *mailslot;
420 int fds[2];
422 if (!(mailslot = create_named_object( root, &mailslot_ops, name, attr, sd ))) return NULL;
424 mailslot->fd = NULL;
425 mailslot->write_fd = -1;
426 mailslot->max_msgsize = max_msgsize;
427 mailslot->read_timeout = read_timeout;
428 list_init( &mailslot->writers );
430 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
432 fcntl( fds[0], F_SETFL, O_NONBLOCK );
433 fcntl( fds[1], F_SETFL, O_NONBLOCK );
434 shutdown( fds[0], SHUT_RD );
435 mailslot->write_fd = fds[0];
436 if ((mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
437 FILE_SYNCHRONOUS_IO_NONALERT )))
439 allow_fd_caching( mailslot->fd );
440 return mailslot;
443 else file_set_error();
445 release_object( mailslot );
446 return NULL;
449 static void mail_writer_dump( struct object *obj, int verbose )
451 fprintf( stderr, "Mailslot writer\n" );
454 static void mail_writer_destroy( struct object *obj)
456 struct mail_writer *writer = (struct mail_writer *) obj;
458 if (writer->fd) release_object( writer->fd );
459 list_remove( &writer->entry );
460 release_object( writer->mailslot );
463 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
465 return FD_TYPE_MAILSLOT;
468 static struct fd *mail_writer_get_fd( struct object *obj )
470 struct mail_writer *writer = (struct mail_writer *) obj;
471 return (struct fd *)grab_object( writer->fd );
474 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
476 /* mailslot writers can only get write access */
477 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
478 if (access & GENERIC_ALL) access |= FILE_GENERIC_WRITE;
479 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
482 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
483 unsigned int access )
485 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
489 /* create a mailslot */
490 DECL_HANDLER(create_mailslot)
492 struct mailslot *mailslot;
493 struct unicode_str name;
494 struct object *root;
495 const struct security_descriptor *sd;
496 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
498 if (!objattr) return;
500 if (!name.len) /* mailslots need a root directory even without a name */
502 if (!objattr->rootdir)
504 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
505 return;
507 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
510 if ((mailslot = create_mailslot( root, &name, objattr->attributes, req->max_msgsize,
511 req->read_timeout, sd )))
513 reply->handle = alloc_handle( current->process, mailslot, req->access, objattr->attributes );
514 release_object( mailslot );
517 if (root) release_object( root );
521 /* set mailslot information */
522 DECL_HANDLER(set_mailslot_info)
524 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
526 if (mailslot)
528 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
529 mailslot->read_timeout = req->read_timeout;
530 reply->max_msgsize = mailslot->max_msgsize;
531 reply->read_timeout = mailslot->read_timeout;
532 release_object( mailslot );