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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 struct mailslot_device
67 struct object obj
; /* object header */
68 struct namespace *mailslots
; /* mailslot namespace */
71 /* mailslot functions */
72 static void mailslot_dump( struct object
*, int );
73 static struct fd
*mailslot_get_fd( struct object
* );
74 static void mailslot_destroy( struct object
* );
76 static const struct object_ops mailslot_ops
=
78 sizeof(struct mailslot
), /* size */
79 mailslot_dump
, /* dump */
80 default_fd_add_queue
, /* add_queue */
81 default_fd_remove_queue
, /* remove_queue */
82 default_fd_signaled
, /* signaled */
83 no_satisfied
, /* satisfied */
84 no_signal
, /* signal */
85 mailslot_get_fd
, /* get_fd */
86 no_lookup_name
, /* lookup_name */
87 no_close_handle
, /* close_handle */
88 mailslot_destroy
/* destroy */
91 static int mailslot_get_info( struct fd
* );
92 static void mailslot_queue_async( struct fd
*, void*, void*, void*, int, int );
94 static const struct fd_ops mailslot_fd_ops
=
96 default_fd_get_poll_events
, /* get_poll_events */
97 default_poll_event
, /* poll_event */
99 mailslot_get_info
, /* get_file_info */
100 mailslot_queue_async
, /* queue_async */
101 default_fd_cancel_async
/* cancel_async */
107 struct mailslot
*mailslot
;
113 static void mail_writer_dump( struct object
*obj
, int verbose
);
114 static struct fd
*mail_writer_get_fd( struct object
*obj
);
115 static void mail_writer_destroy( struct object
*obj
);
117 static const struct object_ops mail_writer_ops
=
119 sizeof(struct mail_writer
), /* size */
120 mail_writer_dump
, /* dump */
121 no_add_queue
, /* add_queue */
122 NULL
, /* remove_queue */
124 NULL
, /* satisfied */
125 no_signal
, /* signal */
126 mail_writer_get_fd
, /* get_fd */
127 no_lookup_name
, /* lookup_name */
128 no_close_handle
, /* close_handle */
129 mail_writer_destroy
/* destroy */
132 static int mail_writer_get_info( struct fd
*fd
);
134 static const struct fd_ops mail_writer_fd_ops
=
136 NULL
, /* get_poll_events */
137 NULL
, /* poll_event */
138 no_flush
, /* flush */
139 mail_writer_get_info
, /* get_file_info */
140 no_queue_async
, /* queue_async */
141 NULL
/* cancel_async */
144 static void mailslot_device_dump( struct object
*obj
, int verbose
);
145 static struct object
*mailslot_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
147 static void mailslot_device_destroy( struct object
*obj
);
149 static const struct object_ops mailslot_device_ops
=
151 sizeof(struct mailslot_device
), /* size */
152 mailslot_device_dump
, /* dump */
153 no_add_queue
, /* add_queue */
154 NULL
, /* remove_queue */
156 no_satisfied
, /* satisfied */
157 no_signal
, /* signal */
158 no_get_fd
, /* get_fd */
159 mailslot_device_lookup_name
, /* lookup_name */
160 no_close_handle
, /* close_handle */
161 mailslot_device_destroy
/* destroy */
164 static void mailslot_destroy( struct object
*obj
)
166 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
168 assert( mailslot
->fd
);
169 assert( mailslot
->write_fd
);
171 release_object( mailslot
->fd
);
172 release_object( mailslot
->write_fd
);
175 static void mailslot_dump( struct object
*obj
, int verbose
)
177 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
179 assert( obj
->ops
== &mailslot_ops
);
180 fprintf( stderr
, "Mailslot max_msgsize=%d read_timeout=%d\n",
181 mailslot
->max_msgsize
, mailslot
->read_timeout
);
184 static int mailslot_message_count(struct mailslot
*mailslot
)
188 /* poll the socket to see if there's any messages */
189 pfd
.fd
= get_unix_fd( mailslot
->fd
);
192 return (poll( &pfd
, 1, 0 ) == 1) ? 1 : 0;
195 static int mailslot_next_msg_size( struct mailslot
*mailslot
)
200 fd
= get_unix_fd( mailslot
->fd
);
201 ioctl( fd
, FIONREAD
, &size
);
205 static int mailslot_get_info( struct fd
*fd
)
207 struct mailslot
*mailslot
= get_fd_user( fd
);
208 assert( mailslot
->obj
.ops
== &mailslot_ops
);
209 return FD_FLAG_TIMEOUT
| FD_FLAG_AVAILABLE
;
212 static struct fd
*mailslot_get_fd( struct object
*obj
)
214 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
216 return (struct fd
*)grab_object( mailslot
->fd
);
219 static void mailslot_queue_async( struct fd
*fd
, void *apc
, void *user
,
220 void *iosb
, int type
, int count
)
222 struct mailslot
*mailslot
= get_fd_user( fd
);
225 assert(mailslot
->obj
.ops
== &mailslot_ops
);
227 if (type
!= ASYNC_TYPE_READ
)
229 set_error(STATUS_INVALID_PARAMETER
);
233 if (list_empty( &mailslot
->writers
) ||
234 !mailslot_message_count( mailslot
))
236 set_error(STATUS_IO_TIMEOUT
);
240 if (mailslot
->read_timeout
!= -1) timeout
= &mailslot
->read_timeout
;
242 fd_queue_async_timeout( fd
, apc
, user
, iosb
, type
, count
, timeout
);
245 static void mailslot_device_dump( struct object
*obj
, int verbose
)
247 assert( obj
->ops
== &mailslot_device_ops
);
248 fprintf( stderr
, "Mail slot device\n" );
251 static struct object
*mailslot_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
254 struct mailslot_device
*device
= (struct mailslot_device
*)obj
;
255 struct object
*found
;
257 assert( obj
->ops
== &mailslot_device_ops
);
259 if ((found
= find_object( device
->mailslots
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
265 static void mailslot_device_destroy( struct object
*obj
)
267 struct mailslot_device
*device
= (struct mailslot_device
*)obj
;
268 assert( obj
->ops
== &mailslot_device_ops
);
269 free( device
->mailslots
);
272 struct mailslot_device
*create_mailslot_device( struct directory
*root
, const struct unicode_str
*name
)
274 struct mailslot_device
*dev
;
276 if ((dev
= create_named_object_dir( root
, name
, 0, &mailslot_device_ops
)) &&
277 get_error() != STATUS_OBJECT_NAME_EXISTS
)
279 if (!(dev
->mailslots
= create_namespace( 7 )))
281 release_object( dev
);
288 static struct mailslot
*create_mailslot( struct directory
*root
,
289 const struct unicode_str
*name
, unsigned int attr
,
290 int max_msgsize
, int read_timeout
)
293 struct unicode_str new_name
;
294 struct mailslot_device
*dev
;
295 struct mailslot
*mailslot
;
298 if (!name
|| !name
->len
) return alloc_object( &mailslot_ops
);
300 if (!(obj
= find_object_dir( root
, name
, attr
, &new_name
))) return NULL
;
304 if (attr
& OBJ_OPENIF
&& obj
->ops
== &mailslot_ops
)
305 /* it already exists - there can only be one mailslot to read from */
306 set_error( STATUS_OBJECT_NAME_EXISTS
);
307 else if (attr
& OBJ_OPENIF
)
308 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
310 set_error( STATUS_OBJECT_NAME_COLLISION
);
311 release_object( obj
);
315 if (obj
->ops
!= &mailslot_device_ops
)
317 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
318 release_object( obj
);
322 dev
= (struct mailslot_device
*)obj
;
323 mailslot
= create_object( dev
->mailslots
, &mailslot_ops
, &new_name
, NULL
);
324 release_object( dev
);
326 if (!mailslot
) return NULL
;
329 mailslot
->write_fd
= NULL
;
330 mailslot
->max_msgsize
= max_msgsize
;
331 mailslot
->read_timeout
= read_timeout
;
332 list_init( &mailslot
->writers
);
334 if (!socketpair( PF_UNIX
, SOCK_DGRAM
, 0, fds
))
336 fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
337 fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
338 mailslot
->fd
= create_anonymous_fd( &mailslot_fd_ops
,
339 fds
[1], &mailslot
->obj
);
340 mailslot
->write_fd
= create_anonymous_fd( &mail_writer_fd_ops
,
341 fds
[0], &mailslot
->obj
);
342 if (mailslot
->fd
&& mailslot
->write_fd
) return mailslot
;
344 else file_set_error();
346 release_object( mailslot
);
350 static void mail_writer_dump( struct object
*obj
, int verbose
)
352 fprintf( stderr
, "Mailslot writer\n" );
355 static void mail_writer_destroy( struct object
*obj
)
357 struct mail_writer
*writer
= (struct mail_writer
*) obj
;
359 list_remove( &writer
->entry
);
360 release_object( writer
->mailslot
);
363 static int mail_writer_get_info( struct fd
*fd
)
368 static struct fd
*mail_writer_get_fd( struct object
*obj
)
370 struct mail_writer
*writer
= (struct mail_writer
*) obj
;
372 return (struct fd
*)grab_object( writer
->mailslot
->write_fd
);
376 * Readers and writers cannot be mixed.
377 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
379 static struct mail_writer
*create_mail_writer( struct mailslot
*mailslot
, unsigned int access
,
380 unsigned int sharing
)
382 struct mail_writer
*writer
;
384 if (!list_empty( &mailslot
->writers
))
386 writer
= LIST_ENTRY( list_head(&mailslot
->writers
), struct mail_writer
, entry
);
388 if (((access
& GENERIC_WRITE
) || (writer
->access
& GENERIC_WRITE
)) &&
389 !((sharing
& FILE_SHARE_WRITE
) && (writer
->sharing
& FILE_SHARE_WRITE
)))
391 set_error( STATUS_SHARING_VIOLATION
);
396 writer
= alloc_object( &mail_writer_ops
);
400 grab_object( mailslot
);
401 writer
->mailslot
= mailslot
;
402 writer
->access
= access
;
403 writer
->sharing
= sharing
;
405 list_add_head( &mailslot
->writers
, &writer
->entry
);
410 static struct mailslot
*get_mailslot_obj( struct process
*process
, obj_handle_t handle
,
411 unsigned int access
)
413 return (struct mailslot
*)get_handle_obj( process
, handle
, access
, &mailslot_ops
);
417 /* create a mailslot */
418 DECL_HANDLER(create_mailslot
)
420 struct mailslot
*mailslot
;
421 struct unicode_str name
;
422 struct directory
*root
= NULL
;
425 get_req_unicode_str( &name
);
426 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
429 if ((mailslot
= create_mailslot( root
, &name
, req
->attributes
, req
->max_msgsize
,
430 req
->read_timeout
)))
432 reply
->handle
= alloc_handle( current
->process
, mailslot
, req
->access
, req
->attributes
);
433 release_object( mailslot
);
436 if (root
) release_object( root
);
440 /* open an existing mailslot */
441 DECL_HANDLER(open_mailslot
)
443 struct mailslot
*mailslot
;
444 struct unicode_str name
;
445 struct directory
*root
= NULL
;
448 get_req_unicode_str( &name
);
450 if (!(req
->sharing
& FILE_SHARE_READ
))
452 set_error( STATUS_SHARING_VIOLATION
);
456 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
458 mailslot
= open_object_dir( root
, &name
, req
->attributes
, &mailslot_ops
);
459 if (root
) release_object( root
);
463 struct mail_writer
*writer
;
465 writer
= create_mail_writer( mailslot
, req
->access
, req
->sharing
);
468 reply
->handle
= alloc_handle( current
->process
, writer
, req
->access
, req
->attributes
);
469 release_object( writer
);
471 release_object( mailslot
);
474 set_error( STATUS_NO_SUCH_FILE
);
478 /* set mailslot information */
479 DECL_HANDLER(set_mailslot_info
)
481 struct mailslot
*mailslot
= get_mailslot_obj( current
->process
, req
->handle
, 0 );
485 if (req
->flags
& MAILSLOT_SET_READ_TIMEOUT
)
486 mailslot
->read_timeout
= req
->read_timeout
;
487 reply
->max_msgsize
= mailslot
->max_msgsize
;
488 reply
->read_timeout
= mailslot
->read_timeout
;
489 reply
->msg_count
= mailslot_message_count(mailslot
);
491 /* get the size of the next message */
492 if (reply
->msg_count
)
493 reply
->next_msgsize
= mailslot_next_msg_size(mailslot
);
495 reply
->next_msgsize
= MAILSLOT_NO_MESSAGE
;
497 release_object( mailslot
);