include: Add MSITRANSFORM_ERROR enum definition.
[wine.git] / server / mailslot.c
blob781e6f3141aa88b181b77b3cb4fb52af613385ed
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
28 #include <assert.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
39 #endif
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
42 #endif
43 #ifdef HAVE_SYS_FILIO_H
44 #include <sys/filio.h>
45 #endif
46 #include "windef.h"
47 #include "winternl.h"
49 #include "file.h"
50 #include "handle.h"
51 #include "thread.h"
52 #include "request.h"
54 struct mailslot
56 struct object obj;
57 struct fd *fd;
58 int write_fd;
59 unsigned int max_msgsize;
60 timeout_t read_timeout;
61 struct list writers;
64 /* mailslot functions */
65 static void mailslot_dump( struct object*, int );
66 static struct fd *mailslot_get_fd( struct object * );
67 static unsigned int mailslot_map_access( struct object *obj, unsigned int access );
68 static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent );
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 file_get_type, /* get_type */
78 add_queue, /* add_queue */
79 remove_queue, /* remove_queue */
80 default_fd_signaled, /* signaled */
81 no_satisfied, /* satisfied */
82 no_signal, /* signal */
83 mailslot_get_fd, /* get_fd */
84 mailslot_map_access, /* map_access */
85 default_get_sd, /* get_sd */
86 default_set_sd, /* set_sd */
87 no_lookup_name, /* lookup_name */
88 mailslot_link_name, /* link_name */
89 default_unlink_name, /* unlink_name */
90 mailslot_open_file, /* open_file */
91 no_kernel_obj_list, /* get_kernel_obj_list */
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 default_fd_get_file_info, /* get_file_info */
108 no_fd_get_volume_info, /* get_volume_info */
109 default_fd_ioctl, /* ioctl */
110 mailslot_queue_async, /* queue_async */
111 default_fd_reselect_async /* reselect_async */
115 struct mail_writer
117 struct object obj;
118 struct fd *fd;
119 struct mailslot *mailslot;
120 struct list entry;
121 unsigned int access;
122 unsigned int sharing;
125 static void mail_writer_dump( struct object *obj, int verbose );
126 static struct fd *mail_writer_get_fd( struct object *obj );
127 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
128 static void mail_writer_destroy( struct object *obj);
130 static const struct object_ops mail_writer_ops =
132 sizeof(struct mail_writer), /* size */
133 mail_writer_dump, /* dump */
134 file_get_type, /* get_type */
135 no_add_queue, /* add_queue */
136 NULL, /* remove_queue */
137 NULL, /* signaled */
138 NULL, /* satisfied */
139 no_signal, /* signal */
140 mail_writer_get_fd, /* get_fd */
141 mail_writer_map_access, /* map_access */
142 default_get_sd, /* get_sd */
143 default_set_sd, /* set_sd */
144 no_lookup_name, /* lookup_name */
145 no_link_name, /* link_name */
146 NULL, /* unlink_name */
147 no_open_file, /* open_file */
148 no_kernel_obj_list, /* get_kernel_obj_list */
149 fd_close_handle, /* close_handle */
150 mail_writer_destroy /* destroy */
153 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
155 static const struct fd_ops mail_writer_fd_ops =
157 default_fd_get_poll_events, /* get_poll_events */
158 default_poll_event, /* poll_event */
159 mail_writer_get_fd_type, /* get_fd_type */
160 no_fd_read, /* read */
161 no_fd_write, /* write */
162 no_fd_flush, /* flush */
163 default_fd_get_file_info, /* get_file_info */
164 no_fd_get_volume_info, /* get_volume_info */
165 default_fd_ioctl, /* ioctl */
166 default_fd_queue_async, /* queue_async */
167 default_fd_reselect_async /* reselect_async */
171 struct mailslot_device
173 struct object obj; /* object header */
174 struct fd *fd; /* pseudo-fd for ioctls */
175 struct namespace *mailslots; /* mailslot namespace */
178 static void mailslot_device_dump( struct object *obj, int verbose );
179 static struct object_type *mailslot_device_get_type( struct object *obj );
180 static struct fd *mailslot_device_get_fd( struct object *obj );
181 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
182 unsigned int attr );
183 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
184 unsigned int sharing, unsigned int options );
185 static void mailslot_device_destroy( struct object *obj );
186 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
188 static const struct object_ops mailslot_device_ops =
190 sizeof(struct mailslot_device), /* size */
191 mailslot_device_dump, /* dump */
192 mailslot_device_get_type, /* get_type */
193 no_add_queue, /* add_queue */
194 NULL, /* remove_queue */
195 NULL, /* signaled */
196 no_satisfied, /* satisfied */
197 no_signal, /* signal */
198 mailslot_device_get_fd, /* get_fd */
199 no_map_access, /* map_access */
200 default_get_sd, /* get_sd */
201 default_set_sd, /* set_sd */
202 mailslot_device_lookup_name, /* lookup_name */
203 directory_link_name, /* link_name */
204 default_unlink_name, /* unlink_name */
205 mailslot_device_open_file, /* open_file */
206 no_kernel_obj_list, /* get_kernel_obj_list */
207 fd_close_handle, /* close_handle */
208 mailslot_device_destroy /* destroy */
211 static const struct fd_ops mailslot_device_fd_ops =
213 default_fd_get_poll_events, /* get_poll_events */
214 default_poll_event, /* poll_event */
215 mailslot_device_get_fd_type, /* get_fd_type */
216 no_fd_read, /* read */
217 no_fd_write, /* write */
218 no_fd_flush, /* flush */
219 default_fd_get_file_info, /* get_file_info */
220 no_fd_get_volume_info, /* get_volume_info */
221 default_fd_ioctl, /* ioctl */
222 default_fd_queue_async, /* queue_async */
223 default_fd_reselect_async /* reselect_async */
226 static void mailslot_destroy( struct object *obj)
228 struct mailslot *mailslot = (struct mailslot *) obj;
230 assert( mailslot->fd );
232 if (mailslot->write_fd != -1)
234 shutdown( mailslot->write_fd, SHUT_RDWR );
235 close( mailslot->write_fd );
237 release_object( mailslot->fd );
240 static void mailslot_dump( struct object *obj, int verbose )
242 struct mailslot *mailslot = (struct mailslot *) obj;
244 assert( obj->ops == &mailslot_ops );
245 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
246 mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
249 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
251 return FD_TYPE_MAILSLOT;
254 static struct fd *mailslot_get_fd( struct object *obj )
256 struct mailslot *mailslot = (struct mailslot *) obj;
258 return (struct fd *)grab_object( mailslot->fd );
261 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
263 /* mailslots can only be read */
264 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
265 if (access & GENERIC_ALL) access |= FILE_GENERIC_READ;
266 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
269 static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent )
271 struct mailslot_device *dev = (struct mailslot_device *)parent;
273 if (parent->ops != &mailslot_device_ops)
275 set_error( STATUS_OBJECT_NAME_INVALID );
276 return 0;
278 namespace_add( dev->mailslots, name );
279 name->parent = grab_object( parent );
280 return 1;
283 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
284 unsigned int sharing, unsigned int options )
286 struct mailslot *mailslot = (struct mailslot *)obj;
287 struct mail_writer *writer;
288 int unix_fd;
290 if (!(sharing & FILE_SHARE_READ))
292 set_error( STATUS_SHARING_VIOLATION );
293 return NULL;
296 if (!list_empty( &mailslot->writers ))
298 /* Readers and writers cannot be mixed.
299 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
301 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
303 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
304 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
306 set_error( STATUS_SHARING_VIOLATION );
307 return NULL;
311 if ((unix_fd = dup( mailslot->write_fd )) == -1)
313 file_set_error();
314 return NULL;
316 if (!(writer = alloc_object( &mail_writer_ops )))
318 close( unix_fd );
319 return NULL;
321 grab_object( mailslot );
322 writer->mailslot = mailslot;
323 writer->access = mail_writer_map_access( &writer->obj, access );
324 writer->sharing = sharing;
325 list_add_head( &mailslot->writers, &writer->entry );
327 if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
329 release_object( writer );
330 return NULL;
332 allow_fd_caching( writer->fd );
333 return &writer->obj;
336 static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count )
338 struct mailslot *mailslot = get_fd_user( fd );
340 assert(mailslot->obj.ops == &mailslot_ops);
342 fd_queue_async( fd, async, type );
343 async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
344 STATUS_IO_TIMEOUT );
345 set_error( STATUS_PENDING );
348 static void mailslot_device_dump( struct object *obj, int verbose )
350 fputs( "Mailslot device\n", stderr );
353 static struct object_type *mailslot_device_get_type( struct object *obj )
355 static const WCHAR name[] = {'D','e','v','i','c','e'};
356 static const struct unicode_str str = { name, sizeof(name) };
357 return get_object_type( &str );
360 static struct fd *mailslot_device_get_fd( struct object *obj )
362 struct mailslot_device *device = (struct mailslot_device *)obj;
363 return (struct fd *)grab_object( device->fd );
366 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
367 unsigned int attr )
369 struct mailslot_device *device = (struct mailslot_device*)obj;
370 struct object *found;
372 assert( obj->ops == &mailslot_device_ops );
374 if (!name) return NULL; /* open the device itself */
376 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
377 name->len = 0;
379 return found;
382 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
383 unsigned int sharing, unsigned int options )
385 return grab_object( obj );
388 static void mailslot_device_destroy( struct object *obj )
390 struct mailslot_device *device = (struct mailslot_device*)obj;
391 assert( obj->ops == &mailslot_device_ops );
392 if (device->fd) release_object( device->fd );
393 free( device->mailslots );
396 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
398 return FD_TYPE_DEVICE;
401 struct object *create_mailslot_device( struct object *root, const struct unicode_str *name )
403 struct mailslot_device *dev;
405 if ((dev = create_named_object( root, &mailslot_device_ops, name, 0, NULL )) &&
406 get_error() != STATUS_OBJECT_NAME_EXISTS)
408 dev->mailslots = NULL;
409 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj, 0 )) ||
410 !(dev->mailslots = create_namespace( 7 )))
412 release_object( dev );
413 dev = NULL;
416 return &dev->obj;
419 static struct mailslot *create_mailslot( struct object *root,
420 const struct unicode_str *name, unsigned int attr,
421 int max_msgsize, timeout_t read_timeout,
422 const struct security_descriptor *sd )
424 struct mailslot *mailslot;
425 int fds[2];
427 if (!(mailslot = create_named_object( root, &mailslot_ops, name, attr, sd ))) return NULL;
429 mailslot->fd = NULL;
430 mailslot->write_fd = -1;
431 mailslot->max_msgsize = max_msgsize;
432 mailslot->read_timeout = read_timeout;
433 list_init( &mailslot->writers );
435 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
437 fcntl( fds[0], F_SETFL, O_NONBLOCK );
438 fcntl( fds[1], F_SETFL, O_NONBLOCK );
439 shutdown( fds[0], SHUT_RD );
440 mailslot->write_fd = fds[0];
441 if ((mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
442 FILE_SYNCHRONOUS_IO_NONALERT )))
444 allow_fd_caching( mailslot->fd );
445 return mailslot;
448 else file_set_error();
450 release_object( mailslot );
451 return NULL;
454 static void mail_writer_dump( struct object *obj, int verbose )
456 fprintf( stderr, "Mailslot writer\n" );
459 static void mail_writer_destroy( struct object *obj)
461 struct mail_writer *writer = (struct mail_writer *) obj;
463 if (writer->fd) release_object( writer->fd );
464 list_remove( &writer->entry );
465 release_object( writer->mailslot );
468 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
470 return FD_TYPE_MAILSLOT;
473 static struct fd *mail_writer_get_fd( struct object *obj )
475 struct mail_writer *writer = (struct mail_writer *) obj;
476 return (struct fd *)grab_object( writer->fd );
479 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
481 /* mailslot writers can only get write access */
482 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
483 if (access & GENERIC_ALL) access |= FILE_GENERIC_WRITE;
484 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
487 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
488 unsigned int access )
490 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
494 /* create a mailslot */
495 DECL_HANDLER(create_mailslot)
497 struct mailslot *mailslot;
498 struct unicode_str name;
499 struct object *root;
500 const struct security_descriptor *sd;
501 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
503 if (!objattr) return;
505 if (!name.len) /* mailslots need a root directory even without a name */
507 if (!objattr->rootdir)
509 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
510 return;
512 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
515 if ((mailslot = create_mailslot( root, &name, objattr->attributes, req->max_msgsize,
516 req->read_timeout, sd )))
518 reply->handle = alloc_handle( current->process, mailslot, req->access, objattr->attributes );
519 release_object( mailslot );
522 if (root) release_object( root );
526 /* set mailslot information */
527 DECL_HANDLER(set_mailslot_info)
529 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
531 if (mailslot)
533 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
534 mailslot->read_timeout = req->read_timeout;
535 reply->max_msgsize = mailslot->max_msgsize;
536 reply->read_timeout = mailslot->read_timeout;
537 release_object( mailslot );