usp10/tests: Move initialization to a separate function, add another ScriptShape...
[wine.git] / server / mailslot.c
blobd7affa514bf30be8112a61a6252809a50f7cd028
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 default_fd_ioctl, /* ioctl */
108 mailslot_queue_async, /* queue_async */
109 default_fd_reselect_async /* reselect_async */
113 struct mail_writer
115 struct object obj;
116 struct fd *fd;
117 struct mailslot *mailslot;
118 struct list entry;
119 unsigned int access;
120 unsigned int sharing;
123 static void mail_writer_dump( struct object *obj, int verbose );
124 static struct fd *mail_writer_get_fd( struct object *obj );
125 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
126 static void mail_writer_destroy( struct object *obj);
128 static const struct object_ops mail_writer_ops =
130 sizeof(struct mail_writer), /* size */
131 mail_writer_dump, /* dump */
132 no_get_type, /* get_type */
133 no_add_queue, /* add_queue */
134 NULL, /* remove_queue */
135 NULL, /* signaled */
136 NULL, /* satisfied */
137 no_signal, /* signal */
138 mail_writer_get_fd, /* get_fd */
139 mail_writer_map_access, /* map_access */
140 default_get_sd, /* get_sd */
141 default_set_sd, /* set_sd */
142 no_lookup_name, /* lookup_name */
143 no_link_name, /* link_name */
144 NULL, /* unlink_name */
145 no_open_file, /* open_file */
146 fd_close_handle, /* close_handle */
147 mail_writer_destroy /* destroy */
150 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
152 static const struct fd_ops mail_writer_fd_ops =
154 default_fd_get_poll_events, /* get_poll_events */
155 default_poll_event, /* poll_event */
156 mail_writer_get_fd_type, /* get_fd_type */
157 no_fd_read, /* read */
158 no_fd_write, /* write */
159 no_fd_flush, /* flush */
160 default_fd_ioctl, /* ioctl */
161 default_fd_queue_async, /* queue_async */
162 default_fd_reselect_async /* reselect_async */
166 struct mailslot_device
168 struct object obj; /* object header */
169 struct fd *fd; /* pseudo-fd for ioctls */
170 struct namespace *mailslots; /* mailslot namespace */
173 static void mailslot_device_dump( struct object *obj, int verbose );
174 static struct object_type *mailslot_device_get_type( struct object *obj );
175 static struct fd *mailslot_device_get_fd( struct object *obj );
176 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
177 unsigned int attr );
178 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
179 unsigned int sharing, unsigned int options );
180 static void mailslot_device_destroy( struct object *obj );
181 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
183 static const struct object_ops mailslot_device_ops =
185 sizeof(struct mailslot_device), /* size */
186 mailslot_device_dump, /* dump */
187 mailslot_device_get_type, /* get_type */
188 no_add_queue, /* add_queue */
189 NULL, /* remove_queue */
190 NULL, /* signaled */
191 no_satisfied, /* satisfied */
192 no_signal, /* signal */
193 mailslot_device_get_fd, /* get_fd */
194 no_map_access, /* map_access */
195 default_get_sd, /* get_sd */
196 default_set_sd, /* set_sd */
197 mailslot_device_lookup_name, /* lookup_name */
198 directory_link_name, /* link_name */
199 default_unlink_name, /* unlink_name */
200 mailslot_device_open_file, /* open_file */
201 fd_close_handle, /* close_handle */
202 mailslot_device_destroy /* destroy */
205 static const struct fd_ops mailslot_device_fd_ops =
207 default_fd_get_poll_events, /* get_poll_events */
208 default_poll_event, /* poll_event */
209 mailslot_device_get_fd_type, /* get_fd_type */
210 no_fd_read, /* read */
211 no_fd_write, /* write */
212 no_fd_flush, /* flush */
213 default_fd_ioctl, /* ioctl */
214 default_fd_queue_async, /* queue_async */
215 default_fd_reselect_async /* reselect_async */
218 static void mailslot_destroy( struct object *obj)
220 struct mailslot *mailslot = (struct mailslot *) obj;
222 assert( mailslot->fd );
224 if (mailslot->write_fd != -1)
226 shutdown( mailslot->write_fd, SHUT_RDWR );
227 close( mailslot->write_fd );
229 release_object( mailslot->fd );
232 static void mailslot_dump( struct object *obj, int verbose )
234 struct mailslot *mailslot = (struct mailslot *) obj;
236 assert( obj->ops == &mailslot_ops );
237 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
238 mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
241 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
243 return FD_TYPE_MAILSLOT;
246 static struct fd *mailslot_get_fd( struct object *obj )
248 struct mailslot *mailslot = (struct mailslot *) obj;
250 return (struct fd *)grab_object( mailslot->fd );
253 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
255 /* mailslots can only be read */
256 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
257 if (access & GENERIC_ALL) access |= FILE_GENERIC_READ;
258 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
261 static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent )
263 struct mailslot_device *dev = (struct mailslot_device *)parent;
265 if (parent->ops != &mailslot_device_ops)
267 set_error( STATUS_OBJECT_NAME_INVALID );
268 return 0;
270 namespace_add( dev->mailslots, name );
271 name->parent = grab_object( parent );
272 return 1;
275 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
276 unsigned int sharing, unsigned int options )
278 struct mailslot *mailslot = (struct mailslot *)obj;
279 struct mail_writer *writer;
280 int unix_fd;
282 if (!(sharing & FILE_SHARE_READ))
284 set_error( STATUS_SHARING_VIOLATION );
285 return NULL;
288 if (!list_empty( &mailslot->writers ))
290 /* Readers and writers cannot be mixed.
291 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
293 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
295 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
296 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
298 set_error( STATUS_SHARING_VIOLATION );
299 return NULL;
303 if ((unix_fd = dup( mailslot->write_fd )) == -1)
305 file_set_error();
306 return NULL;
308 if (!(writer = alloc_object( &mail_writer_ops )))
310 close( unix_fd );
311 return NULL;
313 grab_object( mailslot );
314 writer->mailslot = mailslot;
315 writer->access = mail_writer_map_access( &writer->obj, access );
316 writer->sharing = sharing;
317 list_add_head( &mailslot->writers, &writer->entry );
319 if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
321 release_object( writer );
322 return NULL;
324 allow_fd_caching( writer->fd );
325 return &writer->obj;
328 static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count )
330 struct mailslot *mailslot = get_fd_user( fd );
332 assert(mailslot->obj.ops == &mailslot_ops);
334 fd_queue_async( fd, async, type );
335 async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
336 STATUS_IO_TIMEOUT );
337 set_error( STATUS_PENDING );
340 static void mailslot_device_dump( struct object *obj, int verbose )
342 fputs( "Mailslot device\n", stderr );
345 static struct object_type *mailslot_device_get_type( struct object *obj )
347 static const WCHAR name[] = {'D','e','v','i','c','e'};
348 static const struct unicode_str str = { name, sizeof(name) };
349 return get_object_type( &str );
352 static struct fd *mailslot_device_get_fd( struct object *obj )
354 struct mailslot_device *device = (struct mailslot_device *)obj;
355 return (struct fd *)grab_object( device->fd );
358 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
359 unsigned int attr )
361 struct mailslot_device *device = (struct mailslot_device*)obj;
362 struct object *found;
364 assert( obj->ops == &mailslot_device_ops );
366 if (!name) return NULL; /* open the device itself */
368 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
369 name->len = 0;
371 return found;
374 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
375 unsigned int sharing, unsigned int options )
377 return grab_object( obj );
380 static void mailslot_device_destroy( struct object *obj )
382 struct mailslot_device *device = (struct mailslot_device*)obj;
383 assert( obj->ops == &mailslot_device_ops );
384 if (device->fd) release_object( device->fd );
385 free( device->mailslots );
388 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
390 return FD_TYPE_DEVICE;
393 struct object *create_mailslot_device( struct object *root, const struct unicode_str *name )
395 struct mailslot_device *dev;
397 if ((dev = create_named_object( root, &mailslot_device_ops, name, 0, NULL )) &&
398 get_error() != STATUS_OBJECT_NAME_EXISTS)
400 dev->mailslots = NULL;
401 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj, 0 )) ||
402 !(dev->mailslots = create_namespace( 7 )))
404 release_object( dev );
405 dev = NULL;
408 return &dev->obj;
411 static struct mailslot *create_mailslot( struct object *root,
412 const struct unicode_str *name, unsigned int attr,
413 int max_msgsize, timeout_t read_timeout,
414 const struct security_descriptor *sd )
416 struct mailslot *mailslot;
417 int fds[2];
419 if (!(mailslot = create_named_object( root, &mailslot_ops, name, attr, sd ))) return NULL;
421 mailslot->fd = NULL;
422 mailslot->write_fd = -1;
423 mailslot->max_msgsize = max_msgsize;
424 mailslot->read_timeout = read_timeout;
425 list_init( &mailslot->writers );
427 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
429 fcntl( fds[0], F_SETFL, O_NONBLOCK );
430 fcntl( fds[1], F_SETFL, O_NONBLOCK );
431 shutdown( fds[0], SHUT_RD );
432 mailslot->write_fd = fds[0];
433 if ((mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
434 FILE_SYNCHRONOUS_IO_NONALERT )))
436 allow_fd_caching( mailslot->fd );
437 return mailslot;
440 else file_set_error();
442 release_object( mailslot );
443 return NULL;
446 static void mail_writer_dump( struct object *obj, int verbose )
448 fprintf( stderr, "Mailslot writer\n" );
451 static void mail_writer_destroy( struct object *obj)
453 struct mail_writer *writer = (struct mail_writer *) obj;
455 if (writer->fd) release_object( writer->fd );
456 list_remove( &writer->entry );
457 release_object( writer->mailslot );
460 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
462 return FD_TYPE_MAILSLOT;
465 static struct fd *mail_writer_get_fd( struct object *obj )
467 struct mail_writer *writer = (struct mail_writer *) obj;
468 return (struct fd *)grab_object( writer->fd );
471 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
473 /* mailslot writers can only get write access */
474 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
475 if (access & GENERIC_ALL) access |= FILE_GENERIC_WRITE;
476 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
479 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
480 unsigned int access )
482 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
486 /* create a mailslot */
487 DECL_HANDLER(create_mailslot)
489 struct mailslot *mailslot;
490 struct unicode_str name;
491 struct object *root;
492 const struct security_descriptor *sd;
493 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
495 if (!objattr) return;
497 if (!name.len) /* mailslots need a root directory even without a name */
499 if (!objattr->rootdir)
501 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
502 return;
504 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
507 if ((mailslot = create_mailslot( root, &name, objattr->attributes, req->max_msgsize,
508 req->read_timeout, sd )))
510 reply->handle = alloc_handle( current->process, mailslot, req->access, objattr->attributes );
511 release_object( mailslot );
514 if (root) release_object( root );
518 /* set mailslot information */
519 DECL_HANDLER(set_mailslot_info)
521 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
523 if (mailslot)
525 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
526 mailslot->read_timeout = req->read_timeout;
527 reply->max_msgsize = mailslot->max_msgsize;
528 reply->read_timeout = mailslot->read_timeout;
529 release_object( mailslot );