msvcr120: Add [_]strtoimax[_l] and [_]strtoumax[_l].
[wine.git] / server / mailslot.c
blob58d650cbb256ac21d3c723d7e5a765600655d0c5
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 namespace *mailslots; /* mailslot namespace */
177 struct mailslot_device_file
179 struct object obj; /* object header */
180 struct fd *fd; /* pseudo-fd for ioctls */
181 struct mailslot_device *device; /* mailslot device */
184 static void mailslot_device_dump( struct object *obj, int verbose );
185 static struct object_type *mailslot_device_get_type( struct object *obj );
186 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
187 unsigned int attr );
188 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
189 unsigned int sharing, unsigned int options );
190 static void mailslot_device_destroy( struct object *obj );
192 static const struct object_ops mailslot_device_ops =
194 sizeof(struct mailslot_device), /* size */
195 mailslot_device_dump, /* dump */
196 mailslot_device_get_type, /* get_type */
197 no_add_queue, /* add_queue */
198 NULL, /* remove_queue */
199 NULL, /* signaled */
200 no_satisfied, /* satisfied */
201 no_signal, /* signal */
202 no_get_fd, /* get_fd */
203 no_map_access, /* map_access */
204 default_get_sd, /* get_sd */
205 default_set_sd, /* set_sd */
206 mailslot_device_lookup_name, /* lookup_name */
207 directory_link_name, /* link_name */
208 default_unlink_name, /* unlink_name */
209 mailslot_device_open_file, /* open_file */
210 no_kernel_obj_list, /* get_kernel_obj_list */
211 no_close_handle, /* close_handle */
212 mailslot_device_destroy /* destroy */
215 static void mailslot_device_file_dump( struct object *obj, int verbose );
216 static struct fd *mailslot_device_file_get_fd( struct object *obj );
217 static void mailslot_device_file_destroy( struct object *obj );
218 static enum server_fd_type mailslot_device_file_get_fd_type( struct fd *fd );
220 static const struct object_ops mailslot_device_file_ops =
222 sizeof(struct mailslot_device_file), /* size */
223 mailslot_device_file_dump, /* dump */
224 file_get_type, /* get_type */
225 add_queue, /* add_queue */
226 remove_queue, /* remove_queue */
227 default_fd_signaled, /* signaled */
228 no_satisfied, /* satisfied */
229 no_signal, /* signal */
230 mailslot_device_file_get_fd, /* get_fd */
231 default_fd_map_access, /* map_access */
232 default_get_sd, /* get_sd */
233 default_set_sd, /* set_sd */
234 no_lookup_name, /* lookup_name */
235 no_link_name, /* link_name */
236 NULL, /* unlink_name */
237 no_open_file, /* open_file */
238 no_kernel_obj_list, /* get_kernel_obj_list */
239 fd_close_handle, /* close_handle */
240 mailslot_device_file_destroy /* destroy */
243 static const struct fd_ops mailslot_device_fd_ops =
245 default_fd_get_poll_events, /* get_poll_events */
246 default_poll_event, /* poll_event */
247 mailslot_device_file_get_fd_type, /* get_fd_type */
248 no_fd_read, /* read */
249 no_fd_write, /* write */
250 no_fd_flush, /* flush */
251 default_fd_get_file_info, /* get_file_info */
252 no_fd_get_volume_info, /* get_volume_info */
253 default_fd_ioctl, /* ioctl */
254 default_fd_queue_async, /* queue_async */
255 default_fd_reselect_async /* reselect_async */
258 static void mailslot_destroy( struct object *obj)
260 struct mailslot *mailslot = (struct mailslot *) obj;
262 assert( mailslot->fd );
264 if (mailslot->write_fd != -1)
266 shutdown( mailslot->write_fd, SHUT_RDWR );
267 close( mailslot->write_fd );
269 release_object( mailslot->fd );
272 static void mailslot_dump( struct object *obj, int verbose )
274 struct mailslot *mailslot = (struct mailslot *) obj;
276 assert( obj->ops == &mailslot_ops );
277 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
278 mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
281 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
283 return FD_TYPE_MAILSLOT;
286 static struct fd *mailslot_get_fd( struct object *obj )
288 struct mailslot *mailslot = (struct mailslot *) obj;
290 return (struct fd *)grab_object( mailslot->fd );
293 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
295 /* mailslots can only be read */
296 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
297 if (access & GENERIC_ALL) access |= FILE_GENERIC_READ;
298 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
301 static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent )
303 struct mailslot_device *dev = (struct mailslot_device *)parent;
305 if (parent->ops != &mailslot_device_ops)
307 set_error( STATUS_OBJECT_NAME_INVALID );
308 return 0;
310 namespace_add( dev->mailslots, name );
311 name->parent = grab_object( parent );
312 return 1;
315 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
316 unsigned int sharing, unsigned int options )
318 struct mailslot *mailslot = (struct mailslot *)obj;
319 struct mail_writer *writer;
320 int unix_fd;
322 if (!(sharing & FILE_SHARE_READ))
324 set_error( STATUS_SHARING_VIOLATION );
325 return NULL;
328 if (!list_empty( &mailslot->writers ))
330 /* Readers and writers cannot be mixed.
331 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
333 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
335 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
336 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
338 set_error( STATUS_SHARING_VIOLATION );
339 return NULL;
343 if ((unix_fd = dup( mailslot->write_fd )) == -1)
345 file_set_error();
346 return NULL;
348 if (!(writer = alloc_object( &mail_writer_ops )))
350 close( unix_fd );
351 return NULL;
353 grab_object( mailslot );
354 writer->mailslot = mailslot;
355 writer->access = mail_writer_map_access( &writer->obj, access );
356 writer->sharing = sharing;
357 list_add_head( &mailslot->writers, &writer->entry );
359 if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
361 release_object( writer );
362 return NULL;
364 allow_fd_caching( writer->fd );
365 return &writer->obj;
368 static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count )
370 struct mailslot *mailslot = get_fd_user( fd );
372 assert(mailslot->obj.ops == &mailslot_ops);
374 fd_queue_async( fd, async, type );
375 async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
376 STATUS_IO_TIMEOUT );
377 set_error( STATUS_PENDING );
380 static void mailslot_device_dump( struct object *obj, int verbose )
382 fputs( "Mailslot device\n", stderr );
385 static struct object_type *mailslot_device_get_type( struct object *obj )
387 static const WCHAR name[] = {'D','e','v','i','c','e'};
388 static const struct unicode_str str = { name, sizeof(name) };
389 return get_object_type( &str );
392 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
393 unsigned int attr )
395 struct mailslot_device *device = (struct mailslot_device*)obj;
396 struct object *found;
398 assert( obj->ops == &mailslot_device_ops );
400 if (!name) return NULL; /* open the device itself */
402 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
403 name->len = 0;
405 return found;
408 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
409 unsigned int sharing, unsigned int options )
411 struct mailslot_device_file *file;
413 if (!(file = alloc_object( &mailslot_device_file_ops ))) return NULL;
414 file->device = (struct mailslot_device *)grab_object( obj );
415 if (!(file->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, obj, options )))
417 release_object( file );
418 return NULL;
420 allow_fd_caching( file->fd );
421 return &file->obj;
424 static void mailslot_device_destroy( struct object *obj )
426 struct mailslot_device *device = (struct mailslot_device*)obj;
427 assert( obj->ops == &mailslot_device_ops );
428 free( device->mailslots );
431 struct object *create_mailslot_device( struct object *root, const struct unicode_str *name )
433 struct mailslot_device *dev;
435 if ((dev = create_named_object( root, &mailslot_device_ops, name, 0, NULL )) &&
436 get_error() != STATUS_OBJECT_NAME_EXISTS)
438 dev->mailslots = NULL;
439 if (!(dev->mailslots = create_namespace( 7 )))
441 release_object( dev );
442 dev = NULL;
445 return &dev->obj;
448 static void mailslot_device_file_dump( struct object *obj, int verbose )
450 struct mailslot_device_file *file = (struct mailslot_device_file *)obj;
452 fprintf( stderr, "File on mailslot device %p\n", file->device );
455 static struct fd *mailslot_device_file_get_fd( struct object *obj )
457 struct mailslot_device_file *file = (struct mailslot_device_file *)obj;
458 return (struct fd *)grab_object( file->fd );
461 static void mailslot_device_file_destroy( struct object *obj )
463 struct mailslot_device_file *file = (struct mailslot_device_file*)obj;
464 assert( obj->ops == &mailslot_device_file_ops );
465 if (file->fd) release_object( file->fd );
466 release_object( file->device );
469 static enum server_fd_type mailslot_device_file_get_fd_type( struct fd *fd )
471 return FD_TYPE_DEVICE;
474 static struct mailslot *create_mailslot( struct object *root,
475 const struct unicode_str *name, unsigned int attr,
476 int max_msgsize, timeout_t read_timeout,
477 const struct security_descriptor *sd )
479 struct mailslot *mailslot;
480 int fds[2];
482 if (!(mailslot = create_named_object( root, &mailslot_ops, name, attr, sd ))) return NULL;
484 mailslot->fd = NULL;
485 mailslot->write_fd = -1;
486 mailslot->max_msgsize = max_msgsize;
487 mailslot->read_timeout = read_timeout;
488 list_init( &mailslot->writers );
490 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
492 fcntl( fds[0], F_SETFL, O_NONBLOCK );
493 fcntl( fds[1], F_SETFL, O_NONBLOCK );
494 shutdown( fds[0], SHUT_RD );
495 mailslot->write_fd = fds[0];
496 if ((mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
497 FILE_SYNCHRONOUS_IO_NONALERT )))
499 allow_fd_caching( mailslot->fd );
500 return mailslot;
503 else file_set_error();
505 release_object( mailslot );
506 return NULL;
509 static void mail_writer_dump( struct object *obj, int verbose )
511 fprintf( stderr, "Mailslot writer\n" );
514 static void mail_writer_destroy( struct object *obj)
516 struct mail_writer *writer = (struct mail_writer *) obj;
518 if (writer->fd) release_object( writer->fd );
519 list_remove( &writer->entry );
520 release_object( writer->mailslot );
523 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
525 return FD_TYPE_MAILSLOT;
528 static struct fd *mail_writer_get_fd( struct object *obj )
530 struct mail_writer *writer = (struct mail_writer *) obj;
531 return (struct fd *)grab_object( writer->fd );
534 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
536 /* mailslot writers can only get write access */
537 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
538 if (access & GENERIC_ALL) access |= FILE_GENERIC_WRITE;
539 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
542 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
543 unsigned int access )
545 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
549 /* create a mailslot */
550 DECL_HANDLER(create_mailslot)
552 struct mailslot *mailslot;
553 struct unicode_str name;
554 struct object *root;
555 const struct security_descriptor *sd;
556 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
558 if (!objattr) return;
560 if (!name.len) /* mailslots need a root directory even without a name */
562 if (!objattr->rootdir)
564 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
565 return;
567 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
570 if ((mailslot = create_mailslot( root, &name, objattr->attributes, req->max_msgsize,
571 req->read_timeout, sd )))
573 reply->handle = alloc_handle( current->process, mailslot, req->access, objattr->attributes );
574 release_object( mailslot );
577 if (root) release_object( root );
581 /* set mailslot information */
582 DECL_HANDLER(set_mailslot_info)
584 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
586 if (mailslot)
588 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
589 mailslot->read_timeout = req->read_timeout;
590 reply->max_msgsize = mailslot->max_msgsize;
591 reply->read_timeout = mailslot->read_timeout;
592 release_object( mailslot );