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 &file_type
, /* type */
77 mailslot_dump
, /* dump */
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 no_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 &file_type
, /* type */
135 mail_writer_dump
, /* dump */
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 no_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
*mailslot_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
188 unsigned int attr
, struct object
*root
);
189 static struct object
*mailslot_device_open_file( struct object
*obj
, unsigned int access
,
190 unsigned int sharing
, unsigned int options
);
191 static void mailslot_device_destroy( struct object
*obj
);
193 static const struct object_ops mailslot_device_ops
=
195 sizeof(struct mailslot_device
), /* size */
196 &device_type
, /* type */
197 mailslot_device_dump
, /* dump */
198 no_add_queue
, /* add_queue */
199 NULL
, /* remove_queue */
201 no_satisfied
, /* satisfied */
202 no_signal
, /* signal */
203 no_get_fd
, /* get_fd */
204 default_map_access
, /* map_access */
205 default_get_sd
, /* get_sd */
206 default_set_sd
, /* set_sd */
207 default_get_full_name
, /* get_full_name */
208 mailslot_device_lookup_name
, /* lookup_name */
209 directory_link_name
, /* link_name */
210 default_unlink_name
, /* unlink_name */
211 mailslot_device_open_file
, /* open_file */
212 no_kernel_obj_list
, /* get_kernel_obj_list */
213 no_close_handle
, /* close_handle */
214 mailslot_device_destroy
/* destroy */
217 static void mailslot_device_file_dump( struct object
*obj
, int verbose
);
218 static struct fd
*mailslot_device_file_get_fd( struct object
*obj
);
219 static WCHAR
*mailslot_device_file_get_full_name( struct object
*obj
, data_size_t
*len
);
220 static void mailslot_device_file_destroy( struct object
*obj
);
221 static enum server_fd_type
mailslot_device_file_get_fd_type( struct fd
*fd
);
223 static const struct object_ops mailslot_device_file_ops
=
225 sizeof(struct mailslot_device_file
), /* size */
226 &file_type
, /* type */
227 mailslot_device_file_dump
, /* dump */
228 add_queue
, /* add_queue */
229 remove_queue
, /* remove_queue */
230 default_fd_signaled
, /* signaled */
231 no_satisfied
, /* satisfied */
232 no_signal
, /* signal */
233 mailslot_device_file_get_fd
, /* get_fd */
234 default_map_access
, /* map_access */
235 default_get_sd
, /* get_sd */
236 default_set_sd
, /* set_sd */
237 mailslot_device_file_get_full_name
, /* get_full_name */
238 no_lookup_name
, /* lookup_name */
239 no_link_name
, /* link_name */
240 NULL
, /* unlink_name */
241 no_open_file
, /* open_file */
242 no_kernel_obj_list
, /* get_kernel_obj_list */
243 no_close_handle
, /* close_handle */
244 mailslot_device_file_destroy
/* destroy */
247 static const struct fd_ops mailslot_device_fd_ops
=
249 default_fd_get_poll_events
, /* get_poll_events */
250 default_poll_event
, /* poll_event */
251 mailslot_device_file_get_fd_type
, /* get_fd_type */
252 no_fd_read
, /* read */
253 no_fd_write
, /* write */
254 no_fd_flush
, /* flush */
255 default_fd_get_file_info
, /* get_file_info */
256 no_fd_get_volume_info
, /* get_volume_info */
257 default_fd_ioctl
, /* ioctl */
258 default_fd_queue_async
, /* queue_async */
259 default_fd_reselect_async
/* reselect_async */
262 static void mailslot_destroy( struct object
*obj
)
264 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
266 assert( mailslot
->fd
);
268 if (mailslot
->write_fd
!= -1)
270 shutdown( mailslot
->write_fd
, SHUT_RDWR
);
271 close( mailslot
->write_fd
);
273 release_object( mailslot
->fd
);
276 static void mailslot_dump( struct object
*obj
, int verbose
)
278 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
280 assert( obj
->ops
== &mailslot_ops
);
281 fprintf( stderr
, "Mailslot max_msgsize=%d read_timeout=%s\n",
282 mailslot
->max_msgsize
, get_timeout_str(mailslot
->read_timeout
) );
285 static enum server_fd_type
mailslot_get_fd_type( struct fd
*fd
)
287 return FD_TYPE_MAILSLOT
;
290 static struct fd
*mailslot_get_fd( struct object
*obj
)
292 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
294 return (struct fd
*)grab_object( mailslot
->fd
);
297 static unsigned int mailslot_map_access( struct object
*obj
, unsigned int access
)
299 /* mailslots can only be read */
300 return default_map_access( obj
, access
) & FILE_GENERIC_READ
;
303 static int mailslot_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
305 struct mailslot_device
*dev
= (struct mailslot_device
*)parent
;
307 if (parent
->ops
!= &mailslot_device_ops
)
309 set_error( STATUS_OBJECT_NAME_INVALID
);
312 namespace_add( dev
->mailslots
, name
);
313 name
->parent
= grab_object( parent
);
317 static struct object
*mailslot_open_file( struct object
*obj
, unsigned int access
,
318 unsigned int sharing
, unsigned int options
)
320 struct mailslot
*mailslot
= (struct mailslot
*)obj
;
321 struct mail_writer
*writer
;
324 if (!(sharing
& FILE_SHARE_READ
))
326 set_error( STATUS_SHARING_VIOLATION
);
330 if (!list_empty( &mailslot
->writers
))
332 /* Readers and writers cannot be mixed.
333 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
335 writer
= LIST_ENTRY( list_head(&mailslot
->writers
), struct mail_writer
, entry
);
337 if (((access
& (GENERIC_WRITE
|FILE_WRITE_DATA
)) || (writer
->access
& FILE_WRITE_DATA
)) &&
338 !((sharing
& FILE_SHARE_WRITE
) && (writer
->sharing
& FILE_SHARE_WRITE
)))
340 set_error( STATUS_SHARING_VIOLATION
);
345 if ((unix_fd
= dup( mailslot
->write_fd
)) == -1)
350 if (!(writer
= alloc_object( &mail_writer_ops
)))
355 grab_object( mailslot
);
356 writer
->mailslot
= mailslot
;
357 writer
->access
= mail_writer_map_access( &writer
->obj
, access
);
358 writer
->sharing
= sharing
;
359 list_add_head( &mailslot
->writers
, &writer
->entry
);
361 if (!(writer
->fd
= create_anonymous_fd( &mail_writer_fd_ops
, unix_fd
, &writer
->obj
, options
)))
363 release_object( writer
);
366 allow_fd_caching( writer
->fd
);
370 static void mailslot_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
372 struct mailslot
*mailslot
= get_fd_user( fd
);
374 assert(mailslot
->obj
.ops
== &mailslot_ops
);
376 fd_queue_async( fd
, async
, type
);
377 async_set_timeout( async
, mailslot
->read_timeout
? mailslot
->read_timeout
: -1,
379 set_error( STATUS_PENDING
);
382 static void mailslot_device_dump( struct object
*obj
, int verbose
)
384 fputs( "Mailslot device\n", stderr
);
387 static struct object
*mailslot_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
388 unsigned int attr
, struct object
*root
)
390 struct mailslot_device
*device
= (struct mailslot_device
*)obj
;
391 struct object
*found
;
393 assert( obj
->ops
== &mailslot_device_ops
);
395 if (!name
) return NULL
; /* open the device itself */
397 if ((found
= find_object( device
->mailslots
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
403 static struct object
*mailslot_device_open_file( struct object
*obj
, unsigned int access
,
404 unsigned int sharing
, unsigned int options
)
406 struct mailslot_device_file
*file
;
408 if (!(file
= alloc_object( &mailslot_device_file_ops
))) return NULL
;
409 file
->device
= (struct mailslot_device
*)grab_object( obj
);
410 if (!(file
->fd
= alloc_pseudo_fd( &mailslot_device_fd_ops
, obj
, options
)))
412 release_object( file
);
415 allow_fd_caching( file
->fd
);
419 static void mailslot_device_destroy( struct object
*obj
)
421 struct mailslot_device
*device
= (struct mailslot_device
*)obj
;
422 assert( obj
->ops
== &mailslot_device_ops
);
423 free( device
->mailslots
);
426 struct object
*create_mailslot_device( struct object
*root
, const struct unicode_str
*name
,
427 unsigned int attr
, const struct security_descriptor
*sd
)
429 struct mailslot_device
*dev
;
431 if ((dev
= create_named_object( root
, &mailslot_device_ops
, name
, attr
, sd
)) &&
432 get_error() != STATUS_OBJECT_NAME_EXISTS
)
434 dev
->mailslots
= NULL
;
435 if (!(dev
->mailslots
= create_namespace( 7 )))
437 release_object( dev
);
444 static void mailslot_device_file_dump( struct object
*obj
, int verbose
)
446 struct mailslot_device_file
*file
= (struct mailslot_device_file
*)obj
;
448 fprintf( stderr
, "File on mailslot device %p\n", file
->device
);
451 static struct fd
*mailslot_device_file_get_fd( struct object
*obj
)
453 struct mailslot_device_file
*file
= (struct mailslot_device_file
*)obj
;
454 return (struct fd
*)grab_object( file
->fd
);
457 static WCHAR
*mailslot_device_file_get_full_name( struct object
*obj
, data_size_t
*len
)
459 struct mailslot_device_file
*file
= (struct mailslot_device_file
*)obj
;
460 return file
->device
->obj
.ops
->get_full_name( &file
->device
->obj
, len
);
463 static void mailslot_device_file_destroy( struct object
*obj
)
465 struct mailslot_device_file
*file
= (struct mailslot_device_file
*)obj
;
466 assert( obj
->ops
== &mailslot_device_file_ops
);
467 if (file
->fd
) release_object( file
->fd
);
468 release_object( file
->device
);
471 static enum server_fd_type
mailslot_device_file_get_fd_type( struct fd
*fd
)
473 return FD_TYPE_DEVICE
;
476 static struct mailslot
*create_mailslot( struct object
*root
,
477 const struct unicode_str
*name
, unsigned int attr
,
478 int max_msgsize
, timeout_t read_timeout
,
479 const struct security_descriptor
*sd
)
481 struct mailslot
*mailslot
;
484 if (!(mailslot
= create_named_object( root
, &mailslot_ops
, name
, attr
, sd
))) return NULL
;
487 mailslot
->write_fd
= -1;
488 mailslot
->max_msgsize
= max_msgsize
;
489 mailslot
->read_timeout
= read_timeout
;
490 list_init( &mailslot
->writers
);
492 if (!socketpair( PF_UNIX
, SOCK_DGRAM
, 0, fds
))
494 fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
495 fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
496 shutdown( fds
[0], SHUT_RD
);
497 mailslot
->write_fd
= fds
[0];
498 if ((mailslot
->fd
= create_anonymous_fd( &mailslot_fd_ops
, fds
[1], &mailslot
->obj
,
499 FILE_SYNCHRONOUS_IO_NONALERT
)))
501 allow_fd_caching( mailslot
->fd
);
505 else file_set_error();
507 release_object( mailslot
);
511 static void mail_writer_dump( struct object
*obj
, int verbose
)
513 fprintf( stderr
, "Mailslot writer\n" );
516 static void mail_writer_destroy( struct object
*obj
)
518 struct mail_writer
*writer
= (struct mail_writer
*) obj
;
520 if (writer
->fd
) release_object( writer
->fd
);
521 list_remove( &writer
->entry
);
522 release_object( writer
->mailslot
);
525 static enum server_fd_type
mail_writer_get_fd_type( struct fd
*fd
)
527 return FD_TYPE_MAILSLOT
;
530 static struct fd
*mail_writer_get_fd( struct object
*obj
)
532 struct mail_writer
*writer
= (struct mail_writer
*) obj
;
533 return (struct fd
*)grab_object( writer
->fd
);
536 static unsigned int mail_writer_map_access( struct object
*obj
, unsigned int access
)
538 /* mailslot writers can only get write access */
539 return default_map_access( obj
, access
) & FILE_GENERIC_WRITE
;
542 static struct mailslot
*get_mailslot_obj( struct process
*process
, obj_handle_t handle
,
543 unsigned int access
)
545 return (struct mailslot
*)get_handle_obj( process
, handle
, access
, &mailslot_ops
);
549 /* create a mailslot */
550 DECL_HANDLER(create_mailslot
)
552 struct mailslot
*mailslot
;
553 struct unicode_str name
;
555 const struct security_descriptor
*sd
;
556 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
558 if (!objattr
) return;
560 if (!name
.len
) /* mailslots need a root directory even without a name */
562 if (!objattr
->rootdir
)
564 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
567 if (!(root
= get_directory_obj( current
->process
, objattr
->rootdir
))) return;
570 if ((mailslot
= create_mailslot( root
, &name
, objattr
->attributes
, req
->max_msgsize
,
571 req
->read_timeout
, sd
)))
573 reply
->handle
= alloc_handle( current
->process
, mailslot
, req
->access
, objattr
->attributes
);
574 release_object( mailslot
);
577 if (root
) release_object( root
);
581 /* set mailslot information */
582 DECL_HANDLER(set_mailslot_info
)
584 struct mailslot
*mailslot
= get_mailslot_obj( current
->process
, req
->handle
, 0 );
588 if (req
->flags
& MAILSLOT_SET_READ_TIMEOUT
)
589 mailslot
->read_timeout
= req
->read_timeout
;
590 reply
->max_msgsize
= mailslot
->max_msgsize
;
591 reply
->read_timeout
= mailslot
->read_timeout
;
592 release_object( mailslot
);