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>
46 #define WIN32_NO_STATUS
61 ps_disconnected_server,
69 struct object obj; /* object header */
70 struct fd *fd; /* pipe file descriptor */
71 struct list entry; /* entry in named pipe servers list */
72 enum pipe_state state; /* server state */
73 struct pipe_client *client; /* client that this server is connected to */
74 struct named_pipe *pipe;
75 struct timeout_user *flush_poll;
77 struct list wait_q; /* only a single one can be queued */
78 unsigned int options; /* pipe options */
83 struct object obj; /* object header */
84 struct fd *fd; /* pipe file descriptor */
85 struct pipe_server *server; /* server that this client is connected to */
86 unsigned int flags; /* file flags */
91 struct object obj; /* object header */
93 unsigned int maxinstances;
97 unsigned int instances;
98 struct list servers; /* list of servers using this pipe */
99 struct list waiters; /* list of clients waiting to connect */
102 static void named_pipe_dump( struct object *obj, int verbose );
103 static void named_pipe_destroy( struct object *obj );
105 static const struct object_ops named_pipe_ops =
107 sizeof(struct named_pipe), /* size */
108 named_pipe_dump, /* dump */
109 no_add_queue, /* add_queue */
110 NULL, /* remove_queue */
112 NULL, /* satisfied */
113 no_signal, /* signal */
114 no_get_fd, /* get_fd */
115 no_lookup_name, /* lookup_name */
116 no_close_handle, /* close_handle */
117 named_pipe_destroy /* destroy */
120 /* server end functions */
121 static void pipe_server_dump( struct object *obj, int verbose );
122 static struct fd *pipe_server_get_fd( struct object *obj );
123 static void pipe_server_destroy( struct object *obj);
124 static int pipe_server_flush( struct fd *fd, struct event **event );
125 static int pipe_server_get_info( struct fd *fd );
127 static const struct object_ops pipe_server_ops =
129 sizeof(struct pipe_server), /* size */
130 pipe_server_dump, /* dump */
131 default_fd_add_queue, /* add_queue */
132 default_fd_remove_queue, /* remove_queue */
133 default_fd_signaled, /* signaled */
134 no_satisfied, /* satisfied */
135 no_signal, /* signal */
136 pipe_server_get_fd, /* get_fd */
137 no_lookup_name, /* lookup_name */
138 no_close_handle, /* close_handle */
139 pipe_server_destroy /* destroy */
142 static const struct fd_ops pipe_server_fd_ops =
144 default_fd_get_poll_events, /* get_poll_events */
145 default_poll_event, /* poll_event */
146 pipe_server_flush, /* flush */
147 pipe_server_get_info, /* get_file_info */
148 default_fd_queue_async, /* queue_async */
149 default_fd_cancel_async, /* cancel_async */
152 /* client end functions */
153 static void pipe_client_dump( struct object *obj, int verbose );
154 static struct fd *pipe_client_get_fd( struct object *obj );
155 static void pipe_client_destroy( struct object *obj );
156 static int pipe_client_flush( struct fd *fd, struct event **event );
157 static int pipe_client_get_info( struct fd *fd );
159 static const struct object_ops pipe_client_ops =
161 sizeof(struct pipe_client), /* size */
162 pipe_client_dump, /* dump */
163 default_fd_add_queue, /* add_queue */
164 default_fd_remove_queue, /* remove_queue */
165 default_fd_signaled, /* signaled */
166 no_satisfied, /* satisfied */
167 no_signal, /* signal */
168 pipe_client_get_fd, /* get_fd */
169 no_lookup_name, /* lookup_name */
170 no_close_handle, /* close_handle */
171 pipe_client_destroy /* destroy */
174 static const struct fd_ops pipe_client_fd_ops =
176 default_fd_get_poll_events, /* get_poll_events */
177 default_poll_event, /* poll_event */
178 pipe_client_flush, /* flush */
179 pipe_client_get_info, /* get_file_info */
180 default_fd_queue_async, /* queue_async */
181 default_fd_cancel_async /* cancel_async */
184 static void named_pipe_dump( struct object *obj, int verbose )
186 struct named_pipe *pipe = (struct named_pipe *) obj;
187 assert( obj->ops == &named_pipe_ops );
188 fprintf( stderr, "Named pipe " );
189 dump_object_name( &pipe->obj );
190 fprintf( stderr, "\n" );
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 pipe=%p state=%d\n", server->pipe, 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_client_ops );
204 fprintf( stderr, "Named pipe client server=%p\n", client->server );
207 static void named_pipe_destroy( struct object *obj)
209 struct named_pipe *pipe = (struct named_pipe *) obj;
211 assert( list_empty( &pipe->servers ) );
212 assert( !pipe->instances );
213 async_terminate_queue( &pipe->waiters, STATUS_HANDLES_CLOSED );
216 static struct fd *pipe_client_get_fd( struct object *obj )
218 struct pipe_client *client = (struct pipe_client *) obj;
220 return (struct fd *) grab_object( client->fd );
221 set_error( STATUS_PIPE_DISCONNECTED );
225 static struct fd *pipe_server_get_fd( struct object *obj )
227 struct pipe_server *server = (struct pipe_server *) obj;
229 switch(server->state)
231 case ps_connected_server:
232 case ps_wait_disconnect:
233 assert( server->fd );
234 return (struct fd *) grab_object( server->fd );
238 set_error( STATUS_PIPE_LISTENING );
241 case ps_disconnected_server:
242 case ps_wait_connect:
243 set_error( STATUS_PIPE_DISCONNECTED );
250 static void notify_empty( struct pipe_server *server )
252 if (!server->flush_poll)
254 assert( server->state == ps_connected_server );
255 assert( server->event );
256 remove_timeout_user( server->flush_poll );
257 server->flush_poll = NULL;
258 set_event( server->event );
259 release_object( server->event );
260 server->event = NULL;
263 static void do_disconnect( struct pipe_server *server )
265 /* we may only have a server fd, if the client disconnected */
268 assert( server->client->server == server );
269 assert( server->client->fd );
270 release_object( server->client->fd );
271 server->client->fd = NULL;
273 assert( server->fd );
274 release_object( server->fd );
278 static void pipe_server_destroy( struct object *obj)
280 struct pipe_server *server = (struct pipe_server *)obj;
282 assert( obj->ops == &pipe_server_ops );
286 notify_empty( server );
287 do_disconnect( server );
292 server->client->server = NULL;
293 server->client = NULL;
296 async_terminate_head( &server->wait_q, STATUS_HANDLES_CLOSED );
298 assert( server->pipe->instances );
299 server->pipe->instances--;
301 list_remove( &server->entry );
302 release_object( server->pipe );
305 static void pipe_client_destroy( struct object *obj)
307 struct pipe_client *client = (struct pipe_client *)obj;
308 struct pipe_server *server = client->server;
310 assert( obj->ops == &pipe_client_ops );
314 notify_empty( server );
316 switch(server->state)
318 case ps_connected_server:
319 /* Don't destroy the server's fd here as we can't
320 do a successful flush without it. */
321 server->state = ps_wait_disconnect;
322 release_object( client->fd );
325 case ps_disconnected_server:
326 server->state = ps_wait_connect;
330 case ps_wait_disconnect:
331 case ps_wait_connect:
334 assert( server->client );
335 server->client = NULL;
336 client->server = NULL;
338 assert( !client->fd );
341 static int pipe_data_remaining( struct pipe_server *server )
346 assert( server->client );
348 fd = get_unix_fd( server->client->fd );
355 if (0 > poll( &pfd, 1, 0 ))
358 return pfd.revents&POLLIN;
361 static void check_flushed( void *arg )
363 struct pipe_server *server = (struct pipe_server*) arg;
365 assert( server->event );
366 if (pipe_data_remaining( server ))
370 gettimeofday( &tv, NULL );
371 add_timeout( &tv, 100 );
372 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
376 /* notify_empty( server ); */
377 server->flush_poll = NULL;
378 set_event( server->event );
379 release_object( server->event );
380 server->event = NULL;
384 static int pipe_server_flush( struct fd *fd, struct event **event )
386 struct pipe_server *server = get_fd_user( fd );
391 if (server->state != ps_connected_server)
394 /* FIXME: if multiple threads flush the same pipe,
395 maybe should create a list of processes to notify */
396 if (server->flush_poll)
399 if (pipe_data_remaining( server ))
403 /* this kind of sux -
404 there's no unix way to be alerted when a pipe becomes empty */
405 server->event = create_event( NULL, NULL, 0, 0, 0 );
408 gettimeofday( &tv, NULL );
409 add_timeout( &tv, 100 );
410 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
411 *event = server->event;
417 static int pipe_client_flush( struct fd *fd, struct event **event )
419 /* FIXME: what do we have to do for this? */
423 static inline int is_overlapped( unsigned int options )
425 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
428 static int pipe_server_get_info( struct fd *fd )
430 struct pipe_server *server = get_fd_user( fd );
431 int flags = FD_FLAG_AVAILABLE;
433 if (is_overlapped( server->options )) flags |= FD_FLAG_OVERLAPPED;
438 static int pipe_client_get_info( struct fd *fd )
440 struct pipe_client *client = get_fd_user( fd );
441 int flags = FD_FLAG_AVAILABLE;
443 if (is_overlapped( client->flags )) flags |= FD_FLAG_OVERLAPPED;
448 static struct named_pipe *create_named_pipe( const struct unicode_str *name, unsigned int attr )
450 struct named_pipe *pipe;
452 pipe = create_named_object( sync_namespace, &named_pipe_ops, name, attr | OBJ_OPENIF );
455 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
457 /* initialize it if it didn't already exist */
459 list_init( &pipe->servers );
460 list_init( &pipe->waiters );
466 static struct named_pipe *open_named_pipe( const struct unicode_str *name, unsigned int attr )
470 if ((obj = find_object( sync_namespace, name, attr )))
472 if (obj->ops == &named_pipe_ops) return (struct named_pipe *)obj;
473 release_object( obj );
474 set_error( STATUS_OBJECT_TYPE_MISMATCH );
476 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
481 static struct pipe_server *get_pipe_server_obj( struct process *process,
482 obj_handle_t handle, unsigned int access )
485 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
486 return (struct pipe_server *) obj;
489 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options )
491 struct pipe_server *server;
493 server = alloc_object( &pipe_server_ops );
499 server->state = ps_idle_server;
500 server->client = NULL;
501 server->flush_poll = NULL;
502 server->options = options;
503 list_init( &server->wait_q );
505 list_add_head( &pipe->servers, &server->entry );
511 static struct pipe_client *create_pipe_client( struct pipe_server *server, unsigned int flags )
513 struct pipe_client *client;
515 client = alloc_object( &pipe_client_ops );
520 client->server = server;
521 client->flags = flags;
526 static inline struct pipe_server *find_server( struct named_pipe *pipe, enum pipe_state state )
528 struct pipe_server *server;
530 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
532 if (server->state == state) return (struct pipe_server *)grab_object( server );
537 static inline struct pipe_server *find_server2( struct named_pipe *pipe,
538 enum pipe_state state1, enum pipe_state state2 )
540 struct pipe_server *server;
542 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
544 if (server->state == state1 || server->state == state2)
545 return (struct pipe_server *)grab_object( server );
550 DECL_HANDLER(create_named_pipe)
552 struct named_pipe *pipe;
553 struct pipe_server *server;
554 struct unicode_str name;
557 get_req_unicode_str( &name );
558 if (!(pipe = create_named_pipe( &name, req->attributes ))) return;
560 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
562 pipe->insize = req->insize;
563 pipe->outsize = req->outsize;
564 pipe->maxinstances = req->maxinstances;
565 pipe->timeout = req->timeout;
566 pipe->flags = req->flags;
570 if (pipe->maxinstances <= pipe->instances)
572 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
573 release_object( pipe );
576 if ((pipe->maxinstances != req->maxinstances) ||
577 (pipe->timeout != req->timeout) ||
578 (pipe->flags != req->flags))
580 set_error( STATUS_ACCESS_DENIED );
581 release_object( pipe );
584 set_error( 0 ); /* clear the name collision */
587 server = create_pipe_server( pipe, req->options );
590 reply->handle = alloc_handle( current->process, server,
591 req->access, req->attributes & OBJ_INHERIT );
592 server->pipe->instances++;
593 release_object( server );
596 release_object( pipe );
599 DECL_HANDLER(open_named_pipe)
601 struct pipe_server *server;
602 struct pipe_client *client;
603 struct unicode_str name;
604 struct named_pipe *pipe;
607 get_req_unicode_str( &name );
608 if (!(pipe = open_named_pipe( &name, req->attributes ))) return;
610 server = find_server2( pipe, ps_idle_server, ps_wait_open );
611 release_object( pipe );
615 set_error( STATUS_PIPE_NOT_AVAILABLE );
619 client = create_pipe_client( server, req->flags );
622 if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
626 assert( !client->fd );
627 assert( !server->fd );
629 /* for performance reasons, only set nonblocking mode when using
630 * overlapped I/O. Otherwise, we will be doing too much busy
632 if (is_overlapped( req->flags ))
633 res = fcntl( fds[1], F_SETFL, O_NONBLOCK );
634 if ((res != -1) && is_overlapped( server->options ))
635 res = fcntl( fds[0], F_SETFL, O_NONBLOCK );
637 client->fd = create_anonymous_fd( &pipe_client_fd_ops,
638 fds[1], &client->obj );
639 server->fd = create_anonymous_fd( &pipe_server_fd_ops,
640 fds[0], &server->obj );
641 if (client->fd && server->fd && res != 1)
643 if (server->state == ps_wait_open)
644 async_terminate_head( &server->wait_q, STATUS_SUCCESS );
645 assert( list_empty( &server->wait_q ) );
646 server->state = ps_connected_server;
647 server->client = client;
648 client->server = server;
649 reply->handle = alloc_handle( current->process, client,
650 req->access, req->attributes & OBJ_INHERIT );
656 release_object( client );
658 release_object( server );
661 DECL_HANDLER(connect_named_pipe)
663 struct pipe_server *server;
665 server = get_pipe_server_obj(current->process, req->handle, 0);
669 switch(server->state)
672 case ps_wait_connect:
673 assert( !server->fd );
674 server->state = ps_wait_open;
675 create_async( current, NULL, &server->wait_q,
676 req->func, req->event, NULL );
677 async_terminate_queue( &server->pipe->waiters, STATUS_SUCCESS );
679 case ps_connected_server:
680 assert( server->fd );
681 set_error( STATUS_PIPE_CONNECTED );
683 case ps_disconnected_server:
684 set_error( STATUS_PIPE_BUSY );
686 case ps_wait_disconnect:
687 set_error( STATUS_NO_DATA_DETECTED );
690 set_error( STATUS_INVALID_HANDLE );
694 release_object(server);
697 DECL_HANDLER(wait_named_pipe)
699 struct named_pipe *pipe;
700 struct pipe_server *server;
701 struct unicode_str name;
703 get_req_unicode_str( &name );
704 if (!(pipe = open_named_pipe( &name, OBJ_CASE_INSENSITIVE )))
706 set_error( STATUS_PIPE_NOT_AVAILABLE );
709 server = find_server( pipe, ps_wait_open );
712 /* there's already a server waiting for a client to connect */
713 thread_queue_apc( current, NULL, req->func, APC_ASYNC_IO,
714 1, req->overlapped, NULL, (void *)STATUS_SUCCESS );
715 release_object( server );
720 if (req->timeout == NMPWAIT_USE_DEFAULT_WAIT)
721 timeout = pipe->timeout;
723 timeout = req->timeout;
725 if (req->timeout == NMPWAIT_WAIT_FOREVER)
726 create_async( current, NULL, &pipe->waiters,
727 req->func, req->overlapped, NULL );
729 create_async( current, &timeout, &pipe->waiters,
730 req->func, req->overlapped, NULL );
733 release_object( pipe );
736 DECL_HANDLER(disconnect_named_pipe)
738 struct pipe_server *server;
741 server = get_pipe_server_obj( current->process, req->handle, 0 );
744 switch(server->state)
746 case ps_connected_server:
747 assert( server->fd );
748 assert( server->client );
749 assert( server->client->fd );
751 notify_empty( server );
753 /* Dump the client and server fds, but keep the pointers
754 around - client loses all waiting data */
755 server->state = ps_disconnected_server;
756 do_disconnect( server );
757 reply->fd = flush_cached_fd( current->process, req->handle );
760 case ps_wait_disconnect:
761 assert( !server->client );
762 assert( server->fd );
763 do_disconnect( server );
764 server->state = ps_wait_connect;
765 reply->fd = flush_cached_fd( current->process, req->handle );
770 case ps_disconnected_server:
771 case ps_wait_connect:
772 set_error( STATUS_PIPE_DISCONNECTED );
775 release_object( server );
778 DECL_HANDLER(get_named_pipe_info)
780 struct pipe_server *server;
782 server = get_pipe_server_obj( current->process, req->handle, 0 );
786 reply->flags = server->pipe->flags;
787 reply->maxinstances = server->pipe->maxinstances;
788 reply->insize = server->pipe->insize;
789 reply->outsize = server->pipe->outsize;
791 release_object(server);