po: Some more updates to Norwegian translation.
[wine.git] / server / mailslot.c
blob13e67031c16f607be6696b814c3c0c2aa03a49d3
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, const async_data_t *data, 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 */
110 default_fd_cancel_async /* cancel_async */
114 struct mail_writer
116 struct object obj;
117 struct fd *fd;
118 struct mailslot *mailslot;
119 struct list entry;
120 unsigned int access;
121 unsigned int sharing;
124 static void mail_writer_dump( struct object *obj, int verbose );
125 static struct fd *mail_writer_get_fd( struct object *obj );
126 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
127 static void mail_writer_destroy( struct object *obj);
129 static const struct object_ops mail_writer_ops =
131 sizeof(struct mail_writer), /* size */
132 mail_writer_dump, /* dump */
133 no_get_type, /* get_type */
134 no_add_queue, /* add_queue */
135 NULL, /* remove_queue */
136 NULL, /* signaled */
137 NULL, /* satisfied */
138 no_signal, /* signal */
139 mail_writer_get_fd, /* get_fd */
140 mail_writer_map_access, /* map_access */
141 default_get_sd, /* get_sd */
142 default_set_sd, /* set_sd */
143 no_lookup_name, /* lookup_name */
144 no_link_name, /* link_name */
145 NULL, /* unlink_name */
146 no_open_file, /* open_file */
147 fd_close_handle, /* close_handle */
148 mail_writer_destroy /* destroy */
151 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
153 static const struct fd_ops mail_writer_fd_ops =
155 default_fd_get_poll_events, /* get_poll_events */
156 default_poll_event, /* poll_event */
157 mail_writer_get_fd_type, /* get_fd_type */
158 no_fd_read, /* read */
159 no_fd_write, /* write */
160 no_fd_flush, /* flush */
161 default_fd_ioctl, /* ioctl */
162 default_fd_queue_async, /* queue_async */
163 default_fd_reselect_async, /* reselect_async */
164 default_fd_cancel_async /* cancel_async */
168 struct mailslot_device
170 struct object obj; /* object header */
171 struct fd *fd; /* pseudo-fd for ioctls */
172 struct namespace *mailslots; /* mailslot namespace */
175 static void mailslot_device_dump( struct object *obj, int verbose );
176 static struct object_type *mailslot_device_get_type( struct object *obj );
177 static struct fd *mailslot_device_get_fd( struct object *obj );
178 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
179 unsigned int attr );
180 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
181 unsigned int sharing, unsigned int options );
182 static void mailslot_device_destroy( struct object *obj );
183 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
185 static const struct object_ops mailslot_device_ops =
187 sizeof(struct mailslot_device), /* size */
188 mailslot_device_dump, /* dump */
189 mailslot_device_get_type, /* get_type */
190 no_add_queue, /* add_queue */
191 NULL, /* remove_queue */
192 NULL, /* signaled */
193 no_satisfied, /* satisfied */
194 no_signal, /* signal */
195 mailslot_device_get_fd, /* get_fd */
196 no_map_access, /* map_access */
197 default_get_sd, /* get_sd */
198 default_set_sd, /* set_sd */
199 mailslot_device_lookup_name, /* lookup_name */
200 directory_link_name, /* link_name */
201 default_unlink_name, /* unlink_name */
202 mailslot_device_open_file, /* open_file */
203 fd_close_handle, /* close_handle */
204 mailslot_device_destroy /* destroy */
207 static const struct fd_ops mailslot_device_fd_ops =
209 default_fd_get_poll_events, /* get_poll_events */
210 default_poll_event, /* poll_event */
211 mailslot_device_get_fd_type, /* get_fd_type */
212 no_fd_read, /* read */
213 no_fd_write, /* write */
214 no_fd_flush, /* flush */
215 default_fd_ioctl, /* ioctl */
216 default_fd_queue_async, /* queue_async */
217 default_fd_reselect_async, /* reselect_async */
218 default_fd_cancel_async /* cancel_async */
221 static void mailslot_destroy( struct object *obj)
223 struct mailslot *mailslot = (struct mailslot *) obj;
225 assert( mailslot->fd );
227 if (mailslot->write_fd != -1)
229 shutdown( mailslot->write_fd, SHUT_RDWR );
230 close( mailslot->write_fd );
232 release_object( mailslot->fd );
235 static void mailslot_dump( struct object *obj, int verbose )
237 struct mailslot *mailslot = (struct mailslot *) obj;
239 assert( obj->ops == &mailslot_ops );
240 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
241 mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
244 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
246 return FD_TYPE_MAILSLOT;
249 static struct fd *mailslot_get_fd( struct object *obj )
251 struct mailslot *mailslot = (struct mailslot *) obj;
253 return (struct fd *)grab_object( mailslot->fd );
256 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
258 /* mailslots can only be read */
259 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
260 if (access & GENERIC_ALL) access |= FILE_GENERIC_READ;
261 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
264 static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent )
266 struct mailslot_device *dev = (struct mailslot_device *)parent;
268 if (parent->ops != &mailslot_device_ops)
270 set_error( STATUS_OBJECT_NAME_INVALID );
271 return 0;
273 namespace_add( dev->mailslots, name );
274 name->parent = grab_object( parent );
275 return 1;
278 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
279 unsigned int sharing, unsigned int options )
281 struct mailslot *mailslot = (struct mailslot *)obj;
282 struct mail_writer *writer;
283 int unix_fd;
285 if (!(sharing & FILE_SHARE_READ))
287 set_error( STATUS_SHARING_VIOLATION );
288 return NULL;
291 if (!list_empty( &mailslot->writers ))
293 /* Readers and writers cannot be mixed.
294 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
296 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
298 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
299 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
301 set_error( STATUS_SHARING_VIOLATION );
302 return NULL;
306 if ((unix_fd = dup( mailslot->write_fd )) == -1)
308 file_set_error();
309 return NULL;
311 if (!(writer = alloc_object( &mail_writer_ops )))
313 close( unix_fd );
314 return NULL;
316 grab_object( mailslot );
317 writer->mailslot = mailslot;
318 writer->access = mail_writer_map_access( &writer->obj, access );
319 writer->sharing = sharing;
320 list_add_head( &mailslot->writers, &writer->entry );
322 if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
324 release_object( writer );
325 return NULL;
327 allow_fd_caching( writer->fd );
328 return &writer->obj;
331 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
333 struct mailslot *mailslot = get_fd_user( fd );
334 struct async *async;
336 assert(mailslot->obj.ops == &mailslot_ops);
338 if ((async = fd_queue_async( fd, data, type )))
340 async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
341 STATUS_IO_TIMEOUT );
342 release_object( async );
343 set_error( STATUS_PENDING );
347 static void mailslot_device_dump( struct object *obj, int verbose )
349 fputs( "Mailslot device\n", stderr );
352 static struct object_type *mailslot_device_get_type( struct object *obj )
354 static const WCHAR name[] = {'D','e','v','i','c','e'};
355 static const struct unicode_str str = { name, sizeof(name) };
356 return get_object_type( &str );
359 static struct fd *mailslot_device_get_fd( struct object *obj )
361 struct mailslot_device *device = (struct mailslot_device *)obj;
362 return (struct fd *)grab_object( device->fd );
365 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
366 unsigned int attr )
368 struct mailslot_device *device = (struct mailslot_device*)obj;
369 struct object *found;
371 assert( obj->ops == &mailslot_device_ops );
373 if (!name) return NULL; /* open the device itself */
375 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
376 name->len = 0;
378 return found;
381 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
382 unsigned int sharing, unsigned int options )
384 return grab_object( obj );
387 static void mailslot_device_destroy( struct object *obj )
389 struct mailslot_device *device = (struct mailslot_device*)obj;
390 assert( obj->ops == &mailslot_device_ops );
391 if (device->fd) release_object( device->fd );
392 free( device->mailslots );
395 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
397 return FD_TYPE_DEVICE;
400 struct object *create_mailslot_device( struct object *root, const struct unicode_str *name )
402 struct mailslot_device *dev;
404 if ((dev = create_named_object( root, &mailslot_device_ops, name, 0, NULL )) &&
405 get_error() != STATUS_OBJECT_NAME_EXISTS)
407 dev->mailslots = NULL;
408 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj, 0 )) ||
409 !(dev->mailslots = create_namespace( 7 )))
411 release_object( dev );
412 dev = NULL;
415 return &dev->obj;
418 static struct mailslot *create_mailslot( struct object *root,
419 const struct unicode_str *name, unsigned int attr,
420 int max_msgsize, timeout_t read_timeout,
421 const struct security_descriptor *sd )
423 struct mailslot *mailslot;
424 int fds[2];
426 if (!(mailslot = create_named_object( root, &mailslot_ops, name, attr, sd ))) return NULL;
428 mailslot->fd = NULL;
429 mailslot->write_fd = -1;
430 mailslot->max_msgsize = max_msgsize;
431 mailslot->read_timeout = read_timeout;
432 list_init( &mailslot->writers );
434 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
436 fcntl( fds[0], F_SETFL, O_NONBLOCK );
437 fcntl( fds[1], F_SETFL, O_NONBLOCK );
438 shutdown( fds[0], SHUT_RD );
439 mailslot->write_fd = fds[0];
440 if ((mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
441 FILE_SYNCHRONOUS_IO_NONALERT )))
443 allow_fd_caching( mailslot->fd );
444 return mailslot;
447 else file_set_error();
449 release_object( mailslot );
450 return NULL;
453 static void mail_writer_dump( struct object *obj, int verbose )
455 fprintf( stderr, "Mailslot writer\n" );
458 static void mail_writer_destroy( struct object *obj)
460 struct mail_writer *writer = (struct mail_writer *) obj;
462 if (writer->fd) release_object( writer->fd );
463 list_remove( &writer->entry );
464 release_object( writer->mailslot );
467 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
469 return FD_TYPE_MAILSLOT;
472 static struct fd *mail_writer_get_fd( struct object *obj )
474 struct mail_writer *writer = (struct mail_writer *) obj;
475 return (struct fd *)grab_object( writer->fd );
478 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
480 /* mailslot writers can only get write access */
481 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
482 if (access & GENERIC_ALL) access |= FILE_GENERIC_WRITE;
483 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
486 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
487 unsigned int access )
489 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
493 /* create a mailslot */
494 DECL_HANDLER(create_mailslot)
496 struct mailslot *mailslot;
497 struct unicode_str name;
498 struct object *root;
499 const struct security_descriptor *sd;
500 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
502 if (!objattr) return;
504 if (!name.len) /* mailslots need a root directory even without a name */
506 if (!objattr->rootdir)
508 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
509 return;
511 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
514 if ((mailslot = create_mailslot( root, &name, objattr->attributes, req->max_msgsize,
515 req->read_timeout, sd )))
517 reply->handle = alloc_handle( current->process, mailslot, req->access, objattr->attributes );
518 release_object( mailslot );
521 if (root) release_object( root );
525 /* set mailslot information */
526 DECL_HANDLER(set_mailslot_info)
528 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
530 if (mailslot)
532 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
533 mailslot->read_timeout = req->read_timeout;
534 reply->max_msgsize = mailslot->max_msgsize;
535 reply->read_timeout = mailslot->read_timeout;
536 release_object( mailslot );