server: Rename the get_file_info function to get_fd_type and get rid of the flags.
[wine/multimedia.git] / server / mailslot.c
blob8e178627897cf10948f1df4a707238cd93e38f1b
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 struct fd *write_fd;
60 unsigned int max_msgsize;
61 int 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 no_lookup_name, /* lookup_name */
85 mailslot_open_file, /* open_file */
86 fd_close_handle, /* close_handle */
87 mailslot_destroy /* destroy */
90 static enum server_fd_type mailslot_get_fd_type( struct fd *fd );
91 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
93 static const struct fd_ops mailslot_fd_ops =
95 default_fd_get_poll_events, /* get_poll_events */
96 default_poll_event, /* poll_event */
97 no_flush, /* flush */
98 mailslot_get_fd_type, /* get_fd_type */
99 mailslot_queue_async, /* queue_async */
100 default_fd_reselect_async, /* reselect_async */
101 default_fd_cancel_async /* cancel_async */
105 struct mail_writer
107 struct object obj;
108 struct mailslot *mailslot;
109 struct list entry;
110 unsigned int access;
111 unsigned int sharing;
114 static void mail_writer_dump( struct object *obj, int verbose );
115 static struct fd *mail_writer_get_fd( struct object *obj );
116 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
117 static void mail_writer_destroy( struct object *obj);
119 static const struct object_ops mail_writer_ops =
121 sizeof(struct mail_writer), /* size */
122 mail_writer_dump, /* dump */
123 no_add_queue, /* add_queue */
124 NULL, /* remove_queue */
125 NULL, /* signaled */
126 NULL, /* satisfied */
127 no_signal, /* signal */
128 mail_writer_get_fd, /* get_fd */
129 mail_writer_map_access, /* map_access */
130 no_lookup_name, /* lookup_name */
131 no_open_file, /* open_file */
132 fd_close_handle, /* close_handle */
133 mail_writer_destroy /* destroy */
136 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
138 static const struct fd_ops mail_writer_fd_ops =
140 NULL, /* get_poll_events */
141 NULL, /* poll_event */
142 no_flush, /* flush */
143 mail_writer_get_fd_type, /* get_fd_type */
144 no_queue_async, /* queue_async */
145 NULL /* cancel_async */
149 struct mailslot_device
151 struct object obj; /* object header */
152 struct fd *fd; /* pseudo-fd for ioctls */
153 struct namespace *mailslots; /* mailslot namespace */
156 static void mailslot_device_dump( struct object *obj, int verbose );
157 static struct fd *mailslot_device_get_fd( struct object *obj );
158 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
159 unsigned int attr );
160 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
161 unsigned int sharing, unsigned int options );
162 static void mailslot_device_destroy( struct object *obj );
163 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
165 static const struct object_ops mailslot_device_ops =
167 sizeof(struct mailslot_device), /* size */
168 mailslot_device_dump, /* dump */
169 no_add_queue, /* add_queue */
170 NULL, /* remove_queue */
171 NULL, /* signaled */
172 no_satisfied, /* satisfied */
173 no_signal, /* signal */
174 mailslot_device_get_fd, /* get_fd */
175 no_map_access, /* map_access */
176 mailslot_device_lookup_name, /* lookup_name */
177 mailslot_device_open_file, /* open_file */
178 fd_close_handle, /* close_handle */
179 mailslot_device_destroy /* destroy */
182 static const struct fd_ops mailslot_device_fd_ops =
184 default_fd_get_poll_events, /* get_poll_events */
185 default_poll_event, /* poll_event */
186 no_flush, /* flush */
187 mailslot_device_get_fd_type, /* get_fd_type */
188 default_fd_queue_async, /* queue_async */
189 default_fd_reselect_async, /* reselect_async */
190 default_fd_cancel_async /* cancel_async */
193 static void mailslot_destroy( struct object *obj)
195 struct mailslot *mailslot = (struct mailslot *) obj;
197 assert( mailslot->fd );
198 assert( mailslot->write_fd );
200 shutdown( get_unix_fd( mailslot->fd ), SHUT_RDWR );
201 release_object( mailslot->fd );
202 release_object( mailslot->write_fd );
205 static void mailslot_dump( struct object *obj, int verbose )
207 struct mailslot *mailslot = (struct mailslot *) obj;
209 assert( obj->ops == &mailslot_ops );
210 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%d\n",
211 mailslot->max_msgsize, mailslot->read_timeout );
214 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
216 return FD_TYPE_MAILSLOT;
219 static struct fd *mailslot_get_fd( struct object *obj )
221 struct mailslot *mailslot = (struct mailslot *) obj;
223 return (struct fd *)grab_object( mailslot->fd );
226 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
228 /* mailslots can only be read */
229 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
230 if (access & GENERIC_ALL) access |= FILE_GENERIC_READ;
231 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
234 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
235 unsigned int sharing, unsigned int options )
237 struct mailslot *mailslot = (struct mailslot *)obj;
238 struct mail_writer *writer;
240 if (!(sharing & FILE_SHARE_READ))
242 set_error( STATUS_SHARING_VIOLATION );
243 return NULL;
246 if (!list_empty( &mailslot->writers ))
248 /* Readers and writers cannot be mixed.
249 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
251 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
253 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
254 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
256 set_error( STATUS_SHARING_VIOLATION );
257 return NULL;
261 if (!(writer = alloc_object( &mail_writer_ops ))) return NULL;
262 grab_object( mailslot );
263 writer->mailslot = mailslot;
264 writer->access = mail_writer_map_access( &writer->obj, access );
265 writer->sharing = sharing;
267 list_add_head( &mailslot->writers, &writer->entry );
268 return &writer->obj;
271 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
273 struct mailslot *mailslot = get_fd_user( fd );
274 struct async *async;
276 assert(mailslot->obj.ops == &mailslot_ops);
278 if ((async = fd_queue_async( fd, data, type, count )))
280 if (mailslot->read_timeout != -1)
282 struct timeval when = current_time;
283 add_timeout( &when, max(1,mailslot->read_timeout) );
284 async_set_timeout( async, &when, STATUS_IO_TIMEOUT );
286 release_object( async );
287 set_error( STATUS_PENDING );
291 static void mailslot_device_dump( struct object *obj, int verbose )
293 assert( obj->ops == &mailslot_device_ops );
294 fprintf( stderr, "Mail slot device\n" );
297 static struct fd *mailslot_device_get_fd( struct object *obj )
299 struct mailslot_device *device = (struct mailslot_device *)obj;
300 return (struct fd *)grab_object( device->fd );
303 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
304 unsigned int attr )
306 struct mailslot_device *device = (struct mailslot_device*)obj;
307 struct object *found;
309 assert( obj->ops == &mailslot_device_ops );
311 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
312 name->len = 0;
314 return found;
317 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
318 unsigned int sharing, unsigned int options )
320 return grab_object( obj );
323 static void mailslot_device_destroy( struct object *obj )
325 struct mailslot_device *device = (struct mailslot_device*)obj;
326 assert( obj->ops == &mailslot_device_ops );
327 if (device->fd) release_object( device->fd );
328 free( device->mailslots );
331 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
333 return FD_TYPE_DEVICE;
336 void create_mailslot_device( struct directory *root, const struct unicode_str *name )
338 struct mailslot_device *dev;
340 if ((dev = create_named_object_dir( root, name, 0, &mailslot_device_ops )) &&
341 get_error() != STATUS_OBJECT_NAME_EXISTS)
343 dev->mailslots = NULL;
344 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj )) ||
345 !(dev->mailslots = create_namespace( 7 )))
347 release_object( dev );
348 dev = NULL;
351 if (dev) make_object_static( &dev->obj );
354 static struct mailslot *create_mailslot( struct directory *root,
355 const struct unicode_str *name, unsigned int attr,
356 int max_msgsize, int read_timeout )
358 struct object *obj;
359 struct unicode_str new_name;
360 struct mailslot_device *dev;
361 struct mailslot *mailslot;
362 int fds[2];
364 if (!name || !name->len) return alloc_object( &mailslot_ops );
366 if (!(obj = find_object_dir( root, name, attr, &new_name )))
368 set_error( STATUS_OBJECT_NAME_INVALID );
369 return NULL;
372 if (!new_name.len)
374 if (attr & OBJ_OPENIF && obj->ops == &mailslot_ops)
375 /* it already exists - there can only be one mailslot to read from */
376 set_error( STATUS_OBJECT_NAME_EXISTS );
377 else if (attr & OBJ_OPENIF)
378 set_error( STATUS_OBJECT_TYPE_MISMATCH );
379 else
380 set_error( STATUS_OBJECT_NAME_COLLISION );
381 release_object( obj );
382 return NULL;
385 if (obj->ops != &mailslot_device_ops)
387 set_error( STATUS_OBJECT_NAME_INVALID );
388 release_object( obj );
389 return NULL;
392 dev = (struct mailslot_device *)obj;
393 mailslot = create_object( dev->mailslots, &mailslot_ops, &new_name, NULL );
394 release_object( dev );
396 if (!mailslot) return NULL;
398 mailslot->fd = NULL;
399 mailslot->write_fd = NULL;
400 mailslot->max_msgsize = max_msgsize;
401 mailslot->read_timeout = read_timeout;
402 list_init( &mailslot->writers );
404 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
406 fcntl( fds[0], F_SETFL, O_NONBLOCK );
407 fcntl( fds[1], F_SETFL, O_NONBLOCK );
408 shutdown( fds[0], SHUT_RD );
409 mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
410 FILE_SYNCHRONOUS_IO_NONALERT );
411 mailslot->write_fd = create_anonymous_fd( &mail_writer_fd_ops, fds[0], &mailslot->obj,
412 FILE_SYNCHRONOUS_IO_NONALERT );
413 if (mailslot->fd && mailslot->write_fd) return mailslot;
415 else file_set_error();
417 release_object( mailslot );
418 return NULL;
421 static void mail_writer_dump( struct object *obj, int verbose )
423 fprintf( stderr, "Mailslot writer\n" );
426 static void mail_writer_destroy( struct object *obj)
428 struct mail_writer *writer = (struct mail_writer *) obj;
430 list_remove( &writer->entry );
431 release_object( writer->mailslot );
434 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
436 return FD_TYPE_MAILSLOT;
439 static struct fd *mail_writer_get_fd( struct object *obj )
441 struct mail_writer *writer = (struct mail_writer *) obj;
443 return (struct fd *)grab_object( writer->mailslot->write_fd );
446 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
448 /* mailslot writers can only get write access */
449 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
450 if (access & GENERIC_ALL) access |= FILE_GENERIC_WRITE;
451 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
454 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
455 unsigned int access )
457 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
461 /* create a mailslot */
462 DECL_HANDLER(create_mailslot)
464 struct mailslot *mailslot;
465 struct unicode_str name;
466 struct directory *root = NULL;
468 reply->handle = 0;
469 get_req_unicode_str( &name );
470 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
471 return;
473 if ((mailslot = create_mailslot( root, &name, req->attributes, req->max_msgsize,
474 req->read_timeout )))
476 reply->handle = alloc_handle( current->process, mailslot, req->access, req->attributes );
477 release_object( mailslot );
480 if (root) release_object( root );
484 /* set mailslot information */
485 DECL_HANDLER(set_mailslot_info)
487 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
489 if (mailslot)
491 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
492 mailslot->read_timeout = req->read_timeout;
493 reply->max_msgsize = mailslot->max_msgsize;
494 reply->read_timeout = mailslot->read_timeout;
495 release_object( mailslot );