No need continuing tests if no NetUserGetInfo.
[wine/multimedia.git] / server / named_pipe.c
blobee1fceb468e1dd1db84b9ded01622e65b90d94ac
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 "winbase.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 wait_info
65 struct thread *thread;
66 void *func;
67 void *overlapped;
70 struct named_pipe;
72 struct pipe_server
74 struct object obj; /* object header */
75 struct fd *fd; /* pipe file descriptor */
76 struct list entry; /* entry in named pipe servers list */
77 enum pipe_state state; /* server state */
78 struct pipe_client *client; /* client that this server is connected to */
79 struct named_pipe *pipe;
80 struct timeout_user *flush_poll;
81 struct event *event;
82 struct wait_info wait;
85 struct pipe_client
87 struct object obj; /* object header */
88 struct fd *fd; /* pipe file descriptor */
89 struct pipe_server *server; /* server that this client is connected to */
90 struct wait_info wait;
93 struct connect_wait
95 struct list entry; /* entry in named pipe wait list */
96 struct wait_info wait;
97 struct timeout_user *timeout_user;
100 struct named_pipe
102 struct object obj; /* object header */
103 unsigned int pipemode;
104 unsigned int maxinstances;
105 unsigned int outsize;
106 unsigned int insize;
107 unsigned int timeout;
108 unsigned int instances;
109 struct list servers; /* list of servers using this pipe */
110 struct list waiters; /* list of clients waiting to connect */
113 static void named_pipe_dump( struct object *obj, int verbose );
114 static void named_pipe_destroy( struct object *obj );
116 static const struct object_ops named_pipe_ops =
118 sizeof(struct named_pipe), /* size */
119 named_pipe_dump, /* dump */
120 no_add_queue, /* add_queue */
121 NULL, /* remove_queue */
122 NULL, /* signaled */
123 NULL, /* satisfied */
124 no_get_fd, /* get_fd */
125 named_pipe_destroy /* destroy */
128 /* common to clients and servers */
129 static int pipe_end_get_poll_events( struct fd *fd );
130 static int pipe_end_get_info( struct fd *fd );
132 /* server end functions */
133 static void pipe_server_dump( struct object *obj, int verbose );
134 static struct fd *pipe_server_get_fd( struct object *obj );
135 static void pipe_server_destroy( struct object *obj);
136 static int pipe_server_flush( struct fd *fd, struct event **event );
138 static const struct object_ops pipe_server_ops =
140 sizeof(struct pipe_server), /* size */
141 pipe_server_dump, /* dump */
142 default_fd_add_queue, /* add_queue */
143 default_fd_remove_queue, /* remove_queue */
144 default_fd_signaled, /* signaled */
145 no_satisfied, /* satisfied */
146 pipe_server_get_fd, /* get_fd */
147 pipe_server_destroy /* destroy */
150 static const struct fd_ops pipe_server_fd_ops =
152 pipe_end_get_poll_events, /* get_poll_events */
153 default_poll_event, /* poll_event */
154 pipe_server_flush, /* flush */
155 pipe_end_get_info, /* get_file_info */
156 no_queue_async, /* queue_async */
157 no_cancel_async, /* cancel_async */
160 /* client end functions */
161 static void pipe_client_dump( struct object *obj, int verbose );
162 static struct fd *pipe_client_get_fd( struct object *obj );
163 static void pipe_client_destroy( struct object *obj );
164 static int pipe_client_flush( struct fd *fd, struct event **event );
166 static const struct object_ops pipe_client_ops =
168 sizeof(struct pipe_client), /* size */
169 pipe_client_dump, /* dump */
170 default_fd_add_queue, /* add_queue */
171 default_fd_remove_queue, /* remove_queue */
172 default_fd_signaled, /* signaled */
173 no_satisfied, /* satisfied */
174 pipe_client_get_fd, /* get_fd */
175 pipe_client_destroy /* destroy */
178 static const struct fd_ops pipe_client_fd_ops =
180 pipe_end_get_poll_events, /* get_poll_events */
181 default_poll_event, /* poll_event */
182 pipe_client_flush, /* flush */
183 pipe_end_get_info, /* get_file_info */
184 no_queue_async, /* queue_async */
185 no_cancel_async /* cancel_async */
188 static void named_pipe_dump( struct object *obj, int verbose )
190 struct named_pipe *pipe = (struct named_pipe *) obj;
191 assert( obj->ops == &named_pipe_ops );
192 fprintf( stderr, "Named pipe " );
193 dump_object_name( &pipe->obj );
194 fprintf( stderr, "\n" );
197 static void pipe_server_dump( struct object *obj, int verbose )
199 struct pipe_server *server = (struct pipe_server *) obj;
200 assert( obj->ops == &pipe_server_ops );
201 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
204 static void pipe_client_dump( struct object *obj, int verbose )
206 struct pipe_client *client = (struct pipe_client *) obj;
207 assert( obj->ops == &pipe_client_ops );
208 fprintf( stderr, "Named pipe client server=%p\n", client->server );
211 static void notify_waiter( struct wait_info *wait, unsigned int status )
213 if( wait->thread && wait->func && wait->overlapped )
215 /* queue a system APC, to notify a waiting thread */
216 thread_queue_apc( wait->thread, NULL, wait->func, APC_ASYNC,
217 1, wait->overlapped, (void *)status, NULL );
219 if( wait->thread ) release_object( wait->thread );
220 wait->thread = NULL;
223 static void set_waiter( struct wait_info *wait, void *func, void *ov )
225 wait->thread = (struct thread *) grab_object( current );
226 wait->func = func;
227 wait->overlapped = ov;
230 static void notify_connect_waiter( struct connect_wait *waiter, unsigned int status )
232 notify_waiter( &waiter->wait, status );
233 list_remove( &waiter->entry );
234 free( waiter );
237 static void notify_all_connect_waiters( struct named_pipe *pipe, unsigned int status )
239 struct list *ptr;
241 while ((ptr = list_head( &pipe->waiters )) != NULL)
243 struct connect_wait *waiter = LIST_ENTRY( ptr, struct connect_wait, entry );
244 if (waiter->timeout_user) remove_timeout_user( waiter->timeout_user );
245 notify_connect_waiter( waiter, status );
249 /* pipe connect wait timeout */
250 static void connect_timeout( void *ptr )
252 struct connect_wait *waiter = (struct connect_wait *)ptr;
253 notify_connect_waiter( waiter, STATUS_TIMEOUT );
256 static void named_pipe_destroy( struct object *obj)
258 struct named_pipe *pipe = (struct named_pipe *) obj;
260 assert( list_empty( &pipe->servers ) );
261 assert( !pipe->instances );
262 notify_all_connect_waiters( pipe, STATUS_HANDLES_CLOSED );
265 static void queue_connect_waiter( struct named_pipe *pipe, void *func,
266 void *overlapped, unsigned int *timeout )
268 struct connect_wait *waiter;
270 waiter = mem_alloc( sizeof(*waiter) );
271 if( waiter )
273 struct timeval tv;
275 set_waiter( &waiter->wait, func, overlapped );
276 list_add_tail( &pipe->waiters, &waiter->entry );
278 if (timeout)
280 gettimeofday( &tv, 0 );
281 add_timeout( &tv, *timeout );
282 waiter->timeout_user = add_timeout_user( &tv, connect_timeout,
283 waiter );
285 else
286 waiter->timeout_user = NULL;
290 static struct fd *pipe_client_get_fd( struct object *obj )
292 struct pipe_client *client = (struct pipe_client *) obj;
293 if( client->fd )
294 return (struct fd *) grab_object( client->fd );
295 set_error( STATUS_PIPE_DISCONNECTED );
296 return NULL;
299 static struct fd *pipe_server_get_fd( struct object *obj )
301 struct pipe_server *server = (struct pipe_server *) obj;
303 switch(server->state)
305 case ps_connected_server:
306 case ps_wait_disconnect:
307 assert( server->fd );
308 return (struct fd *) grab_object( server->fd );
310 case ps_wait_open:
311 case ps_idle_server:
312 set_error( STATUS_PIPE_LISTENING );
313 break;
315 case ps_disconnected_server:
316 case ps_wait_connect:
317 set_error( STATUS_PIPE_DISCONNECTED );
318 break;
320 return NULL;
324 static void notify_empty( struct pipe_server *server )
326 if( !server->flush_poll )
327 return;
328 assert( server->state == ps_connected_server );
329 assert( server->event );
330 remove_timeout_user( server->flush_poll );
331 server->flush_poll = NULL;
332 set_event( server->event );
333 release_object( server->event );
334 server->event = NULL;
337 static void do_disconnect( struct pipe_server *server )
339 /* we may only have a server fd, if the client disconnected */
340 if( server->client )
342 assert( server->client->server == server );
343 assert( server->client->fd );
344 release_object( server->client->fd );
345 server->client->fd = NULL;
347 assert( server->fd );
348 release_object( server->fd );
349 server->fd = NULL;
352 static void pipe_server_destroy( struct object *obj)
354 struct pipe_server *server = (struct pipe_server *)obj;
356 assert( obj->ops == &pipe_server_ops );
358 if( server->fd )
360 notify_empty( server );
361 do_disconnect( server );
364 if( server->client )
366 server->client->server = NULL;
367 server->client = NULL;
370 notify_waiter( &server->wait, STATUS_HANDLES_CLOSED );
372 assert( server->pipe->instances );
373 server->pipe->instances--;
375 list_remove( &server->entry );
376 release_object( server->pipe );
379 static void pipe_client_destroy( struct object *obj)
381 struct pipe_client *client = (struct pipe_client *)obj;
382 struct pipe_server *server = client->server;
384 assert( obj->ops == &pipe_client_ops );
386 notify_waiter( &client->wait, STATUS_HANDLES_CLOSED );
388 if( server )
390 notify_empty( server );
392 switch( server->state )
394 case ps_connected_server:
395 /* Don't destroy the server's fd here as we can't
396 do a successful flush without it. */
397 server->state = ps_wait_disconnect;
398 release_object( client->fd );
399 client->fd = NULL;
400 break;
401 case ps_disconnected_server:
402 server->state = ps_wait_connect;
403 break;
404 case ps_idle_server:
405 case ps_wait_open:
406 case ps_wait_disconnect:
407 case ps_wait_connect:
408 assert( 0 );
410 assert( server->client );
411 server->client = NULL;
412 client->server = NULL;
414 assert( !client->fd );
417 static int pipe_end_get_poll_events( struct fd *fd )
419 return POLLIN | POLLOUT; /* FIXME */
422 static int pipe_data_remaining( struct pipe_server *server )
424 struct pollfd pfd;
425 int fd;
427 assert( server->client );
429 fd = get_unix_fd( server->client->fd );
430 if( fd < 0 )
431 return 0;
432 pfd.fd = fd;
433 pfd.events = POLLIN;
434 pfd.revents = 0;
436 if( 0 > poll( &pfd, 1, 0 ) )
437 return 0;
439 return pfd.revents&POLLIN;
442 static void check_flushed( void *arg )
444 struct pipe_server *server = (struct pipe_server*) arg;
446 assert( server->event );
447 if( pipe_data_remaining( server ) )
449 struct timeval tv;
451 gettimeofday( &tv, 0 );
452 add_timeout( &tv, 100 );
453 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
455 else
457 /* notify_empty( server ); */
458 server->flush_poll = NULL;
459 set_event( server->event );
460 release_object( server->event );
461 server->event = NULL;
465 static int pipe_server_flush( struct fd *fd, struct event **event )
467 struct pipe_server *server = get_fd_user( fd );
469 if( !server )
470 return 0;
472 if( server->state != ps_connected_server )
473 return 0;
475 /* FIXME: if multiple threads flush the same pipe,
476 maybe should create a list of processes to notify */
477 if( server->flush_poll )
478 return 0;
480 if( pipe_data_remaining( server ) )
482 struct timeval tv;
484 /* this kind of sux -
485 there's no unix way to be alerted when a pipe becomes empty */
486 server->event = create_event( NULL, 0, 0, 0 );
487 if( !server->event )
488 return 0;
489 gettimeofday( &tv, 0 );
490 add_timeout( &tv, 100 );
491 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
492 *event = server->event;
495 return 0;
498 static int pipe_client_flush( struct fd *fd, struct event **event )
500 /* FIXME: what do we have to do for this? */
501 return 0;
504 static int pipe_end_get_info( struct fd *fd )
506 return 0;
509 static struct named_pipe *create_named_pipe( const WCHAR *name, size_t len )
511 struct named_pipe *pipe;
513 pipe = create_named_object( sync_namespace, &named_pipe_ops, name, len );
514 if( pipe )
516 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
518 /* initialize it if it didn't already exist */
519 pipe->instances = 0;
520 list_init( &pipe->servers );
521 list_init( &pipe->waiters );
524 return pipe;
527 static struct named_pipe *open_named_pipe( const WCHAR *name, size_t len )
529 struct object *obj;
531 if ((obj = find_object( sync_namespace, name, len )))
533 if (obj->ops == &named_pipe_ops) return (struct named_pipe *)obj;
534 release_object( obj );
535 set_error( STATUS_OBJECT_TYPE_MISMATCH );
537 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
539 return NULL;
542 static struct pipe_server *get_pipe_server_obj( struct process *process,
543 obj_handle_t handle, unsigned int access )
545 struct object *obj;
546 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
547 return (struct pipe_server *) obj;
550 static struct pipe_server *create_pipe_server( struct named_pipe *pipe )
552 struct pipe_server *server;
554 server = alloc_object( &pipe_server_ops );
555 if( !server )
556 return NULL;
558 server->fd = NULL;
559 server->pipe = pipe;
560 server->state = ps_idle_server;
561 server->client = NULL;
562 server->flush_poll = NULL;
563 server->wait.thread = NULL;
565 list_add_head( &pipe->servers, &server->entry );
566 grab_object( pipe );
568 return server;
571 static struct pipe_client *create_pipe_client( struct pipe_server *server )
573 struct pipe_client *client;
575 client = alloc_object( &pipe_client_ops );
576 if( !client )
577 return NULL;
579 client->fd = NULL;
580 client->server = server;
581 client->wait.thread = NULL;
583 return client;
586 static inline struct pipe_server *find_server( struct named_pipe *pipe, enum pipe_state state )
588 struct pipe_server *server;
590 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
592 if (server->state == state) return (struct pipe_server *)grab_object( server );
594 return NULL;
597 static inline struct pipe_server *find_server2( struct named_pipe *pipe,
598 enum pipe_state state1, enum pipe_state state2 )
600 struct pipe_server *server;
602 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
604 if (server->state == state1 || server->state == state2)
605 return (struct pipe_server *)grab_object( server );
607 return NULL;
610 DECL_HANDLER(create_named_pipe)
612 struct named_pipe *pipe;
613 struct pipe_server *server;
615 reply->handle = 0;
616 pipe = create_named_pipe( get_req_data(), get_req_data_size() );
617 if( !pipe )
618 return;
620 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
622 pipe->insize = req->insize;
623 pipe->outsize = req->outsize;
624 pipe->maxinstances = req->maxinstances;
625 pipe->timeout = req->timeout;
626 pipe->pipemode = req->pipemode;
628 else
630 set_error( 0 ); /* clear the name collision */
631 if( pipe->maxinstances <= pipe->instances )
633 set_error( STATUS_PIPE_BUSY );
634 release_object( pipe );
635 return;
637 if( ( pipe->maxinstances != req->maxinstances ) ||
638 ( pipe->timeout != req->timeout ) ||
639 ( pipe->pipemode != req->pipemode ) )
641 set_error( STATUS_ACCESS_DENIED );
642 release_object( pipe );
643 return;
647 server = create_pipe_server( pipe );
648 if(server)
650 reply->handle = alloc_handle( current->process, server,
651 GENERIC_READ|GENERIC_WRITE, req->inherit );
652 server->pipe->instances++;
653 release_object( server );
656 release_object( pipe );
659 DECL_HANDLER(open_named_pipe)
661 struct pipe_server *server;
662 struct pipe_client *client;
663 struct named_pipe *pipe;
664 int fds[2];
666 pipe = open_named_pipe( get_req_data(), get_req_data_size() );
667 if ( !pipe )
669 set_error( STATUS_NO_SUCH_FILE );
670 return;
673 server = find_server2( pipe, ps_idle_server, ps_wait_open );
674 release_object( pipe );
676 if ( !server )
678 set_error( STATUS_PIPE_NOT_AVAILABLE );
679 return;
682 client = create_pipe_client( server );
683 if( client )
685 if( !socketpair( PF_UNIX, SOCK_STREAM, 0, fds ) )
687 assert( !client->fd );
688 assert( !server->fd );
689 client->fd = create_anonymous_fd( &pipe_server_fd_ops,
690 fds[1], &client->obj );
691 server->fd = create_anonymous_fd( &pipe_server_fd_ops,
692 fds[0], &server->obj );
693 if (client->fd && server->fd)
695 if( server->state == ps_wait_open )
696 notify_waiter( &server->wait, STATUS_SUCCESS );
697 assert( !server->wait.thread );
698 server->state = ps_connected_server;
699 server->client = client;
700 client->server = server;
701 reply->handle = alloc_handle( current->process, client,
702 req->access, req->inherit );
705 else
706 file_set_error();
708 release_object( client );
710 release_object( server );
713 DECL_HANDLER(connect_named_pipe)
715 struct pipe_server *server;
717 server = get_pipe_server_obj(current->process, req->handle, 0);
718 if(!server)
719 return;
721 switch( server->state )
723 case ps_idle_server:
724 case ps_wait_connect:
725 assert( !server->fd );
726 server->state = ps_wait_open;
727 set_waiter( &server->wait, req->func, req->overlapped );
728 notify_all_connect_waiters( server->pipe, STATUS_SUCCESS );
729 break;
730 case ps_connected_server:
731 assert( server->fd );
732 set_error( STATUS_PIPE_CONNECTED );
733 break;
734 case ps_disconnected_server:
735 set_error( STATUS_PIPE_BUSY );
736 break;
737 case ps_wait_disconnect:
738 set_error( STATUS_NO_DATA_DETECTED );
739 break;
740 case ps_wait_open:
741 set_error( STATUS_INVALID_HANDLE );
742 break;
745 release_object(server);
748 DECL_HANDLER(wait_named_pipe)
750 struct named_pipe *pipe;
751 struct pipe_server *server;
753 if (!(pipe = open_named_pipe( get_req_data(), get_req_data_size() )))
755 set_error( STATUS_PIPE_NOT_AVAILABLE );
756 return;
758 server = find_server( pipe, ps_wait_open );
759 if( server )
761 /* there's already a server waiting for a client to connect */
762 struct wait_info wait;
763 set_waiter( &wait, req->func, req->overlapped );
764 notify_waiter( &wait, STATUS_SUCCESS );
765 release_object( server );
767 else
769 unsigned int timeout;
770 if (req->timeout == NMPWAIT_USE_DEFAULT_WAIT)
771 timeout = pipe->timeout;
772 else
773 timeout = req->timeout;
775 if (req->timeout == NMPWAIT_WAIT_FOREVER)
776 queue_connect_waiter( pipe, req->func, req->overlapped, NULL );
777 else
778 queue_connect_waiter( pipe, req->func, req->overlapped, &timeout );
781 release_object( pipe );
784 DECL_HANDLER(disconnect_named_pipe)
786 struct pipe_server *server;
788 reply->fd = -1;
789 server = get_pipe_server_obj( current->process, req->handle, 0 );
790 if( !server )
791 return;
792 switch( server->state )
794 case ps_connected_server:
795 assert( server->fd );
796 assert( server->client );
797 assert( server->client->fd );
799 notify_empty( server );
800 notify_waiter( &server->client->wait, STATUS_PIPE_DISCONNECTED );
802 /* Dump the client and server fds, but keep the pointers
803 around - client loses all waiting data */
804 server->state = ps_disconnected_server;
805 do_disconnect( server );
806 reply->fd = flush_cached_fd( current->process, req->handle );
807 break;
809 case ps_wait_disconnect:
810 assert( !server->client );
811 assert( server->fd );
812 do_disconnect( server );
813 server->state = ps_wait_connect;
814 reply->fd = flush_cached_fd( current->process, req->handle );
815 break;
817 case ps_idle_server:
818 case ps_wait_open:
819 case ps_disconnected_server:
820 case ps_wait_connect:
821 set_error( STATUS_PIPE_DISCONNECTED );
822 break;
824 release_object( server );
827 DECL_HANDLER(get_named_pipe_info)
829 struct pipe_server *server;
831 server = get_pipe_server_obj( current->process, req->handle, 0 );
832 if(!server)
833 return;
835 reply->flags = server->pipe->pipemode;
836 reply->maxinstances = server->pipe->maxinstances;
837 reply->insize = server->pipe->insize;
838 reply->outsize = server->pipe->outsize;
840 release_object(server);