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
,
65 struct thread
*thread
;
74 struct object obj
; /* object header */
75 struct fd
*fd
; /* pipe file descriptor */
76 struct list entry
; /* entry in named pipe servers list */
77 enum pipe_state state
; /* server state */
78 struct pipe_client
*client
; /* client that this server is connected to */
79 struct named_pipe
*pipe
;
80 struct timeout_user
*flush_poll
;
82 struct wait_info wait
;
87 struct object obj
; /* object header */
88 struct fd
*fd
; /* pipe file descriptor */
89 struct pipe_server
*server
; /* server that this client is connected to */
90 struct wait_info wait
;
95 struct list entry
; /* entry in named pipe wait list */
96 struct wait_info wait
;
97 struct timeout_user
*timeout_user
;
102 struct object obj
; /* object header */
103 unsigned int pipemode
;
104 unsigned int maxinstances
;
105 unsigned int outsize
;
107 unsigned int timeout
;
108 unsigned int instances
;
109 struct list servers
; /* list of servers using this pipe */
110 struct list waiters
; /* list of clients waiting to connect */
113 static void named_pipe_dump( struct object
*obj
, int verbose
);
114 static void named_pipe_destroy( struct object
*obj
);
116 static const struct object_ops named_pipe_ops
=
118 sizeof(struct named_pipe
), /* size */
119 named_pipe_dump
, /* dump */
120 no_add_queue
, /* add_queue */
121 NULL
, /* remove_queue */
123 NULL
, /* satisfied */
124 no_get_fd
, /* get_fd */
125 named_pipe_destroy
/* destroy */
128 /* common to clients and servers */
129 static int pipe_end_get_poll_events( struct fd
*fd
);
130 static int pipe_end_get_info( struct fd
*fd
);
132 /* server end functions */
133 static void pipe_server_dump( struct object
*obj
, int verbose
);
134 static struct fd
*pipe_server_get_fd( struct object
*obj
);
135 static void pipe_server_destroy( struct object
*obj
);
136 static int pipe_server_flush( struct fd
*fd
, struct event
**event
);
138 static const struct object_ops pipe_server_ops
=
140 sizeof(struct pipe_server
), /* size */
141 pipe_server_dump
, /* dump */
142 default_fd_add_queue
, /* add_queue */
143 default_fd_remove_queue
, /* remove_queue */
144 default_fd_signaled
, /* signaled */
145 no_satisfied
, /* satisfied */
146 pipe_server_get_fd
, /* get_fd */
147 pipe_server_destroy
/* destroy */
150 static const struct fd_ops pipe_server_fd_ops
=
152 pipe_end_get_poll_events
, /* get_poll_events */
153 default_poll_event
, /* poll_event */
154 pipe_server_flush
, /* flush */
155 pipe_end_get_info
, /* get_file_info */
156 no_queue_async
, /* queue_async */
157 no_cancel_async
, /* cancel_async */
160 /* client end functions */
161 static void pipe_client_dump( struct object
*obj
, int verbose
);
162 static struct fd
*pipe_client_get_fd( struct object
*obj
);
163 static void pipe_client_destroy( struct object
*obj
);
164 static int pipe_client_flush( struct fd
*fd
, struct event
**event
);
166 static const struct object_ops pipe_client_ops
=
168 sizeof(struct pipe_client
), /* size */
169 pipe_client_dump
, /* dump */
170 default_fd_add_queue
, /* add_queue */
171 default_fd_remove_queue
, /* remove_queue */
172 default_fd_signaled
, /* signaled */
173 no_satisfied
, /* satisfied */
174 pipe_client_get_fd
, /* get_fd */
175 pipe_client_destroy
/* destroy */
178 static const struct fd_ops pipe_client_fd_ops
=
180 pipe_end_get_poll_events
, /* get_poll_events */
181 default_poll_event
, /* poll_event */
182 pipe_client_flush
, /* flush */
183 pipe_end_get_info
, /* get_file_info */
184 no_queue_async
, /* queue_async */
185 no_cancel_async
/* cancel_async */
188 static void named_pipe_dump( struct object
*obj
, int verbose
)
190 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
191 assert( obj
->ops
== &named_pipe_ops
);
192 fprintf( stderr
, "Named pipe " );
193 dump_object_name( &pipe
->obj
);
194 fprintf( stderr
, "\n" );
197 static void pipe_server_dump( struct object
*obj
, int verbose
)
199 struct pipe_server
*server
= (struct pipe_server
*) obj
;
200 assert( obj
->ops
== &pipe_server_ops
);
201 fprintf( stderr
, "Named pipe server pipe=%p state=%d\n", server
->pipe
, server
->state
);
204 static void pipe_client_dump( struct object
*obj
, int verbose
)
206 struct pipe_client
*client
= (struct pipe_client
*) obj
;
207 assert( obj
->ops
== &pipe_client_ops
);
208 fprintf( stderr
, "Named pipe client server=%p\n", client
->server
);
211 static void notify_waiter( struct wait_info
*wait
, unsigned int status
)
213 if( wait
->thread
&& wait
->func
&& wait
->overlapped
)
215 /* queue a system APC, to notify a waiting thread */
216 thread_queue_apc( wait
->thread
, NULL
, wait
->func
, APC_ASYNC
,
217 1, wait
->overlapped
, (void *)status
, NULL
);
219 if( wait
->thread
) release_object( wait
->thread
);
223 static void set_waiter( struct wait_info
*wait
, void *func
, void *ov
)
225 wait
->thread
= (struct thread
*) grab_object( current
);
227 wait
->overlapped
= ov
;
230 static void notify_connect_waiter( struct connect_wait
*waiter
, unsigned int status
)
232 notify_waiter( &waiter
->wait
, status
);
233 list_remove( &waiter
->entry
);
237 static void notify_all_connect_waiters( struct named_pipe
*pipe
, unsigned int status
)
241 while ((ptr
= list_head( &pipe
->waiters
)) != NULL
)
243 struct connect_wait
*waiter
= LIST_ENTRY( ptr
, struct connect_wait
, entry
);
244 if (waiter
->timeout_user
) remove_timeout_user( waiter
->timeout_user
);
245 notify_connect_waiter( waiter
, status
);
249 /* pipe connect wait timeout */
250 static void connect_timeout( void *ptr
)
252 struct connect_wait
*waiter
= (struct connect_wait
*)ptr
;
253 notify_connect_waiter( waiter
, STATUS_TIMEOUT
);
256 static void named_pipe_destroy( struct object
*obj
)
258 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
260 assert( list_empty( &pipe
->servers
) );
261 assert( !pipe
->instances
);
262 notify_all_connect_waiters( pipe
, STATUS_HANDLES_CLOSED
);
265 static void queue_connect_waiter( struct named_pipe
*pipe
, void *func
,
266 void *overlapped
, unsigned int *timeout
)
268 struct connect_wait
*waiter
;
270 waiter
= mem_alloc( sizeof(*waiter
) );
275 set_waiter( &waiter
->wait
, func
, overlapped
);
276 list_add_tail( &pipe
->waiters
, &waiter
->entry
);
280 gettimeofday( &tv
, 0 );
281 add_timeout( &tv
, *timeout
);
282 waiter
->timeout_user
= add_timeout_user( &tv
, connect_timeout
,
286 waiter
->timeout_user
= NULL
;
290 static struct fd
*pipe_client_get_fd( struct object
*obj
)
292 struct pipe_client
*client
= (struct pipe_client
*) obj
;
294 return (struct fd
*) grab_object( client
->fd
);
295 set_error( STATUS_PIPE_DISCONNECTED
);
299 static struct fd
*pipe_server_get_fd( struct object
*obj
)
301 struct pipe_server
*server
= (struct pipe_server
*) obj
;
303 switch(server
->state
)
305 case ps_connected_server
:
306 case ps_wait_disconnect
:
307 assert( server
->fd
);
308 return (struct fd
*) grab_object( server
->fd
);
312 set_error( STATUS_PIPE_LISTENING
);
315 case ps_disconnected_server
:
316 case ps_wait_connect
:
317 set_error( STATUS_PIPE_DISCONNECTED
);
324 static void notify_empty( struct pipe_server
*server
)
326 if( !server
->flush_poll
)
328 assert( server
->state
== ps_connected_server
);
329 assert( server
->event
);
330 remove_timeout_user( server
->flush_poll
);
331 server
->flush_poll
= NULL
;
332 set_event( server
->event
);
333 release_object( server
->event
);
334 server
->event
= NULL
;
337 static void do_disconnect( struct pipe_server
*server
)
339 /* we may only have a server fd, if the client disconnected */
342 assert( server
->client
->server
== server
);
343 assert( server
->client
->fd
);
344 release_object( server
->client
->fd
);
345 server
->client
->fd
= NULL
;
347 assert( server
->fd
);
348 release_object( server
->fd
);
352 static void pipe_server_destroy( struct object
*obj
)
354 struct pipe_server
*server
= (struct pipe_server
*)obj
;
356 assert( obj
->ops
== &pipe_server_ops
);
360 notify_empty( server
);
361 do_disconnect( server
);
366 server
->client
->server
= NULL
;
367 server
->client
= NULL
;
370 notify_waiter( &server
->wait
, STATUS_HANDLES_CLOSED
);
372 assert( server
->pipe
->instances
);
373 server
->pipe
->instances
--;
375 list_remove( &server
->entry
);
376 release_object( server
->pipe
);
379 static void pipe_client_destroy( struct object
*obj
)
381 struct pipe_client
*client
= (struct pipe_client
*)obj
;
382 struct pipe_server
*server
= client
->server
;
384 assert( obj
->ops
== &pipe_client_ops
);
386 notify_waiter( &client
->wait
, STATUS_HANDLES_CLOSED
);
390 notify_empty( server
);
392 switch( server
->state
)
394 case ps_connected_server
:
395 /* Don't destroy the server's fd here as we can't
396 do a successful flush without it. */
397 server
->state
= ps_wait_disconnect
;
398 release_object( client
->fd
);
401 case ps_disconnected_server
:
402 server
->state
= ps_wait_connect
;
406 case ps_wait_disconnect
:
407 case ps_wait_connect
:
410 assert( server
->client
);
411 server
->client
= NULL
;
412 client
->server
= NULL
;
414 assert( !client
->fd
);
417 static int pipe_end_get_poll_events( struct fd
*fd
)
419 return POLLIN
| POLLOUT
; /* FIXME */
422 static int pipe_data_remaining( struct pipe_server
*server
)
427 assert( server
->client
);
429 fd
= get_unix_fd( server
->client
->fd
);
436 if( 0 > poll( &pfd
, 1, 0 ) )
439 return pfd
.revents
&POLLIN
;
442 static void check_flushed( void *arg
)
444 struct pipe_server
*server
= (struct pipe_server
*) arg
;
446 assert( server
->event
);
447 if( pipe_data_remaining( server
) )
451 gettimeofday( &tv
, 0 );
452 add_timeout( &tv
, 100 );
453 server
->flush_poll
= add_timeout_user( &tv
, check_flushed
, server
);
457 /* notify_empty( server ); */
458 server
->flush_poll
= NULL
;
459 set_event( server
->event
);
460 release_object( server
->event
);
461 server
->event
= NULL
;
465 static int pipe_server_flush( struct fd
*fd
, struct event
**event
)
467 struct pipe_server
*server
= get_fd_user( fd
);
472 if( server
->state
!= ps_connected_server
)
475 /* FIXME: if multiple threads flush the same pipe,
476 maybe should create a list of processes to notify */
477 if( server
->flush_poll
)
480 if( pipe_data_remaining( server
) )
484 /* this kind of sux -
485 there's no unix way to be alerted when a pipe becomes empty */
486 server
->event
= create_event( NULL
, 0, 0, 0 );
489 gettimeofday( &tv
, 0 );
490 add_timeout( &tv
, 100 );
491 server
->flush_poll
= add_timeout_user( &tv
, check_flushed
, server
);
492 *event
= server
->event
;
498 static int pipe_client_flush( struct fd
*fd
, struct event
**event
)
500 /* FIXME: what do we have to do for this? */
504 static int pipe_end_get_info( struct fd
*fd
)
509 static struct named_pipe
*create_named_pipe( const WCHAR
*name
, size_t len
)
511 struct named_pipe
*pipe
;
513 pipe
= create_named_object( sync_namespace
, &named_pipe_ops
, name
, len
);
516 if( get_error() != STATUS_OBJECT_NAME_COLLISION
)
518 /* initialize it if it didn't already exist */
520 list_init( &pipe
->servers
);
521 list_init( &pipe
->waiters
);
527 static struct named_pipe
*open_named_pipe( const WCHAR
*name
, size_t len
)
531 if ((obj
= find_object( sync_namespace
, name
, len
)))
533 if (obj
->ops
== &named_pipe_ops
) return (struct named_pipe
*)obj
;
534 release_object( obj
);
535 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
537 else set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
542 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
543 obj_handle_t handle
, unsigned int access
)
546 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
547 return (struct pipe_server
*) obj
;
550 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
)
552 struct pipe_server
*server
;
554 server
= alloc_object( &pipe_server_ops
);
560 server
->state
= ps_idle_server
;
561 server
->client
= NULL
;
562 server
->flush_poll
= NULL
;
563 server
->wait
.thread
= NULL
;
565 list_add_head( &pipe
->servers
, &server
->entry
);
571 static struct pipe_client
*create_pipe_client( struct pipe_server
*server
)
573 struct pipe_client
*client
;
575 client
= alloc_object( &pipe_client_ops
);
580 client
->server
= server
;
581 client
->wait
.thread
= NULL
;
586 static inline struct pipe_server
*find_server( struct named_pipe
*pipe
, enum pipe_state state
)
588 struct pipe_server
*server
;
590 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
592 if (server
->state
== state
) return (struct pipe_server
*)grab_object( server
);
597 static inline struct pipe_server
*find_server2( struct named_pipe
*pipe
,
598 enum pipe_state state1
, enum pipe_state state2
)
600 struct pipe_server
*server
;
602 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
604 if (server
->state
== state1
|| server
->state
== state2
)
605 return (struct pipe_server
*)grab_object( server
);
610 DECL_HANDLER(create_named_pipe
)
612 struct named_pipe
*pipe
;
613 struct pipe_server
*server
;
616 pipe
= create_named_pipe( get_req_data(), get_req_data_size() );
620 if( get_error() != STATUS_OBJECT_NAME_COLLISION
)
622 pipe
->insize
= req
->insize
;
623 pipe
->outsize
= req
->outsize
;
624 pipe
->maxinstances
= req
->maxinstances
;
625 pipe
->timeout
= req
->timeout
;
626 pipe
->pipemode
= req
->pipemode
;
630 set_error( 0 ); /* clear the name collision */
631 if( pipe
->maxinstances
<= pipe
->instances
)
633 set_error( STATUS_PIPE_BUSY
);
634 release_object( pipe
);
637 if( ( pipe
->maxinstances
!= req
->maxinstances
) ||
638 ( pipe
->timeout
!= req
->timeout
) ||
639 ( pipe
->pipemode
!= req
->pipemode
) )
641 set_error( STATUS_ACCESS_DENIED
);
642 release_object( pipe
);
647 server
= create_pipe_server( pipe
);
650 reply
->handle
= alloc_handle( current
->process
, server
,
651 GENERIC_READ
|GENERIC_WRITE
, req
->inherit
);
652 server
->pipe
->instances
++;
653 release_object( server
);
656 release_object( pipe
);
659 DECL_HANDLER(open_named_pipe
)
661 struct pipe_server
*server
;
662 struct pipe_client
*client
;
663 struct named_pipe
*pipe
;
666 pipe
= open_named_pipe( get_req_data(), get_req_data_size() );
669 set_error( STATUS_NO_SUCH_FILE
);
673 server
= find_server2( pipe
, ps_idle_server
, ps_wait_open
);
674 release_object( pipe
);
678 set_error( STATUS_PIPE_NOT_AVAILABLE
);
682 client
= create_pipe_client( server
);
685 if( !socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
) )
687 assert( !client
->fd
);
688 assert( !server
->fd
);
689 client
->fd
= create_anonymous_fd( &pipe_server_fd_ops
,
690 fds
[1], &client
->obj
);
691 server
->fd
= create_anonymous_fd( &pipe_server_fd_ops
,
692 fds
[0], &server
->obj
);
693 if (client
->fd
&& server
->fd
)
695 if( server
->state
== ps_wait_open
)
696 notify_waiter( &server
->wait
, STATUS_SUCCESS
);
697 assert( !server
->wait
.thread
);
698 server
->state
= ps_connected_server
;
699 server
->client
= client
;
700 client
->server
= server
;
701 reply
->handle
= alloc_handle( current
->process
, client
,
702 req
->access
, req
->inherit
);
708 release_object( client
);
710 release_object( server
);
713 DECL_HANDLER(connect_named_pipe
)
715 struct pipe_server
*server
;
717 server
= get_pipe_server_obj(current
->process
, req
->handle
, 0);
721 switch( server
->state
)
724 case ps_wait_connect
:
725 assert( !server
->fd
);
726 server
->state
= ps_wait_open
;
727 set_waiter( &server
->wait
, req
->func
, req
->overlapped
);
728 notify_all_connect_waiters( server
->pipe
, STATUS_SUCCESS
);
730 case ps_connected_server
:
731 assert( server
->fd
);
732 set_error( STATUS_PIPE_CONNECTED
);
734 case ps_disconnected_server
:
735 set_error( STATUS_PIPE_BUSY
);
737 case ps_wait_disconnect
:
738 set_error( STATUS_NO_DATA_DETECTED
);
741 set_error( STATUS_INVALID_HANDLE
);
745 release_object(server
);
748 DECL_HANDLER(wait_named_pipe
)
750 struct named_pipe
*pipe
;
751 struct pipe_server
*server
;
753 if (!(pipe
= open_named_pipe( get_req_data(), get_req_data_size() )))
755 set_error( STATUS_PIPE_NOT_AVAILABLE
);
758 server
= find_server( pipe
, ps_wait_open
);
761 /* there's already a server waiting for a client to connect */
762 struct wait_info wait
;
763 set_waiter( &wait
, req
->func
, req
->overlapped
);
764 notify_waiter( &wait
, STATUS_SUCCESS
);
765 release_object( server
);
769 unsigned int timeout
;
770 if (req
->timeout
== NMPWAIT_USE_DEFAULT_WAIT
)
771 timeout
= pipe
->timeout
;
773 timeout
= req
->timeout
;
775 if (req
->timeout
== NMPWAIT_WAIT_FOREVER
)
776 queue_connect_waiter( pipe
, req
->func
, req
->overlapped
, NULL
);
778 queue_connect_waiter( pipe
, req
->func
, req
->overlapped
, &timeout
);
781 release_object( pipe
);
784 DECL_HANDLER(disconnect_named_pipe
)
786 struct pipe_server
*server
;
789 server
= get_pipe_server_obj( current
->process
, req
->handle
, 0 );
792 switch( server
->state
)
794 case ps_connected_server
:
795 assert( server
->fd
);
796 assert( server
->client
);
797 assert( server
->client
->fd
);
799 notify_empty( server
);
800 notify_waiter( &server
->client
->wait
, STATUS_PIPE_DISCONNECTED
);
802 /* Dump the client and server fds, but keep the pointers
803 around - client loses all waiting data */
804 server
->state
= ps_disconnected_server
;
805 do_disconnect( server
);
806 reply
->fd
= flush_cached_fd( current
->process
, req
->handle
);
809 case ps_wait_disconnect
:
810 assert( !server
->client
);
811 assert( server
->fd
);
812 do_disconnect( server
);
813 server
->state
= ps_wait_connect
;
814 reply
->fd
= flush_cached_fd( current
->process
, req
->handle
);
819 case ps_disconnected_server
:
820 case ps_wait_connect
:
821 set_error( STATUS_PIPE_DISCONNECTED
);
824 release_object( server
);
827 DECL_HANDLER(get_named_pipe_info
)
829 struct pipe_server
*server
;
831 server
= get_pipe_server_obj( current
->process
, req
->handle
, 0 );
835 reply
->flags
= server
->pipe
->pipemode
;
836 reply
->maxinstances
= server
->pipe
->maxinstances
;
837 reply
->insize
= server
->pipe
->insize
;
838 reply
->outsize
= server
->pipe
->outsize
;
840 release_object(server
);