wined3d: Properly handle back-buffers in context_get_rt_size().
[wine.git] / server / mailslot.c
blob643577cd72c863412da0551a8d9c2612159f26fb
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, struct async *async, 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 */
113 struct mail_writer
115 struct object obj;
116 struct fd *fd;
117 struct mailslot *mailslot;
118 struct list entry;
119 unsigned int access;
120 unsigned int sharing;
123 static void mail_writer_dump( struct object *obj, int verbose );
124 static struct fd *mail_writer_get_fd( struct object *obj );
125 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
126 static void mail_writer_destroy( struct object *obj);
128 static const struct object_ops mail_writer_ops =
130 sizeof(struct mail_writer), /* size */
131 mail_writer_dump, /* dump */
132 no_get_type, /* get_type */
133 no_add_queue, /* add_queue */
134 NULL, /* remove_queue */
135 NULL, /* signaled */
136 NULL, /* satisfied */
137 no_signal, /* signal */
138 mail_writer_get_fd, /* get_fd */
139 mail_writer_map_access, /* map_access */
140 default_get_sd, /* get_sd */
141 default_set_sd, /* set_sd */
142 no_lookup_name, /* lookup_name */
143 no_link_name, /* link_name */
144 NULL, /* unlink_name */
145 no_open_file, /* open_file */
146 fd_close_handle, /* close_handle */
147 mail_writer_destroy /* destroy */
150 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
152 static const struct fd_ops mail_writer_fd_ops =
154 default_fd_get_poll_events, /* get_poll_events */
155 default_poll_event, /* poll_event */
156 mail_writer_get_fd_type, /* get_fd_type */
157 no_fd_read, /* read */
158 no_fd_write, /* write */
159 no_fd_flush, /* flush */
160 default_fd_ioctl, /* ioctl */
161 default_fd_queue_async, /* queue_async */
162 default_fd_reselect_async /* reselect_async */
166 struct mailslot_device
168 struct object obj; /* object header */
169 struct fd *fd; /* pseudo-fd for ioctls */
170 struct namespace *mailslots; /* mailslot namespace */
173 static void mailslot_device_dump( struct object *obj, int verbose );
174 static struct object_type *mailslot_device_get_type( struct object *obj );
175 static struct fd *mailslot_device_get_fd( struct object *obj );
176 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
177 unsigned int attr );
178 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
179 unsigned int sharing, unsigned int options );
180 static void mailslot_device_destroy( struct object *obj );
181 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
183 static const struct object_ops mailslot_device_ops =
185 sizeof(struct mailslot_device), /* size */
186 mailslot_device_dump, /* dump */
187 mailslot_device_get_type, /* get_type */
188 no_add_queue, /* add_queue */
189 NULL, /* remove_queue */
190 NULL, /* signaled */
191 no_satisfied, /* satisfied */
192 no_signal, /* signal */
193 mailslot_device_get_fd, /* get_fd */
194 no_map_access, /* map_access */
195 default_get_sd, /* get_sd */
196 default_set_sd, /* set_sd */
197 mailslot_device_lookup_name, /* lookup_name */
198 directory_link_name, /* link_name */
199 default_unlink_name, /* unlink_name */
200 mailslot_device_open_file, /* open_file */
201 fd_close_handle, /* close_handle */
202 mailslot_device_destroy /* destroy */
205 static const struct fd_ops mailslot_device_fd_ops =
207 default_fd_get_poll_events, /* get_poll_events */
208 default_poll_event, /* poll_event */
209 mailslot_device_get_fd_type, /* get_fd_type */
210 no_fd_read, /* read */
211 no_fd_write, /* write */
212 no_fd_flush, /* flush */
213 default_fd_ioctl, /* ioctl */
214 default_fd_queue_async, /* queue_async */
215 default_fd_reselect_async /* reselect_async */
218 static void mailslot_destroy( struct object *obj)
220 struct mailslot *mailslot = (struct mailslot *) obj;
222 assert( mailslot->fd );
224 if (mailslot->write_fd != -1)
226 shutdown( mailslot->write_fd, SHUT_RDWR );
227 close( mailslot->write_fd );
229 release_object( mailslot->fd );
232 static void mailslot_dump( struct object *obj, int verbose )
234 struct mailslot *mailslot = (struct mailslot *) obj;
236 assert( obj->ops == &mailslot_ops );
237 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
238 mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
241 static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
243 return FD_TYPE_MAILSLOT;
246 static struct fd *mailslot_get_fd( struct object *obj )
248 struct mailslot *mailslot = (struct mailslot *) obj;
250 return (struct fd *)grab_object( mailslot->fd );
253 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
255 /* mailslots can only be read */
256 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
257 if (access & GENERIC_ALL) access |= FILE_GENERIC_READ;
258 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
261 static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent )
263 struct mailslot_device *dev = (struct mailslot_device *)parent;
265 if (parent->ops != &mailslot_device_ops)
267 set_error( STATUS_OBJECT_NAME_INVALID );
268 return 0;
270 namespace_add( dev->mailslots, name );
271 name->parent = grab_object( parent );
272 return 1;
275 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
276 unsigned int sharing, unsigned int options )
278 struct mailslot *mailslot = (struct mailslot *)obj;
279 struct mail_writer *writer;
280 int unix_fd;
282 if (!(sharing & FILE_SHARE_READ))
284 set_error( STATUS_SHARING_VIOLATION );
285 return NULL;
288 if (!list_empty( &mailslot->writers ))
290 /* Readers and writers cannot be mixed.
291 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
293 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
295 if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
296 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
298 set_error( STATUS_SHARING_VIOLATION );
299 return NULL;
303 if ((unix_fd = dup( mailslot->write_fd )) == -1)
305 file_set_error();
306 return NULL;
308 if (!(writer = alloc_object( &mail_writer_ops )))
310 close( unix_fd );
311 return NULL;
313 grab_object( mailslot );
314 writer->mailslot = mailslot;
315 writer->access = mail_writer_map_access( &writer->obj, access );
316 writer->sharing = sharing;
317 list_add_head( &mailslot->writers, &writer->entry );
319 if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
321 release_object( writer );
322 return NULL;
324 allow_fd_caching( writer->fd );
325 return &writer->obj;
328 static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count )
330 struct mailslot *mailslot = get_fd_user( fd );
332 assert(mailslot->obj.ops == &mailslot_ops);
334 if (fd_queue_async( fd, async, type ))
336 async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
337 STATUS_IO_TIMEOUT );
338 set_error( STATUS_PENDING );
342 static void mailslot_device_dump( struct object *obj, int verbose )
344 fputs( "Mailslot device\n", stderr );
347 static struct object_type *mailslot_device_get_type( struct object *obj )
349 static const WCHAR name[] = {'D','e','v','i','c','e'};
350 static const struct unicode_str str = { name, sizeof(name) };
351 return get_object_type( &str );
354 static struct fd *mailslot_device_get_fd( struct object *obj )
356 struct mailslot_device *device = (struct mailslot_device *)obj;
357 return (struct fd *)grab_object( device->fd );
360 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
361 unsigned int attr )
363 struct mailslot_device *device = (struct mailslot_device*)obj;
364 struct object *found;
366 assert( obj->ops == &mailslot_device_ops );
368 if (!name) return NULL; /* open the device itself */
370 if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
371 name->len = 0;
373 return found;
376 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
377 unsigned int sharing, unsigned int options )
379 return grab_object( obj );
382 static void mailslot_device_destroy( struct object *obj )
384 struct mailslot_device *device = (struct mailslot_device*)obj;
385 assert( obj->ops == &mailslot_device_ops );
386 if (device->fd) release_object( device->fd );
387 free( device->mailslots );
390 static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
392 return FD_TYPE_DEVICE;
395 struct object *create_mailslot_device( struct object *root, const struct unicode_str *name )
397 struct mailslot_device *dev;
399 if ((dev = create_named_object( root, &mailslot_device_ops, name, 0, NULL )) &&
400 get_error() != STATUS_OBJECT_NAME_EXISTS)
402 dev->mailslots = NULL;
403 if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj, 0 )) ||
404 !(dev->mailslots = create_namespace( 7 )))
406 release_object( dev );
407 dev = NULL;
410 return &dev->obj;
413 static struct mailslot *create_mailslot( struct object *root,
414 const struct unicode_str *name, unsigned int attr,
415 int max_msgsize, timeout_t read_timeout,
416 const struct security_descriptor *sd )
418 struct mailslot *mailslot;
419 int fds[2];
421 if (!(mailslot = create_named_object( root, &mailslot_ops, name, attr, sd ))) return NULL;
423 mailslot->fd = NULL;
424 mailslot->write_fd = -1;
425 mailslot->max_msgsize = max_msgsize;
426 mailslot->read_timeout = read_timeout;
427 list_init( &mailslot->writers );
429 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
431 fcntl( fds[0], F_SETFL, O_NONBLOCK );
432 fcntl( fds[1], F_SETFL, O_NONBLOCK );
433 shutdown( fds[0], SHUT_RD );
434 mailslot->write_fd = fds[0];
435 if ((mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
436 FILE_SYNCHRONOUS_IO_NONALERT )))
438 allow_fd_caching( mailslot->fd );
439 return mailslot;
442 else file_set_error();
444 release_object( mailslot );
445 return NULL;
448 static void mail_writer_dump( struct object *obj, int verbose )
450 fprintf( stderr, "Mailslot writer\n" );
453 static void mail_writer_destroy( struct object *obj)
455 struct mail_writer *writer = (struct mail_writer *) obj;
457 if (writer->fd) release_object( writer->fd );
458 list_remove( &writer->entry );
459 release_object( writer->mailslot );
462 static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
464 return FD_TYPE_MAILSLOT;
467 static struct fd *mail_writer_get_fd( struct object *obj )
469 struct mail_writer *writer = (struct mail_writer *) obj;
470 return (struct fd *)grab_object( writer->fd );
473 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
475 /* mailslot writers can only get write access */
476 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
477 if (access & GENERIC_ALL) access |= FILE_GENERIC_WRITE;
478 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
481 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
482 unsigned int access )
484 return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
488 /* create a mailslot */
489 DECL_HANDLER(create_mailslot)
491 struct mailslot *mailslot;
492 struct unicode_str name;
493 struct object *root;
494 const struct security_descriptor *sd;
495 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
497 if (!objattr) return;
499 if (!name.len) /* mailslots need a root directory even without a name */
501 if (!objattr->rootdir)
503 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
504 return;
506 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
509 if ((mailslot = create_mailslot( root, &name, objattr->attributes, req->max_msgsize,
510 req->read_timeout, sd )))
512 reply->handle = alloc_handle( current->process, mailslot, req->access, objattr->attributes );
513 release_object( mailslot );
516 if (root) release_object( root );
520 /* set mailslot information */
521 DECL_HANDLER(set_mailslot_info)
523 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
525 if (mailslot)
527 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
528 mailslot->read_timeout = req->read_timeout;
529 reply->max_msgsize = mailslot->max_msgsize;
530 reply->read_timeout = mailslot->read_timeout;
531 release_object( mailslot );