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"
34 #include <sys/types.h>
35 #ifdef HAVE_SYS_SOCKET_H
36 #include <sys/socket.h>
55 ps_disconnected_server,
61 struct thread *thread;
72 enum pipe_state state;
73 struct pipe_client *client;
74 struct named_pipe *pipe;
75 struct pipe_server *next;
76 struct pipe_server *prev;
77 struct timeout_user *flush_poll;
79 struct wait_info wait;
86 struct pipe_server *server;
87 struct wait_info wait;
92 struct wait_info wait;
93 struct connect_wait *next;
98 struct object obj; /* object header */
99 unsigned int pipemode;
100 unsigned int maxinstances;
101 unsigned int outsize;
103 unsigned int timeout;
104 unsigned int instances;
105 struct pipe_server *servers;
106 struct connect_wait *connect_waiters;
109 static void named_pipe_dump( struct object *obj, int verbose );
110 static void named_pipe_destroy( struct object *obj );
112 static const struct object_ops named_pipe_ops =
114 sizeof(struct named_pipe), /* size */
115 named_pipe_dump, /* dump */
116 no_add_queue, /* add_queue */
117 NULL, /* remove_queue */
119 NULL, /* satisfied */
120 no_get_fd, /* get_fd */
121 named_pipe_destroy /* destroy */
124 /* common to clients and servers */
125 static int pipe_end_get_poll_events( struct fd *fd );
126 static int pipe_end_get_info( struct fd *fd,
127 struct get_file_info_reply *reply, int *flags );
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 */
156 /* client end functions */
157 static void pipe_client_dump( struct object *obj, int verbose );
158 static struct fd *pipe_client_get_fd( struct object *obj );
159 static void pipe_client_destroy( struct object *obj );
160 static int pipe_client_flush( struct fd *fd, struct event **event );
162 static const struct object_ops pipe_client_ops =
164 sizeof(struct pipe_client), /* size */
165 pipe_client_dump, /* dump */
166 default_fd_add_queue, /* add_queue */
167 default_fd_remove_queue, /* remove_queue */
168 default_fd_signaled, /* signaled */
169 no_satisfied, /* satisfied */
170 pipe_client_get_fd, /* get_fd */
171 pipe_client_destroy /* destroy */
174 static const struct fd_ops pipe_client_fd_ops =
176 pipe_end_get_poll_events, /* get_poll_events */
177 default_poll_event, /* poll_event */
178 pipe_client_flush, /* flush */
179 pipe_end_get_info, /* get_file_info */
180 no_queue_async /* queue_async */
183 static void named_pipe_dump( struct object *obj, int verbose )
185 struct named_pipe *pipe = (struct named_pipe *) obj;
186 assert( obj->ops == &named_pipe_ops );
187 fprintf( stderr, "named pipe %p\n" ,pipe);
190 static void pipe_server_dump( struct object *obj, int verbose )
192 struct pipe_server *server = (struct pipe_server *) obj;
193 assert( obj->ops == &pipe_server_ops );
194 fprintf( stderr, "named pipe server %p (state %d)\n",
195 server, server->state );
198 static void pipe_client_dump( struct object *obj, int verbose )
200 struct pipe_client *client = (struct pipe_client *) obj;
201 assert( obj->ops == &pipe_server_ops );
202 fprintf( stderr, "named pipe client %p (server state %d)\n",
203 client, client->server->state );
206 static void named_pipe_destroy( struct object *obj)
208 struct named_pipe *pipe = (struct named_pipe *) obj;
209 assert( !pipe->servers );
210 assert( !pipe->instances );
213 static void notify_waiter( struct wait_info *wait, unsigned int status )
215 if( wait->thread && wait->func && wait->overlapped )
217 /* queue a system APC, to notify a waiting thread */
218 thread_queue_apc( wait->thread, NULL, wait->func, APC_ASYNC,
219 1, wait->overlapped, (void *)status, NULL );
221 if( wait->thread ) release_object( wait->thread );
225 static void set_waiter( struct wait_info *wait, void *func, void *ov )
227 wait->thread = (struct thread *) grab_object( current );
229 wait->overlapped = ov;
232 static void notify_connect_waiters( struct named_pipe *pipe )
234 struct connect_wait *cw, **x = &pipe->connect_waiters;
239 notify_waiter( &cw->wait, STATUS_SUCCESS );
240 release_object( pipe );
246 static void queue_connect_waiter( struct named_pipe *pipe,
247 void *func, void *overlapped )
249 struct connect_wait *waiter;
251 waiter = mem_alloc( sizeof(*waiter) );
254 set_waiter( &waiter->wait, func, overlapped );
255 waiter->next = pipe->connect_waiters;
256 pipe->connect_waiters = waiter;
261 static struct fd *pipe_client_get_fd( struct object *obj )
263 struct pipe_client *client = (struct pipe_client *) obj;
265 return (struct fd *) grab_object( client->fd );
266 set_error( STATUS_PIPE_DISCONNECTED );
270 static struct fd *pipe_server_get_fd( struct object *obj )
272 struct pipe_server *server = (struct pipe_server *) obj;
274 switch(server->state)
276 case ps_connected_server:
277 case ps_wait_disconnect:
278 assert( server->fd );
279 return (struct fd *) grab_object( server->fd );
283 set_error( STATUS_PIPE_LISTENING );
286 case ps_disconnected_server:
287 case ps_wait_connect:
288 set_error( STATUS_PIPE_DISCONNECTED );
298 static void notify_empty( struct pipe_server *server )
300 if( !server->flush_poll )
302 assert( server->state == ps_connected_server );
303 assert( server->event );
304 remove_timeout_user( server->flush_poll );
305 server->flush_poll = NULL;
306 set_event( server->event );
307 release_object( server->event );
308 server->event = NULL;
311 static void do_disconnect( struct pipe_server *server )
313 /* we may only have a server fd, if the client disconnected */
316 assert( server->client->server == server );
317 assert( server->client->fd );
318 release_object( server->client->fd );
319 server->client->fd = NULL;
321 assert( server->fd );
322 release_object( server->fd );
326 static void pipe_server_destroy( struct object *obj)
328 struct pipe_server *server = (struct pipe_server *)obj;
330 assert( obj->ops == &pipe_server_ops );
334 notify_empty( server );
335 do_disconnect( server );
340 server->client->server = NULL;
341 server->client = NULL;
344 notify_waiter( &server->wait, STATUS_HANDLES_CLOSED );
346 assert( server->pipe->instances );
347 server->pipe->instances--;
349 /* remove server from pipe's server list */
350 if( server->next ) server->next->prev = server->prev;
351 if( server->prev ) server->prev->next = server->next;
352 else server->pipe->servers = server->next;
353 release_object( server->pipe );
356 static void pipe_client_destroy( struct object *obj)
358 struct pipe_client *client = (struct pipe_client *)obj;
359 struct pipe_server *server = client->server;
361 assert( obj->ops == &pipe_client_ops );
363 notify_waiter( &client->wait, STATUS_HANDLES_CLOSED );
367 notify_empty( server );
369 switch( server->state )
371 case ps_connected_server:
372 /* Don't destroy the server's fd here as we can't
373 do a successful flush without it. */
374 server->state = ps_wait_disconnect;
375 release_object( client->fd );
378 case ps_disconnected_server:
379 server->state = ps_wait_connect;
384 assert( server->client );
385 server->client = NULL;
386 client->server = NULL;
388 assert( !client->fd );
391 static int pipe_end_get_poll_events( struct fd *fd )
393 return POLLIN | POLLOUT; /* FIXME */
396 static int pipe_data_remaining( struct pipe_server *server )
401 assert( server->client );
403 fd = get_unix_fd( server->client->fd );
410 if( 0 > poll( &pfd, 1, 0 ) )
413 return pfd.revents&POLLIN;
416 static void check_flushed( void *arg )
418 struct pipe_server *server = (struct pipe_server*) arg;
420 assert( server->event );
421 if( pipe_data_remaining( server ) )
425 gettimeofday( &tv, 0 );
426 add_timeout( &tv, 100 );
427 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
430 notify_empty( server );
433 static int pipe_server_flush( struct fd *fd, struct event **event )
435 struct pipe_server *server = get_fd_user( fd );
440 if( server->state != ps_connected_server )
443 /* FIXME: if multiple threads flush the same pipe,
444 maybe should create a list of processes to notify */
445 if( server->flush_poll )
448 if( pipe_data_remaining( server ) )
452 /* this kind of sux -
453 there's no unix way to be alerted when a pipe becomes empty */
454 server->event = create_event( NULL, 0, 0, 0 );
457 gettimeofday( &tv, 0 );
458 add_timeout( &tv, 100 );
459 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
460 *event = server->event;
466 static int pipe_client_flush( struct fd *fd, struct event **event )
468 /* FIXME: what do we have to do for this? */
472 static int pipe_end_get_info( struct fd *fd,
473 struct get_file_info_reply *reply, int *flags )
477 reply->type = FILE_TYPE_PIPE;
479 reply->access_time = 0;
480 reply->write_time = 0;
481 reply->size_high = 0;
484 reply->index_high = 0;
485 reply->index_low = 0;
489 return FD_TYPE_DEFAULT;
492 static struct named_pipe *create_named_pipe( const WCHAR *name, size_t len )
494 struct named_pipe *pipe;
496 pipe = create_named_object( sync_namespace, &named_pipe_ops, name, len );
499 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
501 /* initialize it if it didn't already exist */
504 pipe->connect_waiters = NULL;
510 static struct named_pipe *open_named_pipe( const WCHAR *name, size_t len )
514 if ((obj = find_object( sync_namespace, name, len )))
516 if (obj->ops == &named_pipe_ops) return (struct named_pipe *)obj;
517 release_object( obj );
518 set_error( STATUS_OBJECT_TYPE_MISMATCH );
520 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
525 static struct pipe_server *get_pipe_server_obj( struct process *process,
526 obj_handle_t handle, unsigned int access )
529 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
530 return (struct pipe_server *) obj;
533 static struct pipe_server *create_pipe_server( struct named_pipe *pipe )
535 struct pipe_server *server;
537 server = alloc_object( &pipe_server_ops );
543 server->state = ps_none;
544 server->client = NULL;
545 server->flush_poll = NULL;
546 server->wait.thread = NULL;
548 /* add to list of pipe servers */
549 if ((server->next = pipe->servers)) server->next->prev = server;
551 pipe->servers = server;
558 static struct pipe_client *create_pipe_client( struct pipe_server *server )
560 struct pipe_client *client;
562 client = alloc_object( &pipe_client_ops );
567 client->server = server;
568 client->wait.thread = NULL;
573 static struct pipe_server *find_server( struct named_pipe *pipe,
574 enum pipe_state state )
576 struct pipe_server *x;
578 for( x = pipe->servers; x; x = x->next )
579 if( x->state == state )
585 return (struct pipe_server *) grab_object( x );
588 DECL_HANDLER(create_named_pipe)
590 struct named_pipe *pipe;
591 struct pipe_server *server;
594 pipe = create_named_pipe( get_req_data(), get_req_data_size() );
598 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
600 pipe->insize = req->insize;
601 pipe->outsize = req->outsize;
602 pipe->maxinstances = req->maxinstances;
603 pipe->timeout = req->timeout;
604 pipe->pipemode = req->pipemode;
608 set_error( 0 ); /* clear the name collision */
609 if( pipe->maxinstances <= pipe->instances )
611 set_error( STATUS_PIPE_BUSY );
612 release_object( pipe );
615 if( ( pipe->maxinstances != req->maxinstances ) ||
616 ( pipe->timeout != req->timeout ) ||
617 ( pipe->pipemode != req->pipemode ) )
619 set_error( STATUS_ACCESS_DENIED );
620 release_object( pipe );
625 server = create_pipe_server( pipe );
628 server->state = ps_idle_server;
629 reply->handle = alloc_handle( current->process, server,
630 GENERIC_READ|GENERIC_WRITE, 0 );
631 server->pipe->instances++;
632 release_object( server );
635 release_object( pipe );
638 DECL_HANDLER(open_named_pipe)
640 struct pipe_server *server;
641 struct pipe_client *client;
642 struct named_pipe *pipe;
647 pipe = open_named_pipe( get_req_data(), get_req_data_size() );
650 set_error( STATUS_NO_SUCH_FILE );
654 for( server = pipe->servers; server; server = server->next )
655 if( ( server->state==ps_idle_server ) ||
656 ( server->state==ps_wait_open ) )
658 release_object( pipe );
662 set_error( STATUS_PIPE_NOT_AVAILABLE );
666 client = create_pipe_client( server );
669 if( !socketpair( PF_UNIX, SOCK_STREAM, 0, fds ) )
671 assert( !client->fd );
672 assert( !server->fd );
673 client->fd = create_anonymous_fd( &pipe_server_fd_ops,
674 fds[1], &client->obj );
675 server->fd = create_anonymous_fd( &pipe_server_fd_ops,
676 fds[0], &server->obj );
677 if (client->fd && server->fd)
679 if( server->state == ps_wait_open )
680 notify_waiter( &server->wait, STATUS_SUCCESS );
681 assert( !server->wait.thread );
682 server->state = ps_connected_server;
683 server->client = client;
684 client->server = server;
685 reply->handle = alloc_handle( current->process, client,
686 req->access, req->inherit );
692 release_object( client );
696 DECL_HANDLER(connect_named_pipe)
698 struct pipe_server *server;
700 server = get_pipe_server_obj(current->process, req->handle, 0);
704 switch( server->state )
707 case ps_wait_connect:
708 assert( !server->fd );
709 server->state = ps_wait_open;
710 set_waiter( &server->wait, req->func, req->overlapped );
711 notify_connect_waiters( server->pipe );
713 case ps_connected_server:
714 assert( server->fd );
715 set_error( STATUS_PIPE_CONNECTED );
717 case ps_disconnected_server:
718 set_error( STATUS_PIPE_BUSY );
720 case ps_wait_disconnect:
721 set_error( STATUS_NO_DATA_DETECTED );
724 set_error( STATUS_INVALID_HANDLE );
728 release_object(server);
731 DECL_HANDLER(wait_named_pipe)
733 struct named_pipe *pipe;
734 struct pipe_server *server;
736 if (!(pipe = open_named_pipe( get_req_data(), get_req_data_size() )))
738 set_error( STATUS_PIPE_NOT_AVAILABLE );
741 server = find_server( pipe, ps_wait_open );
744 /* there's already a server waiting for a client to connect */
745 struct wait_info wait;
746 set_waiter( &wait, req->func, req->overlapped );
747 notify_waiter( &wait, STATUS_SUCCESS );
748 release_object( server );
751 queue_connect_waiter( pipe, req->func, req->overlapped );
753 release_object( pipe );
756 DECL_HANDLER(disconnect_named_pipe)
758 struct pipe_server *server;
761 server = get_pipe_server_obj( current->process, req->handle, 0 );
764 switch( server->state )
766 case ps_connected_server:
767 assert( server->fd );
768 assert( server->client );
769 assert( server->client->fd );
771 notify_empty( server );
772 notify_waiter( &server->client->wait, STATUS_PIPE_DISCONNECTED );
774 /* Dump the client and server fds, but keep the pointers
775 around - client loses all waiting data */
776 server->state = ps_disconnected_server;
777 do_disconnect( server );
778 reply->fd = flush_cached_fd( current->process, req->handle );
781 case ps_wait_disconnect:
782 assert( !server->client );
783 assert( server->fd );
784 do_disconnect( server );
785 server->state = ps_wait_connect;
786 reply->fd = flush_cached_fd( current->process, req->handle );
790 set_error( STATUS_PIPE_DISCONNECTED );
792 release_object( server );
795 DECL_HANDLER(get_named_pipe_info)
797 struct pipe_server *server;
799 server = get_pipe_server_obj( current->process, req->handle, 0 );
803 reply->flags = server->pipe->pipemode;
804 reply->maxinstances = server->pipe->maxinstances;
805 reply->insize = server->pipe->insize;
806 reply->outsize = server->pipe->outsize;
808 release_object(server);