comctl32: Constify some variables.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "wine/unicode.h"
28
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>
37
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"
49
50 #include "file.h"
51 #include "handle.h"
52 #include "thread.h"
53 #include "request.h"
54
55 struct mailslot
56 {
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;
63 };
64
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 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 * );
72
73 static const struct object_ops mailslot_ops =
74 {
75     sizeof(struct mailslot),   /* size */
76     mailslot_dump,             /* dump */
77     default_fd_add_queue,      /* add_queue */
78     default_fd_remove_queue,   /* remove_queue */
79     default_fd_signaled,       /* signaled */
80     no_satisfied,              /* satisfied */
81     no_signal,                 /* signal */
82     mailslot_get_fd,           /* get_fd */
83     mailslot_map_access,       /* map_access */
84     no_lookup_name,            /* lookup_name */
85     mailslot_open_file,        /* open_file */
86     fd_close_handle,           /* close_handle */
87     mailslot_destroy           /* destroy */
88 };
89
90 static enum server_fd_type mailslot_get_info( struct fd *fd, int *flags );
91 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
92
93 static const struct fd_ops mailslot_fd_ops =
94 {
95     default_fd_get_poll_events, /* get_poll_events */
96     default_poll_event,         /* poll_event */
97     no_flush,                   /* flush */
98     mailslot_get_info,          /* get_file_info */
99     mailslot_queue_async,       /* queue_async */
100     default_fd_cancel_async     /* cancel_async */
101 };
102
103
104 struct mail_writer
105 {
106     struct object         obj;
107     struct mailslot      *mailslot;
108     struct list           entry;
109     unsigned int          access;
110     unsigned int          sharing;
111 };
112
113 static void mail_writer_dump( struct object *obj, int verbose );
114 static struct fd *mail_writer_get_fd( struct object *obj );
115 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
116 static void mail_writer_destroy( struct object *obj);
117
118 static const struct object_ops mail_writer_ops =
119 {
120     sizeof(struct mail_writer), /* size */
121     mail_writer_dump,           /* dump */
122     no_add_queue,               /* add_queue */
123     NULL,                       /* remove_queue */
124     NULL,                       /* signaled */
125     NULL,                       /* satisfied */
126     no_signal,                  /* signal */
127     mail_writer_get_fd,         /* get_fd */
128     mail_writer_map_access,     /* map_access */
129     no_lookup_name,             /* lookup_name */
130     no_open_file,               /* open_file */
131     fd_close_handle,            /* close_handle */
132     mail_writer_destroy         /* destroy */
133 };
134
135 static enum server_fd_type mail_writer_get_info( struct fd *fd, int *flags );
136
137 static const struct fd_ops mail_writer_fd_ops =
138 {
139     NULL,                        /* get_poll_events */
140     NULL,                        /* poll_event */
141     no_flush,                    /* flush */
142     mail_writer_get_info,        /* get_file_info */
143     no_queue_async,              /* queue_async */
144     NULL                         /* cancel_async */
145 };
146
147
148 struct mailslot_device
149 {
150     struct object       obj;         /* object header */
151     struct fd          *fd;          /* pseudo-fd for ioctls */
152     struct namespace   *mailslots;   /* mailslot namespace */
153 };
154
155 static void mailslot_device_dump( struct object *obj, int verbose );
156 static struct fd *mailslot_device_get_fd( struct object *obj );
157 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
158                                                    unsigned int attr );
159 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
160                                                  unsigned int sharing, unsigned int options );
161 static void mailslot_device_destroy( struct object *obj );
162 static enum server_fd_type mailslot_device_get_file_info( struct fd *fd, int *flags );
163
164 static const struct object_ops mailslot_device_ops =
165 {
166     sizeof(struct mailslot_device), /* size */
167     mailslot_device_dump,           /* dump */
168     no_add_queue,                   /* add_queue */
169     NULL,                           /* remove_queue */
170     NULL,                           /* signaled */
171     no_satisfied,                   /* satisfied */
172     no_signal,                      /* signal */
173     mailslot_device_get_fd,         /* get_fd */
174     no_map_access,                  /* map_access */
175     mailslot_device_lookup_name,    /* lookup_name */
176     mailslot_device_open_file,      /* open_file */
177     fd_close_handle,                /* close_handle */
178     mailslot_device_destroy         /* destroy */
179 };
180
181 static const struct fd_ops mailslot_device_fd_ops =
182 {
183     default_fd_get_poll_events,     /* get_poll_events */
184     default_poll_event,             /* poll_event */
185     no_flush,                       /* flush */
186     mailslot_device_get_file_info,  /* get_file_info */
187     default_fd_queue_async,         /* queue_async */
188     default_fd_cancel_async         /* cancel_async */
189 };
190
191 static void mailslot_destroy( struct object *obj)
192 {
193     struct mailslot *mailslot = (struct mailslot *) obj;
194
195     assert( mailslot->fd );
196     assert( mailslot->write_fd );
197
198     release_object( mailslot->fd );
199     release_object( mailslot->write_fd );
200 }
201
202 static void mailslot_dump( struct object *obj, int verbose )
203 {
204     struct mailslot *mailslot = (struct mailslot *) obj;
205
206     assert( obj->ops == &mailslot_ops );
207     fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%d\n",
208              mailslot->max_msgsize, mailslot->read_timeout );
209 }
210
211 static int mailslot_message_count(struct mailslot *mailslot)
212 {
213     struct pollfd pfd;
214
215     /* poll the socket to see if there's any messages */
216     pfd.fd = get_unix_fd( mailslot->fd );
217     pfd.events = POLLIN;
218     pfd.revents = 0;
219     return (poll( &pfd, 1, 0 ) == 1) ? 1 : 0;
220 }
221
222 static enum server_fd_type mailslot_get_info( struct fd *fd, int *flags )
223 {
224     struct mailslot *mailslot = get_fd_user( fd );
225     assert( mailslot->obj.ops == &mailslot_ops );
226     *flags = FD_FLAG_TIMEOUT | FD_FLAG_AVAILABLE;
227     return FD_TYPE_MAILSLOT;
228 }
229
230 static struct fd *mailslot_get_fd( struct object *obj )
231 {
232     struct mailslot *mailslot = (struct mailslot *) obj;
233
234     return (struct fd *)grab_object( mailslot->fd );
235 }
236
237 static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
238 {
239     if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
240     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
241     if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
242     if (access & GENERIC_ALL)     access |= FILE_ALL_ACCESS;
243     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
244 }
245
246 static struct object *mailslot_open_file( struct object *obj, unsigned int access,
247                                           unsigned int sharing, unsigned int options )
248 {
249     struct mailslot *mailslot = (struct mailslot *)obj;
250     struct mail_writer *writer;
251
252     if (!(sharing & FILE_SHARE_READ))
253     {
254         set_error( STATUS_SHARING_VIOLATION );
255         return NULL;
256     }
257
258     if (!list_empty( &mailslot->writers ))
259     {
260         /* Readers and writers cannot be mixed.
261          * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
262          */
263         writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );
264
265         if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
266            !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
267         {
268             set_error( STATUS_SHARING_VIOLATION );
269             return NULL;
270         }
271     }
272
273     if (!(writer = alloc_object( &mail_writer_ops ))) return NULL;
274     grab_object( mailslot );
275     writer->mailslot = mailslot;
276     writer->access   = mail_writer_map_access( &writer->obj, access );
277     writer->sharing  = sharing;
278
279     list_add_head( &mailslot->writers, &writer->entry );
280     return &writer->obj;
281 }
282
283 static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
284 {
285     struct mailslot *mailslot = get_fd_user( fd );
286
287     assert(mailslot->obj.ops == &mailslot_ops);
288
289     if (type != ASYNC_TYPE_READ)
290     {
291         set_error(STATUS_INVALID_PARAMETER);
292         return;
293     }
294
295     if (list_empty( &mailslot->writers ) ||
296         !mailslot_message_count( mailslot ))
297     {
298         set_error(STATUS_IO_TIMEOUT);
299         return;
300     }
301
302     if (mailslot->read_timeout != -1)
303     {
304         struct timeval when = current_time;
305         add_timeout( &when, mailslot->read_timeout );
306         fd_queue_async_timeout( fd, data, type, count, &when );
307     }
308     else fd_queue_async_timeout( fd, data, type, count, NULL );
309 }
310
311 static void mailslot_device_dump( struct object *obj, int verbose )
312 {
313     assert( obj->ops == &mailslot_device_ops );
314     fprintf( stderr, "Mail slot device\n" );
315 }
316
317 static struct fd *mailslot_device_get_fd( struct object *obj )
318 {
319     struct mailslot_device *device = (struct mailslot_device *)obj;
320     return (struct fd *)grab_object( device->fd );
321 }
322
323 static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
324                                                    unsigned int attr )
325 {
326     struct mailslot_device *device = (struct mailslot_device*)obj;
327     struct object *found;
328
329     assert( obj->ops == &mailslot_device_ops );
330
331     if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
332         name->len = 0;
333
334     return found;
335 }
336
337 static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
338                                                  unsigned int sharing, unsigned int options )
339 {
340     return grab_object( obj );
341 }
342
343 static void mailslot_device_destroy( struct object *obj )
344 {
345     struct mailslot_device *device = (struct mailslot_device*)obj;
346     assert( obj->ops == &mailslot_device_ops );
347     if (device->fd) release_object( device->fd );
348     free( device->mailslots );
349 }
350
351 static enum server_fd_type mailslot_device_get_file_info( struct fd *fd, int *flags )
352 {
353     *flags = 0;
354     return FD_TYPE_DEVICE;
355 }
356
357 void create_mailslot_device( struct directory *root, const struct unicode_str *name )
358 {
359     struct mailslot_device *dev;
360
361     if ((dev = create_named_object_dir( root, name, 0, &mailslot_device_ops )) &&
362         get_error() != STATUS_OBJECT_NAME_EXISTS)
363     {
364         dev->mailslots = NULL;
365         if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj )) ||
366             !(dev->mailslots = create_namespace( 7 )))
367         {
368             release_object( dev );
369             dev = NULL;
370         }
371     }
372     if (dev) make_object_static( &dev->obj );
373 }
374
375 static struct mailslot *create_mailslot( struct directory *root,
376                                          const struct unicode_str *name, unsigned int attr,
377                                          int max_msgsize, int read_timeout )
378 {
379     struct object *obj;
380     struct unicode_str new_name;
381     struct mailslot_device *dev;
382     struct mailslot *mailslot;
383     int fds[2];
384
385     if (!name || !name->len) return alloc_object( &mailslot_ops );
386
387     if (!(obj = find_object_dir( root, name, attr, &new_name )))
388     {
389         set_error( STATUS_OBJECT_NAME_INVALID );
390         return NULL;
391     }
392
393     if (!new_name.len)
394     {
395         if (attr & OBJ_OPENIF && obj->ops == &mailslot_ops)
396             /* it already exists - there can only be one mailslot to read from */
397             set_error( STATUS_OBJECT_NAME_EXISTS );
398         else if (attr & OBJ_OPENIF)
399             set_error( STATUS_OBJECT_TYPE_MISMATCH );
400         else
401             set_error( STATUS_OBJECT_NAME_COLLISION );
402         release_object( obj );
403         return NULL;
404     }
405
406     if (obj->ops != &mailslot_device_ops)
407     {
408         set_error( STATUS_OBJECT_NAME_INVALID );
409         release_object( obj );
410         return NULL;
411     }
412
413     dev = (struct mailslot_device *)obj;
414     mailslot = create_object( dev->mailslots, &mailslot_ops, &new_name, NULL );
415     release_object( dev );
416
417     if (!mailslot) return NULL;
418
419     mailslot->fd = NULL;
420     mailslot->write_fd = NULL;
421     mailslot->max_msgsize = max_msgsize;
422     mailslot->read_timeout = read_timeout;
423     list_init( &mailslot->writers );
424
425     if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
426     {
427         fcntl( fds[0], F_SETFL, O_NONBLOCK );
428         fcntl( fds[1], F_SETFL, O_NONBLOCK );
429         mailslot->fd = create_anonymous_fd( &mailslot_fd_ops,
430                                 fds[1], &mailslot->obj );
431         mailslot->write_fd = create_anonymous_fd( &mail_writer_fd_ops,
432                                 fds[0], &mailslot->obj );
433         if (mailslot->fd && mailslot->write_fd) return mailslot;
434     }
435     else file_set_error();
436
437     release_object( mailslot );
438     return NULL;
439 }
440
441 static void mail_writer_dump( struct object *obj, int verbose )
442 {
443     fprintf( stderr, "Mailslot writer\n" );
444 }
445
446 static void mail_writer_destroy( struct object *obj)
447 {
448     struct mail_writer *writer = (struct mail_writer *) obj;
449
450     list_remove( &writer->entry );
451     release_object( writer->mailslot );
452 }
453
454 static enum server_fd_type mail_writer_get_info( struct fd *fd, int *flags )
455 {
456     *flags = 0;
457     return FD_TYPE_MAILSLOT;
458 }
459
460 static struct fd *mail_writer_get_fd( struct object *obj )
461 {
462     struct mail_writer *writer = (struct mail_writer *) obj;
463
464     return (struct fd *)grab_object( writer->mailslot->write_fd );
465 }
466
467 static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
468 {
469     /* mailslot writers can only get write access */
470     if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
471     if (access & GENERIC_ALL)     access |= FILE_GENERIC_WRITE;
472     return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
473 }
474
475 static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
476                                           unsigned int access )
477 {
478     return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
479 }
480
481
482 /* create a mailslot */
483 DECL_HANDLER(create_mailslot)
484 {
485     struct mailslot *mailslot;
486     struct unicode_str name;
487     struct directory *root = NULL;
488
489     reply->handle = 0;
490     get_req_unicode_str( &name );
491     if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
492         return;
493
494     if ((mailslot = create_mailslot( root, &name, req->attributes, req->max_msgsize,
495                                      req->read_timeout )))
496     {
497         reply->handle = alloc_handle( current->process, mailslot, req->access, req->attributes );
498         release_object( mailslot );
499     }
500
501     if (root) release_object( root );
502 }
503
504
505 /* set mailslot information */
506 DECL_HANDLER(set_mailslot_info)
507 {
508     struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );
509
510     if (mailslot)
511     {
512         if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
513             mailslot->read_timeout = req->read_timeout;
514         reply->max_msgsize = mailslot->max_msgsize;
515         reply->read_timeout = mailslot->read_timeout;
516         release_object( mailslot );
517     }
518 }