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
;
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
* );
73 static const struct object_ops mailslot_ops
=
75 sizeof(struct mailslot
), /* size */
76 mailslot_dump
, /* dump */
77 add_queue
, /* add_queue */
78 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 */
90 static enum server_fd_type
mailslot_get_fd_type( struct fd
*fd
);
91 static void mailslot_queue_async( struct fd
*fd
, const async_data_t
*data
, int type
, int count
);
93 static const struct fd_ops mailslot_fd_ops
=
95 default_fd_get_poll_events
, /* get_poll_events */
96 default_poll_event
, /* poll_event */
98 mailslot_get_fd_type
, /* get_fd_type */
99 mailslot_queue_async
, /* queue_async */
100 default_fd_reselect_async
, /* reselect_async */
101 default_fd_cancel_async
/* cancel_async */
109 struct mailslot
*mailslot
;
112 unsigned int sharing
;
115 static void mail_writer_dump( struct object
*obj
, int verbose
);
116 static struct fd
*mail_writer_get_fd( struct object
*obj
);
117 static unsigned int mail_writer_map_access( struct object
*obj
, unsigned int access
);
118 static void mail_writer_destroy( struct object
*obj
);
120 static const struct object_ops mail_writer_ops
=
122 sizeof(struct mail_writer
), /* size */
123 mail_writer_dump
, /* dump */
124 no_add_queue
, /* add_queue */
125 NULL
, /* remove_queue */
127 NULL
, /* satisfied */
128 no_signal
, /* signal */
129 mail_writer_get_fd
, /* get_fd */
130 mail_writer_map_access
, /* map_access */
131 no_lookup_name
, /* lookup_name */
132 no_open_file
, /* open_file */
133 fd_close_handle
, /* close_handle */
134 mail_writer_destroy
/* destroy */
137 static enum server_fd_type
mail_writer_get_fd_type( struct fd
*fd
);
139 static const struct fd_ops mail_writer_fd_ops
=
141 default_fd_get_poll_events
, /* get_poll_events */
142 default_poll_event
, /* poll_event */
143 no_flush
, /* flush */
144 mail_writer_get_fd_type
, /* get_fd_type */
145 default_fd_queue_async
, /* queue_async */
146 default_fd_reselect_async
, /* reselect_async */
147 default_fd_cancel_async
/* cancel_async */
151 struct mailslot_device
153 struct object obj
; /* object header */
154 struct fd
*fd
; /* pseudo-fd for ioctls */
155 struct namespace *mailslots
; /* mailslot namespace */
158 static void mailslot_device_dump( struct object
*obj
, int verbose
);
159 static struct fd
*mailslot_device_get_fd( struct object
*obj
);
160 static struct object
*mailslot_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
162 static struct object
*mailslot_device_open_file( struct object
*obj
, unsigned int access
,
163 unsigned int sharing
, unsigned int options
);
164 static void mailslot_device_destroy( struct object
*obj
);
165 static enum server_fd_type
mailslot_device_get_fd_type( struct fd
*fd
);
167 static const struct object_ops mailslot_device_ops
=
169 sizeof(struct mailslot_device
), /* size */
170 mailslot_device_dump
, /* dump */
171 no_add_queue
, /* add_queue */
172 NULL
, /* remove_queue */
174 no_satisfied
, /* satisfied */
175 no_signal
, /* signal */
176 mailslot_device_get_fd
, /* get_fd */
177 no_map_access
, /* map_access */
178 mailslot_device_lookup_name
, /* lookup_name */
179 mailslot_device_open_file
, /* open_file */
180 fd_close_handle
, /* close_handle */
181 mailslot_device_destroy
/* destroy */
184 static const struct fd_ops mailslot_device_fd_ops
=
186 default_fd_get_poll_events
, /* get_poll_events */
187 default_poll_event
, /* poll_event */
188 no_flush
, /* flush */
189 mailslot_device_get_fd_type
, /* get_fd_type */
190 default_fd_queue_async
, /* queue_async */
191 default_fd_reselect_async
, /* reselect_async */
192 default_fd_cancel_async
/* cancel_async */
195 static void mailslot_destroy( struct object
*obj
)
197 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
199 assert( mailslot
->fd
);
201 if (mailslot
->write_fd
!= -1)
203 shutdown( mailslot
->write_fd
, SHUT_RDWR
);
204 close( mailslot
->write_fd
);
206 release_object( mailslot
->fd
);
209 static void mailslot_dump( struct object
*obj
, int verbose
)
211 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
213 assert( obj
->ops
== &mailslot_ops
);
214 fprintf( stderr
, "Mailslot max_msgsize=%d read_timeout=%d\n",
215 mailslot
->max_msgsize
, mailslot
->read_timeout
);
218 static enum server_fd_type
mailslot_get_fd_type( struct fd
*fd
)
220 return FD_TYPE_MAILSLOT
;
223 static struct fd
*mailslot_get_fd( struct object
*obj
)
225 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
227 return (struct fd
*)grab_object( mailslot
->fd
);
230 static unsigned int mailslot_map_access( struct object
*obj
, unsigned int access
)
232 /* mailslots can only be read */
233 if (access
& GENERIC_READ
) access
|= FILE_GENERIC_READ
;
234 if (access
& GENERIC_ALL
) access
|= FILE_GENERIC_READ
;
235 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
238 static struct object
*mailslot_open_file( struct object
*obj
, unsigned int access
,
239 unsigned int sharing
, unsigned int options
)
241 struct mailslot
*mailslot
= (struct mailslot
*)obj
;
242 struct mail_writer
*writer
;
245 if (!(sharing
& FILE_SHARE_READ
))
247 set_error( STATUS_SHARING_VIOLATION
);
251 if (!list_empty( &mailslot
->writers
))
253 /* Readers and writers cannot be mixed.
254 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
256 writer
= LIST_ENTRY( list_head(&mailslot
->writers
), struct mail_writer
, entry
);
258 if (((access
& (GENERIC_WRITE
|FILE_WRITE_DATA
)) || (writer
->access
& FILE_WRITE_DATA
)) &&
259 !((sharing
& FILE_SHARE_WRITE
) && (writer
->sharing
& FILE_SHARE_WRITE
)))
261 set_error( STATUS_SHARING_VIOLATION
);
266 if ((unix_fd
= dup( mailslot
->write_fd
)) == -1)
271 if (!(writer
= alloc_object( &mail_writer_ops
)))
276 grab_object( mailslot
);
277 writer
->mailslot
= mailslot
;
278 writer
->access
= mail_writer_map_access( &writer
->obj
, access
);
279 writer
->sharing
= sharing
;
280 list_add_head( &mailslot
->writers
, &writer
->entry
);
282 if (!(writer
->fd
= create_anonymous_fd( &mail_writer_fd_ops
, unix_fd
, &writer
->obj
, options
)))
284 release_object( writer
);
290 static void mailslot_queue_async( struct fd
*fd
, const async_data_t
*data
, int type
, int count
)
292 struct mailslot
*mailslot
= get_fd_user( fd
);
295 assert(mailslot
->obj
.ops
== &mailslot_ops
);
297 if ((async
= fd_queue_async( fd
, data
, type
, count
)))
299 if (mailslot
->read_timeout
!= -1)
301 struct timeval when
= current_time
;
302 add_timeout( &when
, max(1,mailslot
->read_timeout
) );
303 async_set_timeout( async
, &when
, STATUS_IO_TIMEOUT
);
305 release_object( async
);
306 set_error( STATUS_PENDING
);
310 static void mailslot_device_dump( struct object
*obj
, int verbose
)
312 assert( obj
->ops
== &mailslot_device_ops
);
313 fprintf( stderr
, "Mail slot device\n" );
316 static struct fd
*mailslot_device_get_fd( struct object
*obj
)
318 struct mailslot_device
*device
= (struct mailslot_device
*)obj
;
319 return (struct fd
*)grab_object( device
->fd
);
322 static struct object
*mailslot_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
325 struct mailslot_device
*device
= (struct mailslot_device
*)obj
;
326 struct object
*found
;
328 assert( obj
->ops
== &mailslot_device_ops
);
330 if ((found
= find_object( device
->mailslots
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
336 static struct object
*mailslot_device_open_file( struct object
*obj
, unsigned int access
,
337 unsigned int sharing
, unsigned int options
)
339 return grab_object( obj
);
342 static void mailslot_device_destroy( struct object
*obj
)
344 struct mailslot_device
*device
= (struct mailslot_device
*)obj
;
345 assert( obj
->ops
== &mailslot_device_ops
);
346 if (device
->fd
) release_object( device
->fd
);
347 free( device
->mailslots
);
350 static enum server_fd_type
mailslot_device_get_fd_type( struct fd
*fd
)
352 return FD_TYPE_DEVICE
;
355 void create_mailslot_device( struct directory
*root
, const struct unicode_str
*name
)
357 struct mailslot_device
*dev
;
359 if ((dev
= create_named_object_dir( root
, name
, 0, &mailslot_device_ops
)) &&
360 get_error() != STATUS_OBJECT_NAME_EXISTS
)
362 dev
->mailslots
= NULL
;
363 if (!(dev
->fd
= alloc_pseudo_fd( &mailslot_device_fd_ops
, &dev
->obj
)) ||
364 !(dev
->mailslots
= create_namespace( 7 )))
366 release_object( dev
);
370 if (dev
) make_object_static( &dev
->obj
);
373 static struct mailslot
*create_mailslot( struct directory
*root
,
374 const struct unicode_str
*name
, unsigned int attr
,
375 int max_msgsize
, int read_timeout
)
378 struct unicode_str new_name
;
379 struct mailslot_device
*dev
;
380 struct mailslot
*mailslot
;
383 if (!name
|| !name
->len
) return alloc_object( &mailslot_ops
);
385 if (!(obj
= find_object_dir( root
, name
, attr
, &new_name
)))
387 set_error( STATUS_OBJECT_NAME_INVALID
);
393 if (attr
& OBJ_OPENIF
&& obj
->ops
== &mailslot_ops
)
394 /* it already exists - there can only be one mailslot to read from */
395 set_error( STATUS_OBJECT_NAME_EXISTS
);
396 else if (attr
& OBJ_OPENIF
)
397 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
399 set_error( STATUS_OBJECT_NAME_COLLISION
);
400 release_object( obj
);
404 if (obj
->ops
!= &mailslot_device_ops
)
406 set_error( STATUS_OBJECT_NAME_INVALID
);
407 release_object( obj
);
411 dev
= (struct mailslot_device
*)obj
;
412 mailslot
= create_object( dev
->mailslots
, &mailslot_ops
, &new_name
, NULL
);
413 release_object( dev
);
415 if (!mailslot
) return NULL
;
418 mailslot
->write_fd
= -1;
419 mailslot
->max_msgsize
= max_msgsize
;
420 mailslot
->read_timeout
= read_timeout
;
421 list_init( &mailslot
->writers
);
423 if (!socketpair( PF_UNIX
, SOCK_DGRAM
, 0, fds
))
425 fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
426 fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
427 shutdown( fds
[0], SHUT_RD
);
428 mailslot
->write_fd
= fds
[0];
429 mailslot
->fd
= create_anonymous_fd( &mailslot_fd_ops
, fds
[1], &mailslot
->obj
,
430 FILE_SYNCHRONOUS_IO_NONALERT
);
431 if (mailslot
->fd
) return mailslot
;
433 else file_set_error();
435 release_object( mailslot
);
439 static void mail_writer_dump( struct object
*obj
, int verbose
)
441 fprintf( stderr
, "Mailslot writer\n" );
444 static void mail_writer_destroy( struct object
*obj
)
446 struct mail_writer
*writer
= (struct mail_writer
*) obj
;
448 if (writer
->fd
) release_object( writer
->fd
);
449 list_remove( &writer
->entry
);
450 release_object( writer
->mailslot
);
453 static enum server_fd_type
mail_writer_get_fd_type( struct fd
*fd
)
455 return FD_TYPE_MAILSLOT
;
458 static struct fd
*mail_writer_get_fd( struct object
*obj
)
460 struct mail_writer
*writer
= (struct mail_writer
*) obj
;
461 return (struct fd
*)grab_object( writer
->fd
);
464 static unsigned int mail_writer_map_access( struct object
*obj
, unsigned int access
)
466 /* mailslot writers can only get write access */
467 if (access
& GENERIC_WRITE
) access
|= FILE_GENERIC_WRITE
;
468 if (access
& GENERIC_ALL
) access
|= FILE_GENERIC_WRITE
;
469 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
472 static struct mailslot
*get_mailslot_obj( struct process
*process
, obj_handle_t handle
,
473 unsigned int access
)
475 return (struct mailslot
*)get_handle_obj( process
, handle
, access
, &mailslot_ops
);
479 /* create a mailslot */
480 DECL_HANDLER(create_mailslot
)
482 struct mailslot
*mailslot
;
483 struct unicode_str name
;
484 struct directory
*root
= NULL
;
487 get_req_unicode_str( &name
);
488 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
491 if ((mailslot
= create_mailslot( root
, &name
, req
->attributes
, req
->max_msgsize
,
492 req
->read_timeout
)))
494 reply
->handle
= alloc_handle( current
->process
, mailslot
, req
->access
, req
->attributes
);
495 release_object( mailslot
);
498 if (root
) release_object( root
);
502 /* set mailslot information */
503 DECL_HANDLER(set_mailslot_info
)
505 struct mailslot
*mailslot
= get_mailslot_obj( current
->process
, req
->handle
, 0 );
509 if (req
->flags
& MAILSLOT_SET_READ_TIMEOUT
)
510 mailslot
->read_timeout
= req
->read_timeout
;
511 reply
->max_msgsize
= mailslot
->max_msgsize
;
512 reply
->read_timeout
= mailslot
->read_timeout
;
513 release_object( mailslot
);