ntdll: Fix the Mac build with SDKs older than 10.14.
[wine.git] / server / mailslot.c
blob49e79e9acd13fb0e8e0451c637b01fdb6be33a85
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 file_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 no_kernel_obj_list, /* get_kernel_obj_list */
93 fd_close_handle, /* close_handle */
94 mailslot_destroy /* destroy */
97 static enum server_fd_type mailslot_get_fd_type( struct fd *fd );
98 static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count );
100 static const struct fd_ops mailslot_fd_ops =
102 default_fd_get_poll_events, /* get_poll_events */
103 default_poll_event, /* poll_event */
104 mailslot_get_fd_type, /* get_fd_type */
105 no_fd_read, /* read */
106 no_fd_write, /* write */
107 no_fd_flush, /* flush */
108 default_fd_get_file_info, /* get_file_info */
109 no_fd_get_volume_info, /* get_volume_info */
110 default_fd_ioctl, /* ioctl */
111 mailslot_queue_async, /* queue_async */
112 default_fd_reselect_async /* reselect_async */
116 struct mail_writer
118 struct object obj;
119 struct fd *fd;
120 struct mailslot *mailslot;
121 struct list entry;
122 unsigned int access;
123 unsigned int sharing;
126 static void mail_writer_dump( struct object *obj, int verbose );
127 static struct fd *mail_writer_get_fd( struct object *obj );
128 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
129 static void mail_writer_destroy( struct object *obj);
131 static const struct object_ops mail_writer_ops =
133 sizeof(struct mail_writer), /* size */
134 mail_writer_dump, /* dump */
135 file_get_type, /* get_type */
136 no_add_queue, /* add_queue */
137 NULL, /* remove_queue */
138 NULL, /* signaled */
139 NULL, /* satisfied */
140 no_signal, /* signal */
141 mail_writer_get_fd, /* get_fd */
142 mail_writer_map_access, /* map_access */
143 default_get_sd, /* get_sd */
144 default_set_sd, /* set_sd */
145 no_lookup_name, /* lookup_name */
146 no_link_name, /* link_name */
147 NULL, /* unlink_name */
148 no_open_file, /* open_file */
149 no_kernel_obj_list, /* get_kernel_obj_list */
150 fd_close_handle, /* close_handle */
151 mail_writer_destroy /* destroy */
154 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
156 static const struct fd_ops mail_writer_fd_ops =
158 default_fd_get_poll_events, /* get_poll_events */
159 default_poll_event, /* poll_event */
160 mail_writer_get_fd_type, /* get_fd_type */
161 no_fd_read, /* read */
162 no_fd_write, /* write */
163 no_fd_flush, /* flush */
164 default_fd_get_file_info, /* get_file_info */
165 no_fd_get_volume_info, /* get_volume_info */
166 default_fd_ioctl, /* ioctl */
167 default_fd_queue_async, /* queue_async */
168 default_fd_reselect_async /* reselect_async */
172 struct mailslot_device
174 struct object obj; /* object header */
175 struct fd *fd; /* pseudo-fd for ioctls */
176 struct namespace *mailslots; /* mailslot namespace */
179 static void mailslot_device_dump( struct object *obj, int verbose );
180 static struct object_type *mailslot_device_get_type( struct object *obj );
181 static struct fd *mailslot_device_get_fd( struct object *obj );
182 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
183 unsigned int attr );
184 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
185 unsigned int sharing, unsigned int options );
186 static void mailslot_device_destroy( struct object *obj );
187 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
189 static const struct object_ops mailslot_device_ops =
191 sizeof(struct mailslot_device), /* size */
192 mailslot_device_dump, /* dump */
193 mailslot_device_get_type, /* get_type */
194 no_add_queue, /* add_queue */
195 NULL, /* remove_queue */
196 NULL, /* signaled */
197 no_satisfied, /* satisfied */
198 no_signal, /* signal */
199 mailslot_device_get_fd, /* get_fd */
200 no_map_access, /* map_access */
201 default_get_sd, /* get_sd */
202 default_set_sd, /* set_sd */
203 mailslot_device_lookup_name, /* lookup_name */
204 directory_link_name, /* link_name */
205 default_unlink_name, /* unlink_name */
206 mailslot_device_open_file, /* open_file */
207 no_kernel_obj_list, /* get_kernel_obj_list */
208 fd_close_handle, /* close_handle */
209 mailslot_device_destroy /* destroy */
212 static const struct fd_ops mailslot_device_fd_ops =
214 default_fd_get_poll_events, /* get_poll_events */
215 default_poll_event, /* poll_event */
216 mailslot_device_get_fd_type, /* get_fd_type */
217 no_fd_read, /* read */
218 no_fd_write, /* write */
219 no_fd_flush, /* flush */
220 default_fd_get_file_info, /* get_file_info */
221 no_fd_get_volume_info, /* get_volume_info */
222 default_fd_ioctl, /* ioctl */
223 default_fd_queue_async, /* queue_async */
224 default_fd_reselect_async /* reselect_async */
227 static void mailslot_destroy( struct object *obj)
229 struct mailslot *mailslot = (struct mailslot *) obj;
231 assert( mailslot->fd );
233 if (mailslot->write_fd != -1)
235 shutdown( mailslot->write_fd, SHUT_RDWR );
236 close( mailslot->write_fd );
238 release_object( mailslot->fd );
241 static void mailslot_dump( struct object *obj, int verbose )
243 struct mailslot *mailslot = (struct mailslot *) obj;
245 assert( obj->ops == &mailslot_ops );
246 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
247 mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
250 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
252 return FD_TYPE_MAILSLOT;
255 static struct fd *mailslot_get_fd( struct object *obj )
257 struct mailslot *mailslot = (struct mailslot *) obj;
259 return (struct fd *)grab_object( mailslot->fd );
262 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
264 /* mailslots can only be read */
265 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
266 if (access & GENERIC_ALL) access |= FILE_GENERIC_READ;
267 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
270 static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent )
272 struct mailslot_device *dev = (struct mailslot_device *)parent;
274 if (parent->ops != &mailslot_device_ops)
276 set_error( STATUS_OBJECT_NAME_INVALID );
277 return 0;
279 namespace_add( dev->mailslots, name );
280 name->parent = grab_object( parent );
281 return 1;
284 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
285 unsigned int sharing, unsigned int options )
287 struct mailslot *mailslot = (struct mailslot *)obj;
288 struct mail_writer *writer;
289 int unix_fd;
291 if (!(sharing & FILE_SHARE_READ))
293 set_error( STATUS_SHARING_VIOLATION );
294 return NULL;
297 if (!list_empty( &mailslot->writers ))
299 /* Readers and writers cannot be mixed.
300 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
302 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
304 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
305 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
307 set_error( STATUS_SHARING_VIOLATION );
308 return NULL;
312 if ((unix_fd = dup( mailslot->write_fd )) == -1)
314 file_set_error();
315 return NULL;
317 if (!(writer = alloc_object( &mail_writer_ops )))
319 close( unix_fd );
320 return NULL;
322 grab_object( mailslot );
323 writer->mailslot = mailslot;
324 writer->access = mail_writer_map_access( &writer->obj, access );
325 writer->sharing = sharing;
326 list_add_head( &mailslot->writers, &writer->entry );
328 if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
330 release_object( writer );
331 return NULL;
333 allow_fd_caching( writer->fd );
334 return &writer->obj;
337 static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count )
339 struct mailslot *mailslot = get_fd_user( fd );
341 assert(mailslot->obj.ops == &mailslot_ops);
343 fd_queue_async( fd, async, type );
344 async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
345 STATUS_IO_TIMEOUT );
346 set_error( STATUS_PENDING );
349 static void mailslot_device_dump( struct object *obj, int verbose )
351 fputs( "Mailslot device\n", stderr );
354 static struct object_type *mailslot_device_get_type( struct object *obj )
356 static const WCHAR name[] = {'D','e','v','i','c','e'};
357 static const struct unicode_str str = { name, sizeof(name) };
358 return get_object_type( &str );
361 static struct fd *mailslot_device_get_fd( struct object *obj )
363 struct mailslot_device *device = (struct mailslot_device *)obj;
364 return (struct fd *)grab_object( device->fd );
367 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
368 unsigned int attr )
370 struct mailslot_device *device = (struct mailslot_device*)obj;
371 struct object *found;
373 assert( obj->ops == &mailslot_device_ops );
375 if (!name) return NULL; /* open the device itself */
377 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
378 name->len = 0;
380 return found;
383 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
384 unsigned int sharing, unsigned int options )
386 return grab_object( obj );
389 static void mailslot_device_destroy( struct object *obj )
391 struct mailslot_device *device = (struct mailslot_device*)obj;
392 assert( obj->ops == &mailslot_device_ops );
393 if (device->fd) release_object( device->fd );
394 free( device->mailslots );
397 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
399 return FD_TYPE_DEVICE;
402 struct object *create_mailslot_device( struct object *root, const struct unicode_str *name )
404 struct mailslot_device *dev;
406 if ((dev = create_named_object( root, &mailslot_device_ops, name, 0, NULL )) &&
407 get_error() != STATUS_OBJECT_NAME_EXISTS)
409 dev->mailslots = NULL;
410 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj, 0 )) ||
411 !(dev->mailslots = create_namespace( 7 )))
413 release_object( dev );
414 dev = NULL;
417 return &dev->obj;
420 static struct mailslot *create_mailslot( struct object *root,
421 const struct unicode_str *name, unsigned int attr,
422 int max_msgsize, timeout_t read_timeout,
423 const struct security_descriptor *sd )
425 struct mailslot *mailslot;
426 int fds[2];
428 if (!(mailslot = create_named_object( root, &mailslot_ops, name, attr, sd ))) return NULL;
430 mailslot->fd = NULL;
431 mailslot->write_fd = -1;
432 mailslot->max_msgsize = max_msgsize;
433 mailslot->read_timeout = read_timeout;
434 list_init( &mailslot->writers );
436 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
438 fcntl( fds[0], F_SETFL, O_NONBLOCK );
439 fcntl( fds[1], F_SETFL, O_NONBLOCK );
440 shutdown( fds[0], SHUT_RD );
441 mailslot->write_fd = fds[0];
442 if ((mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
443 FILE_SYNCHRONOUS_IO_NONALERT )))
445 allow_fd_caching( mailslot->fd );
446 return mailslot;
449 else file_set_error();
451 release_object( mailslot );
452 return NULL;
455 static void mail_writer_dump( struct object *obj, int verbose )
457 fprintf( stderr, "Mailslot writer\n" );
460 static void mail_writer_destroy( struct object *obj)
462 struct mail_writer *writer = (struct mail_writer *) obj;
464 if (writer->fd) release_object( writer->fd );
465 list_remove( &writer->entry );
466 release_object( writer->mailslot );
469 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
471 return FD_TYPE_MAILSLOT;
474 static struct fd *mail_writer_get_fd( struct object *obj )
476 struct mail_writer *writer = (struct mail_writer *) obj;
477 return (struct fd *)grab_object( writer->fd );
480 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
482 /* mailslot writers can only get write access */
483 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
484 if (access & GENERIC_ALL) access |= FILE_GENERIC_WRITE;
485 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
488 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
489 unsigned int access )
491 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
495 /* create a mailslot */
496 DECL_HANDLER(create_mailslot)
498 struct mailslot *mailslot;
499 struct unicode_str name;
500 struct object *root;
501 const struct security_descriptor *sd;
502 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
504 if (!objattr) return;
506 if (!name.len) /* mailslots need a root directory even without a name */
508 if (!objattr->rootdir)
510 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
511 return;
513 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
516 if ((mailslot = create_mailslot( root, &name, objattr->attributes, req->max_msgsize,
517 req->read_timeout, sd )))
519 reply->handle = alloc_handle( current->process, mailslot, req->access, objattr->attributes );
520 release_object( mailslot );
523 if (root) release_object( root );
527 /* set mailslot information */
528 DECL_HANDLER(set_mailslot_info)
530 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
532 if (mailslot)
534 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
535 mailslot->read_timeout = req->read_timeout;
536 reply->max_msgsize = mailslot->max_msgsize;
537 reply->read_timeout = mailslot->read_timeout;
538 release_object( mailslot );