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>
57 ps_disconnected_server
,
63 struct thread
*thread
;
74 enum pipe_state state
;
75 struct pipe_client
*client
;
76 struct named_pipe
*pipe
;
77 struct pipe_server
*next
;
78 struct pipe_server
*prev
;
79 struct timeout_user
*flush_poll
;
81 struct wait_info wait
;
88 struct pipe_server
*server
;
89 struct wait_info wait
;
94 struct wait_info wait
;
95 struct connect_wait
*next
;
100 struct object obj
; /* object header */
101 unsigned int pipemode
;
102 unsigned int maxinstances
;
103 unsigned int outsize
;
105 unsigned int timeout
;
106 unsigned int instances
;
107 struct pipe_server
*servers
;
108 struct connect_wait
*connect_waiters
;
111 static void named_pipe_dump( struct object
*obj
, int verbose
);
112 static void named_pipe_destroy( struct object
*obj
);
114 static const struct object_ops named_pipe_ops
=
116 sizeof(struct named_pipe
), /* size */
117 named_pipe_dump
, /* dump */
118 no_add_queue
, /* add_queue */
119 NULL
, /* remove_queue */
121 NULL
, /* satisfied */
122 no_get_fd
, /* get_fd */
123 named_pipe_destroy
/* destroy */
126 /* common to clients and servers */
127 static int pipe_end_get_poll_events( struct fd
*fd
);
128 static int pipe_end_get_info( struct fd
*fd
);
130 /* server end functions */
131 static void pipe_server_dump( struct object
*obj
, int verbose
);
132 static struct fd
*pipe_server_get_fd( struct object
*obj
);
133 static void pipe_server_destroy( struct object
*obj
);
134 static int pipe_server_flush( struct fd
*fd
, struct event
**event
);
136 static const struct object_ops pipe_server_ops
=
138 sizeof(struct pipe_server
), /* size */
139 pipe_server_dump
, /* dump */
140 default_fd_add_queue
, /* add_queue */
141 default_fd_remove_queue
, /* remove_queue */
142 default_fd_signaled
, /* signaled */
143 no_satisfied
, /* satisfied */
144 pipe_server_get_fd
, /* get_fd */
145 pipe_server_destroy
/* destroy */
148 static const struct fd_ops pipe_server_fd_ops
=
150 pipe_end_get_poll_events
, /* get_poll_events */
151 default_poll_event
, /* poll_event */
152 pipe_server_flush
, /* flush */
153 pipe_end_get_info
, /* get_file_info */
154 no_queue_async
, /* queue_async */
155 no_cancel_async
, /* cancel_async */
158 /* client end functions */
159 static void pipe_client_dump( struct object
*obj
, int verbose
);
160 static struct fd
*pipe_client_get_fd( struct object
*obj
);
161 static void pipe_client_destroy( struct object
*obj
);
162 static int pipe_client_flush( struct fd
*fd
, struct event
**event
);
164 static const struct object_ops pipe_client_ops
=
166 sizeof(struct pipe_client
), /* size */
167 pipe_client_dump
, /* dump */
168 default_fd_add_queue
, /* add_queue */
169 default_fd_remove_queue
, /* remove_queue */
170 default_fd_signaled
, /* signaled */
171 no_satisfied
, /* satisfied */
172 pipe_client_get_fd
, /* get_fd */
173 pipe_client_destroy
/* destroy */
176 static const struct fd_ops pipe_client_fd_ops
=
178 pipe_end_get_poll_events
, /* get_poll_events */
179 default_poll_event
, /* poll_event */
180 pipe_client_flush
, /* flush */
181 pipe_end_get_info
, /* get_file_info */
182 no_queue_async
, /* queue_async */
183 no_cancel_async
/* cancel_async */
186 static void named_pipe_dump( struct object
*obj
, int verbose
)
188 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
189 assert( obj
->ops
== &named_pipe_ops
);
190 fprintf( stderr
, "named pipe %p\n" ,pipe
);
193 static void pipe_server_dump( struct object
*obj
, int verbose
)
195 struct pipe_server
*server
= (struct pipe_server
*) obj
;
196 assert( obj
->ops
== &pipe_server_ops
);
197 fprintf( stderr
, "named pipe server %p (state %d)\n",
198 server
, server
->state
);
201 static void pipe_client_dump( struct object
*obj
, int verbose
)
203 struct pipe_client
*client
= (struct pipe_client
*) obj
;
204 assert( obj
->ops
== &pipe_server_ops
);
205 fprintf( stderr
, "named pipe client %p (server state %d)\n",
206 client
, client
->server
->state
);
209 static void named_pipe_destroy( struct object
*obj
)
211 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
212 assert( !pipe
->servers
);
213 assert( !pipe
->instances
);
216 static void notify_waiter( struct wait_info
*wait
, unsigned int status
)
218 if( wait
->thread
&& wait
->func
&& wait
->overlapped
)
220 /* queue a system APC, to notify a waiting thread */
221 thread_queue_apc( wait
->thread
, NULL
, wait
->func
, APC_ASYNC
,
222 1, wait
->overlapped
, (void *)status
, NULL
);
224 if( wait
->thread
) release_object( wait
->thread
);
228 static void set_waiter( struct wait_info
*wait
, void *func
, void *ov
)
230 wait
->thread
= (struct thread
*) grab_object( current
);
232 wait
->overlapped
= ov
;
235 static void notify_connect_waiters( struct named_pipe
*pipe
)
237 struct connect_wait
*cw
, **x
= &pipe
->connect_waiters
;
242 notify_waiter( &cw
->wait
, STATUS_SUCCESS
);
243 release_object( pipe
);
249 static void queue_connect_waiter( struct named_pipe
*pipe
,
250 void *func
, void *overlapped
)
252 struct connect_wait
*waiter
;
254 waiter
= mem_alloc( sizeof(*waiter
) );
257 set_waiter( &waiter
->wait
, func
, overlapped
);
258 waiter
->next
= pipe
->connect_waiters
;
259 pipe
->connect_waiters
= waiter
;
264 static struct fd
*pipe_client_get_fd( struct object
*obj
)
266 struct pipe_client
*client
= (struct pipe_client
*) obj
;
268 return (struct fd
*) grab_object( client
->fd
);
269 set_error( STATUS_PIPE_DISCONNECTED
);
273 static struct fd
*pipe_server_get_fd( struct object
*obj
)
275 struct pipe_server
*server
= (struct pipe_server
*) obj
;
277 switch(server
->state
)
279 case ps_connected_server
:
280 case ps_wait_disconnect
:
281 assert( server
->fd
);
282 return (struct fd
*) grab_object( server
->fd
);
286 set_error( STATUS_PIPE_LISTENING
);
289 case ps_disconnected_server
:
290 case ps_wait_connect
:
291 set_error( STATUS_PIPE_DISCONNECTED
);
301 static void notify_empty( struct pipe_server
*server
)
303 if( !server
->flush_poll
)
305 assert( server
->state
== ps_connected_server
);
306 assert( server
->event
);
307 remove_timeout_user( server
->flush_poll
);
308 server
->flush_poll
= NULL
;
309 set_event( server
->event
);
310 release_object( server
->event
);
311 server
->event
= NULL
;
314 static void do_disconnect( struct pipe_server
*server
)
316 /* we may only have a server fd, if the client disconnected */
319 assert( server
->client
->server
== server
);
320 assert( server
->client
->fd
);
321 release_object( server
->client
->fd
);
322 server
->client
->fd
= NULL
;
324 assert( server
->fd
);
325 release_object( server
->fd
);
329 static void pipe_server_destroy( struct object
*obj
)
331 struct pipe_server
*server
= (struct pipe_server
*)obj
;
333 assert( obj
->ops
== &pipe_server_ops
);
337 notify_empty( server
);
338 do_disconnect( server
);
343 server
->client
->server
= NULL
;
344 server
->client
= NULL
;
347 notify_waiter( &server
->wait
, STATUS_HANDLES_CLOSED
);
349 assert( server
->pipe
->instances
);
350 server
->pipe
->instances
--;
352 /* remove server from pipe's server list */
353 if( server
->next
) server
->next
->prev
= server
->prev
;
354 if( server
->prev
) server
->prev
->next
= server
->next
;
355 else server
->pipe
->servers
= server
->next
;
356 release_object( server
->pipe
);
359 static void pipe_client_destroy( struct object
*obj
)
361 struct pipe_client
*client
= (struct pipe_client
*)obj
;
362 struct pipe_server
*server
= client
->server
;
364 assert( obj
->ops
== &pipe_client_ops
);
366 notify_waiter( &client
->wait
, STATUS_HANDLES_CLOSED
);
370 notify_empty( server
);
372 switch( server
->state
)
374 case ps_connected_server
:
375 /* Don't destroy the server's fd here as we can't
376 do a successful flush without it. */
377 server
->state
= ps_wait_disconnect
;
378 release_object( client
->fd
);
381 case ps_disconnected_server
:
382 server
->state
= ps_wait_connect
;
387 assert( server
->client
);
388 server
->client
= NULL
;
389 client
->server
= NULL
;
391 assert( !client
->fd
);
394 static int pipe_end_get_poll_events( struct fd
*fd
)
396 return POLLIN
| POLLOUT
; /* FIXME */
399 static int pipe_data_remaining( struct pipe_server
*server
)
404 assert( server
->client
);
406 fd
= get_unix_fd( server
->client
->fd
);
413 if( 0 > poll( &pfd
, 1, 0 ) )
416 return pfd
.revents
&POLLIN
;
419 static void check_flushed( void *arg
)
421 struct pipe_server
*server
= (struct pipe_server
*) arg
;
423 assert( server
->event
);
424 if( pipe_data_remaining( server
) )
428 gettimeofday( &tv
, 0 );
429 add_timeout( &tv
, 100 );
430 server
->flush_poll
= add_timeout_user( &tv
, check_flushed
, server
);
434 /* notify_empty( server ); */
435 server
->flush_poll
= NULL
;
436 set_event( server
->event
);
437 release_object( server
->event
);
438 server
->event
= NULL
;
442 static int pipe_server_flush( struct fd
*fd
, struct event
**event
)
444 struct pipe_server
*server
= get_fd_user( fd
);
449 if( server
->state
!= ps_connected_server
)
452 /* FIXME: if multiple threads flush the same pipe,
453 maybe should create a list of processes to notify */
454 if( server
->flush_poll
)
457 if( pipe_data_remaining( server
) )
461 /* this kind of sux -
462 there's no unix way to be alerted when a pipe becomes empty */
463 server
->event
= create_event( NULL
, 0, 0, 0 );
466 gettimeofday( &tv
, 0 );
467 add_timeout( &tv
, 100 );
468 server
->flush_poll
= add_timeout_user( &tv
, check_flushed
, server
);
469 *event
= server
->event
;
475 static int pipe_client_flush( struct fd
*fd
, struct event
**event
)
477 /* FIXME: what do we have to do for this? */
481 static int pipe_end_get_info( struct fd
*fd
)
486 static struct named_pipe
*create_named_pipe( const WCHAR
*name
, size_t len
)
488 struct named_pipe
*pipe
;
490 pipe
= create_named_object( sync_namespace
, &named_pipe_ops
, name
, len
);
493 if( get_error() != STATUS_OBJECT_NAME_COLLISION
)
495 /* initialize it if it didn't already exist */
498 pipe
->connect_waiters
= NULL
;
504 static struct named_pipe
*open_named_pipe( const WCHAR
*name
, size_t len
)
508 if ((obj
= find_object( sync_namespace
, name
, len
)))
510 if (obj
->ops
== &named_pipe_ops
) return (struct named_pipe
*)obj
;
511 release_object( obj
);
512 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
514 else set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
519 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
520 obj_handle_t handle
, unsigned int access
)
523 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
524 return (struct pipe_server
*) obj
;
527 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
)
529 struct pipe_server
*server
;
531 server
= alloc_object( &pipe_server_ops
);
537 server
->state
= ps_none
;
538 server
->client
= NULL
;
539 server
->flush_poll
= NULL
;
540 server
->wait
.thread
= NULL
;
542 /* add to list of pipe servers */
543 if ((server
->next
= pipe
->servers
)) server
->next
->prev
= server
;
545 pipe
->servers
= server
;
552 static struct pipe_client
*create_pipe_client( struct pipe_server
*server
)
554 struct pipe_client
*client
;
556 client
= alloc_object( &pipe_client_ops
);
561 client
->server
= server
;
562 client
->wait
.thread
= NULL
;
567 static struct pipe_server
*find_server( struct named_pipe
*pipe
,
568 enum pipe_state state
)
570 struct pipe_server
*x
;
572 for( x
= pipe
->servers
; x
; x
= x
->next
)
573 if( x
->state
== state
)
579 return (struct pipe_server
*) grab_object( x
);
582 DECL_HANDLER(create_named_pipe
)
584 struct named_pipe
*pipe
;
585 struct pipe_server
*server
;
588 pipe
= create_named_pipe( get_req_data(), get_req_data_size() );
592 if( get_error() != STATUS_OBJECT_NAME_COLLISION
)
594 pipe
->insize
= req
->insize
;
595 pipe
->outsize
= req
->outsize
;
596 pipe
->maxinstances
= req
->maxinstances
;
597 pipe
->timeout
= req
->timeout
;
598 pipe
->pipemode
= req
->pipemode
;
602 set_error( 0 ); /* clear the name collision */
603 if( pipe
->maxinstances
<= pipe
->instances
)
605 set_error( STATUS_PIPE_BUSY
);
606 release_object( pipe
);
609 if( ( pipe
->maxinstances
!= req
->maxinstances
) ||
610 ( pipe
->timeout
!= req
->timeout
) ||
611 ( pipe
->pipemode
!= req
->pipemode
) )
613 set_error( STATUS_ACCESS_DENIED
);
614 release_object( pipe
);
619 server
= create_pipe_server( pipe
);
622 server
->state
= ps_idle_server
;
623 reply
->handle
= alloc_handle( current
->process
, server
,
624 GENERIC_READ
|GENERIC_WRITE
, req
->inherit
);
625 server
->pipe
->instances
++;
626 release_object( server
);
629 release_object( pipe
);
632 DECL_HANDLER(open_named_pipe
)
634 struct pipe_server
*server
;
635 struct pipe_client
*client
;
636 struct named_pipe
*pipe
;
641 pipe
= open_named_pipe( get_req_data(), get_req_data_size() );
644 set_error( STATUS_NO_SUCH_FILE
);
648 for( server
= pipe
->servers
; server
; server
= server
->next
)
649 if( ( server
->state
==ps_idle_server
) ||
650 ( server
->state
==ps_wait_open
) )
652 release_object( pipe
);
656 set_error( STATUS_PIPE_NOT_AVAILABLE
);
660 client
= create_pipe_client( server
);
663 if( !socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
) )
665 assert( !client
->fd
);
666 assert( !server
->fd
);
667 client
->fd
= create_anonymous_fd( &pipe_server_fd_ops
,
668 fds
[1], &client
->obj
);
669 server
->fd
= create_anonymous_fd( &pipe_server_fd_ops
,
670 fds
[0], &server
->obj
);
671 if (client
->fd
&& server
->fd
)
673 if( server
->state
== ps_wait_open
)
674 notify_waiter( &server
->wait
, STATUS_SUCCESS
);
675 assert( !server
->wait
.thread
);
676 server
->state
= ps_connected_server
;
677 server
->client
= client
;
678 client
->server
= server
;
679 reply
->handle
= alloc_handle( current
->process
, client
,
680 req
->access
, req
->inherit
);
686 release_object( client
);
690 DECL_HANDLER(connect_named_pipe
)
692 struct pipe_server
*server
;
694 server
= get_pipe_server_obj(current
->process
, req
->handle
, 0);
698 switch( server
->state
)
701 case ps_wait_connect
:
702 assert( !server
->fd
);
703 server
->state
= ps_wait_open
;
704 set_waiter( &server
->wait
, req
->func
, req
->overlapped
);
705 notify_connect_waiters( server
->pipe
);
707 case ps_connected_server
:
708 assert( server
->fd
);
709 set_error( STATUS_PIPE_CONNECTED
);
711 case ps_disconnected_server
:
712 set_error( STATUS_PIPE_BUSY
);
714 case ps_wait_disconnect
:
715 set_error( STATUS_NO_DATA_DETECTED
);
718 set_error( STATUS_INVALID_HANDLE
);
722 release_object(server
);
725 DECL_HANDLER(wait_named_pipe
)
727 struct named_pipe
*pipe
;
728 struct pipe_server
*server
;
730 if (!(pipe
= open_named_pipe( get_req_data(), get_req_data_size() )))
732 set_error( STATUS_PIPE_NOT_AVAILABLE
);
735 server
= find_server( pipe
, ps_wait_open
);
738 /* there's already a server waiting for a client to connect */
739 struct wait_info wait
;
740 set_waiter( &wait
, req
->func
, req
->overlapped
);
741 notify_waiter( &wait
, STATUS_SUCCESS
);
742 release_object( server
);
745 queue_connect_waiter( pipe
, req
->func
, req
->overlapped
);
747 release_object( pipe
);
750 DECL_HANDLER(disconnect_named_pipe
)
752 struct pipe_server
*server
;
755 server
= get_pipe_server_obj( current
->process
, req
->handle
, 0 );
758 switch( server
->state
)
760 case ps_connected_server
:
761 assert( server
->fd
);
762 assert( server
->client
);
763 assert( server
->client
->fd
);
765 notify_empty( server
);
766 notify_waiter( &server
->client
->wait
, STATUS_PIPE_DISCONNECTED
);
768 /* Dump the client and server fds, but keep the pointers
769 around - client loses all waiting data */
770 server
->state
= ps_disconnected_server
;
771 do_disconnect( server
);
772 reply
->fd
= flush_cached_fd( current
->process
, req
->handle
);
775 case ps_wait_disconnect
:
776 assert( !server
->client
);
777 assert( server
->fd
);
778 do_disconnect( server
);
779 server
->state
= ps_wait_connect
;
780 reply
->fd
= flush_cached_fd( current
->process
, req
->handle
);
784 set_error( STATUS_PIPE_DISCONNECTED
);
786 release_object( server
);
789 DECL_HANDLER(get_named_pipe_info
)
791 struct pipe_server
*server
;
793 server
= get_pipe_server_obj( current
->process
, req
->handle
, 0 );
797 reply
->flags
= server
->pipe
->pipemode
;
798 reply
->maxinstances
= server
->pipe
->maxinstances
;
799 reply
->insize
= server
->pipe
->insize
;
800 reply
->outsize
= server
->pipe
->outsize
;
802 release_object(server
);