winewayland.drv: Implement vkGetPhysicalDeviceSurfaceSupportKHR.
[wine.git] / server / mailslot.c
blob2d8697ec9bd7f5d7e94fa09e9f185283f80e0126
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"
25 #include <assert.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <sys/ioctl.h>
35 #include <sys/socket.h>
36 #ifdef HAVE_SYS_FILIO_H
37 #include <sys/filio.h>
38 #endif
40 #include "ntstatus.h"
41 #define WIN32_NO_STATUS
42 #include "windef.h"
43 #include "winternl.h"
45 #include "file.h"
46 #include "handle.h"
47 #include "thread.h"
48 #include "request.h"
50 struct mailslot
52 struct object obj;
53 struct fd *fd;
54 int write_fd;
55 unsigned int max_msgsize;
56 timeout_t read_timeout;
57 struct list writers;
60 /* mailslot functions */
61 static void mailslot_dump( struct object*, int );
62 static struct fd *mailslot_get_fd( struct object * );
63 static unsigned int mailslot_map_access( struct object *obj, unsigned int access );
64 static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent );
65 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
66 unsigned int sharing, unsigned int options );
67 static void mailslot_destroy( struct object * );
69 static const struct object_ops mailslot_ops =
71 sizeof(struct mailslot), /* size */
72 &file_type, /* type */
73 mailslot_dump, /* dump */
74 add_queue, /* add_queue */
75 remove_queue, /* remove_queue */
76 default_fd_signaled, /* signaled */
77 no_satisfied, /* satisfied */
78 no_signal, /* signal */
79 mailslot_get_fd, /* get_fd */
80 mailslot_map_access, /* map_access */
81 default_get_sd, /* get_sd */
82 default_set_sd, /* set_sd */
83 default_get_full_name, /* get_full_name */
84 no_lookup_name, /* lookup_name */
85 mailslot_link_name, /* link_name */
86 default_unlink_name, /* unlink_name */
87 mailslot_open_file, /* open_file */
88 no_kernel_obj_list, /* get_kernel_obj_list */
89 no_close_handle, /* close_handle */
90 mailslot_destroy /* destroy */
93 static enum server_fd_type mailslot_get_fd_type( struct fd *fd );
94 static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count );
96 static const struct fd_ops mailslot_fd_ops =
98 default_fd_get_poll_events, /* get_poll_events */
99 default_poll_event, /* poll_event */
100 mailslot_get_fd_type, /* get_fd_type */
101 no_fd_read, /* read */
102 no_fd_write, /* write */
103 no_fd_flush, /* flush */
104 default_fd_get_file_info, /* get_file_info */
105 no_fd_get_volume_info, /* get_volume_info */
106 default_fd_ioctl, /* ioctl */
107 default_fd_cancel_async, /* cancel_async */
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 &file_type, /* type */
132 mail_writer_dump, /* dump */
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_get_full_name, /* get_full_name */
143 no_lookup_name, /* lookup_name */
144 no_link_name, /* link_name */
145 NULL, /* unlink_name */
146 no_open_file, /* open_file */
147 no_kernel_obj_list, /* get_kernel_obj_list */
148 no_close_handle, /* close_handle */
149 mail_writer_destroy /* destroy */
152 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
154 static const struct fd_ops mail_writer_fd_ops =
156 default_fd_get_poll_events, /* get_poll_events */
157 default_poll_event, /* poll_event */
158 mail_writer_get_fd_type, /* get_fd_type */
159 no_fd_read, /* read */
160 no_fd_write, /* write */
161 no_fd_flush, /* flush */
162 default_fd_get_file_info, /* get_file_info */
163 no_fd_get_volume_info, /* get_volume_info */
164 default_fd_ioctl, /* ioctl */
165 default_fd_cancel_async, /* cancel_async */
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 *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
186 unsigned int attr, struct object *root );
187 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
188 unsigned int sharing, unsigned int options );
189 static void mailslot_device_destroy( struct object *obj );
191 static const struct object_ops mailslot_device_ops =
193 sizeof(struct mailslot_device), /* size */
194 &device_type, /* type */
195 mailslot_device_dump, /* dump */
196 no_add_queue, /* add_queue */
197 NULL, /* remove_queue */
198 NULL, /* signaled */
199 no_satisfied, /* satisfied */
200 no_signal, /* signal */
201 no_get_fd, /* get_fd */
202 default_map_access, /* map_access */
203 default_get_sd, /* get_sd */
204 default_set_sd, /* set_sd */
205 default_get_full_name, /* get_full_name */
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 WCHAR *mailslot_device_file_get_full_name( struct object *obj, data_size_t *len );
218 static void mailslot_device_file_destroy( struct object *obj );
219 static enum server_fd_type mailslot_device_file_get_fd_type( struct fd *fd );
221 static const struct object_ops mailslot_device_file_ops =
223 sizeof(struct mailslot_device_file), /* size */
224 &file_type, /* type */
225 mailslot_device_file_dump, /* dump */
226 add_queue, /* add_queue */
227 remove_queue, /* remove_queue */
228 default_fd_signaled, /* signaled */
229 no_satisfied, /* satisfied */
230 no_signal, /* signal */
231 mailslot_device_file_get_fd, /* get_fd */
232 default_map_access, /* map_access */
233 default_get_sd, /* get_sd */
234 default_set_sd, /* set_sd */
235 mailslot_device_file_get_full_name, /* get_full_name */
236 no_lookup_name, /* lookup_name */
237 no_link_name, /* link_name */
238 NULL, /* unlink_name */
239 no_open_file, /* open_file */
240 no_kernel_obj_list, /* get_kernel_obj_list */
241 no_close_handle, /* close_handle */
242 mailslot_device_file_destroy /* destroy */
245 static const struct fd_ops mailslot_device_fd_ops =
247 default_fd_get_poll_events, /* get_poll_events */
248 default_poll_event, /* poll_event */
249 mailslot_device_file_get_fd_type, /* get_fd_type */
250 no_fd_read, /* read */
251 no_fd_write, /* write */
252 no_fd_flush, /* flush */
253 default_fd_get_file_info, /* get_file_info */
254 no_fd_get_volume_info, /* get_volume_info */
255 default_fd_ioctl, /* ioctl */
256 default_fd_cancel_async, /* cancel_async */
257 default_fd_queue_async, /* queue_async */
258 default_fd_reselect_async /* reselect_async */
261 static void mailslot_destroy( struct object *obj)
263 struct mailslot *mailslot = (struct mailslot *) obj;
265 assert( mailslot->fd );
267 if (mailslot->write_fd != -1)
269 shutdown( mailslot->write_fd, SHUT_RDWR );
270 close( mailslot->write_fd );
272 release_object( mailslot->fd );
275 static void mailslot_dump( struct object *obj, int verbose )
277 struct mailslot *mailslot = (struct mailslot *) obj;
279 assert( obj->ops == &mailslot_ops );
280 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
281 mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
284 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
286 return FD_TYPE_MAILSLOT;
289 static struct fd *mailslot_get_fd( struct object *obj )
291 struct mailslot *mailslot = (struct mailslot *) obj;
293 return (struct fd *)grab_object( mailslot->fd );
296 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
298 /* mailslots can only be read */
299 return default_map_access( obj, access ) & FILE_GENERIC_READ;
302 static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent )
304 struct mailslot_device *dev = (struct mailslot_device *)parent;
306 if (parent->ops != &mailslot_device_ops)
308 set_error( STATUS_OBJECT_NAME_INVALID );
309 return 0;
311 namespace_add( dev->mailslots, name );
312 name->parent = grab_object( parent );
313 return 1;
316 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
317 unsigned int sharing, unsigned int options )
319 struct mailslot *mailslot = (struct mailslot *)obj;
320 struct mail_writer *writer;
321 int unix_fd;
323 if (!(sharing & FILE_SHARE_READ))
325 set_error( STATUS_SHARING_VIOLATION );
326 return NULL;
329 if (!list_empty( &mailslot->writers ))
331 /* Readers and writers cannot be mixed.
332 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
334 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
336 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
337 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
339 set_error( STATUS_SHARING_VIOLATION );
340 return NULL;
344 if ((unix_fd = dup( mailslot->write_fd )) == -1)
346 file_set_error();
347 return NULL;
349 if (!(writer = alloc_object( &mail_writer_ops )))
351 close( unix_fd );
352 return NULL;
354 grab_object( mailslot );
355 writer->mailslot = mailslot;
356 writer->access = mail_writer_map_access( &writer->obj, access );
357 writer->sharing = sharing;
358 list_add_head( &mailslot->writers, &writer->entry );
360 if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
362 release_object( writer );
363 return NULL;
365 allow_fd_caching( writer->fd );
366 return &writer->obj;
369 static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count )
371 struct mailslot *mailslot = get_fd_user( fd );
373 assert(mailslot->obj.ops == &mailslot_ops);
375 fd_queue_async( fd, async, type );
376 async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
377 STATUS_IO_TIMEOUT );
378 set_error( STATUS_PENDING );
381 static void mailslot_device_dump( struct object *obj, int verbose )
383 fputs( "Mailslot device\n", stderr );
386 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
387 unsigned int attr, struct object *root )
389 struct mailslot_device *device = (struct mailslot_device*)obj;
390 struct object *found;
392 assert( obj->ops == &mailslot_device_ops );
394 if (!name) return NULL; /* open the device itself */
396 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
397 name->len = 0;
399 return found;
402 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
403 unsigned int sharing, unsigned int options )
405 struct mailslot_device_file *file;
407 if (!(file = alloc_object( &mailslot_device_file_ops ))) return NULL;
408 file->device = (struct mailslot_device *)grab_object( obj );
409 if (!(file->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, obj, options )))
411 release_object( file );
412 return NULL;
414 allow_fd_caching( file->fd );
415 return &file->obj;
418 static void mailslot_device_destroy( struct object *obj )
420 struct mailslot_device *device = (struct mailslot_device*)obj;
421 assert( obj->ops == &mailslot_device_ops );
422 free( device->mailslots );
425 struct object *create_mailslot_device( struct object *root, const struct unicode_str *name,
426 unsigned int attr, const struct security_descriptor *sd )
428 struct mailslot_device *dev;
430 if ((dev = create_named_object( root, &mailslot_device_ops, name, attr, sd )) &&
431 get_error() != STATUS_OBJECT_NAME_EXISTS)
433 dev->mailslots = NULL;
434 if (!(dev->mailslots = create_namespace( 7 )))
436 release_object( dev );
437 dev = NULL;
440 return &dev->obj;
443 static void mailslot_device_file_dump( struct object *obj, int verbose )
445 struct mailslot_device_file *file = (struct mailslot_device_file *)obj;
447 fprintf( stderr, "File on mailslot device %p\n", file->device );
450 static struct fd *mailslot_device_file_get_fd( struct object *obj )
452 struct mailslot_device_file *file = (struct mailslot_device_file *)obj;
453 return (struct fd *)grab_object( file->fd );
456 static WCHAR *mailslot_device_file_get_full_name( struct object *obj, data_size_t *len )
458 struct mailslot_device_file *file = (struct mailslot_device_file *)obj;
459 return file->device->obj.ops->get_full_name( &file->device->obj, len );
462 static void mailslot_device_file_destroy( struct object *obj )
464 struct mailslot_device_file *file = (struct mailslot_device_file*)obj;
465 assert( obj->ops == &mailslot_device_file_ops );
466 if (file->fd) release_object( file->fd );
467 release_object( file->device );
470 static enum server_fd_type mailslot_device_file_get_fd_type( struct fd *fd )
472 return FD_TYPE_DEVICE;
475 static struct mailslot *create_mailslot( struct object *root,
476 const struct unicode_str *name, unsigned int attr,
477 int max_msgsize, timeout_t read_timeout,
478 const struct security_descriptor *sd )
480 struct mailslot *mailslot;
481 int fds[2];
483 if (!(mailslot = create_named_object( root, &mailslot_ops, name, attr, sd ))) return NULL;
485 mailslot->fd = NULL;
486 mailslot->write_fd = -1;
487 mailslot->max_msgsize = max_msgsize;
488 mailslot->read_timeout = read_timeout;
489 list_init( &mailslot->writers );
491 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
493 fcntl( fds[0], F_SETFL, O_NONBLOCK );
494 fcntl( fds[1], F_SETFL, O_NONBLOCK );
495 shutdown( fds[0], SHUT_RD );
496 mailslot->write_fd = fds[0];
497 if ((mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
498 FILE_SYNCHRONOUS_IO_NONALERT )))
500 allow_fd_caching( mailslot->fd );
501 return mailslot;
504 else file_set_error();
506 release_object( mailslot );
507 return NULL;
510 static void mail_writer_dump( struct object *obj, int verbose )
512 fprintf( stderr, "Mailslot writer\n" );
515 static void mail_writer_destroy( struct object *obj)
517 struct mail_writer *writer = (struct mail_writer *) obj;
519 if (writer->fd) release_object( writer->fd );
520 list_remove( &writer->entry );
521 release_object( writer->mailslot );
524 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
526 return FD_TYPE_MAILSLOT;
529 static struct fd *mail_writer_get_fd( struct object *obj )
531 struct mail_writer *writer = (struct mail_writer *) obj;
532 return (struct fd *)grab_object( writer->fd );
535 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
537 /* mailslot writers can only get write access */
538 return default_map_access( obj, access ) & FILE_GENERIC_WRITE;
541 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
542 unsigned int access )
544 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
548 /* create a mailslot */
549 DECL_HANDLER(create_mailslot)
551 struct mailslot *mailslot;
552 struct unicode_str name;
553 struct object *root;
554 const struct security_descriptor *sd;
555 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
557 if (!objattr) return;
559 if (!name.len) /* mailslots need a root directory even without a name */
561 if (!objattr->rootdir)
563 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
564 return;
566 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
569 if ((mailslot = create_mailslot( root, &name, objattr->attributes, req->max_msgsize,
570 req->read_timeout, sd )))
572 reply->handle = alloc_handle( current->process, mailslot, req->access, objattr->attributes );
573 release_object( mailslot );
576 if (root) release_object( root );
580 /* set mailslot information */
581 DECL_HANDLER(set_mailslot_info)
583 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
585 if (mailslot)
587 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
588 mailslot->read_timeout = req->read_timeout;
589 reply->max_msgsize = mailslot->max_msgsize;
590 reply->read_timeout = mailslot->read_timeout;
591 release_object( mailslot );