If the unixfs is rooted at the Desktop folder, forward
[wine/multimedia.git] / server / named_pipe.c
blobcd9d0d6b0f02ec07f578c4ce6f154e507ea1d1e1
1 /*
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
21 * TODO:
22 * message mode
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
38 #endif
39 #include <time.h>
40 #include <unistd.h>
41 #ifdef HAVE_POLL_H
42 #include <poll.h>
43 #endif
45 #include "windef.h"
46 #include "winternl.h"
48 #include "file.h"
49 #include "handle.h"
50 #include "thread.h"
51 #include "request.h"
53 enum pipe_state
55 ps_idle_server,
56 ps_wait_open,
57 ps_connected_server,
58 ps_wait_disconnect,
59 ps_disconnected_server,
60 ps_wait_connect
63 struct named_pipe;
65 struct pipe_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;
74 struct event *event;
75 struct list wait_q; /* only a single one can be queued */
76 unsigned int options; /* pipe options */
79 struct pipe_client
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 */
87 struct named_pipe
89 struct object obj; /* object header */
90 unsigned int flags;
91 unsigned int maxinstances;
92 unsigned int outsize;
93 unsigned int insize;
94 unsigned int timeout;
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 */
109 NULL, /* signaled */
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;
214 if (client->fd)
215 return (struct fd *) grab_object( client->fd );
216 set_error( STATUS_PIPE_DISCONNECTED );
217 return NULL;
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 );
231 case ps_wait_open:
232 case ps_idle_server:
233 set_error( STATUS_PIPE_LISTENING );
234 break;
236 case ps_disconnected_server:
237 case ps_wait_connect:
238 set_error( STATUS_PIPE_DISCONNECTED );
239 break;
241 return NULL;
245 static void notify_empty( struct pipe_server *server )
247 if (!server->flush_poll)
248 return;
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 */
261 if (server->client)
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 );
270 server->fd = NULL;
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 );
279 if (server->fd)
281 notify_empty( server );
282 do_disconnect( server );
285 if (server->client)
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 );
307 if (server)
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 );
318 client->fd = NULL;
319 break;
320 case ps_disconnected_server:
321 server->state = ps_wait_connect;
322 break;
323 case ps_idle_server:
324 case ps_wait_open:
325 case ps_wait_disconnect:
326 case ps_wait_connect:
327 assert( 0 );
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 )
338 struct pollfd pfd;
339 int fd;
341 assert( server->client );
343 fd = get_unix_fd( server->client->fd );
344 if (fd < 0)
345 return 0;
346 pfd.fd = fd;
347 pfd.events = POLLIN;
348 pfd.revents = 0;
350 if (0 > poll( &pfd, 1, 0 ))
351 return 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 ))
363 struct timeval tv;
365 gettimeofday( &tv, NULL );
366 add_timeout( &tv, 100 );
367 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
369 else
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 );
383 if (!server)
384 return 0;
386 if (server->state != ps_connected_server)
387 return 0;
389 /* FIXME: if multiple threads flush the same pipe,
390 maybe should create a list of processes to notify */
391 if (server->flush_poll)
392 return 0;
394 if (pipe_data_remaining( server ))
396 struct timeval tv;
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 );
401 if (!server->event)
402 return 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;
409 return 0;
412 static int pipe_client_flush( struct fd *fd, struct event **event )
414 /* FIXME: what do we have to do for this? */
415 return 0;
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;
430 return flags;
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;
440 return flags;
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 );
448 if (pipe)
450 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
452 /* initialize it if it didn't already exist */
453 pipe->instances = 0;
454 list_init( &pipe->servers );
455 list_init( &pipe->waiters );
458 return pipe;
461 static struct named_pipe *open_named_pipe( const WCHAR *name, size_t len )
463 struct object *obj;
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 );
473 return NULL;
476 static struct pipe_server *get_pipe_server_obj( struct process *process,
477 obj_handle_t handle, unsigned int access )
479 struct object *obj;
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 );
489 if (!server)
490 return NULL;
492 server->fd = NULL;
493 server->pipe = pipe;
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 );
501 grab_object( pipe );
503 return server;
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 );
511 if (!client)
512 return NULL;
514 client->fd = NULL;
515 client->server = server;
516 client->flags = flags;
518 return client;
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 );
529 return NULL;
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 );
542 return NULL;
545 DECL_HANDLER(create_named_pipe)
547 struct named_pipe *pipe;
548 struct pipe_server *server;
550 reply->handle = 0;
551 pipe = create_named_pipe( get_req_data(), get_req_data_size() );
552 if (!pipe)
553 return;
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;
563 else
565 set_error( 0 ); /* clear the name collision */
566 if (pipe->maxinstances <= pipe->instances)
568 set_error( STATUS_PIPE_BUSY );
569 release_object( pipe );
570 return;
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 );
578 return;
582 server = create_pipe_server( pipe, req->options );
583 if (server)
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;
599 int fds[2];
601 pipe = open_named_pipe( get_req_data(), get_req_data_size() );
602 if (!pipe)
604 set_error( STATUS_NO_SUCH_FILE );
605 return;
608 server = find_server2( pipe, ps_idle_server, ps_wait_open );
609 release_object( pipe );
611 if (!server)
613 set_error( STATUS_PIPE_NOT_AVAILABLE );
614 return;
617 client = create_pipe_client( server, req->flags );
618 if (client)
620 if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
622 int res = 0;
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
629 * looping */
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 );
651 else
652 file_set_error();
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);
664 if (!server)
665 return;
667 switch(server->state)
669 case ps_idle_server:
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 );
676 break;
677 case ps_connected_server:
678 assert( server->fd );
679 set_error( STATUS_PIPE_CONNECTED );
680 break;
681 case ps_disconnected_server:
682 set_error( STATUS_PIPE_BUSY );
683 break;
684 case ps_wait_disconnect:
685 set_error( STATUS_NO_DATA_DETECTED );
686 break;
687 case ps_wait_open:
688 set_error( STATUS_INVALID_HANDLE );
689 break;
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 );
703 return;
705 server = find_server( pipe, ps_wait_open );
706 if (server)
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 );
713 else
715 unsigned int timeout;
716 if (req->timeout == NMPWAIT_USE_DEFAULT_WAIT)
717 timeout = pipe->timeout;
718 else
719 timeout = req->timeout;
721 if (req->timeout == NMPWAIT_WAIT_FOREVER)
722 create_async( current, NULL, &pipe->waiters,
723 req->func, req->overlapped, NULL );
724 else
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;
736 reply->fd = -1;
737 server = get_pipe_server_obj( current->process, req->handle, 0 );
738 if (!server)
739 return;
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 );
754 break;
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 );
762 break;
764 case ps_idle_server:
765 case ps_wait_open:
766 case ps_disconnected_server:
767 case ps_wait_connect:
768 set_error( STATUS_PIPE_DISCONNECTED );
769 break;
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 );
779 if (!server)
780 return;
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);