gdiplus: Add missing declarations for imageattributes.c.
[wine/multimedia.git] / server / named_pipe.c
blob54ce8230465ec10667095355a83f55a7db5b6515
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "ntstatus.h"
46 #define WIN32_NO_STATUS
47 #include "windef.h"
48 #include "winternl.h"
49 #include "winioctl.h"
51 #include "file.h"
52 #include "handle.h"
53 #include "thread.h"
54 #include "request.h"
56 enum pipe_state
58 ps_idle_server,
59 ps_wait_open,
60 ps_connected_server,
61 ps_wait_disconnect,
62 ps_disconnected_server,
63 ps_wait_connect
66 struct named_pipe;
68 struct pipe_server
70 struct object obj; /* object header */
71 struct fd *fd; /* pipe file descriptor */
72 struct fd *ioctl_fd; /* file descriptor for ioctls when not connected */
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 unsigned int options; /* pipe options */
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 unsigned int flags; /* file flags */
90 struct named_pipe
92 struct object obj; /* object header */
93 unsigned int flags;
94 unsigned int maxinstances;
95 unsigned int outsize;
96 unsigned int insize;
97 unsigned int instances;
98 timeout_t timeout;
99 struct list servers; /* list of servers using this pipe */
100 struct async_queue *waiters; /* list of clients waiting to connect */
103 struct named_pipe_device
105 struct object obj; /* object header */
106 struct fd *fd; /* pseudo-fd for ioctls */
107 struct namespace *pipes; /* named pipe namespace */
110 static void named_pipe_dump( struct object *obj, int verbose );
111 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
112 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
113 unsigned int sharing, unsigned int options );
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_get_type, /* get_type */
121 no_add_queue, /* add_queue */
122 NULL, /* remove_queue */
123 NULL, /* signaled */
124 NULL, /* satisfied */
125 no_signal, /* signal */
126 no_get_fd, /* get_fd */
127 named_pipe_map_access, /* map_access */
128 default_get_sd, /* get_sd */
129 default_set_sd, /* set_sd */
130 no_lookup_name, /* lookup_name */
131 named_pipe_open_file, /* open_file */
132 no_close_handle, /* close_handle */
133 named_pipe_destroy /* destroy */
136 /* server end functions */
137 static void pipe_server_dump( struct object *obj, int verbose );
138 static struct fd *pipe_server_get_fd( struct object *obj );
139 static void pipe_server_destroy( struct object *obj);
140 static void pipe_server_flush( struct fd *fd, struct event **event );
141 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd );
142 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
143 int blocking, const void *data, data_size_t size );
145 static const struct object_ops pipe_server_ops =
147 sizeof(struct pipe_server), /* size */
148 pipe_server_dump, /* dump */
149 no_get_type, /* get_type */
150 add_queue, /* add_queue */
151 remove_queue, /* remove_queue */
152 default_fd_signaled, /* signaled */
153 no_satisfied, /* satisfied */
154 no_signal, /* signal */
155 pipe_server_get_fd, /* get_fd */
156 default_fd_map_access, /* map_access */
157 default_get_sd, /* get_sd */
158 default_set_sd, /* set_sd */
159 no_lookup_name, /* lookup_name */
160 no_open_file, /* open_file */
161 fd_close_handle, /* close_handle */
162 pipe_server_destroy /* destroy */
165 static const struct fd_ops pipe_server_fd_ops =
167 default_fd_get_poll_events, /* get_poll_events */
168 default_poll_event, /* poll_event */
169 pipe_server_flush, /* flush */
170 pipe_server_get_fd_type, /* get_fd_type */
171 pipe_server_ioctl, /* ioctl */
172 default_fd_queue_async, /* queue_async */
173 default_fd_reselect_async, /* reselect_async */
174 default_fd_cancel_async, /* cancel_async */
177 /* client end functions */
178 static void pipe_client_dump( struct object *obj, int verbose );
179 static int pipe_client_signaled( struct object *obj, struct thread *thread );
180 static struct fd *pipe_client_get_fd( struct object *obj );
181 static void pipe_client_destroy( struct object *obj );
182 static void pipe_client_flush( struct fd *fd, struct event **event );
183 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd );
185 static const struct object_ops pipe_client_ops =
187 sizeof(struct pipe_client), /* size */
188 pipe_client_dump, /* dump */
189 no_get_type, /* get_type */
190 add_queue, /* add_queue */
191 remove_queue, /* remove_queue */
192 pipe_client_signaled, /* signaled */
193 no_satisfied, /* satisfied */
194 no_signal, /* signal */
195 pipe_client_get_fd, /* get_fd */
196 default_fd_map_access, /* map_access */
197 default_get_sd, /* get_sd */
198 default_set_sd, /* set_sd */
199 no_lookup_name, /* lookup_name */
200 no_open_file, /* open_file */
201 fd_close_handle, /* close_handle */
202 pipe_client_destroy /* destroy */
205 static const struct fd_ops pipe_client_fd_ops =
207 default_fd_get_poll_events, /* get_poll_events */
208 default_poll_event, /* poll_event */
209 pipe_client_flush, /* flush */
210 pipe_client_get_fd_type, /* get_fd_type */
211 default_fd_ioctl, /* ioctl */
212 default_fd_queue_async, /* queue_async */
213 default_fd_reselect_async, /* reselect_async */
214 default_fd_cancel_async /* cancel_async */
217 static void named_pipe_device_dump( struct object *obj, int verbose );
218 static struct object_type *named_pipe_device_get_type( struct object *obj );
219 static struct fd *named_pipe_device_get_fd( struct object *obj );
220 static struct object *named_pipe_device_lookup_name( struct object *obj,
221 struct unicode_str *name, unsigned int attr );
222 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
223 unsigned int sharing, unsigned int options );
224 static void named_pipe_device_destroy( struct object *obj );
225 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
226 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
227 int blocking, const void *data, data_size_t size );
229 static const struct object_ops named_pipe_device_ops =
231 sizeof(struct named_pipe_device), /* size */
232 named_pipe_device_dump, /* dump */
233 named_pipe_device_get_type, /* get_type */
234 no_add_queue, /* add_queue */
235 NULL, /* remove_queue */
236 NULL, /* signaled */
237 no_satisfied, /* satisfied */
238 no_signal, /* signal */
239 named_pipe_device_get_fd, /* get_fd */
240 no_map_access, /* map_access */
241 default_get_sd, /* get_sd */
242 default_set_sd, /* set_sd */
243 named_pipe_device_lookup_name, /* lookup_name */
244 named_pipe_device_open_file, /* open_file */
245 fd_close_handle, /* close_handle */
246 named_pipe_device_destroy /* destroy */
249 static const struct fd_ops named_pipe_device_fd_ops =
251 default_fd_get_poll_events, /* get_poll_events */
252 default_poll_event, /* poll_event */
253 no_flush, /* flush */
254 named_pipe_device_get_fd_type, /* get_fd_type */
255 named_pipe_device_ioctl, /* ioctl */
256 default_fd_queue_async, /* queue_async */
257 default_fd_reselect_async, /* reselect_async */
258 default_fd_cancel_async /* cancel_async */
261 static void named_pipe_dump( struct object *obj, int verbose )
263 struct named_pipe *pipe = (struct named_pipe *) obj;
264 assert( obj->ops == &named_pipe_ops );
265 fprintf( stderr, "Named pipe " );
266 dump_object_name( &pipe->obj );
267 fprintf( stderr, "\n" );
270 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
272 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
273 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
274 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
275 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
276 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
279 static void pipe_server_dump( struct object *obj, int verbose )
281 struct pipe_server *server = (struct pipe_server *) obj;
282 assert( obj->ops == &pipe_server_ops );
283 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
286 static void pipe_client_dump( struct object *obj, int verbose )
288 struct pipe_client *client = (struct pipe_client *) obj;
289 assert( obj->ops == &pipe_client_ops );
290 fprintf( stderr, "Named pipe client server=%p\n", client->server );
293 static int pipe_client_signaled( struct object *obj, struct thread *thread )
295 struct pipe_client *client = (struct pipe_client *) obj;
297 return client->fd && is_fd_signaled(client->fd);
300 static void named_pipe_destroy( struct object *obj)
302 struct named_pipe *pipe = (struct named_pipe *) obj;
304 assert( list_empty( &pipe->servers ) );
305 assert( !pipe->instances );
306 free_async_queue( pipe->waiters );
309 static struct fd *pipe_client_get_fd( struct object *obj )
311 struct pipe_client *client = (struct pipe_client *) obj;
312 if (client->fd)
313 return (struct fd *) grab_object( client->fd );
314 set_error( STATUS_PIPE_DISCONNECTED );
315 return NULL;
318 static void set_server_state( struct pipe_server *server, enum pipe_state state )
320 server->state = state;
322 switch(state)
324 case ps_connected_server:
325 case ps_wait_disconnect:
326 assert( server->fd );
327 break;
328 case ps_wait_open:
329 case ps_idle_server:
330 assert( !server->fd );
331 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
332 break;
333 case ps_disconnected_server:
334 case ps_wait_connect:
335 assert( !server->fd );
336 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
337 break;
341 static struct fd *pipe_server_get_fd( struct object *obj )
343 struct pipe_server *server = (struct pipe_server *) obj;
345 return (struct fd *)grab_object( server->fd ? server->fd : server->ioctl_fd );
349 static void notify_empty( struct pipe_server *server )
351 if (!server->flush_poll)
352 return;
353 assert( server->state == ps_connected_server );
354 assert( server->event );
355 remove_timeout_user( server->flush_poll );
356 server->flush_poll = NULL;
357 set_event( server->event );
358 release_object( server->event );
359 server->event = NULL;
362 static void do_disconnect( struct pipe_server *server )
364 /* we may only have a server fd, if the client disconnected */
365 if (server->client)
367 assert( server->client->server == server );
368 assert( server->client->fd );
369 release_object( server->client->fd );
370 server->client->fd = NULL;
372 assert( server->fd );
373 shutdown( get_unix_fd( server->fd ), SHUT_RDWR );
374 release_object( server->fd );
375 server->fd = NULL;
378 static void pipe_server_destroy( struct object *obj)
380 struct pipe_server *server = (struct pipe_server *)obj;
382 assert( obj->ops == &pipe_server_ops );
384 if (server->fd)
386 notify_empty( server );
387 do_disconnect( server );
390 if (server->client)
392 server->client->server = NULL;
393 server->client = NULL;
396 assert( server->pipe->instances );
397 server->pipe->instances--;
399 if (server->ioctl_fd) release_object( server->ioctl_fd );
400 list_remove( &server->entry );
401 release_object( server->pipe );
404 static void pipe_client_destroy( struct object *obj)
406 struct pipe_client *client = (struct pipe_client *)obj;
407 struct pipe_server *server = client->server;
409 assert( obj->ops == &pipe_client_ops );
411 if (server)
413 notify_empty( server );
415 switch(server->state)
417 case ps_connected_server:
418 /* Don't destroy the server's fd here as we can't
419 do a successful flush without it. */
420 set_server_state( server, ps_wait_disconnect );
421 break;
422 case ps_disconnected_server:
423 set_server_state( server, ps_wait_connect );
424 break;
425 case ps_idle_server:
426 case ps_wait_open:
427 case ps_wait_disconnect:
428 case ps_wait_connect:
429 assert( 0 );
431 assert( server->client );
432 server->client = NULL;
433 client->server = NULL;
435 if (client->fd) release_object( client->fd );
438 static void named_pipe_device_dump( struct object *obj, int verbose )
440 assert( obj->ops == &named_pipe_device_ops );
441 fprintf( stderr, "Named pipe device\n" );
444 static struct object_type *named_pipe_device_get_type( struct object *obj )
446 static const WCHAR name[] = {'D','e','v','i','c','e'};
447 static const struct unicode_str str = { name, sizeof(name) };
448 return get_object_type( &str );
451 static struct fd *named_pipe_device_get_fd( struct object *obj )
453 struct named_pipe_device *device = (struct named_pipe_device *)obj;
454 return (struct fd *)grab_object( device->fd );
457 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
458 unsigned int attr )
460 struct named_pipe_device *device = (struct named_pipe_device*)obj;
461 struct object *found;
463 assert( obj->ops == &named_pipe_device_ops );
464 assert( device->pipes );
466 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
467 name->len = 0;
469 return found;
472 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
473 unsigned int sharing, unsigned int options )
475 return grab_object( obj );
478 static void named_pipe_device_destroy( struct object *obj )
480 struct named_pipe_device *device = (struct named_pipe_device*)obj;
481 assert( obj->ops == &named_pipe_device_ops );
482 if (device->fd) release_object( device->fd );
483 free( device->pipes );
486 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
488 return FD_TYPE_DEVICE;
491 void create_named_pipe_device( struct directory *root, const struct unicode_str *name )
493 struct named_pipe_device *dev;
495 if ((dev = create_named_object_dir( root, name, 0, &named_pipe_device_ops )) &&
496 get_error() != STATUS_OBJECT_NAME_EXISTS)
498 dev->pipes = NULL;
499 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
500 !(dev->pipes = create_namespace( 7 )))
502 release_object( dev );
503 dev = NULL;
506 if (dev) make_object_static( &dev->obj );
509 static int pipe_data_remaining( struct pipe_server *server )
511 struct pollfd pfd;
512 int fd;
514 assert( server->client );
516 fd = get_unix_fd( server->client->fd );
517 if (fd < 0)
518 return 0;
519 pfd.fd = fd;
520 pfd.events = POLLIN;
521 pfd.revents = 0;
523 if (0 > poll( &pfd, 1, 0 ))
524 return 0;
526 return pfd.revents&POLLIN;
529 static void check_flushed( void *arg )
531 struct pipe_server *server = (struct pipe_server*) arg;
533 assert( server->event );
534 if (pipe_data_remaining( server ))
536 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
538 else
540 /* notify_empty( server ); */
541 server->flush_poll = NULL;
542 set_event( server->event );
543 release_object( server->event );
544 server->event = NULL;
548 static void pipe_server_flush( struct fd *fd, struct event **event )
550 struct pipe_server *server = get_fd_user( fd );
552 if (!server || server->state != ps_connected_server) return;
554 /* FIXME: if multiple threads flush the same pipe,
555 maybe should create a list of processes to notify */
556 if (server->flush_poll) return;
558 if (pipe_data_remaining( server ))
560 /* this kind of sux -
561 there's no unix way to be alerted when a pipe becomes empty */
562 server->event = create_event( NULL, NULL, 0, 0, 0, NULL );
563 if (!server->event) return;
564 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
565 *event = server->event;
569 static void pipe_client_flush( struct fd *fd, struct event **event )
571 /* FIXME: what do we have to do for this? */
574 static inline int is_overlapped( unsigned int options )
576 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
579 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
581 return FD_TYPE_PIPE;
584 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
586 return FD_TYPE_PIPE;
589 static obj_handle_t alloc_wait_event( struct process *process )
591 obj_handle_t handle = 0;
592 struct event *event = create_event( NULL, NULL, 0, 1, 0, NULL );
594 if (event)
596 handle = alloc_handle( process, event, EVENT_ALL_ACCESS, 0 );
597 release_object( event );
599 return handle;
602 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
603 int blocking, const void *data, data_size_t size )
605 struct pipe_server *server = get_fd_user( fd );
606 struct async *async;
607 obj_handle_t wait_handle = 0;
609 switch(code)
611 case FSCTL_PIPE_LISTEN:
612 switch(server->state)
614 case ps_idle_server:
615 case ps_wait_connect:
616 if (blocking && !is_overlapped( get_fd_options(fd) ))
618 async_data_t new_data = *async_data;
619 if (!(wait_handle = alloc_wait_event( current->process ))) break;
620 new_data.event = wait_handle;
621 if (!(async = fd_queue_async( server->ioctl_fd, &new_data, ASYNC_TYPE_WAIT )))
623 close_handle( current->process, wait_handle );
624 break;
627 else async = fd_queue_async( server->ioctl_fd, async_data, ASYNC_TYPE_WAIT );
629 if (async)
631 set_server_state( server, ps_wait_open );
632 if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
633 release_object( async );
634 set_error( STATUS_PENDING );
635 return wait_handle;
637 break;
638 case ps_connected_server:
639 set_error( STATUS_PIPE_CONNECTED );
640 break;
641 case ps_disconnected_server:
642 set_error( STATUS_PIPE_BUSY );
643 break;
644 case ps_wait_disconnect:
645 set_error( STATUS_NO_DATA_DETECTED );
646 break;
647 case ps_wait_open:
648 set_error( STATUS_INVALID_HANDLE );
649 break;
651 return 0;
653 case FSCTL_PIPE_DISCONNECT:
654 switch(server->state)
656 case ps_connected_server:
657 assert( server->client );
658 assert( server->client->fd );
660 notify_empty( server );
662 /* dump the client and server fds, but keep the pointers
663 around - client loses all waiting data */
664 do_disconnect( server );
665 set_server_state( server, ps_disconnected_server );
666 break;
667 case ps_wait_disconnect:
668 assert( !server->client );
669 do_disconnect( server );
670 set_server_state( server, ps_wait_connect );
671 break;
672 case ps_idle_server:
673 case ps_wait_open:
674 set_error( STATUS_PIPE_LISTENING );
675 break;
676 case ps_disconnected_server:
677 case ps_wait_connect:
678 set_error( STATUS_PIPE_DISCONNECTED );
679 break;
681 return 0;
683 default:
684 return default_fd_ioctl( fd, code, async_data, blocking, data, size );
688 static struct named_pipe *create_named_pipe( struct directory *root, const struct unicode_str *name,
689 unsigned int attr )
691 struct object *obj;
692 struct named_pipe *pipe = NULL;
693 struct unicode_str new_name;
695 if (!name || !name->len) return alloc_object( &named_pipe_ops );
697 if (!(obj = find_object_dir( root, name, attr, &new_name )))
699 set_error( STATUS_OBJECT_NAME_INVALID );
700 return NULL;
702 if (!new_name.len)
704 if (attr & OBJ_OPENIF && obj->ops == &named_pipe_ops)
705 set_error( STATUS_OBJECT_NAME_EXISTS );
706 else
708 release_object( obj );
709 obj = NULL;
710 if (attr & OBJ_OPENIF)
711 set_error( STATUS_OBJECT_TYPE_MISMATCH );
712 else
713 set_error( STATUS_OBJECT_NAME_COLLISION );
715 return (struct named_pipe *)obj;
718 if (obj->ops != &named_pipe_device_ops)
719 set_error( STATUS_OBJECT_NAME_INVALID );
720 else
722 struct named_pipe_device *dev = (struct named_pipe_device *)obj;
723 if ((pipe = create_object( dev->pipes, &named_pipe_ops, &new_name, NULL )))
724 clear_error();
727 release_object( obj );
728 return pipe;
731 static struct pipe_server *get_pipe_server_obj( struct process *process,
732 obj_handle_t handle, unsigned int access )
734 struct object *obj;
735 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
736 return (struct pipe_server *) obj;
739 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options )
741 struct pipe_server *server;
743 server = alloc_object( &pipe_server_ops );
744 if (!server)
745 return NULL;
747 server->fd = NULL;
748 server->pipe = pipe;
749 server->client = NULL;
750 server->flush_poll = NULL;
751 server->options = options;
753 list_add_head( &pipe->servers, &server->entry );
754 grab_object( pipe );
755 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->obj, options )))
757 release_object( server );
758 return NULL;
760 set_server_state( server, ps_idle_server );
761 return server;
764 static struct pipe_client *create_pipe_client( unsigned int flags )
766 struct pipe_client *client;
768 client = alloc_object( &pipe_client_ops );
769 if (!client)
770 return NULL;
772 client->fd = NULL;
773 client->server = NULL;
774 client->flags = flags;
776 return client;
779 static struct pipe_server *find_available_server( struct named_pipe *pipe )
781 struct pipe_server *server;
783 /* look for pipe servers that are listening */
784 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
786 if (server->state == ps_wait_open)
787 return (struct pipe_server *)grab_object( server );
790 /* fall back to pipe servers that are idle */
791 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
793 if (server->state == ps_idle_server)
794 return (struct pipe_server *)grab_object( server );
797 return NULL;
800 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
801 unsigned int sharing, unsigned int options )
803 struct named_pipe *pipe = (struct named_pipe *)obj;
804 struct pipe_server *server;
805 struct pipe_client *client;
806 int fds[2];
808 if (!(server = find_available_server( pipe )))
810 set_error( STATUS_PIPE_NOT_AVAILABLE );
811 return NULL;
814 if ((client = create_pipe_client( options )))
816 if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
818 assert( !server->fd );
820 /* for performance reasons, only set nonblocking mode when using
821 * overlapped I/O. Otherwise, we will be doing too much busy
822 * looping */
823 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
824 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
826 if (pipe->insize)
828 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
829 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
831 if (pipe->outsize)
833 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
834 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
837 client->fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->obj, options );
838 server->fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->obj, server->options );
839 if (client->fd && server->fd)
841 fd_copy_completion( server->ioctl_fd, server->fd );
842 if (server->state == ps_wait_open)
843 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
844 set_server_state( server, ps_connected_server );
845 server->client = client;
846 client->server = server;
848 else
850 release_object( client );
851 client = NULL;
854 else
856 file_set_error();
857 release_object( client );
858 client = NULL;
861 release_object( server );
862 return &client->obj;
865 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
866 int blocking, const void *data, data_size_t size )
868 struct named_pipe_device *device = get_fd_user( fd );
870 switch(code)
872 case FSCTL_PIPE_WAIT:
874 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = data;
875 obj_handle_t wait_handle = 0;
876 struct named_pipe *pipe;
877 struct pipe_server *server;
878 struct unicode_str name;
880 if (size < sizeof(*buffer) ||
881 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
883 set_error( STATUS_INVALID_PARAMETER );
884 return 0;
886 name.str = buffer->Name;
887 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
888 if (!(pipe = (struct named_pipe *)find_object( device->pipes, &name, OBJ_CASE_INSENSITIVE )))
890 set_error( STATUS_PIPE_NOT_AVAILABLE );
891 return 0;
893 if (!(server = find_available_server( pipe )))
895 struct async *async;
897 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
899 if (blocking)
901 async_data_t new_data = *async_data;
902 if (!(wait_handle = alloc_wait_event( current->process ))) goto done;
903 new_data.event = wait_handle;
904 if (!(async = create_async( current, pipe->waiters, &new_data )))
906 close_handle( current->process, wait_handle );
907 wait_handle = 0;
910 else async = create_async( current, pipe->waiters, async_data );
912 if (async)
914 timeout_t when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
915 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
916 release_object( async );
917 set_error( STATUS_PENDING );
920 else release_object( server );
922 done:
923 release_object( pipe );
924 return wait_handle;
927 default:
928 return default_fd_ioctl( fd, code, async_data, blocking, data, size );
933 DECL_HANDLER(create_named_pipe)
935 struct named_pipe *pipe;
936 struct pipe_server *server;
937 struct unicode_str name;
938 struct directory *root = NULL;
940 reply->handle = 0;
941 get_req_unicode_str( &name );
942 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
943 return;
945 pipe = create_named_pipe( root, &name, req->attributes | OBJ_OPENIF );
947 if (root) release_object( root );
948 if (!pipe) return;
950 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
952 /* initialize it if it didn't already exist */
953 pipe->instances = 0;
954 pipe->waiters = NULL;
955 list_init( &pipe->servers );
956 pipe->insize = req->insize;
957 pipe->outsize = req->outsize;
958 pipe->maxinstances = req->maxinstances;
959 pipe->timeout = req->timeout;
960 pipe->flags = req->flags;
962 else
964 if (pipe->maxinstances <= pipe->instances)
966 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
967 release_object( pipe );
968 return;
970 if ((pipe->maxinstances != req->maxinstances) ||
971 (pipe->timeout != req->timeout) ||
972 (pipe->flags != req->flags))
974 set_error( STATUS_ACCESS_DENIED );
975 release_object( pipe );
976 return;
978 clear_error(); /* clear the name collision */
981 server = create_pipe_server( pipe, req->options );
982 if (server)
984 reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
985 server->pipe->instances++;
986 release_object( server );
989 release_object( pipe );
992 DECL_HANDLER(get_named_pipe_info)
994 struct pipe_server *server;
995 struct pipe_client *client = NULL;
997 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
998 if (!server)
1000 clear_error();
1001 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1002 FILE_READ_ATTRIBUTES, &pipe_client_ops );
1003 if (!client) return;
1004 server = client->server;
1007 reply->flags = server->pipe->flags;
1008 reply->maxinstances = server->pipe->maxinstances;
1009 reply->instances = server->pipe->instances;
1010 reply->insize = server->pipe->insize;
1011 reply->outsize = server->pipe->outsize;
1013 if (client)
1014 release_object(client);
1015 else
1017 reply->flags |= NAMED_PIPE_SERVER_END;
1018 release_object(server);