wined3d: Simplify wined3d_rendertarget_view_create_from_sub_resource().
[wine.git] / server / mailslot.c
blobe388f8e51533ff57e52e0800a0527baa8947273d
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 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 no_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_open_file, /* open_file */
89 fd_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, const async_data_t *data, 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_ioctl, /* ioctl */
105 mailslot_queue_async, /* queue_async */
106 default_fd_reselect_async, /* reselect_async */
107 default_fd_cancel_async /* cancel_async */
111 struct mail_writer
113 struct object obj;
114 struct fd *fd;
115 struct mailslot *mailslot;
116 struct list entry;
117 unsigned int access;
118 unsigned int sharing;
121 static void mail_writer_dump( struct object *obj, int verbose );
122 static struct fd *mail_writer_get_fd( struct object *obj );
123 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
124 static void mail_writer_destroy( struct object *obj);
126 static const struct object_ops mail_writer_ops =
128 sizeof(struct mail_writer), /* size */
129 mail_writer_dump, /* dump */
130 no_get_type, /* get_type */
131 no_add_queue, /* add_queue */
132 NULL, /* remove_queue */
133 NULL, /* signaled */
134 NULL, /* satisfied */
135 no_signal, /* signal */
136 mail_writer_get_fd, /* get_fd */
137 mail_writer_map_access, /* map_access */
138 default_get_sd, /* get_sd */
139 default_set_sd, /* set_sd */
140 no_lookup_name, /* lookup_name */
141 no_open_file, /* open_file */
142 fd_close_handle, /* close_handle */
143 mail_writer_destroy /* destroy */
146 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
148 static const struct fd_ops mail_writer_fd_ops =
150 default_fd_get_poll_events, /* get_poll_events */
151 default_poll_event, /* poll_event */
152 mail_writer_get_fd_type, /* get_fd_type */
153 no_fd_read, /* read */
154 no_fd_write, /* write */
155 no_fd_flush, /* flush */
156 default_fd_ioctl, /* ioctl */
157 default_fd_queue_async, /* queue_async */
158 default_fd_reselect_async, /* reselect_async */
159 default_fd_cancel_async /* cancel_async */
163 struct mailslot_device
165 struct object obj; /* object header */
166 struct fd *fd; /* pseudo-fd for ioctls */
167 struct namespace *mailslots; /* mailslot namespace */
170 static void mailslot_device_dump( struct object *obj, int verbose );
171 static struct object_type *mailslot_device_get_type( struct object *obj );
172 static struct fd *mailslot_device_get_fd( struct object *obj );
173 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
174 unsigned int attr );
175 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
176 unsigned int sharing, unsigned int options );
177 static void mailslot_device_destroy( struct object *obj );
178 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
180 static const struct object_ops mailslot_device_ops =
182 sizeof(struct mailslot_device), /* size */
183 mailslot_device_dump, /* dump */
184 mailslot_device_get_type, /* get_type */
185 no_add_queue, /* add_queue */
186 NULL, /* remove_queue */
187 NULL, /* signaled */
188 no_satisfied, /* satisfied */
189 no_signal, /* signal */
190 mailslot_device_get_fd, /* get_fd */
191 no_map_access, /* map_access */
192 default_get_sd, /* get_sd */
193 default_set_sd, /* set_sd */
194 mailslot_device_lookup_name, /* lookup_name */
195 mailslot_device_open_file, /* open_file */
196 fd_close_handle, /* close_handle */
197 mailslot_device_destroy /* destroy */
200 static const struct fd_ops mailslot_device_fd_ops =
202 default_fd_get_poll_events, /* get_poll_events */
203 default_poll_event, /* poll_event */
204 mailslot_device_get_fd_type, /* get_fd_type */
205 no_fd_read, /* read */
206 no_fd_write, /* write */
207 no_fd_flush, /* flush */
208 default_fd_ioctl, /* ioctl */
209 default_fd_queue_async, /* queue_async */
210 default_fd_reselect_async, /* reselect_async */
211 default_fd_cancel_async /* cancel_async */
214 static void mailslot_destroy( struct object *obj)
216 struct mailslot *mailslot = (struct mailslot *) obj;
218 assert( mailslot->fd );
220 if (mailslot->write_fd != -1)
222 shutdown( mailslot->write_fd, SHUT_RDWR );
223 close( mailslot->write_fd );
225 release_object( mailslot->fd );
228 static void mailslot_dump( struct object *obj, int verbose )
230 struct mailslot *mailslot = (struct mailslot *) obj;
232 assert( obj->ops == &mailslot_ops );
233 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
234 mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
237 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
239 return FD_TYPE_MAILSLOT;
242 static struct fd *mailslot_get_fd( struct object *obj )
244 struct mailslot *mailslot = (struct mailslot *) obj;
246 return (struct fd *)grab_object( mailslot->fd );
249 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
251 /* mailslots can only be read */
252 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
253 if (access & GENERIC_ALL) access |= FILE_GENERIC_READ;
254 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
257 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
258 unsigned int sharing, unsigned int options )
260 struct mailslot *mailslot = (struct mailslot *)obj;
261 struct mail_writer *writer;
262 int unix_fd;
264 if (!(sharing & FILE_SHARE_READ))
266 set_error( STATUS_SHARING_VIOLATION );
267 return NULL;
270 if (!list_empty( &mailslot->writers ))
272 /* Readers and writers cannot be mixed.
273 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
275 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
277 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
278 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
280 set_error( STATUS_SHARING_VIOLATION );
281 return NULL;
285 if ((unix_fd = dup( mailslot->write_fd )) == -1)
287 file_set_error();
288 return NULL;
290 if (!(writer = alloc_object( &mail_writer_ops )))
292 close( unix_fd );
293 return NULL;
295 grab_object( mailslot );
296 writer->mailslot = mailslot;
297 writer->access = mail_writer_map_access( &writer->obj, access );
298 writer->sharing = sharing;
299 list_add_head( &mailslot->writers, &writer->entry );
301 if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
303 release_object( writer );
304 return NULL;
306 allow_fd_caching( writer->fd );
307 return &writer->obj;
310 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
312 struct mailslot *mailslot = get_fd_user( fd );
313 struct async *async;
315 assert(mailslot->obj.ops == &mailslot_ops);
317 if ((async = fd_queue_async( fd, data, type )))
319 async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
320 STATUS_IO_TIMEOUT );
321 release_object( async );
322 set_error( STATUS_PENDING );
326 static void mailslot_device_dump( struct object *obj, int verbose )
328 fputs( "Mailslot device\n", stderr );
331 static struct object_type *mailslot_device_get_type( struct object *obj )
333 static const WCHAR name[] = {'D','e','v','i','c','e'};
334 static const struct unicode_str str = { name, sizeof(name) };
335 return get_object_type( &str );
338 static struct fd *mailslot_device_get_fd( struct object *obj )
340 struct mailslot_device *device = (struct mailslot_device *)obj;
341 return (struct fd *)grab_object( device->fd );
344 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
345 unsigned int attr )
347 struct mailslot_device *device = (struct mailslot_device*)obj;
348 struct object *found;
350 assert( obj->ops == &mailslot_device_ops );
352 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
353 name->len = 0;
355 return found;
358 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
359 unsigned int sharing, unsigned int options )
361 return grab_object( obj );
364 static void mailslot_device_destroy( struct object *obj )
366 struct mailslot_device *device = (struct mailslot_device*)obj;
367 assert( obj->ops == &mailslot_device_ops );
368 if (device->fd) release_object( device->fd );
369 free( device->mailslots );
372 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
374 return FD_TYPE_DEVICE;
377 void create_mailslot_device( struct directory *root, const struct unicode_str *name )
379 struct mailslot_device *dev;
381 if ((dev = create_named_object_dir( root, name, 0, &mailslot_device_ops )) &&
382 get_error() != STATUS_OBJECT_NAME_EXISTS)
384 dev->mailslots = NULL;
385 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj, 0 )) ||
386 !(dev->mailslots = create_namespace( 7 )))
388 release_object( dev );
389 dev = NULL;
392 if (dev) make_object_static( &dev->obj );
395 static struct mailslot *create_mailslot( struct directory *root,
396 const struct unicode_str *name, unsigned int attr,
397 int max_msgsize, timeout_t read_timeout,
398 const struct security_descriptor *sd )
400 struct object *obj;
401 struct unicode_str new_name;
402 struct mailslot_device *dev;
403 struct mailslot *mailslot;
404 int fds[2];
406 if (!name || !name->len)
408 mailslot = alloc_object( &mailslot_ops );
409 goto init;
412 if (!(obj = find_object_dir( root, name, attr, &new_name )))
414 set_error( STATUS_OBJECT_NAME_INVALID );
415 return NULL;
418 if (!new_name.len)
420 if (attr & OBJ_OPENIF && obj->ops == &mailslot_ops)
421 /* it already exists - there can only be one mailslot to read from */
422 set_error( STATUS_OBJECT_NAME_EXISTS );
423 else if (attr & OBJ_OPENIF)
424 set_error( STATUS_OBJECT_TYPE_MISMATCH );
425 else
426 set_error( STATUS_OBJECT_NAME_COLLISION );
427 release_object( obj );
428 return NULL;
431 if (obj->ops != &mailslot_device_ops)
433 set_error( STATUS_OBJECT_NAME_INVALID );
434 release_object( obj );
435 return NULL;
438 dev = (struct mailslot_device *)obj;
439 mailslot = create_object( dev->mailslots, &mailslot_ops, &new_name, NULL );
440 release_object( dev );
442 init:
443 if (!mailslot) return NULL;
445 mailslot->fd = NULL;
446 mailslot->write_fd = -1;
447 mailslot->max_msgsize = max_msgsize;
448 mailslot->read_timeout = read_timeout;
449 list_init( &mailslot->writers );
450 if (sd) default_set_sd( &mailslot->obj, sd, OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
451 DACL_SECURITY_INFORMATION | SACL_SECURITY_INFORMATION );
453 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
455 fcntl( fds[0], F_SETFL, O_NONBLOCK );
456 fcntl( fds[1], F_SETFL, O_NONBLOCK );
457 shutdown( fds[0], SHUT_RD );
458 mailslot->write_fd = fds[0];
459 if ((mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
460 FILE_SYNCHRONOUS_IO_NONALERT )))
462 allow_fd_caching( mailslot->fd );
463 return mailslot;
466 else file_set_error();
468 release_object( mailslot );
469 return NULL;
472 static void mail_writer_dump( struct object *obj, int verbose )
474 fprintf( stderr, "Mailslot writer\n" );
477 static void mail_writer_destroy( struct object *obj)
479 struct mail_writer *writer = (struct mail_writer *) obj;
481 if (writer->fd) release_object( writer->fd );
482 list_remove( &writer->entry );
483 release_object( writer->mailslot );
486 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
488 return FD_TYPE_MAILSLOT;
491 static struct fd *mail_writer_get_fd( struct object *obj )
493 struct mail_writer *writer = (struct mail_writer *) obj;
494 return (struct fd *)grab_object( writer->fd );
497 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
499 /* mailslot writers can only get write access */
500 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
501 if (access & GENERIC_ALL) access |= FILE_GENERIC_WRITE;
502 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
505 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
506 unsigned int access )
508 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
512 /* create a mailslot */
513 DECL_HANDLER(create_mailslot)
515 struct mailslot *mailslot;
516 struct unicode_str name;
517 struct directory *root;
518 const struct security_descriptor *sd;
519 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
521 if (!objattr) return;
523 if (!name.len) /* mailslots need a root directory even without a name */
525 if (!objattr->rootdir)
527 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
528 return;
530 else if (!(root = get_directory_obj( current->process, objattr->rootdir, 0 ))) return;
533 if ((mailslot = create_mailslot( root, &name, objattr->attributes, req->max_msgsize,
534 req->read_timeout, sd )))
536 reply->handle = alloc_handle( current->process, mailslot, req->access, objattr->attributes );
537 release_object( mailslot );
540 if (root) release_object( root );
544 /* set mailslot information */
545 DECL_HANDLER(set_mailslot_info)
547 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
549 if (mailslot)
551 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
552 mailslot->read_timeout = req->read_timeout;
553 reply->max_msgsize = mailslot->max_msgsize;
554 reply->read_timeout = mailslot->read_timeout;
555 release_object( mailslot );