Check for overflows with ClrUsed.
[wine.git] / server / named_pipe.c
blobe10f44a89a8cbbb74869e6ad70cc51e0d81987cc
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_idle_server,
53 ps_wait_open,
54 ps_connected_server,
55 ps_wait_disconnect,
56 ps_disconnected_server,
57 ps_wait_connect
60 struct wait_info
62 struct thread *thread;
63 void *func;
64 void *overlapped;
67 struct named_pipe;
69 struct pipe_server
71 struct object obj; /* object header */
72 struct fd *fd; /* pipe file descriptor */
73 struct list entry; /* entry in named pipe servers list */
74 enum pipe_state state; /* server state */
75 struct pipe_client *client; /* client that this server is connected to */
76 struct named_pipe *pipe;
77 struct timeout_user *flush_poll;
78 struct event *event;
79 struct wait_info wait;
82 struct pipe_client
84 struct object obj; /* object header */
85 struct fd *fd; /* pipe file descriptor */
86 struct pipe_server *server; /* server that this client is connected to */
87 struct wait_info wait;
90 struct connect_wait
92 struct list entry; /* entry in named pipe wait list */
93 struct wait_info wait;
94 struct timeout_user *timeout_user;
97 struct named_pipe
99 struct object obj; /* object header */
100 unsigned int pipemode;
101 unsigned int maxinstances;
102 unsigned int outsize;
103 unsigned int insize;
104 unsigned int timeout;
105 unsigned int instances;
106 struct list servers; /* list of servers using this pipe */
107 struct list waiters; /* list of clients waiting to connect */
110 static void named_pipe_dump( struct object *obj, int verbose );
111 static void named_pipe_destroy( struct object *obj );
113 static const struct object_ops named_pipe_ops =
115 sizeof(struct named_pipe), /* size */
116 named_pipe_dump, /* dump */
117 no_add_queue, /* add_queue */
118 NULL, /* remove_queue */
119 NULL, /* signaled */
120 NULL, /* satisfied */
121 no_get_fd, /* get_fd */
122 named_pipe_destroy /* destroy */
125 /* common to clients and servers */
126 static int pipe_end_get_poll_events( struct fd *fd );
127 static int pipe_end_get_info( struct fd *fd );
129 /* server end functions */
130 static void pipe_server_dump( struct object *obj, int verbose );
131 static struct fd *pipe_server_get_fd( struct object *obj );
132 static void pipe_server_destroy( struct object *obj);
133 static int pipe_server_flush( struct fd *fd, struct event **event );
135 static const struct object_ops pipe_server_ops =
137 sizeof(struct pipe_server), /* size */
138 pipe_server_dump, /* dump */
139 default_fd_add_queue, /* add_queue */
140 default_fd_remove_queue, /* remove_queue */
141 default_fd_signaled, /* signaled */
142 no_satisfied, /* satisfied */
143 pipe_server_get_fd, /* get_fd */
144 pipe_server_destroy /* destroy */
147 static const struct fd_ops pipe_server_fd_ops =
149 pipe_end_get_poll_events, /* get_poll_events */
150 default_poll_event, /* poll_event */
151 pipe_server_flush, /* flush */
152 pipe_end_get_info, /* get_file_info */
153 no_queue_async, /* queue_async */
154 no_cancel_async, /* cancel_async */
157 /* client end functions */
158 static void pipe_client_dump( struct object *obj, int verbose );
159 static struct fd *pipe_client_get_fd( struct object *obj );
160 static void pipe_client_destroy( struct object *obj );
161 static int pipe_client_flush( struct fd *fd, struct event **event );
163 static const struct object_ops pipe_client_ops =
165 sizeof(struct pipe_client), /* size */
166 pipe_client_dump, /* dump */
167 default_fd_add_queue, /* add_queue */
168 default_fd_remove_queue, /* remove_queue */
169 default_fd_signaled, /* signaled */
170 no_satisfied, /* satisfied */
171 pipe_client_get_fd, /* get_fd */
172 pipe_client_destroy /* destroy */
175 static const struct fd_ops pipe_client_fd_ops =
177 pipe_end_get_poll_events, /* get_poll_events */
178 default_poll_event, /* poll_event */
179 pipe_client_flush, /* flush */
180 pipe_end_get_info, /* get_file_info */
181 no_queue_async, /* queue_async */
182 no_cancel_async /* cancel_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 " );
190 dump_object_name( &pipe->obj );
191 fprintf( stderr, "\n" );
194 static void pipe_server_dump( struct object *obj, int verbose )
196 struct pipe_server *server = (struct pipe_server *) obj;
197 assert( obj->ops == &pipe_server_ops );
198 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
201 static void pipe_client_dump( struct object *obj, int verbose )
203 struct pipe_client *client = (struct pipe_client *) obj;
204 assert( obj->ops == &pipe_client_ops );
205 fprintf( stderr, "Named pipe client server=%p\n", client->server );
208 static void notify_waiter( struct wait_info *wait, unsigned int status )
210 if( wait->thread && wait->func && wait->overlapped )
212 /* queue a system APC, to notify a waiting thread */
213 thread_queue_apc( wait->thread, NULL, wait->func, APC_ASYNC,
214 1, wait->overlapped, (void *)status, NULL );
216 if( wait->thread ) release_object( wait->thread );
217 wait->thread = NULL;
220 static void set_waiter( struct wait_info *wait, void *func, void *ov )
222 wait->thread = (struct thread *) grab_object( current );
223 wait->func = func;
224 wait->overlapped = ov;
227 static void notify_connect_waiter( struct connect_wait *waiter, unsigned int status )
229 notify_waiter( &waiter->wait, status );
230 list_remove( &waiter->entry );
231 free( waiter );
234 static void notify_all_connect_waiters( struct named_pipe *pipe, unsigned int status )
236 struct list *ptr;
238 while ((ptr = list_head( &pipe->waiters )) != NULL)
240 struct connect_wait *waiter = LIST_ENTRY( ptr, struct connect_wait, entry );
241 if (waiter->timeout_user) remove_timeout_user( waiter->timeout_user );
242 notify_connect_waiter( waiter, status );
246 /* pipe connect wait timeout */
247 static void connect_timeout( void *ptr )
249 struct connect_wait *waiter = (struct connect_wait *)ptr;
250 notify_connect_waiter( waiter, STATUS_TIMEOUT );
253 static void named_pipe_destroy( struct object *obj)
255 struct named_pipe *pipe = (struct named_pipe *) obj;
257 assert( list_empty( &pipe->servers ) );
258 assert( !pipe->instances );
259 notify_all_connect_waiters( pipe, STATUS_HANDLES_CLOSED );
262 static void queue_connect_waiter( struct named_pipe *pipe, void *func,
263 void *overlapped, unsigned int *timeout )
265 struct connect_wait *waiter;
267 waiter = mem_alloc( sizeof(*waiter) );
268 if( waiter )
270 struct timeval tv;
272 set_waiter( &waiter->wait, func, overlapped );
273 list_add_tail( &pipe->waiters, &waiter->entry );
275 if (timeout)
277 gettimeofday( &tv, 0 );
278 add_timeout( &tv, *timeout );
279 waiter->timeout_user = add_timeout_user( &tv, connect_timeout,
280 waiter );
282 else
283 waiter->timeout_user = NULL;
287 static struct fd *pipe_client_get_fd( struct object *obj )
289 struct pipe_client *client = (struct pipe_client *) obj;
290 if( client->fd )
291 return (struct fd *) grab_object( client->fd );
292 set_error( STATUS_PIPE_DISCONNECTED );
293 return NULL;
296 static struct fd *pipe_server_get_fd( struct object *obj )
298 struct pipe_server *server = (struct pipe_server *) obj;
300 switch(server->state)
302 case ps_connected_server:
303 case ps_wait_disconnect:
304 assert( server->fd );
305 return (struct fd *) grab_object( server->fd );
307 case ps_wait_open:
308 case ps_idle_server:
309 set_error( STATUS_PIPE_LISTENING );
310 break;
312 case ps_disconnected_server:
313 case ps_wait_connect:
314 set_error( STATUS_PIPE_DISCONNECTED );
315 break;
317 return NULL;
321 static void notify_empty( struct pipe_server *server )
323 if( !server->flush_poll )
324 return;
325 assert( server->state == ps_connected_server );
326 assert( server->event );
327 remove_timeout_user( server->flush_poll );
328 server->flush_poll = NULL;
329 set_event( server->event );
330 release_object( server->event );
331 server->event = NULL;
334 static void do_disconnect( struct pipe_server *server )
336 /* we may only have a server fd, if the client disconnected */
337 if( server->client )
339 assert( server->client->server == server );
340 assert( server->client->fd );
341 release_object( server->client->fd );
342 server->client->fd = NULL;
344 assert( server->fd );
345 release_object( server->fd );
346 server->fd = NULL;
349 static void pipe_server_destroy( struct object *obj)
351 struct pipe_server *server = (struct pipe_server *)obj;
353 assert( obj->ops == &pipe_server_ops );
355 if( server->fd )
357 notify_empty( server );
358 do_disconnect( server );
361 if( server->client )
363 server->client->server = NULL;
364 server->client = NULL;
367 notify_waiter( &server->wait, STATUS_HANDLES_CLOSED );
369 assert( server->pipe->instances );
370 server->pipe->instances--;
372 list_remove( &server->entry );
373 release_object( server->pipe );
376 static void pipe_client_destroy( struct object *obj)
378 struct pipe_client *client = (struct pipe_client *)obj;
379 struct pipe_server *server = client->server;
381 assert( obj->ops == &pipe_client_ops );
383 notify_waiter( &client->wait, STATUS_HANDLES_CLOSED );
385 if( server )
387 notify_empty( server );
389 switch( server->state )
391 case ps_connected_server:
392 /* Don't destroy the server's fd here as we can't
393 do a successful flush without it. */
394 server->state = ps_wait_disconnect;
395 release_object( client->fd );
396 client->fd = NULL;
397 break;
398 case ps_disconnected_server:
399 server->state = ps_wait_connect;
400 break;
401 case ps_idle_server:
402 case ps_wait_open:
403 case ps_wait_disconnect:
404 case ps_wait_connect:
405 assert( 0 );
407 assert( server->client );
408 server->client = NULL;
409 client->server = NULL;
411 assert( !client->fd );
414 static int pipe_end_get_poll_events( struct fd *fd )
416 return POLLIN | POLLOUT; /* FIXME */
419 static int pipe_data_remaining( struct pipe_server *server )
421 struct pollfd pfd;
422 int fd;
424 assert( server->client );
426 fd = get_unix_fd( server->client->fd );
427 if( fd < 0 )
428 return 0;
429 pfd.fd = fd;
430 pfd.events = POLLIN;
431 pfd.revents = 0;
433 if( 0 > poll( &pfd, 1, 0 ) )
434 return 0;
436 return pfd.revents&POLLIN;
439 static void check_flushed( void *arg )
441 struct pipe_server *server = (struct pipe_server*) arg;
443 assert( server->event );
444 if( pipe_data_remaining( server ) )
446 struct timeval tv;
448 gettimeofday( &tv, 0 );
449 add_timeout( &tv, 100 );
450 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
452 else
454 /* notify_empty( server ); */
455 server->flush_poll = NULL;
456 set_event( server->event );
457 release_object( server->event );
458 server->event = NULL;
462 static int pipe_server_flush( struct fd *fd, struct event **event )
464 struct pipe_server *server = get_fd_user( fd );
466 if( !server )
467 return 0;
469 if( server->state != ps_connected_server )
470 return 0;
472 /* FIXME: if multiple threads flush the same pipe,
473 maybe should create a list of processes to notify */
474 if( server->flush_poll )
475 return 0;
477 if( pipe_data_remaining( server ) )
479 struct timeval tv;
481 /* this kind of sux -
482 there's no unix way to be alerted when a pipe becomes empty */
483 server->event = create_event( NULL, 0, 0, 0 );
484 if( !server->event )
485 return 0;
486 gettimeofday( &tv, 0 );
487 add_timeout( &tv, 100 );
488 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
489 *event = server->event;
492 return 0;
495 static int pipe_client_flush( struct fd *fd, struct event **event )
497 /* FIXME: what do we have to do for this? */
498 return 0;
501 static int pipe_end_get_info( struct fd *fd )
503 return 0;
506 static struct named_pipe *create_named_pipe( const WCHAR *name, size_t len )
508 struct named_pipe *pipe;
510 pipe = create_named_object( sync_namespace, &named_pipe_ops, name, len );
511 if( pipe )
513 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
515 /* initialize it if it didn't already exist */
516 pipe->instances = 0;
517 list_init( &pipe->servers );
518 list_init( &pipe->waiters );
521 return pipe;
524 static struct named_pipe *open_named_pipe( const WCHAR *name, size_t len )
526 struct object *obj;
528 if ((obj = find_object( sync_namespace, name, len )))
530 if (obj->ops == &named_pipe_ops) return (struct named_pipe *)obj;
531 release_object( obj );
532 set_error( STATUS_OBJECT_TYPE_MISMATCH );
534 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
536 return NULL;
539 static struct pipe_server *get_pipe_server_obj( struct process *process,
540 obj_handle_t handle, unsigned int access )
542 struct object *obj;
543 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
544 return (struct pipe_server *) obj;
547 static struct pipe_server *create_pipe_server( struct named_pipe *pipe )
549 struct pipe_server *server;
551 server = alloc_object( &pipe_server_ops );
552 if( !server )
553 return NULL;
555 server->fd = NULL;
556 server->pipe = pipe;
557 server->state = ps_idle_server;
558 server->client = NULL;
559 server->flush_poll = NULL;
560 server->wait.thread = NULL;
562 list_add_head( &pipe->servers, &server->entry );
563 grab_object( pipe );
565 return server;
568 static struct pipe_client *create_pipe_client( struct pipe_server *server )
570 struct pipe_client *client;
572 client = alloc_object( &pipe_client_ops );
573 if( !client )
574 return NULL;
576 client->fd = NULL;
577 client->server = server;
578 client->wait.thread = NULL;
580 return client;
583 static inline struct pipe_server *find_server( struct named_pipe *pipe, enum pipe_state state )
585 struct pipe_server *server;
587 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
589 if (server->state == state) return (struct pipe_server *)grab_object( server );
591 return NULL;
594 static inline struct pipe_server *find_server2( struct named_pipe *pipe,
595 enum pipe_state state1, enum pipe_state state2 )
597 struct pipe_server *server;
599 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
601 if (server->state == state1 || server->state == state2)
602 return (struct pipe_server *)grab_object( server );
604 return NULL;
607 DECL_HANDLER(create_named_pipe)
609 struct named_pipe *pipe;
610 struct pipe_server *server;
612 reply->handle = 0;
613 pipe = create_named_pipe( get_req_data(), get_req_data_size() );
614 if( !pipe )
615 return;
617 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
619 pipe->insize = req->insize;
620 pipe->outsize = req->outsize;
621 pipe->maxinstances = req->maxinstances;
622 pipe->timeout = req->timeout;
623 pipe->pipemode = req->pipemode;
625 else
627 set_error( 0 ); /* clear the name collision */
628 if( pipe->maxinstances <= pipe->instances )
630 set_error( STATUS_PIPE_BUSY );
631 release_object( pipe );
632 return;
634 if( ( pipe->maxinstances != req->maxinstances ) ||
635 ( pipe->timeout != req->timeout ) ||
636 ( pipe->pipemode != req->pipemode ) )
638 set_error( STATUS_ACCESS_DENIED );
639 release_object( pipe );
640 return;
644 server = create_pipe_server( pipe );
645 if(server)
647 reply->handle = alloc_handle( current->process, server,
648 GENERIC_READ|GENERIC_WRITE, req->inherit );
649 server->pipe->instances++;
650 release_object( server );
653 release_object( pipe );
656 DECL_HANDLER(open_named_pipe)
658 struct pipe_server *server;
659 struct pipe_client *client;
660 struct named_pipe *pipe;
661 int fds[2];
663 pipe = open_named_pipe( get_req_data(), get_req_data_size() );
664 if ( !pipe )
666 set_error( STATUS_NO_SUCH_FILE );
667 return;
670 server = find_server2( pipe, ps_idle_server, ps_wait_open );
671 release_object( pipe );
673 if ( !server )
675 set_error( STATUS_PIPE_NOT_AVAILABLE );
676 return;
679 client = create_pipe_client( server );
680 if( client )
682 if( !socketpair( PF_UNIX, SOCK_STREAM, 0, fds ) )
684 assert( !client->fd );
685 assert( !server->fd );
686 client->fd = create_anonymous_fd( &pipe_server_fd_ops,
687 fds[1], &client->obj );
688 server->fd = create_anonymous_fd( &pipe_server_fd_ops,
689 fds[0], &server->obj );
690 if (client->fd && server->fd)
692 if( server->state == ps_wait_open )
693 notify_waiter( &server->wait, STATUS_SUCCESS );
694 assert( !server->wait.thread );
695 server->state = ps_connected_server;
696 server->client = client;
697 client->server = server;
698 reply->handle = alloc_handle( current->process, client,
699 req->access, req->inherit );
702 else
703 file_set_error();
705 release_object( client );
707 release_object( server );
710 DECL_HANDLER(connect_named_pipe)
712 struct pipe_server *server;
714 server = get_pipe_server_obj(current->process, req->handle, 0);
715 if(!server)
716 return;
718 switch( server->state )
720 case ps_idle_server:
721 case ps_wait_connect:
722 assert( !server->fd );
723 server->state = ps_wait_open;
724 set_waiter( &server->wait, req->func, req->overlapped );
725 notify_all_connect_waiters( server->pipe, STATUS_SUCCESS );
726 break;
727 case ps_connected_server:
728 assert( server->fd );
729 set_error( STATUS_PIPE_CONNECTED );
730 break;
731 case ps_disconnected_server:
732 set_error( STATUS_PIPE_BUSY );
733 break;
734 case ps_wait_disconnect:
735 set_error( STATUS_NO_DATA_DETECTED );
736 break;
737 case ps_wait_open:
738 set_error( STATUS_INVALID_HANDLE );
739 break;
742 release_object(server);
745 DECL_HANDLER(wait_named_pipe)
747 struct named_pipe *pipe;
748 struct pipe_server *server;
750 if (!(pipe = open_named_pipe( get_req_data(), get_req_data_size() )))
752 set_error( STATUS_PIPE_NOT_AVAILABLE );
753 return;
755 server = find_server( pipe, ps_wait_open );
756 if( server )
758 /* there's already a server waiting for a client to connect */
759 struct wait_info wait;
760 set_waiter( &wait, req->func, req->overlapped );
761 notify_waiter( &wait, STATUS_SUCCESS );
762 release_object( server );
764 else
766 unsigned int timeout;
767 if (req->timeout == NMPWAIT_USE_DEFAULT_WAIT)
768 timeout = pipe->timeout;
769 else
770 timeout = req->timeout;
772 if (req->timeout == NMPWAIT_WAIT_FOREVER)
773 queue_connect_waiter( pipe, req->func, req->overlapped, NULL );
774 else
775 queue_connect_waiter( pipe, req->func, req->overlapped, &timeout );
778 release_object( pipe );
781 DECL_HANDLER(disconnect_named_pipe)
783 struct pipe_server *server;
785 reply->fd = -1;
786 server = get_pipe_server_obj( current->process, req->handle, 0 );
787 if( !server )
788 return;
789 switch( server->state )
791 case ps_connected_server:
792 assert( server->fd );
793 assert( server->client );
794 assert( server->client->fd );
796 notify_empty( server );
797 notify_waiter( &server->client->wait, STATUS_PIPE_DISCONNECTED );
799 /* Dump the client and server fds, but keep the pointers
800 around - client loses all waiting data */
801 server->state = ps_disconnected_server;
802 do_disconnect( server );
803 reply->fd = flush_cached_fd( current->process, req->handle );
804 break;
806 case ps_wait_disconnect:
807 assert( !server->client );
808 assert( server->fd );
809 do_disconnect( server );
810 server->state = ps_wait_connect;
811 reply->fd = flush_cached_fd( current->process, req->handle );
812 break;
814 case ps_idle_server:
815 case ps_wait_open:
816 case ps_disconnected_server:
817 case ps_wait_connect:
818 set_error( STATUS_PIPE_DISCONNECTED );
819 break;
821 release_object( server );
824 DECL_HANDLER(get_named_pipe_info)
826 struct pipe_server *server;
828 server = get_pipe_server_obj( current->process, req->handle, 0 );
829 if(!server)
830 return;
832 reply->flags = server->pipe->pipemode;
833 reply->maxinstances = server->pipe->maxinstances;
834 reply->insize = server->pipe->insize;
835 reply->outsize = server->pipe->outsize;
837 release_object(server);