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"
25 #include "wine/unicode.h"
34 #include <sys/types.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 #include <sys/ioctl.h>
39 #ifdef HAVE_SYS_SOCKET_H
40 #include <sys/socket.h>
56 unsigned int max_msgsize
;
57 unsigned int read_timeout
;
62 /* mailslot functions */
63 static void mailslot_dump( struct object
*, int );
64 static struct fd
*mailslot_get_fd( struct object
* );
65 static void mailslot_destroy( struct object
* );
67 static const struct object_ops mailslot_ops
=
69 sizeof(struct mailslot
), /* size */
70 mailslot_dump
, /* dump */
71 default_fd_add_queue
, /* add_queue */
72 default_fd_remove_queue
, /* remove_queue */
73 default_fd_signaled
, /* signaled */
74 no_satisfied
, /* satisfied */
75 mailslot_get_fd
, /* get_fd */
76 mailslot_destroy
/* destroy */
79 static int mailslot_get_poll_events( struct fd
* );
80 static void mailslot_poll_event( struct fd
*, int );
81 static int mailslot_get_info( struct fd
* );
82 static void mailslot_queue_async( struct fd
*, void*, void*, void*, int, int );
83 static void mailslot_cancel_async( struct fd
* );
85 static const struct fd_ops mailslot_fd_ops
=
87 mailslot_get_poll_events
, /* get_poll_events */
88 mailslot_poll_event
, /* poll_event */
90 mailslot_get_info
, /* get_file_info */
91 mailslot_queue_async
, /* queue_async */
92 mailslot_cancel_async
/* cancel_async */
98 struct mailslot
*mailslot
;
104 static void mail_writer_dump( struct object
*obj
, int verbose
);
105 static struct fd
*mail_writer_get_fd( struct object
*obj
);
106 static void mail_writer_destroy( struct object
*obj
);
108 static const struct object_ops mail_writer_ops
=
110 sizeof(struct mail_writer
), /* size */
111 mail_writer_dump
, /* dump */
112 no_add_queue
, /* add_queue */
113 NULL
, /* remove_queue */
115 NULL
, /* satisfied */
116 mail_writer_get_fd
, /* get_fd */
117 mail_writer_destroy
/* destroy */
120 static int mail_writer_get_info( struct fd
*fd
);
122 static const struct fd_ops mail_writer_fd_ops
=
124 NULL
, /* get_poll_events */
125 NULL
, /* poll_event */
126 no_flush
, /* flush */
127 mail_writer_get_info
, /* get_file_info */
128 no_queue_async
, /* queue_async */
129 NULL
/* cancel_async */
132 static void mailslot_destroy( struct object
*obj
)
134 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
136 assert( mailslot
->fd
);
137 assert( mailslot
->write_fd
);
139 async_terminate_queue( &mailslot
->read_q
, STATUS_CANCELLED
);
141 release_object( mailslot
->fd
);
142 release_object( mailslot
->write_fd
);
145 static void mailslot_dump( struct object
*obj
, int verbose
)
147 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
149 assert( obj
->ops
== &mailslot_ops
);
150 fprintf( stderr
, "Mailslot max_msgsize=%d read_timeout=%d\n",
151 mailslot
->max_msgsize
, mailslot
->read_timeout
);
154 static int mailslot_message_count(struct mailslot
*mailslot
)
158 /* poll the socket to see if there's any messages */
159 pfd
.fd
= get_unix_fd( mailslot
->fd
);
162 return (poll( &pfd
, 1, 0 ) == 1) ? 1 : 0;
165 static int mailslot_next_msg_size( struct mailslot
*mailslot
)
170 fd
= get_unix_fd( mailslot
->fd
);
171 ioctl( fd
, FIONREAD
, &size
);
175 static int mailslot_get_info( struct fd
*fd
)
177 struct mailslot
*mailslot
= get_fd_user( fd
);
178 assert( mailslot
->obj
.ops
== &mailslot_ops
);
179 return FD_FLAG_TIMEOUT
| FD_FLAG_AVAILABLE
;
182 static struct fd
*mailslot_get_fd( struct object
*obj
)
184 struct mailslot
*mailslot
= (struct mailslot
*) obj
;
186 return (struct fd
*)grab_object( mailslot
->fd
);
189 static int mailslot_get_poll_events( struct fd
*fd
)
191 struct mailslot
*mailslot
= get_fd_user( fd
);
193 assert( mailslot
->obj
.ops
== &mailslot_ops
);
195 if( !list_empty( &mailslot
->read_q
))
201 static void mailslot_poll_event( struct fd
*fd
, int event
)
203 struct mailslot
*mailslot
= get_fd_user( fd
);
205 if( !list_empty( &mailslot
->read_q
) && (POLLIN
& event
) )
206 async_terminate_head( &mailslot
->read_q
, STATUS_ALERTED
);
208 set_fd_events( fd
, mailslot_get_poll_events(fd
) );
211 static void mailslot_queue_async( struct fd
*fd
, void *apc
, void *user
,
212 void *iosb
, int type
, int count
)
214 struct mailslot
*mailslot
= get_fd_user( fd
);
215 int events
, *ptimeout
= NULL
;
217 assert(mailslot
->obj
.ops
== &mailslot_ops
);
219 if( type
!= ASYNC_TYPE_READ
)
221 set_error(STATUS_INVALID_PARAMETER
);
225 if( list_empty( &mailslot
->writers
) ||
226 !mailslot_message_count( mailslot
))
228 set_error(STATUS_IO_TIMEOUT
);
232 if (mailslot
->read_timeout
!= MAILSLOT_WAIT_FOREVER
)
233 ptimeout
= &mailslot
->read_timeout
;
235 if (!create_async( current
, ptimeout
, &mailslot
->read_q
, apc
, user
, iosb
))
238 /* Check if the new pending request can be served immediately */
239 events
= check_fd_events( fd
, mailslot_get_poll_events( fd
) );
242 mailslot_poll_event( fd
, events
);
246 set_fd_events( fd
, mailslot_get_poll_events( fd
));
249 static void mailslot_cancel_async( struct fd
*fd
)
251 struct mailslot
*mailslot
= get_fd_user( fd
);
253 assert(mailslot
->obj
.ops
== &mailslot_ops
);
254 async_terminate_queue( &mailslot
->read_q
, STATUS_CANCELLED
);
257 static struct mailslot
*create_mailslot( const WCHAR
*name
, size_t len
, int max_msgsize
,
260 struct mailslot
*mailslot
;
262 static const WCHAR slot
[] = {'m','a','i','l','s','l','o','t','\\',0};
264 if( ( len
<= strlenW( slot
) ) || strncmpiW( slot
, name
, strlenW( slot
) ) )
266 set_error( STATUS_OBJECT_NAME_INVALID
);
270 mailslot
= create_named_object( sync_namespace
, &mailslot_ops
, name
, len
);
274 /* it already exists - there can only be one mailslot to read from */
275 if( get_error() == STATUS_OBJECT_NAME_COLLISION
)
277 release_object( mailslot
);
282 mailslot
->write_fd
= NULL
;
283 mailslot
->max_msgsize
= max_msgsize
;
284 mailslot
->read_timeout
= read_timeout
;
285 list_init( &mailslot
->writers
);
286 list_init( &mailslot
->read_q
);
288 if( !socketpair( PF_UNIX
, SOCK_DGRAM
, 0, fds
) )
290 fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
291 fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
292 mailslot
->fd
= create_anonymous_fd( &mailslot_fd_ops
,
293 fds
[1], &mailslot
->obj
);
294 mailslot
->write_fd
= create_anonymous_fd( &mail_writer_fd_ops
,
295 fds
[0], &mailslot
->obj
);
296 if( mailslot
->fd
&& mailslot
->write_fd
) return mailslot
;
298 else file_set_error();
300 release_object( mailslot
);
304 static struct mailslot
*open_mailslot( const WCHAR
*name
, size_t len
)
308 obj
= find_object( sync_namespace
, name
, len
);
311 if (obj
->ops
== &mailslot_ops
)
312 return (struct mailslot
*)obj
;
313 release_object( obj
);
314 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
317 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
322 static void mail_writer_dump( struct object
*obj
, int verbose
)
324 fprintf( stderr
, "Mailslot writer\n" );
327 static void mail_writer_destroy( struct object
*obj
)
329 struct mail_writer
*writer
= (struct mail_writer
*) obj
;
331 list_remove( &writer
->entry
);
332 release_object( writer
->mailslot
);
335 static int mail_writer_get_info( struct fd
*fd
)
340 static struct fd
*mail_writer_get_fd( struct object
*obj
)
342 struct mail_writer
*writer
= (struct mail_writer
*) obj
;
344 return (struct fd
*)grab_object( writer
->mailslot
->write_fd
);
348 * Readers and writers cannot be mixed.
349 * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
351 static struct mail_writer
*create_mail_writer( struct mailslot
*mailslot
, unsigned int access
,
352 unsigned int sharing
)
354 struct mail_writer
*writer
;
356 if (!list_empty( &mailslot
->writers
))
358 writer
= LIST_ENTRY( list_head(&mailslot
->writers
), struct mail_writer
, entry
);
360 if (((access
& GENERIC_WRITE
) || (writer
->access
& GENERIC_WRITE
)) &&
361 !((sharing
& FILE_SHARE_WRITE
) && (writer
->sharing
& FILE_SHARE_WRITE
)))
363 set_error( STATUS_SHARING_VIOLATION
);
368 writer
= alloc_object( &mail_writer_ops
);
372 grab_object( mailslot
);
373 writer
->mailslot
= mailslot
;
374 writer
->access
= access
;
375 writer
->sharing
= sharing
;
377 list_add_head( &mailslot
->writers
, &writer
->entry
);
382 static struct mailslot
*get_mailslot_obj( struct process
*process
, obj_handle_t handle
,
383 unsigned int access
)
386 obj
= get_handle_obj( process
, handle
, access
, &mailslot_ops
);
387 return (struct mailslot
*) obj
;
391 /* create a mailslot */
392 DECL_HANDLER(create_mailslot
)
394 struct mailslot
*mailslot
;
397 mailslot
= create_mailslot( get_req_data(), get_req_data_size(),
398 req
->max_msgsize
, req
->read_timeout
);
401 reply
->handle
= alloc_handle( current
->process
, mailslot
,
402 GENERIC_READ
, req
->inherit
);
403 release_object( mailslot
);
408 /* open an existing mailslot */
409 DECL_HANDLER(open_mailslot
)
411 struct mailslot
*mailslot
;
415 if( ! ( req
->sharing
& FILE_SHARE_READ
) )
417 set_error( STATUS_SHARING_VIOLATION
);
421 mailslot
= open_mailslot( get_req_data(), get_req_data_size() );
424 struct mail_writer
*writer
;
426 writer
= create_mail_writer( mailslot
, req
->access
, req
->sharing
);
429 reply
->handle
= alloc_handle( current
->process
, writer
,
430 req
->access
, req
->inherit
);
431 release_object( writer
);
433 release_object( mailslot
);
436 set_error( STATUS_NO_SUCH_FILE
);
440 /* set mailslot information */
441 DECL_HANDLER(set_mailslot_info
)
443 struct mailslot
*mailslot
= get_mailslot_obj( current
->process
, req
->handle
, 0 );
447 if( req
->flags
& MAILSLOT_SET_READ_TIMEOUT
)
448 mailslot
->read_timeout
= req
->read_timeout
;
449 reply
->max_msgsize
= mailslot
->max_msgsize
;
450 reply
->read_timeout
= mailslot
->read_timeout
;
451 reply
->msg_count
= mailslot_message_count(mailslot
);
453 /* get the size of the next message */
454 if( reply
->msg_count
)
455 reply
->next_msgsize
= mailslot_next_msg_size(mailslot
);
457 reply
->next_msgsize
= MAILSLOT_NO_MESSAGE
;
459 release_object( mailslot
);