Use a better location than HKCU\Wine for saving the temporary
[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 "winbase.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     struct list         read_q;
62 };
63
64 /* mailslot functions */
65 static void mailslot_dump( struct object*, int );
66 static struct fd *mailslot_get_fd( struct object * );
67 static void mailslot_destroy( struct object * );
68
69 static const struct object_ops mailslot_ops =
70 {
71     sizeof(struct mailslot),   /* size */
72     mailslot_dump,             /* dump */
73     default_fd_add_queue,      /* add_queue */
74     default_fd_remove_queue,   /* remove_queue */
75     default_fd_signaled,       /* signaled */
76     no_satisfied,              /* satisfied */
77     no_signal,                 /* signal */
78     mailslot_get_fd,           /* get_fd */
79     no_close_handle,           /* close_handle */
80     mailslot_destroy           /* destroy */
81 };
82
83 static int mailslot_get_poll_events( struct fd * );
84 static void mailslot_poll_event( struct fd *, int );
85 static int mailslot_get_info( struct fd * );
86 static void mailslot_queue_async( struct fd *, void*, void*, void*, int, int );
87 static void mailslot_cancel_async( struct fd * );
88
89 static const struct fd_ops mailslot_fd_ops =
90 {
91     mailslot_get_poll_events,  /* get_poll_events */
92     mailslot_poll_event,       /* poll_event */
93     no_flush,                  /* flush */
94     mailslot_get_info,         /* get_file_info */
95     mailslot_queue_async,      /* queue_async */
96     mailslot_cancel_async      /* cancel_async */
97 };
98
99 struct mail_writer
100 {
101     struct object         obj;
102     struct mailslot      *mailslot;
103     struct list           entry;
104     int                   access;
105     int                   sharing;
106 };
107
108 static void mail_writer_dump( struct object *obj, int verbose );
109 static struct fd *mail_writer_get_fd( struct object *obj );
110 static void mail_writer_destroy( struct object *obj);
111
112 static const struct object_ops mail_writer_ops =
113 {
114     sizeof(struct mail_writer), /* size */
115     mail_writer_dump,           /* dump */
116     no_add_queue,               /* add_queue */
117     NULL,                       /* remove_queue */
118     NULL,                       /* signaled */
119     NULL,                       /* satisfied */
120     no_signal,                  /* signal */
121     mail_writer_get_fd,         /* get_fd */
122     no_close_handle,            /* close_handle */
123     mail_writer_destroy         /* destroy */
124 };
125
126 static int mail_writer_get_info( struct fd *fd );
127
128 static const struct fd_ops mail_writer_fd_ops =
129 {
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 */
136 };
137
138 static void mailslot_destroy( struct object *obj)
139 {
140     struct mailslot *mailslot = (struct mailslot *) obj;
141
142     assert( mailslot->fd );
143     assert( mailslot->write_fd );
144
145     async_terminate_queue( &mailslot->read_q, STATUS_CANCELLED );
146
147     release_object( mailslot->fd );
148     release_object( mailslot->write_fd );
149 }
150
151 static void mailslot_dump( struct object *obj, int verbose )
152 {
153     struct mailslot *mailslot = (struct mailslot *) obj;
154
155     assert( obj->ops == &mailslot_ops );
156     fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%d\n",
157              mailslot->max_msgsize, mailslot->read_timeout );
158 }
159
160 static int mailslot_message_count(struct mailslot *mailslot)
161 {
162     struct pollfd pfd;
163
164     /* poll the socket to see if there's any messages */
165     pfd.fd = get_unix_fd( mailslot->fd );
166     pfd.events = POLLIN;
167     pfd.revents = 0;
168     return (poll( &pfd, 1, 0 ) == 1) ? 1 : 0;
169 }
170
171 static int mailslot_next_msg_size( struct mailslot *mailslot )
172 {
173     int size, fd;
174
175     size = 0;
176     fd = get_unix_fd( mailslot->fd );
177     ioctl( fd, FIONREAD, &size );
178     return size;
179 }
180
181 static int mailslot_get_info( struct fd *fd )
182 {
183     struct mailslot *mailslot = get_fd_user( fd );
184     assert( mailslot->obj.ops == &mailslot_ops );
185     return FD_FLAG_TIMEOUT | FD_FLAG_AVAILABLE;
186 }
187
188 static struct fd *mailslot_get_fd( struct object *obj )
189 {
190     struct mailslot *mailslot = (struct mailslot *) obj;
191
192     return (struct fd *)grab_object( mailslot->fd );
193 }
194
195 static int mailslot_get_poll_events( struct fd *fd )
196 {
197     struct mailslot *mailslot = get_fd_user( fd );
198     int events = 0;
199     assert( mailslot->obj.ops == &mailslot_ops );
200
201     if (!list_empty( &mailslot->read_q ))
202         events |= POLLIN;
203
204     return events;
205 }
206
207 static void mailslot_poll_event( struct fd *fd, int event )
208 {
209     struct mailslot *mailslot = get_fd_user( fd );
210
211     if (!list_empty( &mailslot->read_q ) && (POLLIN & event))
212         async_terminate_head( &mailslot->read_q, STATUS_ALERTED );
213
214     set_fd_events( fd, mailslot_get_poll_events(fd) );
215 }
216
217 static void mailslot_queue_async( struct fd *fd, void *apc, void *user,
218                                   void *iosb, int type, int count )
219 {
220     struct mailslot *mailslot = get_fd_user( fd );
221     int events, *ptimeout = NULL;
222
223     assert(mailslot->obj.ops == &mailslot_ops);
224
225     if (type != ASYNC_TYPE_READ)
226     {
227         set_error(STATUS_INVALID_PARAMETER);
228         return;
229     }
230
231     if (list_empty( &mailslot->writers ) ||
232         !mailslot_message_count( mailslot ))
233     {
234         set_error(STATUS_IO_TIMEOUT);
235         return;
236     }
237
238     if (mailslot->read_timeout != MAILSLOT_WAIT_FOREVER)
239         ptimeout = &mailslot->read_timeout;
240
241     if (!create_async( current, ptimeout, &mailslot->read_q, apc, user, iosb ))
242         return;
243
244     /* Check if the new pending request can be served immediately */
245     events = check_fd_events( fd, mailslot_get_poll_events( fd ) );
246     if (events)
247     {
248         mailslot_poll_event( fd, events );
249         return;
250     }
251
252     set_fd_events( fd, mailslot_get_poll_events( fd ));
253 }
254
255 static void mailslot_cancel_async( struct fd *fd )
256 {
257     struct mailslot *mailslot = get_fd_user( fd );
258
259     assert(mailslot->obj.ops == &mailslot_ops);
260     async_terminate_queue( &mailslot->read_q, STATUS_CANCELLED );
261 }
262
263 static struct mailslot *create_mailslot( const WCHAR *name, size_t len, int max_msgsize,
264                                          int read_timeout )
265 {
266     struct mailslot *mailslot;
267     int fds[2];
268     static const WCHAR slot[] = {'m','a','i','l','s','l','o','t','\\',0};
269
270     if (( len <= strlenW( slot )) || strncmpiW( slot, name, strlenW( slot ) ))
271     {
272         set_error( STATUS_OBJECT_NAME_INVALID );
273         return NULL;
274     }
275
276     mailslot = create_named_object( sync_namespace, &mailslot_ops, name, len );
277     if (!mailslot)
278         return NULL;
279
280     /* it already exists - there can only be one mailslot to read from */
281     if (get_error() == STATUS_OBJECT_NAME_COLLISION)
282     {
283         release_object( mailslot );
284         return NULL;
285     }
286
287     mailslot->fd = NULL;
288     mailslot->write_fd = NULL;
289     mailslot->max_msgsize = max_msgsize;
290     mailslot->read_timeout = read_timeout;
291     list_init( &mailslot->writers );
292     list_init( &mailslot->read_q );
293
294     if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
295     {
296         fcntl( fds[0], F_SETFL, O_NONBLOCK );
297         fcntl( fds[1], F_SETFL, O_NONBLOCK );
298         mailslot->fd = create_anonymous_fd( &mailslot_fd_ops,
299                                 fds[1], &mailslot->obj );
300         mailslot->write_fd = create_anonymous_fd( &mail_writer_fd_ops,
301                                 fds[0], &mailslot->obj );
302         if (mailslot->fd && mailslot->write_fd) return mailslot;
303     }
304     else file_set_error();
305
306     release_object( mailslot );
307     return NULL;
308 }
309
310 static struct mailslot *open_mailslot( const WCHAR *name, size_t len )
311 {
312     struct object *obj;
313
314     obj = find_object( sync_namespace, name, len );
315     if (obj)
316     {
317         if (obj->ops == &mailslot_ops)
318             return (struct mailslot *)obj;
319         release_object( obj );
320         set_error( STATUS_OBJECT_TYPE_MISMATCH );
321     }
322     else
323         set_error( STATUS_OBJECT_NAME_NOT_FOUND );
324
325     return NULL;
326 }
327
328 static void mail_writer_dump( struct object *obj, int verbose )
329 {
330     fprintf( stderr, "Mailslot writer\n" );
331 }
332
333 static void mail_writer_destroy( struct object *obj)
334 {
335     struct mail_writer *writer = (struct mail_writer *) obj;
336
337     list_remove( &writer->entry );
338     release_object( writer->mailslot );
339 }
340
341 static int mail_writer_get_info( struct fd *fd )
342 {
343     return 0;
344 }
345
346 static struct fd *mail_writer_get_fd( struct object *obj )
347 {
348     struct mail_writer *writer = (struct mail_writer *) obj;
349
350     return (struct fd *)grab_object( writer->mailslot->write_fd );
351 }
352
353 /*
354  * Readers and writers cannot be mixed.
355  * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
356  */
357 static struct mail_writer *create_mail_writer( struct mailslot *mailslot, unsigned int access,
358                                                unsigned int sharing )
359 {
360     struct mail_writer *writer;
361
362     if (!list_empty( &mailslot->writers ))
363     {
364         writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
365
366         if (((access & GENERIC_WRITE) || (writer->access & GENERIC_WRITE)) &&
367            !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
368         {
369             set_error( STATUS_SHARING_VIOLATION );
370             return NULL;
371         }
372     }
373
374     writer = alloc_object( &mail_writer_ops );
375     if (!writer)
376         return NULL;
377
378     grab_object( mailslot );
379     writer->mailslot = mailslot;
380     writer->access = access;
381     writer->sharing = sharing;
382
383     list_add_head( &mailslot->writers, &writer->entry );
384
385     return writer;
386 }
387
388 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
389                                           unsigned int access )
390 {
391     struct object *obj;
392     obj = get_handle_obj( process, handle, access, &mailslot_ops );
393     return (struct mailslot *) obj;
394 }
395
396
397 /* create a mailslot */
398 DECL_HANDLER(create_mailslot)
399 {
400     struct mailslot *mailslot;
401
402     reply->handle = 0;
403     mailslot = create_mailslot( get_req_data(), get_req_data_size(),
404                                 req->max_msgsize, req->read_timeout );
405     if (mailslot)
406     {
407         reply->handle = alloc_handle( current->process, mailslot,
408                                       GENERIC_READ, req->inherit );
409         release_object( mailslot );
410     }
411 }
412
413
414 /* open an existing mailslot */
415 DECL_HANDLER(open_mailslot)
416 {
417     struct mailslot *mailslot;
418
419     reply->handle = 0;
420
421     if (!(req->sharing & FILE_SHARE_READ))
422     {
423         set_error( STATUS_SHARING_VIOLATION );
424         return;
425     }
426
427     mailslot = open_mailslot( get_req_data(), get_req_data_size() );
428     if (mailslot)
429     {
430         struct mail_writer *writer;
431
432         writer = create_mail_writer( mailslot, req->access, req->sharing );
433         if (writer)
434         {
435             reply->handle = alloc_handle( current->process, writer,
436                                           req->access, req->inherit );
437             release_object( writer );
438         }
439         release_object( mailslot );
440     }
441     else
442         set_error( STATUS_NO_SUCH_FILE );
443 }
444
445
446 /* set mailslot information */
447 DECL_HANDLER(set_mailslot_info)
448 {
449     struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
450
451     if (mailslot)
452     {
453         if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
454             mailslot->read_timeout = req->read_timeout;
455         reply->max_msgsize = mailslot->max_msgsize;
456         reply->read_timeout = mailslot->read_timeout;
457         reply->msg_count = mailslot_message_count(mailslot);
458
459         /* get the size of the next message */
460         if (reply->msg_count)
461             reply->next_msgsize = mailslot_next_msg_size(mailslot);
462         else
463             reply->next_msgsize = MAILSLOT_NO_MESSAGE;
464
465         release_object( mailslot );
466     }
467 }