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,
129 struct get_file_info_reply *reply, int *flags );
131 /* server end functions */
132 static void pipe_server_dump( struct object *obj, int verbose );
133 static struct fd *pipe_server_get_fd( struct object *obj );
134 static void pipe_server_destroy( struct object *obj);
135 static int pipe_server_flush( struct fd *fd, struct event **event );
137 static const struct object_ops pipe_server_ops =
139 sizeof(struct pipe_server), /* size */
140 pipe_server_dump, /* dump */
141 default_fd_add_queue, /* add_queue */
142 default_fd_remove_queue, /* remove_queue */
143 default_fd_signaled, /* signaled */
144 no_satisfied, /* satisfied */
145 pipe_server_get_fd, /* get_fd */
146 pipe_server_destroy /* destroy */
149 static const struct fd_ops pipe_server_fd_ops =
151 pipe_end_get_poll_events, /* get_poll_events */
152 default_poll_event, /* poll_event */
153 pipe_server_flush, /* flush */
154 pipe_end_get_info, /* get_file_info */
155 no_queue_async /* queue_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 */
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 %p\n" ,pipe);
192 static void pipe_server_dump( struct object *obj, int verbose )
194 struct pipe_server *server = (struct pipe_server *) obj;
195 assert( obj->ops == &pipe_server_ops );
196 fprintf( stderr, "named pipe server %p (state %d)\n",
197 server, server->state );
200 static void pipe_client_dump( struct object *obj, int verbose )
202 struct pipe_client *client = (struct pipe_client *) obj;
203 assert( obj->ops == &pipe_server_ops );
204 fprintf( stderr, "named pipe client %p (server state %d)\n",
205 client, client->server->state );
208 static void named_pipe_destroy( struct object *obj)
210 struct named_pipe *pipe = (struct named_pipe *) obj;
211 assert( !pipe->servers );
212 assert( !pipe->instances );
215 static void notify_waiter( struct wait_info *wait, unsigned int status )
217 if( wait->thread && wait->func && wait->overlapped )
219 /* queue a system APC, to notify a waiting thread */
220 thread_queue_apc( wait->thread, NULL, wait->func, APC_ASYNC,
221 1, wait->overlapped, (void *)status, NULL );
223 if( wait->thread ) release_object( wait->thread );
227 static void set_waiter( struct wait_info *wait, void *func, void *ov )
229 wait->thread = (struct thread *) grab_object( current );
231 wait->overlapped = ov;
234 static void notify_connect_waiters( struct named_pipe *pipe )
236 struct connect_wait *cw, **x = &pipe->connect_waiters;
241 notify_waiter( &cw->wait, STATUS_SUCCESS );
242 release_object( pipe );
248 static void queue_connect_waiter( struct named_pipe *pipe,
249 void *func, void *overlapped )
251 struct connect_wait *waiter;
253 waiter = mem_alloc( sizeof(*waiter) );
256 set_waiter( &waiter->wait, func, overlapped );
257 waiter->next = pipe->connect_waiters;
258 pipe->connect_waiters = waiter;
263 static struct fd *pipe_client_get_fd( struct object *obj )
265 struct pipe_client *client = (struct pipe_client *) obj;
267 return (struct fd *) grab_object( client->fd );
268 set_error( STATUS_PIPE_DISCONNECTED );
272 static struct fd *pipe_server_get_fd( struct object *obj )
274 struct pipe_server *server = (struct pipe_server *) obj;
276 switch(server->state)
278 case ps_connected_server:
279 case ps_wait_disconnect:
280 assert( server->fd );
281 return (struct fd *) grab_object( server->fd );
285 set_error( STATUS_PIPE_LISTENING );
288 case ps_disconnected_server:
289 case ps_wait_connect:
290 set_error( STATUS_PIPE_DISCONNECTED );
300 static void notify_empty( struct pipe_server *server )
302 if( !server->flush_poll )
304 assert( server->state == ps_connected_server );
305 assert( server->event );
306 remove_timeout_user( server->flush_poll );
307 server->flush_poll = NULL;
308 set_event( server->event );
309 release_object( server->event );
310 server->event = NULL;
313 static void do_disconnect( struct pipe_server *server )
315 /* we may only have a server fd, if the client disconnected */
318 assert( server->client->server == server );
319 assert( server->client->fd );
320 release_object( server->client->fd );
321 server->client->fd = NULL;
323 assert( server->fd );
324 release_object( server->fd );
328 static void pipe_server_destroy( struct object *obj)
330 struct pipe_server *server = (struct pipe_server *)obj;
332 assert( obj->ops == &pipe_server_ops );
336 notify_empty( server );
337 do_disconnect( server );
342 server->client->server = NULL;
343 server->client = NULL;
346 notify_waiter( &server->wait, STATUS_HANDLES_CLOSED );
348 assert( server->pipe->instances );
349 server->pipe->instances--;
351 /* remove server from pipe's server list */
352 if( server->next ) server->next->prev = server->prev;
353 if( server->prev ) server->prev->next = server->next;
354 else server->pipe->servers = server->next;
355 release_object( server->pipe );
358 static void pipe_client_destroy( struct object *obj)
360 struct pipe_client *client = (struct pipe_client *)obj;
361 struct pipe_server *server = client->server;
363 assert( obj->ops == &pipe_client_ops );
365 notify_waiter( &client->wait, STATUS_HANDLES_CLOSED );
369 notify_empty( server );
371 switch( server->state )
373 case ps_connected_server:
374 /* Don't destroy the server's fd here as we can't
375 do a successful flush without it. */
376 server->state = ps_wait_disconnect;
377 release_object( client->fd );
380 case ps_disconnected_server:
381 server->state = ps_wait_connect;
386 assert( server->client );
387 server->client = NULL;
388 client->server = NULL;
390 assert( !client->fd );
393 static int pipe_end_get_poll_events( struct fd *fd )
395 return POLLIN | POLLOUT; /* FIXME */
398 static int pipe_data_remaining( struct pipe_server *server )
403 assert( server->client );
405 fd = get_unix_fd( server->client->fd );
412 if( 0 > poll( &pfd, 1, 0 ) )
415 return pfd.revents&POLLIN;
418 static void check_flushed( void *arg )
420 struct pipe_server *server = (struct pipe_server*) arg;
422 assert( server->event );
423 if( pipe_data_remaining( server ) )
427 gettimeofday( &tv, 0 );
428 add_timeout( &tv, 100 );
429 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
432 notify_empty( server );
435 static int pipe_server_flush( struct fd *fd, struct event **event )
437 struct pipe_server *server = get_fd_user( fd );
442 if( server->state != ps_connected_server )
445 /* FIXME: if multiple threads flush the same pipe,
446 maybe should create a list of processes to notify */
447 if( server->flush_poll )
450 if( pipe_data_remaining( server ) )
454 /* this kind of sux -
455 there's no unix way to be alerted when a pipe becomes empty */
456 server->event = create_event( NULL, 0, 0, 0 );
459 gettimeofday( &tv, 0 );
460 add_timeout( &tv, 100 );
461 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
462 *event = server->event;
468 static int pipe_client_flush( struct fd *fd, struct event **event )
470 /* FIXME: what do we have to do for this? */
474 static int pipe_end_get_info( struct fd *fd,
475 struct get_file_info_reply *reply, int *flags )
479 reply->type = FILE_TYPE_PIPE;
481 reply->access_time = 0;
482 reply->write_time = 0;
483 reply->size_high = 0;
486 reply->index_high = 0;
487 reply->index_low = 0;
491 return FD_TYPE_DEFAULT;
494 static struct named_pipe *create_named_pipe( const WCHAR *name, size_t len )
496 struct named_pipe *pipe;
498 pipe = create_named_object( sync_namespace, &named_pipe_ops, name, len );
501 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
503 /* initialize it if it didn't already exist */
506 pipe->connect_waiters = NULL;
512 static struct named_pipe *open_named_pipe( const WCHAR *name, size_t len )
516 if ((obj = find_object( sync_namespace, name, len )))
518 if (obj->ops == &named_pipe_ops) return (struct named_pipe *)obj;
519 release_object( obj );
520 set_error( STATUS_OBJECT_TYPE_MISMATCH );
522 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
527 static struct pipe_server *get_pipe_server_obj( struct process *process,
528 obj_handle_t handle, unsigned int access )
531 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
532 return (struct pipe_server *) obj;
535 static struct pipe_server *create_pipe_server( struct named_pipe *pipe )
537 struct pipe_server *server;
539 server = alloc_object( &pipe_server_ops );
545 server->state = ps_none;
546 server->client = NULL;
547 server->flush_poll = NULL;
548 server->wait.thread = NULL;
550 /* add to list of pipe servers */
551 if ((server->next = pipe->servers)) server->next->prev = server;
553 pipe->servers = server;
560 static struct pipe_client *create_pipe_client( struct pipe_server *server )
562 struct pipe_client *client;
564 client = alloc_object( &pipe_client_ops );
569 client->server = server;
570 client->wait.thread = NULL;
575 static struct pipe_server *find_server( struct named_pipe *pipe,
576 enum pipe_state state )
578 struct pipe_server *x;
580 for( x = pipe->servers; x; x = x->next )
581 if( x->state == state )
587 return (struct pipe_server *) grab_object( x );
590 DECL_HANDLER(create_named_pipe)
592 struct named_pipe *pipe;
593 struct pipe_server *server;
596 pipe = create_named_pipe( get_req_data(), get_req_data_size() );
600 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
602 pipe->insize = req->insize;
603 pipe->outsize = req->outsize;
604 pipe->maxinstances = req->maxinstances;
605 pipe->timeout = req->timeout;
606 pipe->pipemode = req->pipemode;
610 set_error( 0 ); /* clear the name collision */
611 if( pipe->maxinstances <= pipe->instances )
613 set_error( STATUS_PIPE_BUSY );
614 release_object( pipe );
617 if( ( pipe->maxinstances != req->maxinstances ) ||
618 ( pipe->timeout != req->timeout ) ||
619 ( pipe->pipemode != req->pipemode ) )
621 set_error( STATUS_ACCESS_DENIED );
622 release_object( pipe );
627 server = create_pipe_server( pipe );
630 server->state = ps_idle_server;
631 reply->handle = alloc_handle( current->process, server,
632 GENERIC_READ|GENERIC_WRITE, 0 );
633 server->pipe->instances++;
634 release_object( server );
637 release_object( pipe );
640 DECL_HANDLER(open_named_pipe)
642 struct pipe_server *server;
643 struct pipe_client *client;
644 struct named_pipe *pipe;
649 pipe = open_named_pipe( get_req_data(), get_req_data_size() );
652 set_error( STATUS_NO_SUCH_FILE );
656 for( server = pipe->servers; server; server = server->next )
657 if( ( server->state==ps_idle_server ) ||
658 ( server->state==ps_wait_open ) )
660 release_object( pipe );
664 set_error( STATUS_PIPE_NOT_AVAILABLE );
668 client = create_pipe_client( server );
671 if( !socketpair( PF_UNIX, SOCK_STREAM, 0, fds ) )
673 assert( !client->fd );
674 assert( !server->fd );
675 client->fd = create_anonymous_fd( &pipe_server_fd_ops,
676 fds[1], &client->obj );
677 server->fd = create_anonymous_fd( &pipe_server_fd_ops,
678 fds[0], &server->obj );
679 if (client->fd && server->fd)
681 if( server->state == ps_wait_open )
682 notify_waiter( &server->wait, STATUS_SUCCESS );
683 assert( !server->wait.thread );
684 server->state = ps_connected_server;
685 server->client = client;
686 client->server = server;
687 reply->handle = alloc_handle( current->process, client,
688 req->access, req->inherit );
694 release_object( client );
698 DECL_HANDLER(connect_named_pipe)
700 struct pipe_server *server;
702 server = get_pipe_server_obj(current->process, req->handle, 0);
706 switch( server->state )
709 case ps_wait_connect:
710 assert( !server->fd );
711 server->state = ps_wait_open;
712 set_waiter( &server->wait, req->func, req->overlapped );
713 notify_connect_waiters( server->pipe );
715 case ps_connected_server:
716 assert( server->fd );
717 set_error( STATUS_PIPE_CONNECTED );
719 case ps_disconnected_server:
720 set_error( STATUS_PIPE_BUSY );
722 case ps_wait_disconnect:
723 set_error( STATUS_NO_DATA_DETECTED );
726 set_error( STATUS_INVALID_HANDLE );
730 release_object(server);
733 DECL_HANDLER(wait_named_pipe)
735 struct named_pipe *pipe;
736 struct pipe_server *server;
738 if (!(pipe = open_named_pipe( get_req_data(), get_req_data_size() )))
740 set_error( STATUS_PIPE_NOT_AVAILABLE );
743 server = find_server( pipe, ps_wait_open );
746 /* there's already a server waiting for a client to connect */
747 struct wait_info wait;
748 set_waiter( &wait, req->func, req->overlapped );
749 notify_waiter( &wait, STATUS_SUCCESS );
750 release_object( server );
753 queue_connect_waiter( pipe, req->func, req->overlapped );
755 release_object( pipe );
758 DECL_HANDLER(disconnect_named_pipe)
760 struct pipe_server *server;
763 server = get_pipe_server_obj( current->process, req->handle, 0 );
766 switch( server->state )
768 case ps_connected_server:
769 assert( server->fd );
770 assert( server->client );
771 assert( server->client->fd );
773 notify_empty( server );
774 notify_waiter( &server->client->wait, STATUS_PIPE_DISCONNECTED );
776 /* Dump the client and server fds, but keep the pointers
777 around - client loses all waiting data */
778 server->state = ps_disconnected_server;
779 do_disconnect( server );
780 reply->fd = flush_cached_fd( current->process, req->handle );
783 case ps_wait_disconnect:
784 assert( !server->client );
785 assert( server->fd );
786 do_disconnect( server );
787 server->state = ps_wait_connect;
788 reply->fd = flush_cached_fd( current->process, req->handle );
792 set_error( STATUS_PIPE_DISCONNECTED );
794 release_object( server );
797 DECL_HANDLER(get_named_pipe_info)
799 struct pipe_server *server;
801 server = get_pipe_server_obj( current->process, req->handle, 0 );
805 reply->flags = server->pipe->pipemode;
806 reply->maxinstances = server->pipe->maxinstances;
807 reply->insize = server->pipe->insize;
808 reply->outsize = server->pipe->outsize;
810 release_object(server);