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
35 #include <sys/types.h>
37 #ifdef HAVE_SYS_IOCTL_H
38 #include <sys/ioctl.h>
40 #ifdef HAVE_SYS_SOCKET_H
41 #include <sys/socket.h>
43 #ifdef HAVE_SYS_FILIO_H
44 #include <sys/filio.h>
59 unsigned int max_msgsize
;
60 timeout_t read_timeout
;
64 /* mailslot functions */
65 static void mailslot_dump( struct object
*, int );
66 static struct fd
*mailslot_get_fd( struct object
* );
67 static unsigned int mailslot_map_access( struct object
*obj
, unsigned int access
);
68 static int mailslot_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
);
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
* );
73 static const struct object_ops mailslot_ops
=
75 sizeof(struct mailslot
), /* size */
76 mailslot_dump
, /* dump */
77 file_get_type
, /* get_type */
78 add_queue
, /* add_queue */
79 remove_queue
, /* remove_queue */
80 default_fd_signaled
, /* signaled */
81 no_satisfied
, /* satisfied */
82 no_signal
, /* signal */
83 mailslot_get_fd
, /* get_fd */
84 mailslot_map_access
, /* map_access */
85 default_get_sd
, /* get_sd */
86 default_set_sd
, /* set_sd */
87 default_get_full_name
, /* get_full_name */
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 no_kernel_obj_list
, /* get_kernel_obj_list */
93 fd_close_handle
, /* close_handle */
94 mailslot_destroy
/* destroy */
97 static enum server_fd_type
mailslot_get_fd_type( struct fd
*fd
);
98 static void mailslot_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
100 static const struct fd_ops mailslot_fd_ops
=
102 default_fd_get_poll_events
, /* get_poll_events */
103 default_poll_event
, /* poll_event */
104 mailslot_get_fd_type
, /* get_fd_type */
105 no_fd_read
, /* read */
106 no_fd_write
, /* write */
107 no_fd_flush
, /* flush */
108 default_fd_get_file_info
, /* get_file_info */
109 no_fd_get_volume_info
, /* get_volume_info */
110 default_fd_ioctl
, /* ioctl */
111 mailslot_queue_async
, /* queue_async */
112 default_fd_reselect_async
/* reselect_async */
120 struct mailslot
*mailslot
;
123 unsigned int sharing
;
126 static void mail_writer_dump( struct object
*obj
, int verbose
);
127 static struct fd
*mail_writer_get_fd( struct object
*obj
);
128 static unsigned int mail_writer_map_access( struct object
*obj
, unsigned int access
);
129 static void mail_writer_destroy( struct object
*obj
);
131 static const struct object_ops mail_writer_ops
=
133 sizeof(struct mail_writer
), /* size */
134 mail_writer_dump
, /* dump */
135 file_get_type
, /* get_type */
136 no_add_queue
, /* add_queue */
137 NULL
, /* remove_queue */
139 NULL
, /* satisfied */
140 no_signal
, /* signal */
141 mail_writer_get_fd
, /* get_fd */
142 mail_writer_map_access
, /* map_access */
143 default_get_sd
, /* get_sd */
144 default_set_sd
, /* set_sd */
145 no_get_full_name
, /* get_full_name */
146 no_lookup_name
, /* lookup_name */
147 no_link_name
, /* link_name */
148 NULL
, /* unlink_name */
149 no_open_file
, /* open_file */
150 no_kernel_obj_list
, /* get_kernel_obj_list */
151 fd_close_handle
, /* close_handle */
152 mail_writer_destroy
/* destroy */
155 static enum server_fd_type
mail_writer_get_fd_type( struct fd
*fd
);
157 static const struct fd_ops mail_writer_fd_ops
=
159 default_fd_get_poll_events
, /* get_poll_events */
160 default_poll_event
, /* poll_event */
161 mail_writer_get_fd_type
, /* get_fd_type */
162 no_fd_read
, /* read */
163 no_fd_write
, /* write */
164 no_fd_flush
, /* flush */
165 default_fd_get_file_info
, /* get_file_info */
166 no_fd_get_volume_info
, /* get_volume_info */
167 default_fd_ioctl
, /* ioctl */
168 default_fd_queue_async
, /* queue_async */
169 default_fd_reselect_async
/* reselect_async */
173 struct mailslot_device
175 struct object obj
; /* object header */
176 struct namespace *mailslots
; /* mailslot namespace */
179 struct mailslot_device_file
181 struct object obj
; /* object header */
182 struct fd
*fd
; /* pseudo-fd for ioctls */
183 struct mailslot_device
*device
; /* mailslot device */
186 static void mailslot_device_dump( struct object
*obj
, int verbose
);
187 static struct object_type
*mailslot_device_get_type( struct object
*obj
);
188 static struct object
*mailslot_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
189 unsigned int attr
, struct object
*root
);
190 static struct object
*mailslot_device_open_file( struct object
*obj
, unsigned int access
,
191 unsigned int sharing
, unsigned int options
);
192 static void mailslot_device_destroy( struct object
*obj
);
194 static const struct object_ops mailslot_device_ops
=
196 sizeof(struct mailslot_device
), /* size */
197 mailslot_device_dump
, /* dump */
198 mailslot_device_get_type
, /* get_type */
199 no_add_queue
, /* add_queue */
200 NULL
, /* remove_queue */
202 no_satisfied
, /* satisfied */
203 no_signal
, /* signal */
204 no_get_fd
, /* get_fd */
205 no_map_access
, /* map_access */
206 default_get_sd
, /* get_sd */
207 default_set_sd
, /* set_sd */
208 default_get_full_name
, /* get_full_name */
209 mailslot_device_lookup_name
, /* lookup_name */
210 directory_link_name
, /* link_name */
211 default_unlink_name
, /* unlink_name */
212 mailslot_device_open_file
, /* open_file */
213 no_kernel_obj_list
, /* get_kernel_obj_list */
214 no_close_handle
, /* close_handle */
215 mailslot_device_destroy
/* destroy */
218 static void mailslot_device_file_dump( struct object
*obj
, int verbose
);
219 static struct fd
*mailslot_device_file_get_fd( struct object
*obj
);
220 static WCHAR
*mailslot_device_file_get_full_name( struct object
*obj
, data_size_t
*len
);
221 static void mailslot_device_file_destroy( struct object
*obj
);
222 static enum server_fd_type
mailslot_device_file_get_fd_type( struct fd
*fd
);
224 static const struct object_ops mailslot_device_file_ops
=
226 sizeof(struct mailslot_device_file
), /* size */
227 mailslot_device_file_dump
, /* dump */
228 file_get_type
, /* get_type */
229 add_queue
, /* add_queue */
230 remove_queue
, /* remove_queue */
231 default_fd_signaled
, /* signaled */
232 no_satisfied
, /* satisfied */
233 no_signal
, /* signal */
234 mailslot_device_file_get_fd
, /* get_fd */
235 default_fd_map_access
, /* map_access */
236 default_get_sd
, /* get_sd */
237 default_set_sd
, /* set_sd */
238 mailslot_device_file_get_full_name
, /* get_full_name */
239 no_lookup_name
, /* lookup_name */
240 no_link_name
, /* link_name */
241 NULL
, /* unlink_name */
242 no_open_file
, /* open_file */
243 no_kernel_obj_list
, /* get_kernel_obj_list */
244 fd_close_handle
, /* close_handle */
245 mailslot_device_file_destroy
/* destroy */
248 static const struct fd_ops mailslot_device_fd_ops
=
250 default_fd_get_poll_events
, /* get_poll_events */
251 default_poll_event
, /* poll_event */
252 mailslot_device_file_get_fd_type
, /* get_fd_type */
253 no_fd_read
, /* read */
254 no_fd_write
, /* write */
255 no_fd_flush
, /* flush */
256 default_fd_get_file_info
, /* get_file_info */
257 no_fd_get_volume_info
, /* get_volume_info */
258 default_fd_ioctl
, /* ioctl */
259 default_fd_queue_async
, /* queue_async */
260 default_fd_reselect_async
/* reselect_async */
263 static void mailslot_destroy( struct object
*obj
)
265 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
267 assert( mailslot
->fd
);
269 if (mailslot
->write_fd
!= -1)
271 shutdown( mailslot
->write_fd
, SHUT_RDWR
);
272 close( mailslot
->write_fd
);
274 release_object( mailslot
->fd
);
277 static void mailslot_dump( struct object
*obj
, int verbose
)
279 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
281 assert( obj
->ops
== &mailslot_ops
);
282 fprintf( stderr
, "Mailslot max_msgsize=%d read_timeout=%s\n",
283 mailslot
->max_msgsize
, get_timeout_str(mailslot
->read_timeout
) );
286 static enum server_fd_type
mailslot_get_fd_type( struct fd
*fd
)
288 return FD_TYPE_MAILSLOT
;
291 static struct fd
*mailslot_get_fd( struct object
*obj
)
293 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
295 return (struct fd
*)grab_object( mailslot
->fd
);
298 static unsigned int mailslot_map_access( struct object
*obj
, unsigned int access
)
300 /* mailslots can only be read */
301 if (access
& GENERIC_READ
) access
|= FILE_GENERIC_READ
;
302 if (access
& GENERIC_ALL
) access
|= FILE_GENERIC_READ
;
303 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
306 static int mailslot_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
308 struct mailslot_device
*dev
= (struct mailslot_device
*)parent
;
310 if (parent
->ops
!= &mailslot_device_ops
)
312 set_error( STATUS_OBJECT_NAME_INVALID
);
315 namespace_add( dev
->mailslots
, name
);
316 name
->parent
= grab_object( parent
);
320 static struct object
*mailslot_open_file( struct object
*obj
, unsigned int access
,
321 unsigned int sharing
, unsigned int options
)
323 struct mailslot
*mailslot
= (struct mailslot
*)obj
;
324 struct mail_writer
*writer
;
327 if (!(sharing
& FILE_SHARE_READ
))
329 set_error( STATUS_SHARING_VIOLATION
);
333 if (!list_empty( &mailslot
->writers
))
335 /* Readers and writers cannot be mixed.
336 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
338 writer
= LIST_ENTRY( list_head(&mailslot
->writers
), struct mail_writer
, entry
);
340 if (((access
& (GENERIC_WRITE
|FILE_WRITE_DATA
)) || (writer
->access
& FILE_WRITE_DATA
)) &&
341 !((sharing
& FILE_SHARE_WRITE
) && (writer
->sharing
& FILE_SHARE_WRITE
)))
343 set_error( STATUS_SHARING_VIOLATION
);
348 if ((unix_fd
= dup( mailslot
->write_fd
)) == -1)
353 if (!(writer
= alloc_object( &mail_writer_ops
)))
358 grab_object( mailslot
);
359 writer
->mailslot
= mailslot
;
360 writer
->access
= mail_writer_map_access( &writer
->obj
, access
);
361 writer
->sharing
= sharing
;
362 list_add_head( &mailslot
->writers
, &writer
->entry
);
364 if (!(writer
->fd
= create_anonymous_fd( &mail_writer_fd_ops
, unix_fd
, &writer
->obj
, options
)))
366 release_object( writer
);
369 allow_fd_caching( writer
->fd
);
373 static void mailslot_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
375 struct mailslot
*mailslot
= get_fd_user( fd
);
377 assert(mailslot
->obj
.ops
== &mailslot_ops
);
379 fd_queue_async( fd
, async
, type
);
380 async_set_timeout( async
, mailslot
->read_timeout
? mailslot
->read_timeout
: -1,
382 set_error( STATUS_PENDING
);
385 static void mailslot_device_dump( struct object
*obj
, int verbose
)
387 fputs( "Mailslot device\n", stderr
);
390 static struct object_type
*mailslot_device_get_type( struct object
*obj
)
392 static const WCHAR name
[] = {'D','e','v','i','c','e'};
393 static const struct unicode_str str
= { name
, sizeof(name
) };
394 return get_object_type( &str
);
397 static struct object
*mailslot_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
398 unsigned int attr
, struct object
*root
)
400 struct mailslot_device
*device
= (struct mailslot_device
*)obj
;
401 struct object
*found
;
403 assert( obj
->ops
== &mailslot_device_ops
);
405 if (!name
) return NULL
; /* open the device itself */
407 if ((found
= find_object( device
->mailslots
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
413 static struct object
*mailslot_device_open_file( struct object
*obj
, unsigned int access
,
414 unsigned int sharing
, unsigned int options
)
416 struct mailslot_device_file
*file
;
418 if (!(file
= alloc_object( &mailslot_device_file_ops
))) return NULL
;
419 file
->device
= (struct mailslot_device
*)grab_object( obj
);
420 if (!(file
->fd
= alloc_pseudo_fd( &mailslot_device_fd_ops
, obj
, options
)))
422 release_object( file
);
425 allow_fd_caching( file
->fd
);
429 static void mailslot_device_destroy( struct object
*obj
)
431 struct mailslot_device
*device
= (struct mailslot_device
*)obj
;
432 assert( obj
->ops
== &mailslot_device_ops
);
433 free( device
->mailslots
);
436 struct object
*create_mailslot_device( struct object
*root
, const struct unicode_str
*name
,
437 unsigned int attr
, const struct security_descriptor
*sd
)
439 struct mailslot_device
*dev
;
441 if ((dev
= create_named_object( root
, &mailslot_device_ops
, name
, attr
, sd
)) &&
442 get_error() != STATUS_OBJECT_NAME_EXISTS
)
444 dev
->mailslots
= NULL
;
445 if (!(dev
->mailslots
= create_namespace( 7 )))
447 release_object( dev
);
454 static void mailslot_device_file_dump( struct object
*obj
, int verbose
)
456 struct mailslot_device_file
*file
= (struct mailslot_device_file
*)obj
;
458 fprintf( stderr
, "File on mailslot device %p\n", file
->device
);
461 static struct fd
*mailslot_device_file_get_fd( struct object
*obj
)
463 struct mailslot_device_file
*file
= (struct mailslot_device_file
*)obj
;
464 return (struct fd
*)grab_object( file
->fd
);
467 static WCHAR
*mailslot_device_file_get_full_name( struct object
*obj
, data_size_t
*len
)
469 struct mailslot_device_file
*file
= (struct mailslot_device_file
*)obj
;
470 return file
->device
->obj
.ops
->get_full_name( &file
->device
->obj
, len
);
473 static void mailslot_device_file_destroy( struct object
*obj
)
475 struct mailslot_device_file
*file
= (struct mailslot_device_file
*)obj
;
476 assert( obj
->ops
== &mailslot_device_file_ops
);
477 if (file
->fd
) release_object( file
->fd
);
478 release_object( file
->device
);
481 static enum server_fd_type
mailslot_device_file_get_fd_type( struct fd
*fd
)
483 return FD_TYPE_DEVICE
;
486 static struct mailslot
*create_mailslot( struct object
*root
,
487 const struct unicode_str
*name
, unsigned int attr
,
488 int max_msgsize
, timeout_t read_timeout
,
489 const struct security_descriptor
*sd
)
491 struct mailslot
*mailslot
;
494 if (!(mailslot
= create_named_object( root
, &mailslot_ops
, name
, attr
, sd
))) return NULL
;
497 mailslot
->write_fd
= -1;
498 mailslot
->max_msgsize
= max_msgsize
;
499 mailslot
->read_timeout
= read_timeout
;
500 list_init( &mailslot
->writers
);
502 if (!socketpair( PF_UNIX
, SOCK_DGRAM
, 0, fds
))
504 fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
505 fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
506 shutdown( fds
[0], SHUT_RD
);
507 mailslot
->write_fd
= fds
[0];
508 if ((mailslot
->fd
= create_anonymous_fd( &mailslot_fd_ops
, fds
[1], &mailslot
->obj
,
509 FILE_SYNCHRONOUS_IO_NONALERT
)))
511 allow_fd_caching( mailslot
->fd
);
515 else file_set_error();
517 release_object( mailslot
);
521 static void mail_writer_dump( struct object
*obj
, int verbose
)
523 fprintf( stderr
, "Mailslot writer\n" );
526 static void mail_writer_destroy( struct object
*obj
)
528 struct mail_writer
*writer
= (struct mail_writer
*) obj
;
530 if (writer
->fd
) release_object( writer
->fd
);
531 list_remove( &writer
->entry
);
532 release_object( writer
->mailslot
);
535 static enum server_fd_type
mail_writer_get_fd_type( struct fd
*fd
)
537 return FD_TYPE_MAILSLOT
;
540 static struct fd
*mail_writer_get_fd( struct object
*obj
)
542 struct mail_writer
*writer
= (struct mail_writer
*) obj
;
543 return (struct fd
*)grab_object( writer
->fd
);
546 static unsigned int mail_writer_map_access( struct object
*obj
, unsigned int access
)
548 /* mailslot writers can only get write access */
549 if (access
& GENERIC_WRITE
) access
|= FILE_GENERIC_WRITE
;
550 if (access
& GENERIC_ALL
) access
|= FILE_GENERIC_WRITE
;
551 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
554 static struct mailslot
*get_mailslot_obj( struct process
*process
, obj_handle_t handle
,
555 unsigned int access
)
557 return (struct mailslot
*)get_handle_obj( process
, handle
, access
, &mailslot_ops
);
561 /* create a mailslot */
562 DECL_HANDLER(create_mailslot
)
564 struct mailslot
*mailslot
;
565 struct unicode_str name
;
567 const struct security_descriptor
*sd
;
568 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
570 if (!objattr
) return;
572 if (!name
.len
) /* mailslots need a root directory even without a name */
574 if (!objattr
->rootdir
)
576 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
579 if (!(root
= get_directory_obj( current
->process
, objattr
->rootdir
))) return;
582 if ((mailslot
= create_mailslot( root
, &name
, objattr
->attributes
, req
->max_msgsize
,
583 req
->read_timeout
, sd
)))
585 reply
->handle
= alloc_handle( current
->process
, mailslot
, req
->access
, objattr
->attributes
);
586 release_object( mailslot
);
589 if (root
) release_object( root
);
593 /* set mailslot information */
594 DECL_HANDLER(set_mailslot_info
)
596 struct mailslot
*mailslot
= get_mailslot_obj( current
->process
, req
->handle
, 0 );
600 if (req
->flags
& MAILSLOT_SET_READ_TIMEOUT
)
601 mailslot
->read_timeout
= req
->read_timeout
;
602 reply
->max_msgsize
= mailslot
->max_msgsize
;
603 reply
->read_timeout
= mailslot
->read_timeout
;
604 release_object( mailslot
);