wininet: Add handling for remaining special errors to InternetErrorDlg.
[wine.git] / server / mailslot.c
blob1f4cb87d0fc4cb4ca79f657c8d1d63638ac898c2
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
28 #include <assert.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
39 #endif
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
42 #endif
43 #ifdef HAVE_SYS_FILIO_H
44 #include <sys/filio.h>
45 #endif
46 #include "windef.h"
47 #include "winternl.h"
49 #include "file.h"
50 #include "handle.h"
51 #include "thread.h"
52 #include "request.h"
54 struct mailslot
56 struct object obj;
57 struct fd *fd;
58 int write_fd;
59 unsigned int max_msgsize;
60 timeout_t read_timeout;
61 struct list writers;
64 /* mailslot functions */
65 static void mailslot_dump( struct object*, int );
66 static struct fd *mailslot_get_fd( struct object * );
67 static unsigned int mailslot_map_access( struct object *obj, unsigned int access );
68 static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent );
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 &file_type, /* type */
77 mailslot_dump, /* dump */
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 default_get_full_name, /* get_full_name */
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 no_kernel_obj_list, /* get_kernel_obj_list */
93 no_close_handle, /* close_handle */
94 mailslot_destroy /* destroy */
97 static enum server_fd_type mailslot_get_fd_type( struct fd *fd );
98 static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count );
100 static const struct fd_ops mailslot_fd_ops =
102 default_fd_get_poll_events, /* get_poll_events */
103 default_poll_event, /* poll_event */
104 mailslot_get_fd_type, /* get_fd_type */
105 no_fd_read, /* read */
106 no_fd_write, /* write */
107 no_fd_flush, /* flush */
108 default_fd_get_file_info, /* get_file_info */
109 no_fd_get_volume_info, /* get_volume_info */
110 default_fd_ioctl, /* ioctl */
111 default_fd_cancel_async, /* cancel_async */
112 mailslot_queue_async, /* queue_async */
113 default_fd_reselect_async /* reselect_async */
117 struct mail_writer
119 struct object obj;
120 struct fd *fd;
121 struct mailslot *mailslot;
122 struct list entry;
123 unsigned int access;
124 unsigned int sharing;
127 static void mail_writer_dump( struct object *obj, int verbose );
128 static struct fd *mail_writer_get_fd( struct object *obj );
129 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
130 static void mail_writer_destroy( struct object *obj);
132 static const struct object_ops mail_writer_ops =
134 sizeof(struct mail_writer), /* size */
135 &file_type, /* type */
136 mail_writer_dump, /* dump */
137 no_add_queue, /* add_queue */
138 NULL, /* remove_queue */
139 NULL, /* signaled */
140 NULL, /* satisfied */
141 no_signal, /* signal */
142 mail_writer_get_fd, /* get_fd */
143 mail_writer_map_access, /* map_access */
144 default_get_sd, /* get_sd */
145 default_set_sd, /* set_sd */
146 no_get_full_name, /* get_full_name */
147 no_lookup_name, /* lookup_name */
148 no_link_name, /* link_name */
149 NULL, /* unlink_name */
150 no_open_file, /* open_file */
151 no_kernel_obj_list, /* get_kernel_obj_list */
152 no_close_handle, /* close_handle */
153 mail_writer_destroy /* destroy */
156 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
158 static const struct fd_ops mail_writer_fd_ops =
160 default_fd_get_poll_events, /* get_poll_events */
161 default_poll_event, /* poll_event */
162 mail_writer_get_fd_type, /* get_fd_type */
163 no_fd_read, /* read */
164 no_fd_write, /* write */
165 no_fd_flush, /* flush */
166 default_fd_get_file_info, /* get_file_info */
167 no_fd_get_volume_info, /* get_volume_info */
168 default_fd_ioctl, /* ioctl */
169 default_fd_cancel_async, /* cancel_async */
170 default_fd_queue_async, /* queue_async */
171 default_fd_reselect_async /* reselect_async */
175 struct mailslot_device
177 struct object obj; /* object header */
178 struct namespace *mailslots; /* mailslot namespace */
181 struct mailslot_device_file
183 struct object obj; /* object header */
184 struct fd *fd; /* pseudo-fd for ioctls */
185 struct mailslot_device *device; /* mailslot device */
188 static void mailslot_device_dump( struct object *obj, int verbose );
189 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
190 unsigned int attr, struct object *root );
191 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
192 unsigned int sharing, unsigned int options );
193 static void mailslot_device_destroy( struct object *obj );
195 static const struct object_ops mailslot_device_ops =
197 sizeof(struct mailslot_device), /* size */
198 &device_type, /* type */
199 mailslot_device_dump, /* dump */
200 no_add_queue, /* add_queue */
201 NULL, /* remove_queue */
202 NULL, /* signaled */
203 no_satisfied, /* satisfied */
204 no_signal, /* signal */
205 no_get_fd, /* get_fd */
206 default_map_access, /* map_access */
207 default_get_sd, /* get_sd */
208 default_set_sd, /* set_sd */
209 default_get_full_name, /* get_full_name */
210 mailslot_device_lookup_name, /* lookup_name */
211 directory_link_name, /* link_name */
212 default_unlink_name, /* unlink_name */
213 mailslot_device_open_file, /* open_file */
214 no_kernel_obj_list, /* get_kernel_obj_list */
215 no_close_handle, /* close_handle */
216 mailslot_device_destroy /* destroy */
219 static void mailslot_device_file_dump( struct object *obj, int verbose );
220 static struct fd *mailslot_device_file_get_fd( struct object *obj );
221 static WCHAR *mailslot_device_file_get_full_name( struct object *obj, data_size_t *len );
222 static void mailslot_device_file_destroy( struct object *obj );
223 static enum server_fd_type mailslot_device_file_get_fd_type( struct fd *fd );
225 static const struct object_ops mailslot_device_file_ops =
227 sizeof(struct mailslot_device_file), /* size */
228 &file_type, /* type */
229 mailslot_device_file_dump, /* dump */
230 add_queue, /* add_queue */
231 remove_queue, /* remove_queue */
232 default_fd_signaled, /* signaled */
233 no_satisfied, /* satisfied */
234 no_signal, /* signal */
235 mailslot_device_file_get_fd, /* get_fd */
236 default_map_access, /* map_access */
237 default_get_sd, /* get_sd */
238 default_set_sd, /* set_sd */
239 mailslot_device_file_get_full_name, /* get_full_name */
240 no_lookup_name, /* lookup_name */
241 no_link_name, /* link_name */
242 NULL, /* unlink_name */
243 no_open_file, /* open_file */
244 no_kernel_obj_list, /* get_kernel_obj_list */
245 no_close_handle, /* close_handle */
246 mailslot_device_file_destroy /* destroy */
249 static const struct fd_ops mailslot_device_fd_ops =
251 default_fd_get_poll_events, /* get_poll_events */
252 default_poll_event, /* poll_event */
253 mailslot_device_file_get_fd_type, /* get_fd_type */
254 no_fd_read, /* read */
255 no_fd_write, /* write */
256 no_fd_flush, /* flush */
257 default_fd_get_file_info, /* get_file_info */
258 no_fd_get_volume_info, /* get_volume_info */
259 default_fd_ioctl, /* ioctl */
260 default_fd_cancel_async, /* cancel_async */
261 default_fd_queue_async, /* queue_async */
262 default_fd_reselect_async /* reselect_async */
265 static void mailslot_destroy( struct object *obj)
267 struct mailslot *mailslot = (struct mailslot *) obj;
269 assert( mailslot->fd );
271 if (mailslot->write_fd != -1)
273 shutdown( mailslot->write_fd, SHUT_RDWR );
274 close( mailslot->write_fd );
276 release_object( mailslot->fd );
279 static void mailslot_dump( struct object *obj, int verbose )
281 struct mailslot *mailslot = (struct mailslot *) obj;
283 assert( obj->ops == &mailslot_ops );
284 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
285 mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
288 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
290 return FD_TYPE_MAILSLOT;
293 static struct fd *mailslot_get_fd( struct object *obj )
295 struct mailslot *mailslot = (struct mailslot *) obj;
297 return (struct fd *)grab_object( mailslot->fd );
300 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
302 /* mailslots can only be read */
303 return default_map_access( obj, access ) & FILE_GENERIC_READ;
306 static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent )
308 struct mailslot_device *dev = (struct mailslot_device *)parent;
310 if (parent->ops != &mailslot_device_ops)
312 set_error( STATUS_OBJECT_NAME_INVALID );
313 return 0;
315 namespace_add( dev->mailslots, name );
316 name->parent = grab_object( parent );
317 return 1;
320 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
321 unsigned int sharing, unsigned int options )
323 struct mailslot *mailslot = (struct mailslot *)obj;
324 struct mail_writer *writer;
325 int unix_fd;
327 if (!(sharing & FILE_SHARE_READ))
329 set_error( STATUS_SHARING_VIOLATION );
330 return NULL;
333 if (!list_empty( &mailslot->writers ))
335 /* Readers and writers cannot be mixed.
336 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
338 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
340 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
341 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
343 set_error( STATUS_SHARING_VIOLATION );
344 return NULL;
348 if ((unix_fd = dup( mailslot->write_fd )) == -1)
350 file_set_error();
351 return NULL;
353 if (!(writer = alloc_object( &mail_writer_ops )))
355 close( unix_fd );
356 return NULL;
358 grab_object( mailslot );
359 writer->mailslot = mailslot;
360 writer->access = mail_writer_map_access( &writer->obj, access );
361 writer->sharing = sharing;
362 list_add_head( &mailslot->writers, &writer->entry );
364 if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
366 release_object( writer );
367 return NULL;
369 allow_fd_caching( writer->fd );
370 return &writer->obj;
373 static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count )
375 struct mailslot *mailslot = get_fd_user( fd );
377 assert(mailslot->obj.ops == &mailslot_ops);
379 fd_queue_async( fd, async, type );
380 async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
381 STATUS_IO_TIMEOUT );
382 set_error( STATUS_PENDING );
385 static void mailslot_device_dump( struct object *obj, int verbose )
387 fputs( "Mailslot device\n", stderr );
390 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
391 unsigned int attr, struct object *root )
393 struct mailslot_device *device = (struct mailslot_device*)obj;
394 struct object *found;
396 assert( obj->ops == &mailslot_device_ops );
398 if (!name) return NULL; /* open the device itself */
400 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
401 name->len = 0;
403 return found;
406 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
407 unsigned int sharing, unsigned int options )
409 struct mailslot_device_file *file;
411 if (!(file = alloc_object( &mailslot_device_file_ops ))) return NULL;
412 file->device = (struct mailslot_device *)grab_object( obj );
413 if (!(file->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, obj, options )))
415 release_object( file );
416 return NULL;
418 allow_fd_caching( file->fd );
419 return &file->obj;
422 static void mailslot_device_destroy( struct object *obj )
424 struct mailslot_device *device = (struct mailslot_device*)obj;
425 assert( obj->ops == &mailslot_device_ops );
426 free( device->mailslots );
429 struct object *create_mailslot_device( struct object *root, const struct unicode_str *name,
430 unsigned int attr, const struct security_descriptor *sd )
432 struct mailslot_device *dev;
434 if ((dev = create_named_object( root, &mailslot_device_ops, name, attr, sd )) &&
435 get_error() != STATUS_OBJECT_NAME_EXISTS)
437 dev->mailslots = NULL;
438 if (!(dev->mailslots = create_namespace( 7 )))
440 release_object( dev );
441 dev = NULL;
444 return &dev->obj;
447 static void mailslot_device_file_dump( struct object *obj, int verbose )
449 struct mailslot_device_file *file = (struct mailslot_device_file *)obj;
451 fprintf( stderr, "File on mailslot device %p\n", file->device );
454 static struct fd *mailslot_device_file_get_fd( struct object *obj )
456 struct mailslot_device_file *file = (struct mailslot_device_file *)obj;
457 return (struct fd *)grab_object( file->fd );
460 static WCHAR *mailslot_device_file_get_full_name( struct object *obj, data_size_t *len )
462 struct mailslot_device_file *file = (struct mailslot_device_file *)obj;
463 return file->device->obj.ops->get_full_name( &file->device->obj, len );
466 static void mailslot_device_file_destroy( struct object *obj )
468 struct mailslot_device_file *file = (struct mailslot_device_file*)obj;
469 assert( obj->ops == &mailslot_device_file_ops );
470 if (file->fd) release_object( file->fd );
471 release_object( file->device );
474 static enum server_fd_type mailslot_device_file_get_fd_type( struct fd *fd )
476 return FD_TYPE_DEVICE;
479 static struct mailslot *create_mailslot( struct object *root,
480 const struct unicode_str *name, unsigned int attr,
481 int max_msgsize, timeout_t read_timeout,
482 const struct security_descriptor *sd )
484 struct mailslot *mailslot;
485 int fds[2];
487 if (!(mailslot = create_named_object( root, &mailslot_ops, name, attr, sd ))) return NULL;
489 mailslot->fd = NULL;
490 mailslot->write_fd = -1;
491 mailslot->max_msgsize = max_msgsize;
492 mailslot->read_timeout = read_timeout;
493 list_init( &mailslot->writers );
495 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
497 fcntl( fds[0], F_SETFL, O_NONBLOCK );
498 fcntl( fds[1], F_SETFL, O_NONBLOCK );
499 shutdown( fds[0], SHUT_RD );
500 mailslot->write_fd = fds[0];
501 if ((mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
502 FILE_SYNCHRONOUS_IO_NONALERT )))
504 allow_fd_caching( mailslot->fd );
505 return mailslot;
508 else file_set_error();
510 release_object( mailslot );
511 return NULL;
514 static void mail_writer_dump( struct object *obj, int verbose )
516 fprintf( stderr, "Mailslot writer\n" );
519 static void mail_writer_destroy( struct object *obj)
521 struct mail_writer *writer = (struct mail_writer *) obj;
523 if (writer->fd) release_object( writer->fd );
524 list_remove( &writer->entry );
525 release_object( writer->mailslot );
528 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
530 return FD_TYPE_MAILSLOT;
533 static struct fd *mail_writer_get_fd( struct object *obj )
535 struct mail_writer *writer = (struct mail_writer *) obj;
536 return (struct fd *)grab_object( writer->fd );
539 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
541 /* mailslot writers can only get write access */
542 return default_map_access( obj, access ) & FILE_GENERIC_WRITE;
545 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
546 unsigned int access )
548 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
552 /* create a mailslot */
553 DECL_HANDLER(create_mailslot)
555 struct mailslot *mailslot;
556 struct unicode_str name;
557 struct object *root;
558 const struct security_descriptor *sd;
559 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
561 if (!objattr) return;
563 if (!name.len) /* mailslots need a root directory even without a name */
565 if (!objattr->rootdir)
567 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
568 return;
570 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
573 if ((mailslot = create_mailslot( root, &name, objattr->attributes, req->max_msgsize,
574 req->read_timeout, sd )))
576 reply->handle = alloc_handle( current->process, mailslot, req->access, objattr->attributes );
577 release_object( mailslot );
580 if (root) release_object( root );
584 /* set mailslot information */
585 DECL_HANDLER(set_mailslot_info)
587 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
589 if (mailslot)
591 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
592 mailslot->read_timeout = req->read_timeout;
593 reply->max_msgsize = mailslot->max_msgsize;
594 reply->read_timeout = mailslot->read_timeout;
595 release_object( mailslot );