Propsheet pages need WS_EX_CONTROLPARENT style.
[wine.git] / server / named_pipe.c
blob8327e6b2537fdf42b805b5731f5bcc08ffdf8003
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, int *flags );
130 /* server end functions */
131 static void pipe_server_dump( struct object *obj, int verbose );
132 static struct fd *pipe_server_get_fd( struct object *obj );
133 static void pipe_server_destroy( struct object *obj);
134 static int pipe_server_flush( struct fd *fd, struct event **event );
136 static const struct object_ops pipe_server_ops =
138 sizeof(struct pipe_server), /* size */
139 pipe_server_dump, /* dump */
140 default_fd_add_queue, /* add_queue */
141 default_fd_remove_queue, /* remove_queue */
142 default_fd_signaled, /* signaled */
143 no_satisfied, /* satisfied */
144 pipe_server_get_fd, /* get_fd */
145 pipe_server_destroy /* destroy */
148 static const struct fd_ops pipe_server_fd_ops =
150 pipe_end_get_poll_events, /* get_poll_events */
151 default_poll_event, /* poll_event */
152 pipe_server_flush, /* flush */
153 pipe_end_get_info, /* get_file_info */
154 no_queue_async /* queue_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 */
184 static void named_pipe_dump( struct object *obj, int verbose )
186 struct named_pipe *pipe = (struct named_pipe *) obj;
187 assert( obj->ops == &named_pipe_ops );
188 fprintf( stderr, "named pipe %p\n" ,pipe);
191 static void pipe_server_dump( struct object *obj, int verbose )
193 struct pipe_server *server = (struct pipe_server *) obj;
194 assert( obj->ops == &pipe_server_ops );
195 fprintf( stderr, "named pipe server %p (state %d)\n",
196 server, server->state );
199 static void pipe_client_dump( struct object *obj, int verbose )
201 struct pipe_client *client = (struct pipe_client *) obj;
202 assert( obj->ops == &pipe_server_ops );
203 fprintf( stderr, "named pipe client %p (server state %d)\n",
204 client, client->server->state );
207 static void named_pipe_destroy( struct object *obj)
209 struct named_pipe *pipe = (struct named_pipe *) obj;
210 assert( !pipe->servers );
211 assert( !pipe->instances );
214 static void notify_waiter( struct wait_info *wait, unsigned int status )
216 if( wait->thread && wait->func && wait->overlapped )
218 /* queue a system APC, to notify a waiting thread */
219 thread_queue_apc( wait->thread, NULL, wait->func, APC_ASYNC,
220 1, wait->overlapped, (void *)status, NULL );
222 if( wait->thread ) release_object( wait->thread );
223 wait->thread = NULL;
226 static void set_waiter( struct wait_info *wait, void *func, void *ov )
228 wait->thread = (struct thread *) grab_object( current );
229 wait->func = func;
230 wait->overlapped = ov;
233 static void notify_connect_waiters( struct named_pipe *pipe )
235 struct connect_wait *cw, **x = &pipe->connect_waiters;
237 while( *x )
239 cw = *x;
240 notify_waiter( &cw->wait, STATUS_SUCCESS );
241 release_object( pipe );
242 *x = cw->next;
243 free( cw );
247 static void queue_connect_waiter( struct named_pipe *pipe,
248 void *func, void *overlapped )
250 struct connect_wait *waiter;
252 waiter = mem_alloc( sizeof(*waiter) );
253 if( waiter )
255 set_waiter( &waiter->wait, func, overlapped );
256 waiter->next = pipe->connect_waiters;
257 pipe->connect_waiters = waiter;
258 grab_object( pipe );
262 static struct fd *pipe_client_get_fd( struct object *obj )
264 struct pipe_client *client = (struct pipe_client *) obj;
265 if( client->fd )
266 return (struct fd *) grab_object( client->fd );
267 set_error( STATUS_PIPE_DISCONNECTED );
268 return NULL;
271 static struct fd *pipe_server_get_fd( struct object *obj )
273 struct pipe_server *server = (struct pipe_server *) obj;
275 switch(server->state)
277 case ps_connected_server:
278 case ps_wait_disconnect:
279 assert( server->fd );
280 return (struct fd *) grab_object( server->fd );
282 case ps_wait_open:
283 case ps_idle_server:
284 set_error( STATUS_PIPE_LISTENING );
285 break;
287 case ps_disconnected_server:
288 case ps_wait_connect:
289 set_error( STATUS_PIPE_DISCONNECTED );
290 break;
292 default:
293 assert( 0 );
295 return NULL;
299 static void notify_empty( struct pipe_server *server )
301 if( !server->flush_poll )
302 return;
303 assert( server->state == ps_connected_server );
304 assert( server->event );
305 remove_timeout_user( server->flush_poll );
306 server->flush_poll = NULL;
307 set_event( server->event );
308 release_object( server->event );
309 server->event = NULL;
312 static void do_disconnect( struct pipe_server *server )
314 /* we may only have a server fd, if the client disconnected */
315 if( server->client )
317 assert( server->client->server == server );
318 assert( server->client->fd );
319 release_object( server->client->fd );
320 server->client->fd = NULL;
322 assert( server->fd );
323 release_object( server->fd );
324 server->fd = NULL;
327 static void pipe_server_destroy( struct object *obj)
329 struct pipe_server *server = (struct pipe_server *)obj;
331 assert( obj->ops == &pipe_server_ops );
333 if( server->fd )
335 notify_empty( server );
336 do_disconnect( server );
339 if( server->client )
341 server->client->server = NULL;
342 server->client = NULL;
345 notify_waiter( &server->wait, STATUS_HANDLES_CLOSED );
347 assert( server->pipe->instances );
348 server->pipe->instances--;
350 /* remove server from pipe's server list */
351 if( server->next ) server->next->prev = server->prev;
352 if( server->prev ) server->prev->next = server->next;
353 else server->pipe->servers = server->next;
354 release_object( server->pipe );
357 static void pipe_client_destroy( struct object *obj)
359 struct pipe_client *client = (struct pipe_client *)obj;
360 struct pipe_server *server = client->server;
362 assert( obj->ops == &pipe_client_ops );
364 notify_waiter( &client->wait, STATUS_HANDLES_CLOSED );
366 if( server )
368 notify_empty( server );
370 switch( server->state )
372 case ps_connected_server:
373 /* Don't destroy the server's fd here as we can't
374 do a successful flush without it. */
375 server->state = ps_wait_disconnect;
376 release_object( client->fd );
377 client->fd = NULL;
378 break;
379 case ps_disconnected_server:
380 server->state = ps_wait_connect;
381 break;
382 default:
383 assert( 0 );
385 assert( server->client );
386 server->client = NULL;
387 client->server = NULL;
389 assert( !client->fd );
392 static int pipe_end_get_poll_events( struct fd *fd )
394 return POLLIN | POLLOUT; /* FIXME */
397 static int pipe_data_remaining( struct pipe_server *server )
399 struct pollfd pfd;
400 int fd;
402 assert( server->client );
404 fd = get_unix_fd( server->client->fd );
405 if( fd < 0 )
406 return 0;
407 pfd.fd = fd;
408 pfd.events = POLLIN;
409 pfd.revents = 0;
411 if( 0 > poll( &pfd, 1, 0 ) )
412 return 0;
414 return pfd.revents&POLLIN;
417 static void check_flushed( void *arg )
419 struct pipe_server *server = (struct pipe_server*) arg;
421 assert( server->event );
422 if( pipe_data_remaining( server ) )
424 struct timeval tv;
426 gettimeofday( &tv, 0 );
427 add_timeout( &tv, 100 );
428 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
430 else
432 /* notify_empty( server ); */
433 server->flush_poll = NULL;
434 set_event( server->event );
435 release_object( server->event );
436 server->event = NULL;
440 static int pipe_server_flush( struct fd *fd, struct event **event )
442 struct pipe_server *server = get_fd_user( fd );
444 if( !server )
445 return 0;
447 if( server->state != ps_connected_server )
448 return 0;
450 /* FIXME: if multiple threads flush the same pipe,
451 maybe should create a list of processes to notify */
452 if( server->flush_poll )
453 return 0;
455 if( pipe_data_remaining( server ) )
457 struct timeval tv;
459 /* this kind of sux -
460 there's no unix way to be alerted when a pipe becomes empty */
461 server->event = create_event( NULL, 0, 0, 0 );
462 if( !server->event )
463 return 0;
464 gettimeofday( &tv, 0 );
465 add_timeout( &tv, 100 );
466 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
467 *event = server->event;
470 return 0;
473 static int pipe_client_flush( struct fd *fd, struct event **event )
475 /* FIXME: what do we have to do for this? */
476 return 0;
479 static int pipe_end_get_info( struct fd *fd, int *flags )
481 *flags = 0;
482 return FD_TYPE_DEFAULT;
485 static struct named_pipe *create_named_pipe( const WCHAR *name, size_t len )
487 struct named_pipe *pipe;
489 pipe = create_named_object( sync_namespace, &named_pipe_ops, name, len );
490 if( pipe )
492 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
494 /* initialize it if it didn't already exist */
495 pipe->servers = 0;
496 pipe->instances = 0;
497 pipe->connect_waiters = NULL;
500 return pipe;
503 static struct named_pipe *open_named_pipe( const WCHAR *name, size_t len )
505 struct object *obj;
507 if ((obj = find_object( sync_namespace, name, len )))
509 if (obj->ops == &named_pipe_ops) return (struct named_pipe *)obj;
510 release_object( obj );
511 set_error( STATUS_OBJECT_TYPE_MISMATCH );
513 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
515 return NULL;
518 static struct pipe_server *get_pipe_server_obj( struct process *process,
519 obj_handle_t handle, unsigned int access )
521 struct object *obj;
522 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
523 return (struct pipe_server *) obj;
526 static struct pipe_server *create_pipe_server( struct named_pipe *pipe )
528 struct pipe_server *server;
530 server = alloc_object( &pipe_server_ops );
531 if( !server )
532 return NULL;
534 server->fd = NULL;
535 server->pipe = pipe;
536 server->state = ps_none;
537 server->client = NULL;
538 server->flush_poll = NULL;
539 server->wait.thread = NULL;
541 /* add to list of pipe servers */
542 if ((server->next = pipe->servers)) server->next->prev = server;
543 server->prev = NULL;
544 pipe->servers = server;
546 grab_object( pipe );
548 return server;
551 static struct pipe_client *create_pipe_client( struct pipe_server *server )
553 struct pipe_client *client;
555 client = alloc_object( &pipe_client_ops );
556 if( !client )
557 return NULL;
559 client->fd = NULL;
560 client->server = server;
561 client->wait.thread = NULL;
563 return client;
566 static struct pipe_server *find_server( struct named_pipe *pipe,
567 enum pipe_state state )
569 struct pipe_server *x;
571 for( x = pipe->servers; x; x = x->next )
572 if( x->state == state )
573 break;
575 if( !x )
576 return NULL;
578 return (struct pipe_server *) grab_object( x );
581 DECL_HANDLER(create_named_pipe)
583 struct named_pipe *pipe;
584 struct pipe_server *server;
586 reply->handle = 0;
587 pipe = create_named_pipe( get_req_data(), get_req_data_size() );
588 if( !pipe )
589 return;
591 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
593 pipe->insize = req->insize;
594 pipe->outsize = req->outsize;
595 pipe->maxinstances = req->maxinstances;
596 pipe->timeout = req->timeout;
597 pipe->pipemode = req->pipemode;
599 else
601 set_error( 0 ); /* clear the name collision */
602 if( pipe->maxinstances <= pipe->instances )
604 set_error( STATUS_PIPE_BUSY );
605 release_object( pipe );
606 return;
608 if( ( pipe->maxinstances != req->maxinstances ) ||
609 ( pipe->timeout != req->timeout ) ||
610 ( pipe->pipemode != req->pipemode ) )
612 set_error( STATUS_ACCESS_DENIED );
613 release_object( pipe );
614 return;
618 server = create_pipe_server( pipe );
619 if(server)
621 server->state = ps_idle_server;
622 reply->handle = alloc_handle( current->process, server,
623 GENERIC_READ|GENERIC_WRITE, req->inherit );
624 server->pipe->instances++;
625 release_object( server );
628 release_object( pipe );
631 DECL_HANDLER(open_named_pipe)
633 struct pipe_server *server;
634 struct pipe_client *client;
635 struct named_pipe *pipe;
636 int fds[2];
638 reply->handle = 0;
640 pipe = open_named_pipe( get_req_data(), get_req_data_size() );
641 if ( !pipe )
643 set_error( STATUS_NO_SUCH_FILE );
644 return;
647 for( server = pipe->servers; server; server = server->next )
648 if( ( server->state==ps_idle_server ) ||
649 ( server->state==ps_wait_open ) )
650 break;
651 release_object( pipe );
653 if ( !server )
655 set_error( STATUS_PIPE_NOT_AVAILABLE );
656 return;
659 client = create_pipe_client( server );
660 if( client )
662 if( !socketpair( PF_UNIX, SOCK_STREAM, 0, fds ) )
664 assert( !client->fd );
665 assert( !server->fd );
666 client->fd = create_anonymous_fd( &pipe_server_fd_ops,
667 fds[1], &client->obj );
668 server->fd = create_anonymous_fd( &pipe_server_fd_ops,
669 fds[0], &server->obj );
670 if (client->fd && server->fd)
672 if( server->state == ps_wait_open )
673 notify_waiter( &server->wait, STATUS_SUCCESS );
674 assert( !server->wait.thread );
675 server->state = ps_connected_server;
676 server->client = client;
677 client->server = server;
678 reply->handle = alloc_handle( current->process, client,
679 req->access, req->inherit );
682 else
683 file_set_error();
685 release_object( client );
689 DECL_HANDLER(connect_named_pipe)
691 struct pipe_server *server;
693 server = get_pipe_server_obj(current->process, req->handle, 0);
694 if(!server)
695 return;
697 switch( server->state )
699 case ps_idle_server:
700 case ps_wait_connect:
701 assert( !server->fd );
702 server->state = ps_wait_open;
703 set_waiter( &server->wait, req->func, req->overlapped );
704 notify_connect_waiters( server->pipe );
705 break;
706 case ps_connected_server:
707 assert( server->fd );
708 set_error( STATUS_PIPE_CONNECTED );
709 break;
710 case ps_disconnected_server:
711 set_error( STATUS_PIPE_BUSY );
712 break;
713 case ps_wait_disconnect:
714 set_error( STATUS_NO_DATA_DETECTED );
715 break;
716 default:
717 set_error( STATUS_INVALID_HANDLE );
718 break;
721 release_object(server);
724 DECL_HANDLER(wait_named_pipe)
726 struct named_pipe *pipe;
727 struct pipe_server *server;
729 if (!(pipe = open_named_pipe( get_req_data(), get_req_data_size() )))
731 set_error( STATUS_PIPE_NOT_AVAILABLE );
732 return;
734 server = find_server( pipe, ps_wait_open );
735 if( server )
737 /* there's already a server waiting for a client to connect */
738 struct wait_info wait;
739 set_waiter( &wait, req->func, req->overlapped );
740 notify_waiter( &wait, STATUS_SUCCESS );
741 release_object( server );
743 else
744 queue_connect_waiter( pipe, req->func, req->overlapped );
746 release_object( pipe );
749 DECL_HANDLER(disconnect_named_pipe)
751 struct pipe_server *server;
753 reply->fd = -1;
754 server = get_pipe_server_obj( current->process, req->handle, 0 );
755 if( !server )
756 return;
757 switch( server->state )
759 case ps_connected_server:
760 assert( server->fd );
761 assert( server->client );
762 assert( server->client->fd );
764 notify_empty( server );
765 notify_waiter( &server->client->wait, STATUS_PIPE_DISCONNECTED );
767 /* Dump the client and server fds, but keep the pointers
768 around - client loses all waiting data */
769 server->state = ps_disconnected_server;
770 do_disconnect( server );
771 reply->fd = flush_cached_fd( current->process, req->handle );
772 break;
774 case ps_wait_disconnect:
775 assert( !server->client );
776 assert( server->fd );
777 do_disconnect( server );
778 server->state = ps_wait_connect;
779 reply->fd = flush_cached_fd( current->process, req->handle );
780 break;
782 default:
783 set_error( STATUS_PIPE_DISCONNECTED );
785 release_object( server );
788 DECL_HANDLER(get_named_pipe_info)
790 struct pipe_server *server;
792 server = get_pipe_server_obj( current->process, req->handle, 0 );
793 if(!server)
794 return;
796 reply->flags = server->pipe->pipemode;
797 reply->maxinstances = server->pipe->maxinstances;
798 reply->insize = server->pipe->insize;
799 reply->outsize = server->pipe->outsize;
801 release_object(server);