wined3d: Fix handling of a special case in IWineD3DImpl_FillGLCaps() and adjust type...
[wine/wine64.git] / server / mailslot.c
blobee891ffba1ab05b992af6bb916c5b8d867960bd6
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 add_queue, /* add_queue */
78 remove_queue, /* remove_queue */
79 default_fd_signaled, /* signaled */
80 no_satisfied, /* satisfied */
81 no_signal, /* signal */
82 mailslot_get_fd, /* get_fd */
83 mailslot_map_access, /* map_access */
84 default_get_sd, /* get_sd */
85 default_set_sd, /* set_sd */
86 no_lookup_name, /* lookup_name */
87 mailslot_open_file, /* open_file */
88 fd_close_handle, /* close_handle */
89 mailslot_destroy /* destroy */
92 static enum server_fd_type mailslot_get_fd_type( struct fd *fd );
93 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
95 static const struct fd_ops mailslot_fd_ops =
97 default_fd_get_poll_events, /* get_poll_events */
98 default_poll_event, /* poll_event */
99 no_flush, /* flush */
100 mailslot_get_fd_type, /* get_fd_type */
101 default_fd_ioctl, /* ioctl */
102 mailslot_queue_async, /* queue_async */
103 default_fd_reselect_async, /* reselect_async */
104 default_fd_cancel_async /* cancel_async */
108 struct mail_writer
110 struct object obj;
111 struct fd *fd;
112 struct mailslot *mailslot;
113 struct list entry;
114 unsigned int access;
115 unsigned int sharing;
118 static void mail_writer_dump( struct object *obj, int verbose );
119 static struct fd *mail_writer_get_fd( struct object *obj );
120 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
121 static void mail_writer_destroy( struct object *obj);
123 static const struct object_ops mail_writer_ops =
125 sizeof(struct mail_writer), /* size */
126 mail_writer_dump, /* dump */
127 no_add_queue, /* add_queue */
128 NULL, /* remove_queue */
129 NULL, /* signaled */
130 NULL, /* satisfied */
131 no_signal, /* signal */
132 mail_writer_get_fd, /* get_fd */
133 mail_writer_map_access, /* map_access */
134 default_get_sd, /* get_sd */
135 default_set_sd, /* set_sd */
136 no_lookup_name, /* lookup_name */
137 no_open_file, /* open_file */
138 fd_close_handle, /* close_handle */
139 mail_writer_destroy /* destroy */
142 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
144 static const struct fd_ops mail_writer_fd_ops =
146 default_fd_get_poll_events, /* get_poll_events */
147 default_poll_event, /* poll_event */
148 no_flush, /* flush */
149 mail_writer_get_fd_type, /* get_fd_type */
150 default_fd_ioctl, /* ioctl */
151 default_fd_queue_async, /* queue_async */
152 default_fd_reselect_async, /* reselect_async */
153 default_fd_cancel_async /* cancel_async */
157 struct mailslot_device
159 struct object obj; /* object header */
160 struct fd *fd; /* pseudo-fd for ioctls */
161 struct namespace *mailslots; /* mailslot namespace */
164 static void mailslot_device_dump( struct object *obj, int verbose );
165 static struct fd *mailslot_device_get_fd( struct object *obj );
166 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
167 unsigned int attr );
168 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
169 unsigned int sharing, unsigned int options );
170 static void mailslot_device_destroy( struct object *obj );
171 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
173 static const struct object_ops mailslot_device_ops =
175 sizeof(struct mailslot_device), /* size */
176 mailslot_device_dump, /* dump */
177 no_add_queue, /* add_queue */
178 NULL, /* remove_queue */
179 NULL, /* signaled */
180 no_satisfied, /* satisfied */
181 no_signal, /* signal */
182 mailslot_device_get_fd, /* get_fd */
183 no_map_access, /* map_access */
184 default_get_sd, /* get_sd */
185 default_set_sd, /* set_sd */
186 mailslot_device_lookup_name, /* lookup_name */
187 mailslot_device_open_file, /* open_file */
188 fd_close_handle, /* close_handle */
189 mailslot_device_destroy /* destroy */
192 static const struct fd_ops mailslot_device_fd_ops =
194 default_fd_get_poll_events, /* get_poll_events */
195 default_poll_event, /* poll_event */
196 no_flush, /* flush */
197 mailslot_device_get_fd_type, /* get_fd_type */
198 default_fd_ioctl, /* ioctl */
199 default_fd_queue_async, /* queue_async */
200 default_fd_reselect_async, /* reselect_async */
201 default_fd_cancel_async /* cancel_async */
204 static void mailslot_destroy( struct object *obj)
206 struct mailslot *mailslot = (struct mailslot *) obj;
208 assert( mailslot->fd );
210 if (mailslot->write_fd != -1)
212 shutdown( mailslot->write_fd, SHUT_RDWR );
213 close( mailslot->write_fd );
215 release_object( mailslot->fd );
218 static void mailslot_dump( struct object *obj, int verbose )
220 struct mailslot *mailslot = (struct mailslot *) obj;
222 assert( obj->ops == &mailslot_ops );
223 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
224 mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
227 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
229 return FD_TYPE_MAILSLOT;
232 static struct fd *mailslot_get_fd( struct object *obj )
234 struct mailslot *mailslot = (struct mailslot *) obj;
236 return (struct fd *)grab_object( mailslot->fd );
239 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
241 /* mailslots can only be read */
242 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
243 if (access & GENERIC_ALL) access |= FILE_GENERIC_READ;
244 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
247 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
248 unsigned int sharing, unsigned int options )
250 struct mailslot *mailslot = (struct mailslot *)obj;
251 struct mail_writer *writer;
252 int unix_fd;
254 if (!(sharing & FILE_SHARE_READ))
256 set_error( STATUS_SHARING_VIOLATION );
257 return NULL;
260 if (!list_empty( &mailslot->writers ))
262 /* Readers and writers cannot be mixed.
263 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
265 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
267 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
268 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
270 set_error( STATUS_SHARING_VIOLATION );
271 return NULL;
275 if ((unix_fd = dup( mailslot->write_fd )) == -1)
277 file_set_error();
278 return NULL;
280 if (!(writer = alloc_object( &mail_writer_ops )))
282 close( unix_fd );
283 return NULL;
285 grab_object( mailslot );
286 writer->mailslot = mailslot;
287 writer->access = mail_writer_map_access( &writer->obj, access );
288 writer->sharing = sharing;
289 list_add_head( &mailslot->writers, &writer->entry );
291 if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
293 release_object( writer );
294 return NULL;
296 return &writer->obj;
299 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
301 struct mailslot *mailslot = get_fd_user( fd );
302 struct async *async;
304 assert(mailslot->obj.ops == &mailslot_ops);
306 if ((async = fd_queue_async( fd, data, type, count )))
308 async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
309 STATUS_IO_TIMEOUT );
310 release_object( async );
311 set_error( STATUS_PENDING );
315 static void mailslot_device_dump( struct object *obj, int verbose )
317 assert( obj->ops == &mailslot_device_ops );
318 fprintf( stderr, "Mail slot device\n" );
321 static struct fd *mailslot_device_get_fd( struct object *obj )
323 struct mailslot_device *device = (struct mailslot_device *)obj;
324 return (struct fd *)grab_object( device->fd );
327 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
328 unsigned int attr )
330 struct mailslot_device *device = (struct mailslot_device*)obj;
331 struct object *found;
333 assert( obj->ops == &mailslot_device_ops );
335 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
336 name->len = 0;
338 return found;
341 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
342 unsigned int sharing, unsigned int options )
344 return grab_object( obj );
347 static void mailslot_device_destroy( struct object *obj )
349 struct mailslot_device *device = (struct mailslot_device*)obj;
350 assert( obj->ops == &mailslot_device_ops );
351 if (device->fd) release_object( device->fd );
352 free( device->mailslots );
355 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
357 return FD_TYPE_DEVICE;
360 void create_mailslot_device( struct directory *root, const struct unicode_str *name )
362 struct mailslot_device *dev;
364 if ((dev = create_named_object_dir( root, name, 0, &mailslot_device_ops )) &&
365 get_error() != STATUS_OBJECT_NAME_EXISTS)
367 dev->mailslots = NULL;
368 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj, 0 )) ||
369 !(dev->mailslots = create_namespace( 7 )))
371 release_object( dev );
372 dev = NULL;
375 if (dev) make_object_static( &dev->obj );
378 static struct mailslot *create_mailslot( struct directory *root,
379 const struct unicode_str *name, unsigned int attr,
380 int max_msgsize, timeout_t read_timeout )
382 struct object *obj;
383 struct unicode_str new_name;
384 struct mailslot_device *dev;
385 struct mailslot *mailslot;
386 int fds[2];
388 if (!name || !name->len) return alloc_object( &mailslot_ops );
390 if (!(obj = find_object_dir( root, name, attr, &new_name )))
392 set_error( STATUS_OBJECT_NAME_INVALID );
393 return NULL;
396 if (!new_name.len)
398 if (attr & OBJ_OPENIF && obj->ops == &mailslot_ops)
399 /* it already exists - there can only be one mailslot to read from */
400 set_error( STATUS_OBJECT_NAME_EXISTS );
401 else if (attr & OBJ_OPENIF)
402 set_error( STATUS_OBJECT_TYPE_MISMATCH );
403 else
404 set_error( STATUS_OBJECT_NAME_COLLISION );
405 release_object( obj );
406 return NULL;
409 if (obj->ops != &mailslot_device_ops)
411 set_error( STATUS_OBJECT_NAME_INVALID );
412 release_object( obj );
413 return NULL;
416 dev = (struct mailslot_device *)obj;
417 mailslot = create_object( dev->mailslots, &mailslot_ops, &new_name, NULL );
418 release_object( dev );
420 if (!mailslot) return NULL;
422 mailslot->fd = NULL;
423 mailslot->write_fd = -1;
424 mailslot->max_msgsize = max_msgsize;
425 mailslot->read_timeout = read_timeout;
426 list_init( &mailslot->writers );
428 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
430 fcntl( fds[0], F_SETFL, O_NONBLOCK );
431 fcntl( fds[1], F_SETFL, O_NONBLOCK );
432 shutdown( fds[0], SHUT_RD );
433 mailslot->write_fd = fds[0];
434 mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
435 FILE_SYNCHRONOUS_IO_NONALERT );
436 if (mailslot->fd) return mailslot;
438 else file_set_error();
440 release_object( mailslot );
441 return NULL;
444 static void mail_writer_dump( struct object *obj, int verbose )
446 fprintf( stderr, "Mailslot writer\n" );
449 static void mail_writer_destroy( struct object *obj)
451 struct mail_writer *writer = (struct mail_writer *) obj;
453 if (writer->fd) release_object( writer->fd );
454 list_remove( &writer->entry );
455 release_object( writer->mailslot );
458 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
460 return FD_TYPE_MAILSLOT;
463 static struct fd *mail_writer_get_fd( struct object *obj )
465 struct mail_writer *writer = (struct mail_writer *) obj;
466 return (struct fd *)grab_object( writer->fd );
469 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
471 /* mailslot writers can only get write access */
472 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
473 if (access & GENERIC_ALL) access |= FILE_GENERIC_WRITE;
474 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
477 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
478 unsigned int access )
480 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
484 /* create a mailslot */
485 DECL_HANDLER(create_mailslot)
487 struct mailslot *mailslot;
488 struct unicode_str name;
489 struct directory *root = NULL;
491 reply->handle = 0;
492 get_req_unicode_str( &name );
493 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
494 return;
496 if ((mailslot = create_mailslot( root, &name, req->attributes, req->max_msgsize,
497 req->read_timeout )))
499 reply->handle = alloc_handle( current->process, mailslot, req->access, req->attributes );
500 release_object( mailslot );
503 if (root) release_object( root );
507 /* set mailslot information */
508 DECL_HANDLER(set_mailslot_info)
510 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
512 if (mailslot)
514 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
515 mailslot->read_timeout = req->read_timeout;
516 reply->max_msgsize = mailslot->max_msgsize;
517 reply->read_timeout = mailslot->read_timeout;
518 release_object( mailslot );