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
24 #include "wine/port.h"
26 #define WIN32_NO_STATUS
27 #include "wine/unicode.h"
36 #include <sys/types.h>
38 #ifdef HAVE_SYS_IOCTL_H
39 #include <sys/ioctl.h>
41 #ifdef HAVE_SYS_SOCKET_H
42 #include <sys/socket.h>
44 #ifdef HAVE_SYS_FILIO_H
45 #include <sys/filio.h>
60 unsigned int max_msgsize
;
61 timeout_t read_timeout
;
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 file_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_get_file_info
, /* get_file_info */
108 no_fd_get_volume_info
, /* get_volume_info */
109 default_fd_ioctl
, /* ioctl */
110 mailslot_queue_async
, /* queue_async */
111 default_fd_reselect_async
/* reselect_async */
119 struct mailslot
*mailslot
;
122 unsigned int sharing
;
125 static void mail_writer_dump( struct object
*obj
, int verbose
);
126 static struct fd
*mail_writer_get_fd( struct object
*obj
);
127 static unsigned int mail_writer_map_access( struct object
*obj
, unsigned int access
);
128 static void mail_writer_destroy( struct object
*obj
);
130 static const struct object_ops mail_writer_ops
=
132 sizeof(struct mail_writer
), /* size */
133 mail_writer_dump
, /* dump */
134 file_get_type
, /* get_type */
135 no_add_queue
, /* add_queue */
136 NULL
, /* remove_queue */
138 NULL
, /* satisfied */
139 no_signal
, /* signal */
140 mail_writer_get_fd
, /* get_fd */
141 mail_writer_map_access
, /* map_access */
142 default_get_sd
, /* get_sd */
143 default_set_sd
, /* set_sd */
144 no_lookup_name
, /* lookup_name */
145 no_link_name
, /* link_name */
146 NULL
, /* unlink_name */
147 no_open_file
, /* open_file */
148 fd_close_handle
, /* close_handle */
149 mail_writer_destroy
/* destroy */
152 static enum server_fd_type
mail_writer_get_fd_type( struct fd
*fd
);
154 static const struct fd_ops mail_writer_fd_ops
=
156 default_fd_get_poll_events
, /* get_poll_events */
157 default_poll_event
, /* poll_event */
158 mail_writer_get_fd_type
, /* get_fd_type */
159 no_fd_read
, /* read */
160 no_fd_write
, /* write */
161 no_fd_flush
, /* flush */
162 default_fd_get_file_info
, /* get_file_info */
163 no_fd_get_volume_info
, /* get_volume_info */
164 default_fd_ioctl
, /* ioctl */
165 default_fd_queue_async
, /* queue_async */
166 default_fd_reselect_async
/* reselect_async */
170 struct mailslot_device
172 struct object obj
; /* object header */
173 struct fd
*fd
; /* pseudo-fd for ioctls */
174 struct namespace *mailslots
; /* mailslot namespace */
177 static void mailslot_device_dump( struct object
*obj
, int verbose
);
178 static struct object_type
*mailslot_device_get_type( struct object
*obj
);
179 static struct fd
*mailslot_device_get_fd( struct object
*obj
);
180 static struct object
*mailslot_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
182 static struct object
*mailslot_device_open_file( struct object
*obj
, unsigned int access
,
183 unsigned int sharing
, unsigned int options
);
184 static void mailslot_device_destroy( struct object
*obj
);
185 static enum server_fd_type
mailslot_device_get_fd_type( struct fd
*fd
);
187 static const struct object_ops mailslot_device_ops
=
189 sizeof(struct mailslot_device
), /* size */
190 mailslot_device_dump
, /* dump */
191 mailslot_device_get_type
, /* get_type */
192 no_add_queue
, /* add_queue */
193 NULL
, /* remove_queue */
195 no_satisfied
, /* satisfied */
196 no_signal
, /* signal */
197 mailslot_device_get_fd
, /* get_fd */
198 no_map_access
, /* map_access */
199 default_get_sd
, /* get_sd */
200 default_set_sd
, /* set_sd */
201 mailslot_device_lookup_name
, /* lookup_name */
202 directory_link_name
, /* link_name */
203 default_unlink_name
, /* unlink_name */
204 mailslot_device_open_file
, /* open_file */
205 fd_close_handle
, /* close_handle */
206 mailslot_device_destroy
/* destroy */
209 static const struct fd_ops mailslot_device_fd_ops
=
211 default_fd_get_poll_events
, /* get_poll_events */
212 default_poll_event
, /* poll_event */
213 mailslot_device_get_fd_type
, /* get_fd_type */
214 no_fd_read
, /* read */
215 no_fd_write
, /* write */
216 no_fd_flush
, /* flush */
217 default_fd_get_file_info
, /* get_file_info */
218 no_fd_get_volume_info
, /* get_volume_info */
219 default_fd_ioctl
, /* ioctl */
220 default_fd_queue_async
, /* queue_async */
221 default_fd_reselect_async
/* reselect_async */
224 static void mailslot_destroy( struct object
*obj
)
226 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
228 assert( mailslot
->fd
);
230 if (mailslot
->write_fd
!= -1)
232 shutdown( mailslot
->write_fd
, SHUT_RDWR
);
233 close( mailslot
->write_fd
);
235 release_object( mailslot
->fd
);
238 static void mailslot_dump( struct object
*obj
, int verbose
)
240 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
242 assert( obj
->ops
== &mailslot_ops
);
243 fprintf( stderr
, "Mailslot max_msgsize=%d read_timeout=%s\n",
244 mailslot
->max_msgsize
, get_timeout_str(mailslot
->read_timeout
) );
247 static enum server_fd_type
mailslot_get_fd_type( struct fd
*fd
)
249 return FD_TYPE_MAILSLOT
;
252 static struct fd
*mailslot_get_fd( struct object
*obj
)
254 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
256 return (struct fd
*)grab_object( mailslot
->fd
);
259 static unsigned int mailslot_map_access( struct object
*obj
, unsigned int access
)
261 /* mailslots can only be read */
262 if (access
& GENERIC_READ
) access
|= FILE_GENERIC_READ
;
263 if (access
& GENERIC_ALL
) access
|= FILE_GENERIC_READ
;
264 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
267 static int mailslot_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
269 struct mailslot_device
*dev
= (struct mailslot_device
*)parent
;
271 if (parent
->ops
!= &mailslot_device_ops
)
273 set_error( STATUS_OBJECT_NAME_INVALID
);
276 namespace_add( dev
->mailslots
, name
);
277 name
->parent
= grab_object( parent
);
281 static struct object
*mailslot_open_file( struct object
*obj
, unsigned int access
,
282 unsigned int sharing
, unsigned int options
)
284 struct mailslot
*mailslot
= (struct mailslot
*)obj
;
285 struct mail_writer
*writer
;
288 if (!(sharing
& FILE_SHARE_READ
))
290 set_error( STATUS_SHARING_VIOLATION
);
294 if (!list_empty( &mailslot
->writers
))
296 /* Readers and writers cannot be mixed.
297 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
299 writer
= LIST_ENTRY( list_head(&mailslot
->writers
), struct mail_writer
, entry
);
301 if (((access
& (GENERIC_WRITE
|FILE_WRITE_DATA
)) || (writer
->access
& FILE_WRITE_DATA
)) &&
302 !((sharing
& FILE_SHARE_WRITE
) && (writer
->sharing
& FILE_SHARE_WRITE
)))
304 set_error( STATUS_SHARING_VIOLATION
);
309 if ((unix_fd
= dup( mailslot
->write_fd
)) == -1)
314 if (!(writer
= alloc_object( &mail_writer_ops
)))
319 grab_object( mailslot
);
320 writer
->mailslot
= mailslot
;
321 writer
->access
= mail_writer_map_access( &writer
->obj
, access
);
322 writer
->sharing
= sharing
;
323 list_add_head( &mailslot
->writers
, &writer
->entry
);
325 if (!(writer
->fd
= create_anonymous_fd( &mail_writer_fd_ops
, unix_fd
, &writer
->obj
, options
)))
327 release_object( writer
);
330 allow_fd_caching( writer
->fd
);
334 static void mailslot_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
336 struct mailslot
*mailslot
= get_fd_user( fd
);
338 assert(mailslot
->obj
.ops
== &mailslot_ops
);
340 fd_queue_async( fd
, async
, type
);
341 async_set_timeout( async
, mailslot
->read_timeout
? mailslot
->read_timeout
: -1,
343 set_error( STATUS_PENDING
);
346 static void mailslot_device_dump( struct object
*obj
, int verbose
)
348 fputs( "Mailslot device\n", stderr
);
351 static struct object_type
*mailslot_device_get_type( struct object
*obj
)
353 static const WCHAR name
[] = {'D','e','v','i','c','e'};
354 static const struct unicode_str str
= { name
, sizeof(name
) };
355 return get_object_type( &str
);
358 static struct fd
*mailslot_device_get_fd( struct object
*obj
)
360 struct mailslot_device
*device
= (struct mailslot_device
*)obj
;
361 return (struct fd
*)grab_object( device
->fd
);
364 static struct object
*mailslot_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
367 struct mailslot_device
*device
= (struct mailslot_device
*)obj
;
368 struct object
*found
;
370 assert( obj
->ops
== &mailslot_device_ops
);
372 if (!name
) return NULL
; /* open the device itself */
374 if ((found
= find_object( device
->mailslots
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
380 static struct object
*mailslot_device_open_file( struct object
*obj
, unsigned int access
,
381 unsigned int sharing
, unsigned int options
)
383 return grab_object( obj
);
386 static void mailslot_device_destroy( struct object
*obj
)
388 struct mailslot_device
*device
= (struct mailslot_device
*)obj
;
389 assert( obj
->ops
== &mailslot_device_ops
);
390 if (device
->fd
) release_object( device
->fd
);
391 free( device
->mailslots
);
394 static enum server_fd_type
mailslot_device_get_fd_type( struct fd
*fd
)
396 return FD_TYPE_DEVICE
;
399 struct object
*create_mailslot_device( struct object
*root
, const struct unicode_str
*name
)
401 struct mailslot_device
*dev
;
403 if ((dev
= create_named_object( root
, &mailslot_device_ops
, name
, 0, NULL
)) &&
404 get_error() != STATUS_OBJECT_NAME_EXISTS
)
406 dev
->mailslots
= NULL
;
407 if (!(dev
->fd
= alloc_pseudo_fd( &mailslot_device_fd_ops
, &dev
->obj
, 0 )) ||
408 !(dev
->mailslots
= create_namespace( 7 )))
410 release_object( dev
);
417 static struct mailslot
*create_mailslot( struct object
*root
,
418 const struct unicode_str
*name
, unsigned int attr
,
419 int max_msgsize
, timeout_t read_timeout
,
420 const struct security_descriptor
*sd
)
422 struct mailslot
*mailslot
;
425 if (!(mailslot
= create_named_object( root
, &mailslot_ops
, name
, attr
, sd
))) return NULL
;
428 mailslot
->write_fd
= -1;
429 mailslot
->max_msgsize
= max_msgsize
;
430 mailslot
->read_timeout
= read_timeout
;
431 list_init( &mailslot
->writers
);
433 if (!socketpair( PF_UNIX
, SOCK_DGRAM
, 0, fds
))
435 fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
436 fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
437 shutdown( fds
[0], SHUT_RD
);
438 mailslot
->write_fd
= fds
[0];
439 if ((mailslot
->fd
= create_anonymous_fd( &mailslot_fd_ops
, fds
[1], &mailslot
->obj
,
440 FILE_SYNCHRONOUS_IO_NONALERT
)))
442 allow_fd_caching( mailslot
->fd
);
446 else file_set_error();
448 release_object( mailslot
);
452 static void mail_writer_dump( struct object
*obj
, int verbose
)
454 fprintf( stderr
, "Mailslot writer\n" );
457 static void mail_writer_destroy( struct object
*obj
)
459 struct mail_writer
*writer
= (struct mail_writer
*) obj
;
461 if (writer
->fd
) release_object( writer
->fd
);
462 list_remove( &writer
->entry
);
463 release_object( writer
->mailslot
);
466 static enum server_fd_type
mail_writer_get_fd_type( struct fd
*fd
)
468 return FD_TYPE_MAILSLOT
;
471 static struct fd
*mail_writer_get_fd( struct object
*obj
)
473 struct mail_writer
*writer
= (struct mail_writer
*) obj
;
474 return (struct fd
*)grab_object( writer
->fd
);
477 static unsigned int mail_writer_map_access( struct object
*obj
, unsigned int access
)
479 /* mailslot writers can only get write access */
480 if (access
& GENERIC_WRITE
) access
|= FILE_GENERIC_WRITE
;
481 if (access
& GENERIC_ALL
) access
|= FILE_GENERIC_WRITE
;
482 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
485 static struct mailslot
*get_mailslot_obj( struct process
*process
, obj_handle_t handle
,
486 unsigned int access
)
488 return (struct mailslot
*)get_handle_obj( process
, handle
, access
, &mailslot_ops
);
492 /* create a mailslot */
493 DECL_HANDLER(create_mailslot
)
495 struct mailslot
*mailslot
;
496 struct unicode_str name
;
498 const struct security_descriptor
*sd
;
499 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
501 if (!objattr
) return;
503 if (!name
.len
) /* mailslots need a root directory even without a name */
505 if (!objattr
->rootdir
)
507 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
510 if (!(root
= get_directory_obj( current
->process
, objattr
->rootdir
))) return;
513 if ((mailslot
= create_mailslot( root
, &name
, objattr
->attributes
, req
->max_msgsize
,
514 req
->read_timeout
, sd
)))
516 reply
->handle
= alloc_handle( current
->process
, mailslot
, req
->access
, objattr
->attributes
);
517 release_object( mailslot
);
520 if (root
) release_object( root
);
524 /* set mailslot information */
525 DECL_HANDLER(set_mailslot_info
)
527 struct mailslot
*mailslot
= get_mailslot_obj( current
->process
, req
->handle
, 0 );
531 if (req
->flags
& MAILSLOT_SET_READ_TIMEOUT
)
532 mailslot
->read_timeout
= req
->read_timeout
;
533 reply
->max_msgsize
= mailslot
->max_msgsize
;
534 reply
->read_timeout
= mailslot
->read_timeout
;
535 release_object( mailslot
);