2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2001 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
26 #include "wine/port.h"
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
59 ps_disconnected_server
,
67 struct object obj
; /* object header */
68 struct fd
*fd
; /* pipe file descriptor */
69 struct list entry
; /* entry in named pipe servers list */
70 enum pipe_state state
; /* server state */
71 struct pipe_client
*client
; /* client that this server is connected to */
72 struct named_pipe
*pipe
;
73 struct timeout_user
*flush_poll
;
75 struct list wait_q
; /* only a single one can be queued */
76 unsigned int options
; /* pipe options */
81 struct object obj
; /* object header */
82 struct fd
*fd
; /* pipe file descriptor */
83 struct pipe_server
*server
; /* server that this client is connected to */
84 unsigned int flags
; /* file flags */
89 struct object obj
; /* object header */
91 unsigned int maxinstances
;
95 unsigned int instances
;
96 struct list servers
; /* list of servers using this pipe */
97 struct list waiters
; /* list of clients waiting to connect */
100 static void named_pipe_dump( struct object
*obj
, int verbose
);
101 static void named_pipe_destroy( struct object
*obj
);
103 static const struct object_ops named_pipe_ops
=
105 sizeof(struct named_pipe
), /* size */
106 named_pipe_dump
, /* dump */
107 no_add_queue
, /* add_queue */
108 NULL
, /* remove_queue */
110 NULL
, /* satisfied */
111 no_signal
, /* signal */
112 no_get_fd
, /* get_fd */
113 no_close_handle
, /* close_handle */
114 named_pipe_destroy
/* destroy */
117 /* server end functions */
118 static void pipe_server_dump( struct object
*obj
, int verbose
);
119 static struct fd
*pipe_server_get_fd( struct object
*obj
);
120 static void pipe_server_destroy( struct object
*obj
);
121 static int pipe_server_flush( struct fd
*fd
, struct event
**event
);
122 static int pipe_server_get_info( struct fd
*fd
);
124 static const struct object_ops pipe_server_ops
=
126 sizeof(struct pipe_server
), /* size */
127 pipe_server_dump
, /* dump */
128 default_fd_add_queue
, /* add_queue */
129 default_fd_remove_queue
, /* remove_queue */
130 default_fd_signaled
, /* signaled */
131 no_satisfied
, /* satisfied */
132 no_signal
, /* signal */
133 pipe_server_get_fd
, /* get_fd */
134 no_close_handle
, /* close_handle */
135 pipe_server_destroy
/* destroy */
138 static const struct fd_ops pipe_server_fd_ops
=
140 default_fd_get_poll_events
, /* get_poll_events */
141 default_poll_event
, /* poll_event */
142 pipe_server_flush
, /* flush */
143 pipe_server_get_info
, /* get_file_info */
144 default_fd_queue_async
, /* queue_async */
145 default_fd_cancel_async
, /* cancel_async */
148 /* client end functions */
149 static void pipe_client_dump( struct object
*obj
, int verbose
);
150 static struct fd
*pipe_client_get_fd( struct object
*obj
);
151 static void pipe_client_destroy( struct object
*obj
);
152 static int pipe_client_flush( struct fd
*fd
, struct event
**event
);
153 static int pipe_client_get_info( struct fd
*fd
);
155 static const struct object_ops pipe_client_ops
=
157 sizeof(struct pipe_client
), /* size */
158 pipe_client_dump
, /* dump */
159 default_fd_add_queue
, /* add_queue */
160 default_fd_remove_queue
, /* remove_queue */
161 default_fd_signaled
, /* signaled */
162 no_satisfied
, /* satisfied */
163 no_signal
, /* signal */
164 pipe_client_get_fd
, /* get_fd */
165 no_close_handle
, /* close_handle */
166 pipe_client_destroy
/* destroy */
169 static const struct fd_ops pipe_client_fd_ops
=
171 default_fd_get_poll_events
, /* get_poll_events */
172 default_poll_event
, /* poll_event */
173 pipe_client_flush
, /* flush */
174 pipe_client_get_info
, /* get_file_info */
175 default_fd_queue_async
, /* queue_async */
176 default_fd_cancel_async
/* cancel_async */
179 static void named_pipe_dump( struct object
*obj
, int verbose
)
181 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
182 assert( obj
->ops
== &named_pipe_ops
);
183 fprintf( stderr
, "Named pipe " );
184 dump_object_name( &pipe
->obj
);
185 fprintf( stderr
, "\n" );
188 static void pipe_server_dump( struct object
*obj
, int verbose
)
190 struct pipe_server
*server
= (struct pipe_server
*) obj
;
191 assert( obj
->ops
== &pipe_server_ops
);
192 fprintf( stderr
, "Named pipe server pipe=%p state=%d\n", server
->pipe
, server
->state
);
195 static void pipe_client_dump( struct object
*obj
, int verbose
)
197 struct pipe_client
*client
= (struct pipe_client
*) obj
;
198 assert( obj
->ops
== &pipe_client_ops
);
199 fprintf( stderr
, "Named pipe client server=%p\n", client
->server
);
202 static void named_pipe_destroy( struct object
*obj
)
204 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
206 assert( list_empty( &pipe
->servers
) );
207 assert( !pipe
->instances
);
208 async_terminate_queue( &pipe
->waiters
, STATUS_HANDLES_CLOSED
);
211 static struct fd
*pipe_client_get_fd( struct object
*obj
)
213 struct pipe_client
*client
= (struct pipe_client
*) obj
;
215 return (struct fd
*) grab_object( client
->fd
);
216 set_error( STATUS_PIPE_DISCONNECTED
);
220 static struct fd
*pipe_server_get_fd( struct object
*obj
)
222 struct pipe_server
*server
= (struct pipe_server
*) obj
;
224 switch(server
->state
)
226 case ps_connected_server
:
227 case ps_wait_disconnect
:
228 assert( server
->fd
);
229 return (struct fd
*) grab_object( server
->fd
);
233 set_error( STATUS_PIPE_LISTENING
);
236 case ps_disconnected_server
:
237 case ps_wait_connect
:
238 set_error( STATUS_PIPE_DISCONNECTED
);
245 static void notify_empty( struct pipe_server
*server
)
247 if (!server
->flush_poll
)
249 assert( server
->state
== ps_connected_server
);
250 assert( server
->event
);
251 remove_timeout_user( server
->flush_poll
);
252 server
->flush_poll
= NULL
;
253 set_event( server
->event
);
254 release_object( server
->event
);
255 server
->event
= NULL
;
258 static void do_disconnect( struct pipe_server
*server
)
260 /* we may only have a server fd, if the client disconnected */
263 assert( server
->client
->server
== server
);
264 assert( server
->client
->fd
);
265 release_object( server
->client
->fd
);
266 server
->client
->fd
= NULL
;
268 assert( server
->fd
);
269 release_object( server
->fd
);
273 static void pipe_server_destroy( struct object
*obj
)
275 struct pipe_server
*server
= (struct pipe_server
*)obj
;
277 assert( obj
->ops
== &pipe_server_ops
);
281 notify_empty( server
);
282 do_disconnect( server
);
287 server
->client
->server
= NULL
;
288 server
->client
= NULL
;
291 async_terminate_head( &server
->wait_q
, STATUS_HANDLES_CLOSED
);
293 assert( server
->pipe
->instances
);
294 server
->pipe
->instances
--;
296 list_remove( &server
->entry
);
297 release_object( server
->pipe
);
300 static void pipe_client_destroy( struct object
*obj
)
302 struct pipe_client
*client
= (struct pipe_client
*)obj
;
303 struct pipe_server
*server
= client
->server
;
305 assert( obj
->ops
== &pipe_client_ops
);
309 notify_empty( server
);
311 switch(server
->state
)
313 case ps_connected_server
:
314 /* Don't destroy the server's fd here as we can't
315 do a successful flush without it. */
316 server
->state
= ps_wait_disconnect
;
317 release_object( client
->fd
);
320 case ps_disconnected_server
:
321 server
->state
= ps_wait_connect
;
325 case ps_wait_disconnect
:
326 case ps_wait_connect
:
329 assert( server
->client
);
330 server
->client
= NULL
;
331 client
->server
= NULL
;
333 assert( !client
->fd
);
336 static int pipe_data_remaining( struct pipe_server
*server
)
341 assert( server
->client
);
343 fd
= get_unix_fd( server
->client
->fd
);
350 if (0 > poll( &pfd
, 1, 0 ))
353 return pfd
.revents
&POLLIN
;
356 static void check_flushed( void *arg
)
358 struct pipe_server
*server
= (struct pipe_server
*) arg
;
360 assert( server
->event
);
361 if (pipe_data_remaining( server
))
365 gettimeofday( &tv
, NULL
);
366 add_timeout( &tv
, 100 );
367 server
->flush_poll
= add_timeout_user( &tv
, check_flushed
, server
);
371 /* notify_empty( server ); */
372 server
->flush_poll
= NULL
;
373 set_event( server
->event
);
374 release_object( server
->event
);
375 server
->event
= NULL
;
379 static int pipe_server_flush( struct fd
*fd
, struct event
**event
)
381 struct pipe_server
*server
= get_fd_user( fd
);
386 if (server
->state
!= ps_connected_server
)
389 /* FIXME: if multiple threads flush the same pipe,
390 maybe should create a list of processes to notify */
391 if (server
->flush_poll
)
394 if (pipe_data_remaining( server
))
398 /* this kind of sux -
399 there's no unix way to be alerted when a pipe becomes empty */
400 server
->event
= create_event( NULL
, 0, 0, 0, 0 );
403 gettimeofday( &tv
, NULL
);
404 add_timeout( &tv
, 100 );
405 server
->flush_poll
= add_timeout_user( &tv
, check_flushed
, server
);
406 *event
= server
->event
;
412 static int pipe_client_flush( struct fd
*fd
, struct event
**event
)
414 /* FIXME: what do we have to do for this? */
418 static inline int is_overlapped( unsigned int options
)
420 return !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
423 static int pipe_server_get_info( struct fd
*fd
)
425 struct pipe_server
*server
= get_fd_user( fd
);
426 int flags
= FD_FLAG_AVAILABLE
;
428 if (is_overlapped( server
->options
)) flags
|= FD_FLAG_OVERLAPPED
;
433 static int pipe_client_get_info( struct fd
*fd
)
435 struct pipe_client
*client
= get_fd_user( fd
);
436 int flags
= FD_FLAG_AVAILABLE
;
438 if (is_overlapped( client
->flags
)) flags
|= FD_FLAG_OVERLAPPED
;
443 static struct named_pipe
*create_named_pipe( const WCHAR
*name
, size_t len
, unsigned int attr
)
445 struct named_pipe
*pipe
;
447 pipe
= create_named_object( sync_namespace
, &named_pipe_ops
, name
, len
, attr
);
450 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
452 /* initialize it if it didn't already exist */
454 list_init( &pipe
->servers
);
455 list_init( &pipe
->waiters
);
461 static struct named_pipe
*open_named_pipe( const WCHAR
*name
, size_t len
, unsigned int attr
)
465 if ((obj
= find_object( sync_namespace
, name
, len
, attr
)))
467 if (obj
->ops
== &named_pipe_ops
) return (struct named_pipe
*)obj
;
468 release_object( obj
);
469 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
471 else set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
476 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
477 obj_handle_t handle
, unsigned int access
)
480 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
481 return (struct pipe_server
*) obj
;
484 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
, unsigned int options
)
486 struct pipe_server
*server
;
488 server
= alloc_object( &pipe_server_ops
);
494 server
->state
= ps_idle_server
;
495 server
->client
= NULL
;
496 server
->flush_poll
= NULL
;
497 server
->options
= options
;
498 list_init( &server
->wait_q
);
500 list_add_head( &pipe
->servers
, &server
->entry
);
506 static struct pipe_client
*create_pipe_client( struct pipe_server
*server
, unsigned int flags
)
508 struct pipe_client
*client
;
510 client
= alloc_object( &pipe_client_ops
);
515 client
->server
= server
;
516 client
->flags
= flags
;
521 static inline struct pipe_server
*find_server( struct named_pipe
*pipe
, enum pipe_state state
)
523 struct pipe_server
*server
;
525 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
527 if (server
->state
== state
) return (struct pipe_server
*)grab_object( server
);
532 static inline struct pipe_server
*find_server2( struct named_pipe
*pipe
,
533 enum pipe_state state1
, enum pipe_state state2
)
535 struct pipe_server
*server
;
537 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
539 if (server
->state
== state1
|| server
->state
== state2
)
540 return (struct pipe_server
*)grab_object( server
);
545 DECL_HANDLER(create_named_pipe
)
547 struct named_pipe
*pipe
;
548 struct pipe_server
*server
;
551 pipe
= create_named_pipe( get_req_data(), get_req_data_size(), req
->attributes
);
555 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
557 pipe
->insize
= req
->insize
;
558 pipe
->outsize
= req
->outsize
;
559 pipe
->maxinstances
= req
->maxinstances
;
560 pipe
->timeout
= req
->timeout
;
561 pipe
->flags
= req
->flags
;
565 if (pipe
->maxinstances
<= pipe
->instances
)
567 set_error( STATUS_INSTANCE_NOT_AVAILABLE
);
568 release_object( pipe
);
571 if ((pipe
->maxinstances
!= req
->maxinstances
) ||
572 (pipe
->timeout
!= req
->timeout
) ||
573 (pipe
->flags
!= req
->flags
))
575 set_error( STATUS_ACCESS_DENIED
);
576 release_object( pipe
);
579 set_error( 0 ); /* clear the name collision */
582 server
= create_pipe_server( pipe
, req
->options
);
585 reply
->handle
= alloc_handle( current
->process
, server
,
586 req
->access
, req
->attributes
& OBJ_INHERIT
);
587 server
->pipe
->instances
++;
588 release_object( server
);
591 release_object( pipe
);
594 DECL_HANDLER(open_named_pipe
)
596 struct pipe_server
*server
;
597 struct pipe_client
*client
;
598 struct named_pipe
*pipe
;
601 if (!(pipe
= open_named_pipe( get_req_data(), get_req_data_size(), req
->attributes
)))
604 server
= find_server2( pipe
, ps_idle_server
, ps_wait_open
);
605 release_object( pipe
);
609 set_error( STATUS_PIPE_NOT_AVAILABLE
);
613 client
= create_pipe_client( server
, req
->flags
);
616 if (!socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
))
620 assert( !client
->fd
);
621 assert( !server
->fd
);
623 /* for performance reasons, only set nonblocking mode when using
624 * overlapped I/O. Otherwise, we will be doing too much busy
626 if (is_overlapped( req
->flags
))
627 res
= fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
628 if ((res
!= -1) && is_overlapped( server
->options
))
629 res
= fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
631 client
->fd
= create_anonymous_fd( &pipe_client_fd_ops
,
632 fds
[1], &client
->obj
);
633 server
->fd
= create_anonymous_fd( &pipe_server_fd_ops
,
634 fds
[0], &server
->obj
);
635 if (client
->fd
&& server
->fd
&& res
!= 1)
637 if (server
->state
== ps_wait_open
)
638 async_terminate_head( &server
->wait_q
, STATUS_SUCCESS
);
639 assert( list_empty( &server
->wait_q
) );
640 server
->state
= ps_connected_server
;
641 server
->client
= client
;
642 client
->server
= server
;
643 reply
->handle
= alloc_handle( current
->process
, client
,
644 req
->access
, req
->attributes
& OBJ_INHERIT
);
650 release_object( client
);
652 release_object( server
);
655 DECL_HANDLER(connect_named_pipe
)
657 struct pipe_server
*server
;
659 server
= get_pipe_server_obj(current
->process
, req
->handle
, 0);
663 switch(server
->state
)
666 case ps_wait_connect
:
667 assert( !server
->fd
);
668 server
->state
= ps_wait_open
;
669 create_async( current
, NULL
, &server
->wait_q
,
670 req
->func
, req
->event
, NULL
);
671 async_terminate_queue( &server
->pipe
->waiters
, STATUS_SUCCESS
);
673 case ps_connected_server
:
674 assert( server
->fd
);
675 set_error( STATUS_PIPE_CONNECTED
);
677 case ps_disconnected_server
:
678 set_error( STATUS_PIPE_BUSY
);
680 case ps_wait_disconnect
:
681 set_error( STATUS_NO_DATA_DETECTED
);
684 set_error( STATUS_INVALID_HANDLE
);
688 release_object(server
);
691 DECL_HANDLER(wait_named_pipe
)
693 struct named_pipe
*pipe
;
694 struct pipe_server
*server
;
696 if (!(pipe
= open_named_pipe( get_req_data(), get_req_data_size(), OBJ_CASE_INSENSITIVE
)))
698 set_error( STATUS_PIPE_NOT_AVAILABLE
);
701 server
= find_server( pipe
, ps_wait_open
);
704 /* there's already a server waiting for a client to connect */
705 thread_queue_apc( current
, NULL
, req
->func
, APC_ASYNC_IO
,
706 1, req
->overlapped
, NULL
, (void *)STATUS_SUCCESS
);
707 release_object( server
);
712 if (req
->timeout
== NMPWAIT_USE_DEFAULT_WAIT
)
713 timeout
= pipe
->timeout
;
715 timeout
= req
->timeout
;
717 if (req
->timeout
== NMPWAIT_WAIT_FOREVER
)
718 create_async( current
, NULL
, &pipe
->waiters
,
719 req
->func
, req
->overlapped
, NULL
);
721 create_async( current
, &timeout
, &pipe
->waiters
,
722 req
->func
, req
->overlapped
, NULL
);
725 release_object( pipe
);
728 DECL_HANDLER(disconnect_named_pipe
)
730 struct pipe_server
*server
;
733 server
= get_pipe_server_obj( current
->process
, req
->handle
, 0 );
736 switch(server
->state
)
738 case ps_connected_server
:
739 assert( server
->fd
);
740 assert( server
->client
);
741 assert( server
->client
->fd
);
743 notify_empty( server
);
745 /* Dump the client and server fds, but keep the pointers
746 around - client loses all waiting data */
747 server
->state
= ps_disconnected_server
;
748 do_disconnect( server
);
749 reply
->fd
= flush_cached_fd( current
->process
, req
->handle
);
752 case ps_wait_disconnect
:
753 assert( !server
->client
);
754 assert( server
->fd
);
755 do_disconnect( server
);
756 server
->state
= ps_wait_connect
;
757 reply
->fd
= flush_cached_fd( current
->process
, req
->handle
);
762 case ps_disconnected_server
:
763 case ps_wait_connect
:
764 set_error( STATUS_PIPE_DISCONNECTED
);
767 release_object( server
);
770 DECL_HANDLER(get_named_pipe_info
)
772 struct pipe_server
*server
;
774 server
= get_pipe_server_obj( current
->process
, req
->handle
, 0 );
778 reply
->flags
= server
->pipe
->flags
;
779 reply
->maxinstances
= server
->pipe
->maxinstances
;
780 reply
->insize
= server
->pipe
->insize
;
781 reply
->outsize
= server
->pipe
->outsize
;
783 release_object(server
);