Explicitly check for MSICONDITION_TRUE being returned from
[wine] / server / mailslot.c
1 /*
2  * Server-side mailslot management
3  *
4  * Copyright (C) 1998 Alexandre Julliard
5  * Copyright (C) 2005 Mike McCormack
6  *
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.
11  *
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.
16  *
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
20  *
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25 #include "wine/unicode.h"
26
27 #include <assert.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/time.h>
34 #include <sys/types.h>
35
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
38 #endif
39 #ifdef HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
41 #endif
42 #ifdef HAVE_SYS_FILIO_H
43 #include <sys/filio.h>
44 #endif
45 #include "windef.h"
46 #include "winternl.h"
47
48 #include "file.h"
49 #include "handle.h"
50 #include "thread.h"
51 #include "request.h"
52
53 struct mailslot
54 {
55     struct object       obj;
56     struct fd          *fd;
57     struct fd          *write_fd;
58     unsigned int        max_msgsize;
59     unsigned int        read_timeout;
60     struct list         writers;
61 };
62
63 /* mailslot functions */
64 static void mailslot_dump( struct object*, int );
65 static struct fd *mailslot_get_fd( struct object * );
66 static void mailslot_destroy( struct object * );
67
68 static const struct object_ops mailslot_ops =
69 {
70     sizeof(struct mailslot),   /* size */
71     mailslot_dump,             /* dump */
72     default_fd_add_queue,      /* add_queue */
73     default_fd_remove_queue,   /* remove_queue */
74     default_fd_signaled,       /* signaled */
75     no_satisfied,              /* satisfied */
76     no_signal,                 /* signal */
77     mailslot_get_fd,           /* get_fd */
78     no_close_handle,           /* close_handle */
79     mailslot_destroy           /* destroy */
80 };
81
82 static int mailslot_get_info( struct fd * );
83 static void mailslot_queue_async( struct fd *, void*, void*, void*, int, int );
84
85 static const struct fd_ops mailslot_fd_ops =
86 {
87     default_fd_get_poll_events, /* get_poll_events */
88     default_poll_event,         /* poll_event */
89     no_flush,                   /* flush */
90     mailslot_get_info,          /* get_file_info */
91     mailslot_queue_async,       /* queue_async */
92     default_fd_cancel_async     /* cancel_async */
93 };
94
95 struct mail_writer
96 {
97     struct object         obj;
98     struct mailslot      *mailslot;
99     struct list           entry;
100     int                   access;
101     int                   sharing;
102 };
103
104 static void mail_writer_dump( struct object *obj, int verbose );
105 static struct fd *mail_writer_get_fd( struct object *obj );
106 static void mail_writer_destroy( struct object *obj);
107
108 static const struct object_ops mail_writer_ops =
109 {
110     sizeof(struct mail_writer), /* size */
111     mail_writer_dump,           /* dump */
112     no_add_queue,               /* add_queue */
113     NULL,                       /* remove_queue */
114     NULL,                       /* signaled */
115     NULL,                       /* satisfied */
116     no_signal,                  /* signal */
117     mail_writer_get_fd,         /* get_fd */
118     no_close_handle,            /* close_handle */
119     mail_writer_destroy         /* destroy */
120 };
121
122 static int mail_writer_get_info( struct fd *fd );
123
124 static const struct fd_ops mail_writer_fd_ops =
125 {
126     NULL,                        /* get_poll_events */
127     NULL,                        /* poll_event */
128     no_flush,                    /* flush */
129     mail_writer_get_info,        /* get_file_info */
130     no_queue_async,              /* queue_async */
131     NULL                         /* cancel_async */
132 };
133
134 static void mailslot_destroy( struct object *obj)
135 {
136     struct mailslot *mailslot = (struct mailslot *) obj;
137
138     assert( mailslot->fd );
139     assert( mailslot->write_fd );
140
141     release_object( mailslot->fd );
142     release_object( mailslot->write_fd );
143 }
144
145 static void mailslot_dump( struct object *obj, int verbose )
146 {
147     struct mailslot *mailslot = (struct mailslot *) obj;
148
149     assert( obj->ops == &mailslot_ops );
150     fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%d\n",
151              mailslot->max_msgsize, mailslot->read_timeout );
152 }
153
154 static int mailslot_message_count(struct mailslot *mailslot)
155 {
156     struct pollfd pfd;
157
158     /* poll the socket to see if there's any messages */
159     pfd.fd = get_unix_fd( mailslot->fd );
160     pfd.events = POLLIN;
161     pfd.revents = 0;
162     return (poll( &pfd, 1, 0 ) == 1) ? 1 : 0;
163 }
164
165 static int mailslot_next_msg_size( struct mailslot *mailslot )
166 {
167     int size, fd;
168
169     size = 0;
170     fd = get_unix_fd( mailslot->fd );
171     ioctl( fd, FIONREAD, &size );
172     return size;
173 }
174
175 static int mailslot_get_info( struct fd *fd )
176 {
177     struct mailslot *mailslot = get_fd_user( fd );
178     assert( mailslot->obj.ops == &mailslot_ops );
179     return FD_FLAG_TIMEOUT | FD_FLAG_AVAILABLE;
180 }
181
182 static struct fd *mailslot_get_fd( struct object *obj )
183 {
184     struct mailslot *mailslot = (struct mailslot *) obj;
185
186     return (struct fd *)grab_object( mailslot->fd );
187 }
188
189 static void mailslot_queue_async( struct fd *fd, void *apc, void *user,
190                                   void *iosb, int type, int count )
191 {
192     struct mailslot *mailslot = get_fd_user( fd );
193     int *timeout = NULL;
194
195     assert(mailslot->obj.ops == &mailslot_ops);
196
197     if (type != ASYNC_TYPE_READ)
198     {
199         set_error(STATUS_INVALID_PARAMETER);
200         return;
201     }
202
203     if (list_empty( &mailslot->writers ) ||
204         !mailslot_message_count( mailslot ))
205     {
206         set_error(STATUS_IO_TIMEOUT);
207         return;
208     }
209
210     if (mailslot->read_timeout != MAILSLOT_WAIT_FOREVER)
211         timeout = &mailslot->read_timeout;
212
213     fd_queue_async_timeout( fd, apc, user, iosb, type, count, timeout );
214 }
215
216 static struct mailslot *create_mailslot( const WCHAR *name, size_t len, int max_msgsize,
217                                          int read_timeout )
218 {
219     struct mailslot *mailslot;
220     int fds[2];
221     static const WCHAR slot[] = {'m','a','i','l','s','l','o','t','\\',0};
222
223     if (( len <= strlenW( slot )) || strncmpiW( slot, name, strlenW( slot ) ))
224     {
225         set_error( STATUS_OBJECT_NAME_INVALID );
226         return NULL;
227     }
228
229     mailslot = create_named_object( sync_namespace, &mailslot_ops, name, len );
230     if (!mailslot)
231         return NULL;
232
233     /* it already exists - there can only be one mailslot to read from */
234     if (get_error() == STATUS_OBJECT_NAME_COLLISION)
235     {
236         release_object( mailslot );
237         return NULL;
238     }
239
240     mailslot->fd = NULL;
241     mailslot->write_fd = NULL;
242     mailslot->max_msgsize = max_msgsize;
243     mailslot->read_timeout = read_timeout;
244     list_init( &mailslot->writers );
245
246     if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
247     {
248         fcntl( fds[0], F_SETFL, O_NONBLOCK );
249         fcntl( fds[1], F_SETFL, O_NONBLOCK );
250         mailslot->fd = create_anonymous_fd( &mailslot_fd_ops,
251                                 fds[1], &mailslot->obj );
252         mailslot->write_fd = create_anonymous_fd( &mail_writer_fd_ops,
253                                 fds[0], &mailslot->obj );
254         if (mailslot->fd && mailslot->write_fd) return mailslot;
255     }
256     else file_set_error();
257
258     release_object( mailslot );
259     return NULL;
260 }
261
262 static struct mailslot *open_mailslot( const WCHAR *name, size_t len )
263 {
264     struct object *obj;
265
266     obj = find_object( sync_namespace, name, len );
267     if (obj)
268     {
269         if (obj->ops == &mailslot_ops)
270             return (struct mailslot *)obj;
271         release_object( obj );
272         set_error( STATUS_OBJECT_TYPE_MISMATCH );
273     }
274     else
275         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
276
277     return NULL;
278 }
279
280 static void mail_writer_dump( struct object *obj, int verbose )
281 {
282     fprintf( stderr, "Mailslot writer\n" );
283 }
284
285 static void mail_writer_destroy( struct object *obj)
286 {
287     struct mail_writer *writer = (struct mail_writer *) obj;
288
289     list_remove( &writer->entry );
290     release_object( writer->mailslot );
291 }
292
293 static int mail_writer_get_info( struct fd *fd )
294 {
295     return 0;
296 }
297
298 static struct fd *mail_writer_get_fd( struct object *obj )
299 {
300     struct mail_writer *writer = (struct mail_writer *) obj;
301
302     return (struct fd *)grab_object( writer->mailslot->write_fd );
303 }
304
305 /*
306  * Readers and writers cannot be mixed.
307  * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
308  */
309 static struct mail_writer *create_mail_writer( struct mailslot *mailslot, unsigned int access,
310                                                unsigned int sharing )
311 {
312     struct mail_writer *writer;
313
314     if (!list_empty( &mailslot->writers ))
315     {
316         writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
317
318         if (((access & GENERIC_WRITE) || (writer->access & GENERIC_WRITE)) &&
319            !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
320         {
321             set_error( STATUS_SHARING_VIOLATION );
322             return NULL;
323         }
324     }
325
326     writer = alloc_object( &mail_writer_ops );
327     if (!writer)
328         return NULL;
329
330     grab_object( mailslot );
331     writer->mailslot = mailslot;
332     writer->access = access;
333     writer->sharing = sharing;
334
335     list_add_head( &mailslot->writers, &writer->entry );
336
337     return writer;
338 }
339
340 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
341                                           unsigned int access )
342 {
343     struct object *obj;
344     obj = get_handle_obj( process, handle, access, &mailslot_ops );
345     return (struct mailslot *) obj;
346 }
347
348
349 /* create a mailslot */
350 DECL_HANDLER(create_mailslot)
351 {
352     struct mailslot *mailslot;
353
354     reply->handle = 0;
355     mailslot = create_mailslot( get_req_data(), get_req_data_size(),
356                                 req->max_msgsize, req->read_timeout );
357     if (mailslot)
358     {
359         reply->handle = alloc_handle( current->process, mailslot,
360                                       req->access, req->attributes & OBJ_INHERIT );
361         release_object( mailslot );
362     }
363 }
364
365
366 /* open an existing mailslot */
367 DECL_HANDLER(open_mailslot)
368 {
369     struct mailslot *mailslot;
370
371     reply->handle = 0;
372
373     if (!(req->sharing & FILE_SHARE_READ))
374     {
375         set_error( STATUS_SHARING_VIOLATION );
376         return;
377     }
378
379     mailslot = open_mailslot( get_req_data(), get_req_data_size() );
380     if (mailslot)
381     {
382         struct mail_writer *writer;
383
384         writer = create_mail_writer( mailslot, req->access, req->sharing );
385         if (writer)
386         {
387             reply->handle = alloc_handle( current->process, writer,
388                                           req->access, req->attributes & OBJ_INHERIT );
389             release_object( writer );
390         }
391         release_object( mailslot );
392     }
393     else
394         set_error( STATUS_NO_SUCH_FILE );
395 }
396
397
398 /* set mailslot information */
399 DECL_HANDLER(set_mailslot_info)
400 {
401     struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
402
403     if (mailslot)
404     {
405         if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
406             mailslot->read_timeout = req->read_timeout;
407         reply->max_msgsize = mailslot->max_msgsize;
408         reply->read_timeout = mailslot->read_timeout;
409         reply->msg_count = mailslot_message_count(mailslot);
410
411         /* get the size of the next message */
412         if (reply->msg_count)
413             reply->next_msgsize = mailslot_next_msg_size(mailslot);
414         else
415             reply->next_msgsize = MAILSLOT_NO_MESSAGE;
416
417         release_object( mailslot );
418     }
419 }