- add vertex Declaration support (using wined3d)
[wine.git] / server / named_pipe.c
blobd945199dc42327b47077f04a685e9dc8cfa42068
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 );
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 */
155 no_cancel_async, /* cancel_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 */
183 no_cancel_async /* cancel_async */
186 static void named_pipe_dump( struct object *obj, int verbose )
188 struct named_pipe *pipe = (struct named_pipe *) obj;
189 assert( obj->ops == &named_pipe_ops );
190 fprintf( stderr, "named pipe %p\n" ,pipe);
193 static void pipe_server_dump( struct object *obj, int verbose )
195 struct pipe_server *server = (struct pipe_server *) obj;
196 assert( obj->ops == &pipe_server_ops );
197 fprintf( stderr, "named pipe server %p (state %d)\n",
198 server, 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_server_ops );
205 fprintf( stderr, "named pipe client %p (server state %d)\n",
206 client, client->server->state );
209 static void named_pipe_destroy( struct object *obj)
211 struct named_pipe *pipe = (struct named_pipe *) obj;
212 assert( !pipe->servers );
213 assert( !pipe->instances );
216 static void notify_waiter( struct wait_info *wait, unsigned int status )
218 if( wait->thread && wait->func && wait->overlapped )
220 /* queue a system APC, to notify a waiting thread */
221 thread_queue_apc( wait->thread, NULL, wait->func, APC_ASYNC,
222 1, wait->overlapped, (void *)status, NULL );
224 if( wait->thread ) release_object( wait->thread );
225 wait->thread = NULL;
228 static void set_waiter( struct wait_info *wait, void *func, void *ov )
230 wait->thread = (struct thread *) grab_object( current );
231 wait->func = func;
232 wait->overlapped = ov;
235 static void notify_connect_waiters( struct named_pipe *pipe )
237 struct connect_wait *cw, **x = &pipe->connect_waiters;
239 while( *x )
241 cw = *x;
242 notify_waiter( &cw->wait, STATUS_SUCCESS );
243 release_object( pipe );
244 *x = cw->next;
245 free( cw );
249 static void queue_connect_waiter( struct named_pipe *pipe,
250 void *func, void *overlapped )
252 struct connect_wait *waiter;
254 waiter = mem_alloc( sizeof(*waiter) );
255 if( waiter )
257 set_waiter( &waiter->wait, func, overlapped );
258 waiter->next = pipe->connect_waiters;
259 pipe->connect_waiters = waiter;
260 grab_object( pipe );
264 static struct fd *pipe_client_get_fd( struct object *obj )
266 struct pipe_client *client = (struct pipe_client *) obj;
267 if( client->fd )
268 return (struct fd *) grab_object( client->fd );
269 set_error( STATUS_PIPE_DISCONNECTED );
270 return NULL;
273 static struct fd *pipe_server_get_fd( struct object *obj )
275 struct pipe_server *server = (struct pipe_server *) obj;
277 switch(server->state)
279 case ps_connected_server:
280 case ps_wait_disconnect:
281 assert( server->fd );
282 return (struct fd *) grab_object( server->fd );
284 case ps_wait_open:
285 case ps_idle_server:
286 set_error( STATUS_PIPE_LISTENING );
287 break;
289 case ps_disconnected_server:
290 case ps_wait_connect:
291 set_error( STATUS_PIPE_DISCONNECTED );
292 break;
294 default:
295 assert( 0 );
297 return NULL;
301 static void notify_empty( struct pipe_server *server )
303 if( !server->flush_poll )
304 return;
305 assert( server->state == ps_connected_server );
306 assert( server->event );
307 remove_timeout_user( server->flush_poll );
308 server->flush_poll = NULL;
309 set_event( server->event );
310 release_object( server->event );
311 server->event = NULL;
314 static void do_disconnect( struct pipe_server *server )
316 /* we may only have a server fd, if the client disconnected */
317 if( server->client )
319 assert( server->client->server == server );
320 assert( server->client->fd );
321 release_object( server->client->fd );
322 server->client->fd = NULL;
324 assert( server->fd );
325 release_object( server->fd );
326 server->fd = NULL;
329 static void pipe_server_destroy( struct object *obj)
331 struct pipe_server *server = (struct pipe_server *)obj;
333 assert( obj->ops == &pipe_server_ops );
335 if( server->fd )
337 notify_empty( server );
338 do_disconnect( server );
341 if( server->client )
343 server->client->server = NULL;
344 server->client = NULL;
347 notify_waiter( &server->wait, STATUS_HANDLES_CLOSED );
349 assert( server->pipe->instances );
350 server->pipe->instances--;
352 /* remove server from pipe's server list */
353 if( server->next ) server->next->prev = server->prev;
354 if( server->prev ) server->prev->next = server->next;
355 else server->pipe->servers = server->next;
356 release_object( server->pipe );
359 static void pipe_client_destroy( struct object *obj)
361 struct pipe_client *client = (struct pipe_client *)obj;
362 struct pipe_server *server = client->server;
364 assert( obj->ops == &pipe_client_ops );
366 notify_waiter( &client->wait, STATUS_HANDLES_CLOSED );
368 if( server )
370 notify_empty( server );
372 switch( server->state )
374 case ps_connected_server:
375 /* Don't destroy the server's fd here as we can't
376 do a successful flush without it. */
377 server->state = ps_wait_disconnect;
378 release_object( client->fd );
379 client->fd = NULL;
380 break;
381 case ps_disconnected_server:
382 server->state = ps_wait_connect;
383 break;
384 default:
385 assert( 0 );
387 assert( server->client );
388 server->client = NULL;
389 client->server = NULL;
391 assert( !client->fd );
394 static int pipe_end_get_poll_events( struct fd *fd )
396 return POLLIN | POLLOUT; /* FIXME */
399 static int pipe_data_remaining( struct pipe_server *server )
401 struct pollfd pfd;
402 int fd;
404 assert( server->client );
406 fd = get_unix_fd( server->client->fd );
407 if( fd < 0 )
408 return 0;
409 pfd.fd = fd;
410 pfd.events = POLLIN;
411 pfd.revents = 0;
413 if( 0 > poll( &pfd, 1, 0 ) )
414 return 0;
416 return pfd.revents&POLLIN;
419 static void check_flushed( void *arg )
421 struct pipe_server *server = (struct pipe_server*) arg;
423 assert( server->event );
424 if( pipe_data_remaining( server ) )
426 struct timeval tv;
428 gettimeofday( &tv, 0 );
429 add_timeout( &tv, 100 );
430 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
432 else
434 /* notify_empty( server ); */
435 server->flush_poll = NULL;
436 set_event( server->event );
437 release_object( server->event );
438 server->event = NULL;
442 static int pipe_server_flush( struct fd *fd, struct event **event )
444 struct pipe_server *server = get_fd_user( fd );
446 if( !server )
447 return 0;
449 if( server->state != ps_connected_server )
450 return 0;
452 /* FIXME: if multiple threads flush the same pipe,
453 maybe should create a list of processes to notify */
454 if( server->flush_poll )
455 return 0;
457 if( pipe_data_remaining( server ) )
459 struct timeval tv;
461 /* this kind of sux -
462 there's no unix way to be alerted when a pipe becomes empty */
463 server->event = create_event( NULL, 0, 0, 0 );
464 if( !server->event )
465 return 0;
466 gettimeofday( &tv, 0 );
467 add_timeout( &tv, 100 );
468 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
469 *event = server->event;
472 return 0;
475 static int pipe_client_flush( struct fd *fd, struct event **event )
477 /* FIXME: what do we have to do for this? */
478 return 0;
481 static int pipe_end_get_info( struct fd *fd )
483 return 0;
486 static struct named_pipe *create_named_pipe( const WCHAR *name, size_t len )
488 struct named_pipe *pipe;
490 pipe = create_named_object( sync_namespace, &named_pipe_ops, name, len );
491 if( pipe )
493 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
495 /* initialize it if it didn't already exist */
496 pipe->servers = 0;
497 pipe->instances = 0;
498 pipe->connect_waiters = NULL;
501 return pipe;
504 static struct named_pipe *open_named_pipe( const WCHAR *name, size_t len )
506 struct object *obj;
508 if ((obj = find_object( sync_namespace, name, len )))
510 if (obj->ops == &named_pipe_ops) return (struct named_pipe *)obj;
511 release_object( obj );
512 set_error( STATUS_OBJECT_TYPE_MISMATCH );
514 else set_error( STATUS_OBJECT_NAME_NOT_FOUND );
516 return NULL;
519 static struct pipe_server *get_pipe_server_obj( struct process *process,
520 obj_handle_t handle, unsigned int access )
522 struct object *obj;
523 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
524 return (struct pipe_server *) obj;
527 static struct pipe_server *create_pipe_server( struct named_pipe *pipe )
529 struct pipe_server *server;
531 server = alloc_object( &pipe_server_ops );
532 if( !server )
533 return NULL;
535 server->fd = NULL;
536 server->pipe = pipe;
537 server->state = ps_none;
538 server->client = NULL;
539 server->flush_poll = NULL;
540 server->wait.thread = NULL;
542 /* add to list of pipe servers */
543 if ((server->next = pipe->servers)) server->next->prev = server;
544 server->prev = NULL;
545 pipe->servers = server;
547 grab_object( pipe );
549 return server;
552 static struct pipe_client *create_pipe_client( struct pipe_server *server )
554 struct pipe_client *client;
556 client = alloc_object( &pipe_client_ops );
557 if( !client )
558 return NULL;
560 client->fd = NULL;
561 client->server = server;
562 client->wait.thread = NULL;
564 return client;
567 static struct pipe_server *find_server( struct named_pipe *pipe,
568 enum pipe_state state )
570 struct pipe_server *x;
572 for( x = pipe->servers; x; x = x->next )
573 if( x->state == state )
574 break;
576 if( !x )
577 return NULL;
579 return (struct pipe_server *) grab_object( x );
582 DECL_HANDLER(create_named_pipe)
584 struct named_pipe *pipe;
585 struct pipe_server *server;
587 reply->handle = 0;
588 pipe = create_named_pipe( get_req_data(), get_req_data_size() );
589 if( !pipe )
590 return;
592 if( get_error() != STATUS_OBJECT_NAME_COLLISION )
594 pipe->insize = req->insize;
595 pipe->outsize = req->outsize;
596 pipe->maxinstances = req->maxinstances;
597 pipe->timeout = req->timeout;
598 pipe->pipemode = req->pipemode;
600 else
602 set_error( 0 ); /* clear the name collision */
603 if( pipe->maxinstances <= pipe->instances )
605 set_error( STATUS_PIPE_BUSY );
606 release_object( pipe );
607 return;
609 if( ( pipe->maxinstances != req->maxinstances ) ||
610 ( pipe->timeout != req->timeout ) ||
611 ( pipe->pipemode != req->pipemode ) )
613 set_error( STATUS_ACCESS_DENIED );
614 release_object( pipe );
615 return;
619 server = create_pipe_server( pipe );
620 if(server)
622 server->state = ps_idle_server;
623 reply->handle = alloc_handle( current->process, server,
624 GENERIC_READ|GENERIC_WRITE, req->inherit );
625 server->pipe->instances++;
626 release_object( server );
629 release_object( pipe );
632 DECL_HANDLER(open_named_pipe)
634 struct pipe_server *server;
635 struct pipe_client *client;
636 struct named_pipe *pipe;
637 int fds[2];
639 reply->handle = 0;
641 pipe = open_named_pipe( get_req_data(), get_req_data_size() );
642 if ( !pipe )
644 set_error( STATUS_NO_SUCH_FILE );
645 return;
648 for( server = pipe->servers; server; server = server->next )
649 if( ( server->state==ps_idle_server ) ||
650 ( server->state==ps_wait_open ) )
651 break;
652 release_object( pipe );
654 if ( !server )
656 set_error( STATUS_PIPE_NOT_AVAILABLE );
657 return;
660 client = create_pipe_client( server );
661 if( client )
663 if( !socketpair( PF_UNIX, SOCK_STREAM, 0, fds ) )
665 assert( !client->fd );
666 assert( !server->fd );
667 client->fd = create_anonymous_fd( &pipe_server_fd_ops,
668 fds[1], &client->obj );
669 server->fd = create_anonymous_fd( &pipe_server_fd_ops,
670 fds[0], &server->obj );
671 if (client->fd && server->fd)
673 if( server->state == ps_wait_open )
674 notify_waiter( &server->wait, STATUS_SUCCESS );
675 assert( !server->wait.thread );
676 server->state = ps_connected_server;
677 server->client = client;
678 client->server = server;
679 reply->handle = alloc_handle( current->process, client,
680 req->access, req->inherit );
683 else
684 file_set_error();
686 release_object( client );
690 DECL_HANDLER(connect_named_pipe)
692 struct pipe_server *server;
694 server = get_pipe_server_obj(current->process, req->handle, 0);
695 if(!server)
696 return;
698 switch( server->state )
700 case ps_idle_server:
701 case ps_wait_connect:
702 assert( !server->fd );
703 server->state = ps_wait_open;
704 set_waiter( &server->wait, req->func, req->overlapped );
705 notify_connect_waiters( server->pipe );
706 break;
707 case ps_connected_server:
708 assert( server->fd );
709 set_error( STATUS_PIPE_CONNECTED );
710 break;
711 case ps_disconnected_server:
712 set_error( STATUS_PIPE_BUSY );
713 break;
714 case ps_wait_disconnect:
715 set_error( STATUS_NO_DATA_DETECTED );
716 break;
717 default:
718 set_error( STATUS_INVALID_HANDLE );
719 break;
722 release_object(server);
725 DECL_HANDLER(wait_named_pipe)
727 struct named_pipe *pipe;
728 struct pipe_server *server;
730 if (!(pipe = open_named_pipe( get_req_data(), get_req_data_size() )))
732 set_error( STATUS_PIPE_NOT_AVAILABLE );
733 return;
735 server = find_server( pipe, ps_wait_open );
736 if( server )
738 /* there's already a server waiting for a client to connect */
739 struct wait_info wait;
740 set_waiter( &wait, req->func, req->overlapped );
741 notify_waiter( &wait, STATUS_SUCCESS );
742 release_object( server );
744 else
745 queue_connect_waiter( pipe, req->func, req->overlapped );
747 release_object( pipe );
750 DECL_HANDLER(disconnect_named_pipe)
752 struct pipe_server *server;
754 reply->fd = -1;
755 server = get_pipe_server_obj( current->process, req->handle, 0 );
756 if( !server )
757 return;
758 switch( server->state )
760 case ps_connected_server:
761 assert( server->fd );
762 assert( server->client );
763 assert( server->client->fd );
765 notify_empty( server );
766 notify_waiter( &server->client->wait, STATUS_PIPE_DISCONNECTED );
768 /* Dump the client and server fds, but keep the pointers
769 around - client loses all waiting data */
770 server->state = ps_disconnected_server;
771 do_disconnect( server );
772 reply->fd = flush_cached_fd( current->process, req->handle );
773 break;
775 case ps_wait_disconnect:
776 assert( !server->client );
777 assert( server->fd );
778 do_disconnect( server );
779 server->state = ps_wait_connect;
780 reply->fd = flush_cached_fd( current->process, req->handle );
781 break;
783 default:
784 set_error( STATUS_PIPE_DISCONNECTED );
786 release_object( server );
789 DECL_HANDLER(get_named_pipe_info)
791 struct pipe_server *server;
793 server = get_pipe_server_obj( current->process, req->handle, 0 );
794 if(!server)
795 return;
797 reply->flags = server->pipe->pipemode;
798 reply->maxinstances = server->pipe->maxinstances;
799 reply->insize = server->pipe->insize;
800 reply->outsize = server->pipe->outsize;
802 release_object(server);