Fix value renaming. Cleanup code, fix a few leaks.
[wine/multimedia.git] / server / named_pipe.c
blobb69eec68986a696dd41383335b9d2a5144a34785
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>
42 #include "windef.h"
43 #include "winbase.h"
45 #include "file.h"
46 #include "handle.h"
47 #include "thread.h"
48 #include "request.h"
50 enum pipe_state
52 ps_none,
53 ps_idle_server,
54 ps_wait_open,
55 ps_connected_server,
56 ps_wait_disconnect,
57 ps_disconnected_server,
58 ps_wait_connect
61 struct wait_info
63 struct thread *thread;
64 void *func;
65 void *overlapped;
68 struct named_pipe;
70 struct pipe_server
72 struct object obj;
73 struct fd *fd;
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;
80 struct event *event;
81 struct wait_info wait;
84 struct pipe_client
86 struct object obj;
87 struct fd *fd;
88 struct pipe_server *server;
89 struct wait_info wait;
92 struct connect_wait
94 struct wait_info wait;
95 struct connect_wait *next;
98 struct named_pipe
100 struct object obj; /* object header */
101 unsigned int pipemode;
102 unsigned int maxinstances;
103 unsigned int outsize;
104 unsigned int insize;
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 */
120 NULL, /* signaled */
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 );
224 wait->thread = NULL;
227 static void set_waiter( struct wait_info *wait, void *func, void *ov )
229 wait->thread = (struct thread *) grab_object( current );
230 wait->func = func;
231 wait->overlapped = ov;
234 static void notify_connect_waiters( struct named_pipe *pipe )
236 struct connect_wait *cw, **x = &pipe->connect_waiters;
238 while( *x )
240 cw = *x;
241 notify_waiter( &cw->wait, STATUS_SUCCESS );
242 release_object( pipe );
243 *x = cw->next;
244 free( cw );
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) );
254 if( waiter )
256 set_waiter( &waiter->wait, func, overlapped );
257 waiter->next = pipe->connect_waiters;
258 pipe->connect_waiters = waiter;
259 grab_object( pipe );
263 static struct fd *pipe_client_get_fd( struct object *obj )
265 struct pipe_client *client = (struct pipe_client *) obj;
266 if( client->fd )
267 return (struct fd *) grab_object( client->fd );
268 set_error( STATUS_PIPE_DISCONNECTED );
269 return NULL;
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 );
283 case ps_wait_open:
284 case ps_idle_server:
285 set_error( STATUS_PIPE_LISTENING );
286 break;
288 case ps_disconnected_server:
289 case ps_wait_connect:
290 set_error( STATUS_PIPE_DISCONNECTED );
291 break;
293 default:
294 assert( 0 );
296 return NULL;
300 static void notify_empty( struct pipe_server *server )
302 if( !server->flush_poll )
303 return;
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 */
316 if( server->client )
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 );
325 server->fd = NULL;
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 );
334 if( server->fd )
336 notify_empty( server );
337 do_disconnect( server );
340 if( server->client )
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 );
367 if( server )
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 );
378 client->fd = NULL;
379 break;
380 case ps_disconnected_server:
381 server->state = ps_wait_connect;
382 break;
383 default:
384 assert( 0 );
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 )
400 struct pollfd pfd;
401 int fd;
403 assert( server->client );
405 fd = get_unix_fd( server->client->fd );
406 if( fd < 0 )
407 return 0;
408 pfd.fd = fd;
409 pfd.events = POLLIN;
410 pfd.revents = 0;
412 if( 0 > poll( &pfd, 1, 0 ) )
413 return 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 ) )
425 struct timeval tv;
427 gettimeofday( &tv, 0 );
428 add_timeout( &tv, 100 );
429 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
431 else
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 );
439 if( !server )
440 return 0;
442 if( server->state != ps_connected_server )
443 return 0;
445 /* FIXME: if multiple threads flush the same pipe,
446 maybe should create a list of processes to notify */
447 if( server->flush_poll )
448 return 0;
450 if( pipe_data_remaining( server ) )
452 struct timeval tv;
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 );
457 if( !server->event )
458 return 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;
465 return 0;
468 static int pipe_client_flush( struct fd *fd, struct event **event )
470 /* FIXME: what do we have to do for this? */
471 return 0;
474 static int pipe_end_get_info( struct fd *fd,
475 struct get_file_info_reply *reply, int *flags )
477 if (reply)
479 reply->type = FILE_TYPE_PIPE;
480 reply->attr = 0;
481 reply->access_time = 0;
482 reply->write_time = 0;
483 reply->size_high = 0;
484 reply->size_low = 0;
485 reply->links = 0;
486 reply->index_high = 0;
487 reply->index_low = 0;
488 reply->serial = 0;
490 *flags = 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 );
499 if( pipe )
501 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
503 /* initialize it if it didn't already exist */
504 pipe->servers = 0;
505 pipe->instances = 0;
506 pipe->connect_waiters = NULL;
509 return pipe;
512 static struct named_pipe *open_named_pipe( const WCHAR *name, size_t len )
514 struct object *obj;
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 );
524 return NULL;
527 static struct pipe_server *get_pipe_server_obj( struct process *process,
528 obj_handle_t handle, unsigned int access )
530 struct object *obj;
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 );
540 if( !server )
541 return NULL;
543 server->fd = NULL;
544 server->pipe = pipe;
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;
552 server->prev = NULL;
553 pipe->servers = server;
555 grab_object( pipe );
557 return 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 );
565 if( !client )
566 return NULL;
568 client->fd = NULL;
569 client->server = server;
570 client->wait.thread = NULL;
572 return client;
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 )
582 break;
584 if( !x )
585 return NULL;
587 return (struct pipe_server *) grab_object( x );
590 DECL_HANDLER(create_named_pipe)
592 struct named_pipe *pipe;
593 struct pipe_server *server;
595 reply->handle = 0;
596 pipe = create_named_pipe( get_req_data(), get_req_data_size() );
597 if( !pipe )
598 return;
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;
608 else
610 set_error( 0 ); /* clear the name collision */
611 if( pipe->maxinstances <= pipe->instances )
613 set_error( STATUS_PIPE_BUSY );
614 release_object( pipe );
615 return;
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 );
623 return;
627 server = create_pipe_server( pipe );
628 if(server)
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;
645 int fds[2];
647 reply->handle = 0;
649 pipe = open_named_pipe( get_req_data(), get_req_data_size() );
650 if ( !pipe )
652 set_error( STATUS_NO_SUCH_FILE );
653 return;
656 for( server = pipe->servers; server; server = server->next )
657 if( ( server->state==ps_idle_server ) ||
658 ( server->state==ps_wait_open ) )
659 break;
660 release_object( pipe );
662 if ( !server )
664 set_error( STATUS_PIPE_NOT_AVAILABLE );
665 return;
668 client = create_pipe_client( server );
669 if( client )
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 );
691 else
692 file_set_error();
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);
703 if(!server)
704 return;
706 switch( server->state )
708 case ps_idle_server:
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 );
714 break;
715 case ps_connected_server:
716 assert( server->fd );
717 set_error( STATUS_PIPE_CONNECTED );
718 break;
719 case ps_disconnected_server:
720 set_error( STATUS_PIPE_BUSY );
721 break;
722 case ps_wait_disconnect:
723 set_error( STATUS_NO_DATA_DETECTED );
724 break;
725 default:
726 set_error( STATUS_INVALID_HANDLE );
727 break;
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 );
741 return;
743 server = find_server( pipe, ps_wait_open );
744 if( server )
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 );
752 else
753 queue_connect_waiter( pipe, req->func, req->overlapped );
755 release_object( pipe );
758 DECL_HANDLER(disconnect_named_pipe)
760 struct pipe_server *server;
762 reply->fd = -1;
763 server = get_pipe_server_obj( current->process, req->handle, 0 );
764 if( !server )
765 return;
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 );
781 break;
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 );
789 break;
791 default:
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 );
802 if(!server)
803 return;
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);