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>
56 ps_disconnected_server
,
62 struct thread
*thread
;
71 struct object obj
; /* object header */
72 struct fd
*fd
; /* pipe file descriptor */
73 struct list entry
; /* entry in named pipe servers list */
74 enum pipe_state state
; /* server state */
75 struct pipe_client
*client
; /* client that this server is connected to */
76 struct named_pipe
*pipe
;
77 struct timeout_user
*flush_poll
;
79 struct wait_info wait
;
84 struct object obj
; /* object header */
85 struct fd
*fd
; /* pipe file descriptor */
86 struct pipe_server
*server
; /* server that this client is connected to */
87 struct wait_info wait
;
92 struct list entry
; /* entry in named pipe wait list */
93 struct wait_info wait
;
94 struct timeout_user
*timeout_user
;
99 struct object obj
; /* object header */
100 unsigned int pipemode
;
101 unsigned int maxinstances
;
102 unsigned int outsize
;
104 unsigned int timeout
;
105 unsigned int instances
;
106 struct list servers
; /* list of servers using this pipe */
107 struct list waiters
; /* list of clients waiting to connect */
110 static void named_pipe_dump( struct object
*obj
, int verbose
);
111 static void named_pipe_destroy( struct object
*obj
);
113 static const struct object_ops named_pipe_ops
=
115 sizeof(struct named_pipe
), /* size */
116 named_pipe_dump
, /* dump */
117 no_add_queue
, /* add_queue */
118 NULL
, /* remove_queue */
120 NULL
, /* satisfied */
121 no_get_fd
, /* get_fd */
122 named_pipe_destroy
/* destroy */
125 /* common to clients and servers */
126 static int pipe_end_get_poll_events( struct fd
*fd
);
127 static int pipe_end_get_info( struct fd
*fd
);
129 /* server end functions */
130 static void pipe_server_dump( struct object
*obj
, int verbose
);
131 static struct fd
*pipe_server_get_fd( struct object
*obj
);
132 static void pipe_server_destroy( struct object
*obj
);
133 static int pipe_server_flush( struct fd
*fd
, struct event
**event
);
135 static const struct object_ops pipe_server_ops
=
137 sizeof(struct pipe_server
), /* size */
138 pipe_server_dump
, /* dump */
139 default_fd_add_queue
, /* add_queue */
140 default_fd_remove_queue
, /* remove_queue */
141 default_fd_signaled
, /* signaled */
142 no_satisfied
, /* satisfied */
143 pipe_server_get_fd
, /* get_fd */
144 pipe_server_destroy
/* destroy */
147 static const struct fd_ops pipe_server_fd_ops
=
149 pipe_end_get_poll_events
, /* get_poll_events */
150 default_poll_event
, /* poll_event */
151 pipe_server_flush
, /* flush */
152 pipe_end_get_info
, /* get_file_info */
153 no_queue_async
, /* queue_async */
154 no_cancel_async
, /* cancel_async */
157 /* client end functions */
158 static void pipe_client_dump( struct object
*obj
, int verbose
);
159 static struct fd
*pipe_client_get_fd( struct object
*obj
);
160 static void pipe_client_destroy( struct object
*obj
);
161 static int pipe_client_flush( struct fd
*fd
, struct event
**event
);
163 static const struct object_ops pipe_client_ops
=
165 sizeof(struct pipe_client
), /* size */
166 pipe_client_dump
, /* dump */
167 default_fd_add_queue
, /* add_queue */
168 default_fd_remove_queue
, /* remove_queue */
169 default_fd_signaled
, /* signaled */
170 no_satisfied
, /* satisfied */
171 pipe_client_get_fd
, /* get_fd */
172 pipe_client_destroy
/* destroy */
175 static const struct fd_ops pipe_client_fd_ops
=
177 pipe_end_get_poll_events
, /* get_poll_events */
178 default_poll_event
, /* poll_event */
179 pipe_client_flush
, /* flush */
180 pipe_end_get_info
, /* get_file_info */
181 no_queue_async
, /* queue_async */
182 no_cancel_async
/* cancel_async */
185 static void named_pipe_dump( struct object
*obj
, int verbose
)
187 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
188 assert( obj
->ops
== &named_pipe_ops
);
189 fprintf( stderr
, "Named pipe " );
190 dump_object_name( &pipe
->obj
);
191 fprintf( stderr
, "\n" );
194 static void pipe_server_dump( struct object
*obj
, int verbose
)
196 struct pipe_server
*server
= (struct pipe_server
*) obj
;
197 assert( obj
->ops
== &pipe_server_ops
);
198 fprintf( stderr
, "Named pipe server pipe=%p state=%d\n", server
->pipe
, 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_client_ops
);
205 fprintf( stderr
, "Named pipe client server=%p\n", client
->server
);
208 static void notify_waiter( struct wait_info
*wait
, unsigned int status
)
210 if( wait
->thread
&& wait
->func
&& wait
->overlapped
)
212 /* queue a system APC, to notify a waiting thread */
213 thread_queue_apc( wait
->thread
, NULL
, wait
->func
, APC_ASYNC
,
214 1, wait
->overlapped
, (void *)status
, NULL
);
216 if( wait
->thread
) release_object( wait
->thread
);
220 static void set_waiter( struct wait_info
*wait
, void *func
, void *ov
)
222 wait
->thread
= (struct thread
*) grab_object( current
);
224 wait
->overlapped
= ov
;
227 static void notify_connect_waiter( struct connect_wait
*waiter
, unsigned int status
)
229 notify_waiter( &waiter
->wait
, status
);
230 list_remove( &waiter
->entry
);
234 static void notify_all_connect_waiters( struct named_pipe
*pipe
, unsigned int status
)
238 while ((ptr
= list_head( &pipe
->waiters
)) != NULL
)
240 struct connect_wait
*waiter
= LIST_ENTRY( ptr
, struct connect_wait
, entry
);
241 if (waiter
->timeout_user
) remove_timeout_user( waiter
->timeout_user
);
242 notify_connect_waiter( waiter
, status
);
246 /* pipe connect wait timeout */
247 static void connect_timeout( void *ptr
)
249 struct connect_wait
*waiter
= (struct connect_wait
*)ptr
;
250 notify_connect_waiter( waiter
, STATUS_TIMEOUT
);
253 static void named_pipe_destroy( struct object
*obj
)
255 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
257 assert( list_empty( &pipe
->servers
) );
258 assert( !pipe
->instances
);
259 notify_all_connect_waiters( pipe
, STATUS_HANDLES_CLOSED
);
262 static void queue_connect_waiter( struct named_pipe
*pipe
, void *func
,
263 void *overlapped
, unsigned int *timeout
)
265 struct connect_wait
*waiter
;
267 waiter
= mem_alloc( sizeof(*waiter
) );
272 set_waiter( &waiter
->wait
, func
, overlapped
);
273 list_add_tail( &pipe
->waiters
, &waiter
->entry
);
277 gettimeofday( &tv
, 0 );
278 add_timeout( &tv
, *timeout
);
279 waiter
->timeout_user
= add_timeout_user( &tv
, connect_timeout
,
283 waiter
->timeout_user
= NULL
;
287 static struct fd
*pipe_client_get_fd( struct object
*obj
)
289 struct pipe_client
*client
= (struct pipe_client
*) obj
;
291 return (struct fd
*) grab_object( client
->fd
);
292 set_error( STATUS_PIPE_DISCONNECTED
);
296 static struct fd
*pipe_server_get_fd( struct object
*obj
)
298 struct pipe_server
*server
= (struct pipe_server
*) obj
;
300 switch(server
->state
)
302 case ps_connected_server
:
303 case ps_wait_disconnect
:
304 assert( server
->fd
);
305 return (struct fd
*) grab_object( server
->fd
);
309 set_error( STATUS_PIPE_LISTENING
);
312 case ps_disconnected_server
:
313 case ps_wait_connect
:
314 set_error( STATUS_PIPE_DISCONNECTED
);
321 static void notify_empty( struct pipe_server
*server
)
323 if( !server
->flush_poll
)
325 assert( server
->state
== ps_connected_server
);
326 assert( server
->event
);
327 remove_timeout_user( server
->flush_poll
);
328 server
->flush_poll
= NULL
;
329 set_event( server
->event
);
330 release_object( server
->event
);
331 server
->event
= NULL
;
334 static void do_disconnect( struct pipe_server
*server
)
336 /* we may only have a server fd, if the client disconnected */
339 assert( server
->client
->server
== server
);
340 assert( server
->client
->fd
);
341 release_object( server
->client
->fd
);
342 server
->client
->fd
= NULL
;
344 assert( server
->fd
);
345 release_object( server
->fd
);
349 static void pipe_server_destroy( struct object
*obj
)
351 struct pipe_server
*server
= (struct pipe_server
*)obj
;
353 assert( obj
->ops
== &pipe_server_ops
);
357 notify_empty( server
);
358 do_disconnect( server
);
363 server
->client
->server
= NULL
;
364 server
->client
= NULL
;
367 notify_waiter( &server
->wait
, STATUS_HANDLES_CLOSED
);
369 assert( server
->pipe
->instances
);
370 server
->pipe
->instances
--;
372 list_remove( &server
->entry
);
373 release_object( server
->pipe
);
376 static void pipe_client_destroy( struct object
*obj
)
378 struct pipe_client
*client
= (struct pipe_client
*)obj
;
379 struct pipe_server
*server
= client
->server
;
381 assert( obj
->ops
== &pipe_client_ops
);
383 notify_waiter( &client
->wait
, STATUS_HANDLES_CLOSED
);
387 notify_empty( server
);
389 switch( server
->state
)
391 case ps_connected_server
:
392 /* Don't destroy the server's fd here as we can't
393 do a successful flush without it. */
394 server
->state
= ps_wait_disconnect
;
395 release_object( client
->fd
);
398 case ps_disconnected_server
:
399 server
->state
= ps_wait_connect
;
403 case ps_wait_disconnect
:
404 case ps_wait_connect
:
407 assert( server
->client
);
408 server
->client
= NULL
;
409 client
->server
= NULL
;
411 assert( !client
->fd
);
414 static int pipe_end_get_poll_events( struct fd
*fd
)
416 return POLLIN
| POLLOUT
; /* FIXME */
419 static int pipe_data_remaining( struct pipe_server
*server
)
424 assert( server
->client
);
426 fd
= get_unix_fd( server
->client
->fd
);
433 if( 0 > poll( &pfd
, 1, 0 ) )
436 return pfd
.revents
&POLLIN
;
439 static void check_flushed( void *arg
)
441 struct pipe_server
*server
= (struct pipe_server
*) arg
;
443 assert( server
->event
);
444 if( pipe_data_remaining( server
) )
448 gettimeofday( &tv
, 0 );
449 add_timeout( &tv
, 100 );
450 server
->flush_poll
= add_timeout_user( &tv
, check_flushed
, server
);
454 /* notify_empty( server ); */
455 server
->flush_poll
= NULL
;
456 set_event( server
->event
);
457 release_object( server
->event
);
458 server
->event
= NULL
;
462 static int pipe_server_flush( struct fd
*fd
, struct event
**event
)
464 struct pipe_server
*server
= get_fd_user( fd
);
469 if( server
->state
!= ps_connected_server
)
472 /* FIXME: if multiple threads flush the same pipe,
473 maybe should create a list of processes to notify */
474 if( server
->flush_poll
)
477 if( pipe_data_remaining( server
) )
481 /* this kind of sux -
482 there's no unix way to be alerted when a pipe becomes empty */
483 server
->event
= create_event( NULL
, 0, 0, 0 );
486 gettimeofday( &tv
, 0 );
487 add_timeout( &tv
, 100 );
488 server
->flush_poll
= add_timeout_user( &tv
, check_flushed
, server
);
489 *event
= server
->event
;
495 static int pipe_client_flush( struct fd
*fd
, struct event
**event
)
497 /* FIXME: what do we have to do for this? */
501 static int pipe_end_get_info( struct fd
*fd
)
506 static struct named_pipe
*create_named_pipe( const WCHAR
*name
, size_t len
)
508 struct named_pipe
*pipe
;
510 pipe
= create_named_object( sync_namespace
, &named_pipe_ops
, name
, len
);
513 if( get_error() != STATUS_OBJECT_NAME_COLLISION
)
515 /* initialize it if it didn't already exist */
517 list_init( &pipe
->servers
);
518 list_init( &pipe
->waiters
);
524 static struct named_pipe
*open_named_pipe( const WCHAR
*name
, size_t len
)
528 if ((obj
= find_object( sync_namespace
, name
, len
)))
530 if (obj
->ops
== &named_pipe_ops
) return (struct named_pipe
*)obj
;
531 release_object( obj
);
532 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
534 else set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
539 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
540 obj_handle_t handle
, unsigned int access
)
543 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
544 return (struct pipe_server
*) obj
;
547 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
)
549 struct pipe_server
*server
;
551 server
= alloc_object( &pipe_server_ops
);
557 server
->state
= ps_idle_server
;
558 server
->client
= NULL
;
559 server
->flush_poll
= NULL
;
560 server
->wait
.thread
= NULL
;
562 list_add_head( &pipe
->servers
, &server
->entry
);
568 static struct pipe_client
*create_pipe_client( struct pipe_server
*server
)
570 struct pipe_client
*client
;
572 client
= alloc_object( &pipe_client_ops
);
577 client
->server
= server
;
578 client
->wait
.thread
= NULL
;
583 static inline struct pipe_server
*find_server( struct named_pipe
*pipe
, enum pipe_state state
)
585 struct pipe_server
*server
;
587 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
589 if (server
->state
== state
) return (struct pipe_server
*)grab_object( server
);
594 static inline struct pipe_server
*find_server2( struct named_pipe
*pipe
,
595 enum pipe_state state1
, enum pipe_state state2
)
597 struct pipe_server
*server
;
599 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
601 if (server
->state
== state1
|| server
->state
== state2
)
602 return (struct pipe_server
*)grab_object( server
);
607 DECL_HANDLER(create_named_pipe
)
609 struct named_pipe
*pipe
;
610 struct pipe_server
*server
;
613 pipe
= create_named_pipe( get_req_data(), get_req_data_size() );
617 if( get_error() != STATUS_OBJECT_NAME_COLLISION
)
619 pipe
->insize
= req
->insize
;
620 pipe
->outsize
= req
->outsize
;
621 pipe
->maxinstances
= req
->maxinstances
;
622 pipe
->timeout
= req
->timeout
;
623 pipe
->pipemode
= req
->pipemode
;
627 set_error( 0 ); /* clear the name collision */
628 if( pipe
->maxinstances
<= pipe
->instances
)
630 set_error( STATUS_PIPE_BUSY
);
631 release_object( pipe
);
634 if( ( pipe
->maxinstances
!= req
->maxinstances
) ||
635 ( pipe
->timeout
!= req
->timeout
) ||
636 ( pipe
->pipemode
!= req
->pipemode
) )
638 set_error( STATUS_ACCESS_DENIED
);
639 release_object( pipe
);
644 server
= create_pipe_server( pipe
);
647 reply
->handle
= alloc_handle( current
->process
, server
,
648 GENERIC_READ
|GENERIC_WRITE
, req
->inherit
);
649 server
->pipe
->instances
++;
650 release_object( server
);
653 release_object( pipe
);
656 DECL_HANDLER(open_named_pipe
)
658 struct pipe_server
*server
;
659 struct pipe_client
*client
;
660 struct named_pipe
*pipe
;
663 pipe
= open_named_pipe( get_req_data(), get_req_data_size() );
666 set_error( STATUS_NO_SUCH_FILE
);
670 server
= find_server2( pipe
, ps_idle_server
, ps_wait_open
);
671 release_object( pipe
);
675 set_error( STATUS_PIPE_NOT_AVAILABLE
);
679 client
= create_pipe_client( server
);
682 if( !socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
) )
684 assert( !client
->fd
);
685 assert( !server
->fd
);
686 client
->fd
= create_anonymous_fd( &pipe_server_fd_ops
,
687 fds
[1], &client
->obj
);
688 server
->fd
= create_anonymous_fd( &pipe_server_fd_ops
,
689 fds
[0], &server
->obj
);
690 if (client
->fd
&& server
->fd
)
692 if( server
->state
== ps_wait_open
)
693 notify_waiter( &server
->wait
, STATUS_SUCCESS
);
694 assert( !server
->wait
.thread
);
695 server
->state
= ps_connected_server
;
696 server
->client
= client
;
697 client
->server
= server
;
698 reply
->handle
= alloc_handle( current
->process
, client
,
699 req
->access
, req
->inherit
);
705 release_object( client
);
707 release_object( server
);
710 DECL_HANDLER(connect_named_pipe
)
712 struct pipe_server
*server
;
714 server
= get_pipe_server_obj(current
->process
, req
->handle
, 0);
718 switch( server
->state
)
721 case ps_wait_connect
:
722 assert( !server
->fd
);
723 server
->state
= ps_wait_open
;
724 set_waiter( &server
->wait
, req
->func
, req
->overlapped
);
725 notify_all_connect_waiters( server
->pipe
, STATUS_SUCCESS
);
727 case ps_connected_server
:
728 assert( server
->fd
);
729 set_error( STATUS_PIPE_CONNECTED
);
731 case ps_disconnected_server
:
732 set_error( STATUS_PIPE_BUSY
);
734 case ps_wait_disconnect
:
735 set_error( STATUS_NO_DATA_DETECTED
);
738 set_error( STATUS_INVALID_HANDLE
);
742 release_object(server
);
745 DECL_HANDLER(wait_named_pipe
)
747 struct named_pipe
*pipe
;
748 struct pipe_server
*server
;
750 if (!(pipe
= open_named_pipe( get_req_data(), get_req_data_size() )))
752 set_error( STATUS_PIPE_NOT_AVAILABLE
);
755 server
= find_server( pipe
, ps_wait_open
);
758 /* there's already a server waiting for a client to connect */
759 struct wait_info wait
;
760 set_waiter( &wait
, req
->func
, req
->overlapped
);
761 notify_waiter( &wait
, STATUS_SUCCESS
);
762 release_object( server
);
766 unsigned int timeout
;
767 if (req
->timeout
== NMPWAIT_USE_DEFAULT_WAIT
)
768 timeout
= pipe
->timeout
;
770 timeout
= req
->timeout
;
772 if (req
->timeout
== NMPWAIT_WAIT_FOREVER
)
773 queue_connect_waiter( pipe
, req
->func
, req
->overlapped
, NULL
);
775 queue_connect_waiter( pipe
, req
->func
, req
->overlapped
, &timeout
);
778 release_object( pipe
);
781 DECL_HANDLER(disconnect_named_pipe
)
783 struct pipe_server
*server
;
786 server
= get_pipe_server_obj( current
->process
, req
->handle
, 0 );
789 switch( server
->state
)
791 case ps_connected_server
:
792 assert( server
->fd
);
793 assert( server
->client
);
794 assert( server
->client
->fd
);
796 notify_empty( server
);
797 notify_waiter( &server
->client
->wait
, STATUS_PIPE_DISCONNECTED
);
799 /* Dump the client and server fds, but keep the pointers
800 around - client loses all waiting data */
801 server
->state
= ps_disconnected_server
;
802 do_disconnect( server
);
803 reply
->fd
= flush_cached_fd( current
->process
, req
->handle
);
806 case ps_wait_disconnect
:
807 assert( !server
->client
);
808 assert( server
->fd
);
809 do_disconnect( server
);
810 server
->state
= ps_wait_connect
;
811 reply
->fd
= flush_cached_fd( current
->process
, req
->handle
);
816 case ps_disconnected_server
:
817 case ps_wait_connect
:
818 set_error( STATUS_PIPE_DISCONNECTED
);
821 release_object( server
);
824 DECL_HANDLER(get_named_pipe_info
)
826 struct pipe_server
*server
;
828 server
= get_pipe_server_obj( current
->process
, req
->handle
, 0 );
832 reply
->flags
= server
->pipe
->pipemode
;
833 reply
->maxinstances
= server
->pipe
->maxinstances
;
834 reply
->insize
= server
->pipe
->insize
;
835 reply
->outsize
= server
->pipe
->outsize
;
837 release_object(server
);