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
,
67 struct object obj
; /* object header */
68 struct fd
*fd
; /* pipe file descriptor */
69 struct list entry
; /* entry in named pipe servers list */
70 enum pipe_state state
; /* server state */
71 struct pipe_client
*client
; /* client that this server is connected to */
72 struct named_pipe
*pipe
;
73 struct timeout_user
*flush_poll
;
75 struct list wait_q
; /* only a single one can be queued */
76 unsigned int options
; /* pipe options */
81 struct object obj
; /* object header */
82 struct fd
*fd
; /* pipe file descriptor */
83 struct pipe_server
*server
; /* server that this client is connected to */
84 unsigned int flags
; /* file flags */
89 struct object obj
; /* object header */
91 unsigned int maxinstances
;
95 unsigned int instances
;
96 struct list servers
; /* list of servers using this pipe */
97 struct list waiters
; /* list of clients waiting to connect */
100 static void named_pipe_dump( struct object
*obj
, int verbose
);
101 static void named_pipe_destroy( struct object
*obj
);
103 static const struct object_ops named_pipe_ops
=
105 sizeof(struct named_pipe
), /* size */
106 named_pipe_dump
, /* dump */
107 no_add_queue
, /* add_queue */
108 NULL
, /* remove_queue */
110 NULL
, /* satisfied */
111 no_signal
, /* signal */
112 no_get_fd
, /* get_fd */
113 no_close_handle
, /* close_handle */
114 named_pipe_destroy
/* destroy */
117 /* server end functions */
118 static void pipe_server_dump( struct object
*obj
, int verbose
);
119 static struct fd
*pipe_server_get_fd( struct object
*obj
);
120 static void pipe_server_destroy( struct object
*obj
);
121 static int pipe_server_flush( struct fd
*fd
, struct event
**event
);
122 static int pipe_server_get_info( struct fd
*fd
);
124 static const struct object_ops pipe_server_ops
=
126 sizeof(struct pipe_server
), /* size */
127 pipe_server_dump
, /* dump */
128 default_fd_add_queue
, /* add_queue */
129 default_fd_remove_queue
, /* remove_queue */
130 default_fd_signaled
, /* signaled */
131 no_satisfied
, /* satisfied */
132 no_signal
, /* signal */
133 pipe_server_get_fd
, /* get_fd */
134 no_close_handle
, /* close_handle */
135 pipe_server_destroy
/* destroy */
138 static const struct fd_ops pipe_server_fd_ops
=
140 default_fd_get_poll_events
, /* get_poll_events */
141 default_poll_event
, /* poll_event */
142 pipe_server_flush
, /* flush */
143 pipe_server_get_info
, /* get_file_info */
144 default_fd_queue_async
, /* queue_async */
145 default_fd_cancel_async
, /* cancel_async */
148 /* client end functions */
149 static void pipe_client_dump( struct object
*obj
, int verbose
);
150 static struct fd
*pipe_client_get_fd( struct object
*obj
);
151 static void pipe_client_destroy( struct object
*obj
);
152 static int pipe_client_flush( struct fd
*fd
, struct event
**event
);
153 static int pipe_client_get_info( struct fd
*fd
);
155 static const struct object_ops pipe_client_ops
=
157 sizeof(struct pipe_client
), /* size */
158 pipe_client_dump
, /* dump */
159 default_fd_add_queue
, /* add_queue */
160 default_fd_remove_queue
, /* remove_queue */
161 default_fd_signaled
, /* signaled */
162 no_satisfied
, /* satisfied */
163 no_signal
, /* signal */
164 pipe_client_get_fd
, /* get_fd */
165 no_close_handle
, /* close_handle */
166 pipe_client_destroy
/* destroy */
169 static const struct fd_ops pipe_client_fd_ops
=
171 default_fd_get_poll_events
, /* get_poll_events */
172 default_poll_event
, /* poll_event */
173 pipe_client_flush
, /* flush */
174 pipe_client_get_info
, /* get_file_info */
175 default_fd_queue_async
, /* queue_async */
176 default_fd_cancel_async
/* cancel_async */
179 static void named_pipe_dump( struct object
*obj
, int verbose
)
181 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
182 assert( obj
->ops
== &named_pipe_ops
);
183 fprintf( stderr
, "Named pipe " );
184 dump_object_name( &pipe
->obj
);
185 fprintf( stderr
, "\n" );
188 static void pipe_server_dump( struct object
*obj
, int verbose
)
190 struct pipe_server
*server
= (struct pipe_server
*) obj
;
191 assert( obj
->ops
== &pipe_server_ops
);
192 fprintf( stderr
, "Named pipe server pipe=%p state=%d\n", server
->pipe
, server
->state
);
195 static void pipe_client_dump( struct object
*obj
, int verbose
)
197 struct pipe_client
*client
= (struct pipe_client
*) obj
;
198 assert( obj
->ops
== &pipe_client_ops
);
199 fprintf( stderr
, "Named pipe client server=%p\n", client
->server
);
202 static void named_pipe_destroy( struct object
*obj
)
204 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
206 assert( list_empty( &pipe
->servers
) );
207 assert( !pipe
->instances
);
208 async_terminate_queue( &pipe
->waiters
, STATUS_HANDLES_CLOSED
);
211 static struct fd
*pipe_client_get_fd( struct object
*obj
)
213 struct pipe_client
*client
= (struct pipe_client
*) obj
;
215 return (struct fd
*) grab_object( client
->fd
);
216 set_error( STATUS_PIPE_DISCONNECTED
);
220 static struct fd
*pipe_server_get_fd( struct object
*obj
)
222 struct pipe_server
*server
= (struct pipe_server
*) obj
;
224 switch(server
->state
)
226 case ps_connected_server
:
227 case ps_wait_disconnect
:
228 assert( server
->fd
);
229 return (struct fd
*) grab_object( server
->fd
);
233 set_error( STATUS_PIPE_LISTENING
);
236 case ps_disconnected_server
:
237 case ps_wait_connect
:
238 set_error( STATUS_PIPE_DISCONNECTED
);
245 static void notify_empty( struct pipe_server
*server
)
247 if (!server
->flush_poll
)
249 assert( server
->state
== ps_connected_server
);
250 assert( server
->event
);
251 remove_timeout_user( server
->flush_poll
);
252 server
->flush_poll
= NULL
;
253 set_event( server
->event
);
254 release_object( server
->event
);
255 server
->event
= NULL
;
258 static void do_disconnect( struct pipe_server
*server
)
260 /* we may only have a server fd, if the client disconnected */
263 assert( server
->client
->server
== server
);
264 assert( server
->client
->fd
);
265 release_object( server
->client
->fd
);
266 server
->client
->fd
= NULL
;
268 assert( server
->fd
);
269 release_object( server
->fd
);
273 static void pipe_server_destroy( struct object
*obj
)
275 struct pipe_server
*server
= (struct pipe_server
*)obj
;
277 assert( obj
->ops
== &pipe_server_ops
);
281 notify_empty( server
);
282 do_disconnect( server
);
287 server
->client
->server
= NULL
;
288 server
->client
= NULL
;
291 async_terminate_head( &server
->wait_q
, STATUS_HANDLES_CLOSED
);
293 assert( server
->pipe
->instances
);
294 server
->pipe
->instances
--;
296 list_remove( &server
->entry
);
297 release_object( server
->pipe
);
300 static void pipe_client_destroy( struct object
*obj
)
302 struct pipe_client
*client
= (struct pipe_client
*)obj
;
303 struct pipe_server
*server
= client
->server
;
305 assert( obj
->ops
== &pipe_client_ops
);
309 notify_empty( server
);
311 switch(server
->state
)
313 case ps_connected_server
:
314 /* Don't destroy the server's fd here as we can't
315 do a successful flush without it. */
316 server
->state
= ps_wait_disconnect
;
317 release_object( client
->fd
);
320 case ps_disconnected_server
:
321 server
->state
= ps_wait_connect
;
325 case ps_wait_disconnect
:
326 case ps_wait_connect
:
329 assert( server
->client
);
330 server
->client
= NULL
;
331 client
->server
= NULL
;
333 assert( !client
->fd
);
336 static int pipe_data_remaining( struct pipe_server
*server
)
341 assert( server
->client
);
343 fd
= get_unix_fd( server
->client
->fd
);
350 if (0 > poll( &pfd
, 1, 0 ))
353 return pfd
.revents
&POLLIN
;
356 static void check_flushed( void *arg
)
358 struct pipe_server
*server
= (struct pipe_server
*) arg
;
360 assert( server
->event
);
361 if (pipe_data_remaining( server
))
365 gettimeofday( &tv
, NULL
);
366 add_timeout( &tv
, 100 );
367 server
->flush_poll
= add_timeout_user( &tv
, check_flushed
, server
);
371 /* notify_empty( server ); */
372 server
->flush_poll
= NULL
;
373 set_event( server
->event
);
374 release_object( server
->event
);
375 server
->event
= NULL
;
379 static int pipe_server_flush( struct fd
*fd
, struct event
**event
)
381 struct pipe_server
*server
= get_fd_user( fd
);
386 if (server
->state
!= ps_connected_server
)
389 /* FIXME: if multiple threads flush the same pipe,
390 maybe should create a list of processes to notify */
391 if (server
->flush_poll
)
394 if (pipe_data_remaining( server
))
398 /* this kind of sux -
399 there's no unix way to be alerted when a pipe becomes empty */
400 server
->event
= create_event( NULL
, 0, 0, 0 );
403 gettimeofday( &tv
, NULL
);
404 add_timeout( &tv
, 100 );
405 server
->flush_poll
= add_timeout_user( &tv
, check_flushed
, server
);
406 *event
= server
->event
;
412 static int pipe_client_flush( struct fd
*fd
, struct event
**event
)
414 /* FIXME: what do we have to do for this? */
418 static inline int is_overlapped( unsigned int options
)
420 return !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
423 static int pipe_server_get_info( struct fd
*fd
)
425 struct pipe_server
*server
= get_fd_user( fd
);
426 int flags
= FD_FLAG_AVAILABLE
;
428 if (is_overlapped( server
->options
)) flags
|= FD_FLAG_OVERLAPPED
;
433 static int pipe_client_get_info( struct fd
*fd
)
435 struct pipe_client
*client
= get_fd_user( fd
);
436 int flags
= FD_FLAG_AVAILABLE
;
438 if (is_overlapped( client
->flags
)) flags
|= FD_FLAG_OVERLAPPED
;
443 static struct named_pipe
*create_named_pipe( const WCHAR
*name
, size_t len
)
445 struct named_pipe
*pipe
;
447 pipe
= create_named_object( sync_namespace
, &named_pipe_ops
, name
, len
);
450 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
452 /* initialize it if it didn't already exist */
454 list_init( &pipe
->servers
);
455 list_init( &pipe
->waiters
);
461 static struct named_pipe
*open_named_pipe( const WCHAR
*name
, size_t len
)
465 if ((obj
= find_object( sync_namespace
, name
, len
)))
467 if (obj
->ops
== &named_pipe_ops
) return (struct named_pipe
*)obj
;
468 release_object( obj
);
469 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
471 else set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
476 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
477 obj_handle_t handle
, unsigned int access
)
480 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
481 return (struct pipe_server
*) obj
;
484 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
, unsigned int options
)
486 struct pipe_server
*server
;
488 server
= alloc_object( &pipe_server_ops
);
494 server
->state
= ps_idle_server
;
495 server
->client
= NULL
;
496 server
->flush_poll
= NULL
;
497 server
->options
= options
;
498 list_init( &server
->wait_q
);
500 list_add_head( &pipe
->servers
, &server
->entry
);
506 static struct pipe_client
*create_pipe_client( struct pipe_server
*server
, unsigned int flags
)
508 struct pipe_client
*client
;
510 client
= alloc_object( &pipe_client_ops
);
515 client
->server
= server
;
516 client
->flags
= flags
;
521 static inline struct pipe_server
*find_server( struct named_pipe
*pipe
, enum pipe_state state
)
523 struct pipe_server
*server
;
525 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
527 if (server
->state
== state
) return (struct pipe_server
*)grab_object( server
);
532 static inline struct pipe_server
*find_server2( struct named_pipe
*pipe
,
533 enum pipe_state state1
, enum pipe_state state2
)
535 struct pipe_server
*server
;
537 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
539 if (server
->state
== state1
|| server
->state
== state2
)
540 return (struct pipe_server
*)grab_object( server
);
545 DECL_HANDLER(create_named_pipe
)
547 struct named_pipe
*pipe
;
548 struct pipe_server
*server
;
551 pipe
= create_named_pipe( get_req_data(), get_req_data_size() );
555 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
557 pipe
->insize
= req
->insize
;
558 pipe
->outsize
= req
->outsize
;
559 pipe
->maxinstances
= req
->maxinstances
;
560 pipe
->timeout
= req
->timeout
;
561 pipe
->flags
= req
->flags
;
565 set_error( 0 ); /* clear the name collision */
566 if (pipe
->maxinstances
<= pipe
->instances
)
568 set_error( STATUS_PIPE_BUSY
);
569 release_object( pipe
);
572 if ((pipe
->maxinstances
!= req
->maxinstances
) ||
573 (pipe
->timeout
!= req
->timeout
) ||
574 (pipe
->flags
!= req
->flags
))
576 set_error( STATUS_ACCESS_DENIED
);
577 release_object( pipe
);
582 server
= create_pipe_server( pipe
, req
->options
);
585 reply
->handle
= alloc_handle( current
->process
, server
,
586 GENERIC_READ
|GENERIC_WRITE
, req
->inherit
);
587 server
->pipe
->instances
++;
588 release_object( server
);
591 release_object( pipe
);
594 DECL_HANDLER(open_named_pipe
)
596 struct pipe_server
*server
;
597 struct pipe_client
*client
;
598 struct named_pipe
*pipe
;
601 pipe
= open_named_pipe( get_req_data(), get_req_data_size() );
604 set_error( STATUS_NO_SUCH_FILE
);
608 server
= find_server2( pipe
, ps_idle_server
, ps_wait_open
);
609 release_object( pipe
);
613 set_error( STATUS_PIPE_NOT_AVAILABLE
);
617 client
= create_pipe_client( server
, req
->flags
);
620 if (!socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
))
624 assert( !client
->fd
);
625 assert( !server
->fd
);
627 /* for performance reasons, only set nonblocking mode when using
628 * overlapped I/O. Otherwise, we will be doing too much busy
630 if (is_overlapped( req
->flags
))
631 res
= fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
632 if ((res
!= -1) && is_overlapped( server
->options
))
633 res
= fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
635 client
->fd
= create_anonymous_fd( &pipe_client_fd_ops
,
636 fds
[1], &client
->obj
);
637 server
->fd
= create_anonymous_fd( &pipe_server_fd_ops
,
638 fds
[0], &server
->obj
);
639 if (client
->fd
&& server
->fd
&& res
!= 1)
641 if (server
->state
== ps_wait_open
)
642 async_terminate_head( &server
->wait_q
, STATUS_SUCCESS
);
643 assert( list_empty( &server
->wait_q
) );
644 server
->state
= ps_connected_server
;
645 server
->client
= client
;
646 client
->server
= server
;
647 reply
->handle
= alloc_handle( current
->process
, client
,
648 req
->access
, req
->inherit
);
654 release_object( client
);
656 release_object( server
);
659 DECL_HANDLER(connect_named_pipe
)
661 struct pipe_server
*server
;
663 server
= get_pipe_server_obj(current
->process
, req
->handle
, 0);
667 switch(server
->state
)
670 case ps_wait_connect
:
671 assert( !server
->fd
);
672 server
->state
= ps_wait_open
;
673 create_async( current
, NULL
, &server
->wait_q
,
674 req
->func
, req
->overlapped
, NULL
);
675 async_terminate_queue( &server
->pipe
->waiters
, STATUS_SUCCESS
);
677 case ps_connected_server
:
678 assert( server
->fd
);
679 set_error( STATUS_PIPE_CONNECTED
);
681 case ps_disconnected_server
:
682 set_error( STATUS_PIPE_BUSY
);
684 case ps_wait_disconnect
:
685 set_error( STATUS_NO_DATA_DETECTED
);
688 set_error( STATUS_INVALID_HANDLE
);
692 release_object(server
);
695 DECL_HANDLER(wait_named_pipe
)
697 struct named_pipe
*pipe
;
698 struct pipe_server
*server
;
700 if (!(pipe
= open_named_pipe( get_req_data(), get_req_data_size() )))
702 set_error( STATUS_PIPE_NOT_AVAILABLE
);
705 server
= find_server( pipe
, ps_wait_open
);
708 /* there's already a server waiting for a client to connect */
709 thread_queue_apc( current
, NULL
, req
->func
, APC_ASYNC_IO
,
710 1, req
->overlapped
, NULL
, (void *)STATUS_SUCCESS
);
711 release_object( server
);
716 if (req
->timeout
== NMPWAIT_USE_DEFAULT_WAIT
)
717 timeout
= pipe
->timeout
;
719 timeout
= req
->timeout
;
721 if (req
->timeout
== NMPWAIT_WAIT_FOREVER
)
722 create_async( current
, NULL
, &pipe
->waiters
,
723 req
->func
, req
->overlapped
, NULL
);
725 create_async( current
, &timeout
, &pipe
->waiters
,
726 req
->func
, req
->overlapped
, NULL
);
729 release_object( pipe
);
732 DECL_HANDLER(disconnect_named_pipe
)
734 struct pipe_server
*server
;
737 server
= get_pipe_server_obj( current
->process
, req
->handle
, 0 );
740 switch(server
->state
)
742 case ps_connected_server
:
743 assert( server
->fd
);
744 assert( server
->client
);
745 assert( server
->client
->fd
);
747 notify_empty( server
);
749 /* Dump the client and server fds, but keep the pointers
750 around - client loses all waiting data */
751 server
->state
= ps_disconnected_server
;
752 do_disconnect( server
);
753 reply
->fd
= flush_cached_fd( current
->process
, req
->handle
);
756 case ps_wait_disconnect
:
757 assert( !server
->client
);
758 assert( server
->fd
);
759 do_disconnect( server
);
760 server
->state
= ps_wait_connect
;
761 reply
->fd
= flush_cached_fd( current
->process
, req
->handle
);
766 case ps_disconnected_server
:
767 case ps_wait_connect
:
768 set_error( STATUS_PIPE_DISCONNECTED
);
771 release_object( server
);
774 DECL_HANDLER(get_named_pipe_info
)
776 struct pipe_server
*server
;
778 server
= get_pipe_server_obj( current
->process
, req
->handle
, 0 );
782 reply
->flags
= server
->pipe
->flags
;
783 reply
->maxinstances
= server
->pipe
->maxinstances
;
784 reply
->insize
= server
->pipe
->insize
;
785 reply
->outsize
= server
->pipe
->outsize
;
787 release_object(server
);