dbghelp: Implemented StackWalk64.
[wine/gsoc_dplay.git] / server / mailslot.c
blob2c04fe2b89521f5e016674d23aadb3950755130f
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 void mailslot_destroy( struct object * );
70 static const struct object_ops mailslot_ops =
72 sizeof(struct mailslot), /* size */
73 mailslot_dump, /* dump */
74 default_fd_add_queue, /* add_queue */
75 default_fd_remove_queue, /* remove_queue */
76 default_fd_signaled, /* signaled */
77 no_satisfied, /* satisfied */
78 no_signal, /* signal */
79 mailslot_get_fd, /* get_fd */
80 no_lookup_name, /* lookup_name */
81 no_close_handle, /* close_handle */
82 mailslot_destroy /* destroy */
85 static int mailslot_get_info( struct fd * );
86 static void mailslot_queue_async( struct fd *, void*, void*, void*, int, int );
88 static const struct fd_ops mailslot_fd_ops =
90 default_fd_get_poll_events, /* get_poll_events */
91 default_poll_event, /* poll_event */
92 no_flush, /* flush */
93 mailslot_get_info, /* get_file_info */
94 mailslot_queue_async, /* queue_async */
95 default_fd_cancel_async /* cancel_async */
98 struct mail_writer
100 struct object obj;
101 struct mailslot *mailslot;
102 struct list entry;
103 int access;
104 int sharing;
107 static void mail_writer_dump( struct object *obj, int verbose );
108 static struct fd *mail_writer_get_fd( struct object *obj );
109 static void mail_writer_destroy( struct object *obj);
111 static const struct object_ops mail_writer_ops =
113 sizeof(struct mail_writer), /* size */
114 mail_writer_dump, /* dump */
115 no_add_queue, /* add_queue */
116 NULL, /* remove_queue */
117 NULL, /* signaled */
118 NULL, /* satisfied */
119 no_signal, /* signal */
120 mail_writer_get_fd, /* get_fd */
121 no_lookup_name, /* lookup_name */
122 no_close_handle, /* close_handle */
123 mail_writer_destroy /* destroy */
126 static int mail_writer_get_info( struct fd *fd );
128 static const struct fd_ops mail_writer_fd_ops =
130 NULL, /* get_poll_events */
131 NULL, /* poll_event */
132 no_flush, /* flush */
133 mail_writer_get_info, /* get_file_info */
134 no_queue_async, /* queue_async */
135 NULL /* cancel_async */
138 static void mailslot_destroy( struct object *obj)
140 struct mailslot *mailslot = (struct mailslot *) obj;
142 assert( mailslot->fd );
143 assert( mailslot->write_fd );
145 release_object( mailslot->fd );
146 release_object( mailslot->write_fd );
149 static void mailslot_dump( struct object *obj, int verbose )
151 struct mailslot *mailslot = (struct mailslot *) obj;
153 assert( obj->ops == &mailslot_ops );
154 fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%d\n",
155 mailslot->max_msgsize, mailslot->read_timeout );
158 static int mailslot_message_count(struct mailslot *mailslot)
160 struct pollfd pfd;
162 /* poll the socket to see if there's any messages */
163 pfd.fd = get_unix_fd( mailslot->fd );
164 pfd.events = POLLIN;
165 pfd.revents = 0;
166 return (poll( &pfd, 1, 0 ) == 1) ? 1 : 0;
169 static int mailslot_next_msg_size( struct mailslot *mailslot )
171 int size, fd;
173 size = 0;
174 fd = get_unix_fd( mailslot->fd );
175 ioctl( fd, FIONREAD, &size );
176 return size;
179 static int mailslot_get_info( struct fd *fd )
181 struct mailslot *mailslot = get_fd_user( fd );
182 assert( mailslot->obj.ops == &mailslot_ops );
183 return FD_FLAG_TIMEOUT | FD_FLAG_AVAILABLE;
186 static struct fd *mailslot_get_fd( struct object *obj )
188 struct mailslot *mailslot = (struct mailslot *) obj;
190 return (struct fd *)grab_object( mailslot->fd );
193 static void mailslot_queue_async( struct fd *fd, void *apc, void *user,
194 void *iosb, int type, int count )
196 struct mailslot *mailslot = get_fd_user( fd );
197 int *timeout = NULL;
199 assert(mailslot->obj.ops == &mailslot_ops);
201 if (type != ASYNC_TYPE_READ)
203 set_error(STATUS_INVALID_PARAMETER);
204 return;
207 if (list_empty( &mailslot->writers ) ||
208 !mailslot_message_count( mailslot ))
210 set_error(STATUS_IO_TIMEOUT);
211 return;
214 if (mailslot->read_timeout != -1) timeout = &mailslot->read_timeout;
216 fd_queue_async_timeout( fd, apc, user, iosb, type, count, timeout );
219 static struct mailslot *create_mailslot( const struct unicode_str *name, unsigned int attr,
220 int max_msgsize, int read_timeout )
222 struct mailslot *mailslot;
223 int fds[2];
224 static const WCHAR slot[] = {'m','a','i','l','s','l','o','t','\\'};
226 if ((name->len <= sizeof(slot)) || strncmpiW( slot, name->str, sizeof(slot)/sizeof(WCHAR) ))
228 set_error( STATUS_OBJECT_NAME_INVALID );
229 return NULL;
232 mailslot = create_named_object( sync_namespace, &mailslot_ops, name, attr );
233 if (!mailslot)
234 return NULL;
236 /* it already exists - there can only be one mailslot to read from */
237 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
239 release_object( mailslot );
240 return NULL;
243 mailslot->fd = NULL;
244 mailslot->write_fd = NULL;
245 mailslot->max_msgsize = max_msgsize;
246 mailslot->read_timeout = read_timeout;
247 list_init( &mailslot->writers );
249 if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
251 fcntl( fds[0], F_SETFL, O_NONBLOCK );
252 fcntl( fds[1], F_SETFL, O_NONBLOCK );
253 mailslot->fd = create_anonymous_fd( &mailslot_fd_ops,
254 fds[1], &mailslot->obj );
255 mailslot->write_fd = create_anonymous_fd( &mail_writer_fd_ops,
256 fds[0], &mailslot->obj );
257 if (mailslot->fd && mailslot->write_fd) return mailslot;
259 else file_set_error();
261 release_object( mailslot );
262 return NULL;
265 static struct mailslot *open_mailslot( const struct unicode_str *name, unsigned int attr )
267 struct object *obj;
269 obj = find_object( sync_namespace, name, attr );
270 if (obj)
272 if (obj->ops == &mailslot_ops)
273 return (struct mailslot *)obj;
274 release_object( obj );
275 set_error( STATUS_OBJECT_TYPE_MISMATCH );
277 else
278 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
280 return NULL;
283 static void mail_writer_dump( struct object *obj, int verbose )
285 fprintf( stderr, "Mailslot writer\n" );
288 static void mail_writer_destroy( struct object *obj)
290 struct mail_writer *writer = (struct mail_writer *) obj;
292 list_remove( &writer->entry );
293 release_object( writer->mailslot );
296 static int mail_writer_get_info( struct fd *fd )
298 return 0;
301 static struct fd *mail_writer_get_fd( struct object *obj )
303 struct mail_writer *writer = (struct mail_writer *) obj;
305 return (struct fd *)grab_object( writer->mailslot->write_fd );
309 * Readers and writers cannot be mixed.
310 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
312 static struct mail_writer *create_mail_writer( struct mailslot *mailslot, unsigned int access,
313 unsigned int sharing )
315 struct mail_writer *writer;
317 if (!list_empty( &mailslot->writers ))
319 writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
321 if (((access & GENERIC_WRITE) || (writer->access & GENERIC_WRITE)) &&
322 !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
324 set_error( STATUS_SHARING_VIOLATION );
325 return NULL;
329 writer = alloc_object( &mail_writer_ops );
330 if (!writer)
331 return NULL;
333 grab_object( mailslot );
334 writer->mailslot = mailslot;
335 writer->access = access;
336 writer->sharing = sharing;
338 list_add_head( &mailslot->writers, &writer->entry );
340 return writer;
343 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
344 unsigned int access )
346 struct object *obj;
347 obj = get_handle_obj( process, handle, access, &mailslot_ops );
348 return (struct mailslot *) obj;
352 /* create a mailslot */
353 DECL_HANDLER(create_mailslot)
355 struct mailslot *mailslot;
356 struct unicode_str name;
358 reply->handle = 0;
359 get_req_unicode_str( &name );
360 mailslot = create_mailslot( &name, req->attributes, req->max_msgsize, req->read_timeout );
361 if (mailslot)
363 reply->handle = alloc_handle( current->process, mailslot,
364 req->access, req->attributes & OBJ_INHERIT );
365 release_object( mailslot );
370 /* open an existing mailslot */
371 DECL_HANDLER(open_mailslot)
373 struct mailslot *mailslot;
374 struct unicode_str name;
376 reply->handle = 0;
377 get_req_unicode_str( &name );
379 if (!(req->sharing & FILE_SHARE_READ))
381 set_error( STATUS_SHARING_VIOLATION );
382 return;
385 mailslot = open_mailslot( &name, req->attributes );
386 if (mailslot)
388 struct mail_writer *writer;
390 writer = create_mail_writer( mailslot, req->access, req->sharing );
391 if (writer)
393 reply->handle = alloc_handle( current->process, writer,
394 req->access, req->attributes & OBJ_INHERIT );
395 release_object( writer );
397 release_object( mailslot );
399 else
400 set_error( STATUS_NO_SUCH_FILE );
404 /* set mailslot information */
405 DECL_HANDLER(set_mailslot_info)
407 struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
409 if (mailslot)
411 if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
412 mailslot->read_timeout = req->read_timeout;
413 reply->max_msgsize = mailslot->max_msgsize;
414 reply->read_timeout = mailslot->read_timeout;
415 reply->msg_count = mailslot_message_count(mailslot);
417 /* get the size of the next message */
418 if (reply->msg_count)
419 reply->next_msgsize = mailslot_next_msg_size(mailslot);
420 else
421 reply->next_msgsize = MAILSLOT_NO_MESSAGE;
423 release_object( mailslot );