webservices: Implement WsCreateXmlBuffer.
[wine.git] / server / named_pipe.c
blobf5e536c7cba8a62919f069ae322a0f3d8f54383b
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"
55 #include "security.h"
57 enum pipe_state
59 ps_idle_server,
60 ps_wait_open,
61 ps_connected_server,
62 ps_wait_disconnect,
63 ps_disconnected_server,
64 ps_wait_connect
67 struct named_pipe;
69 struct pipe_server
71 struct object obj; /* object header */
72 struct fd *fd; /* pipe file descriptor */
73 struct fd *ioctl_fd; /* file descriptor for ioctls when not connected */
74 struct list entry; /* entry in named pipe servers list */
75 enum pipe_state state; /* server state */
76 struct pipe_client *client; /* client that this server is connected to */
77 struct named_pipe *pipe;
78 struct timeout_user *flush_poll;
79 unsigned int options; /* pipe options */
80 unsigned int pipe_flags;
83 struct pipe_client
85 struct object obj; /* object header */
86 struct fd *fd; /* pipe file descriptor */
87 struct pipe_server *server; /* server that this client is connected to */
88 unsigned int flags; /* file flags */
89 unsigned int pipe_flags;
92 struct named_pipe
94 struct object obj; /* object header */
95 unsigned int flags;
96 unsigned int sharing;
97 unsigned int maxinstances;
98 unsigned int outsize;
99 unsigned int insize;
100 unsigned int instances;
101 timeout_t timeout;
102 struct list servers; /* list of servers using this pipe */
103 struct async_queue *waiters; /* list of clients waiting to connect */
106 struct named_pipe_device
108 struct object obj; /* object header */
109 struct fd *fd; /* pseudo-fd for ioctls */
110 struct namespace *pipes; /* named pipe namespace */
113 static void named_pipe_dump( struct object *obj, int verbose );
114 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
115 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
116 unsigned int sharing, unsigned int options );
117 static void named_pipe_destroy( struct object *obj );
119 static const struct object_ops named_pipe_ops =
121 sizeof(struct named_pipe), /* size */
122 named_pipe_dump, /* dump */
123 no_get_type, /* get_type */
124 no_add_queue, /* add_queue */
125 NULL, /* remove_queue */
126 NULL, /* signaled */
127 NULL, /* satisfied */
128 no_signal, /* signal */
129 no_get_fd, /* get_fd */
130 named_pipe_map_access, /* map_access */
131 default_get_sd, /* get_sd */
132 default_set_sd, /* set_sd */
133 no_lookup_name, /* lookup_name */
134 named_pipe_open_file, /* open_file */
135 no_close_handle, /* close_handle */
136 named_pipe_destroy /* destroy */
139 /* server end functions */
140 static void pipe_server_dump( struct object *obj, int verbose );
141 static struct fd *pipe_server_get_fd( struct object *obj );
142 static void pipe_server_destroy( struct object *obj);
143 static obj_handle_t pipe_server_flush( struct fd *fd, const async_data_t *async, int blocking );
144 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd );
145 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
146 int blocking );
148 static const struct object_ops pipe_server_ops =
150 sizeof(struct pipe_server), /* size */
151 pipe_server_dump, /* dump */
152 no_get_type, /* get_type */
153 add_queue, /* add_queue */
154 remove_queue, /* remove_queue */
155 default_fd_signaled, /* signaled */
156 no_satisfied, /* satisfied */
157 no_signal, /* signal */
158 pipe_server_get_fd, /* get_fd */
159 default_fd_map_access, /* map_access */
160 default_get_sd, /* get_sd */
161 default_set_sd, /* set_sd */
162 no_lookup_name, /* lookup_name */
163 no_open_file, /* open_file */
164 fd_close_handle, /* close_handle */
165 pipe_server_destroy /* destroy */
168 static const struct fd_ops pipe_server_fd_ops =
170 default_fd_get_poll_events, /* get_poll_events */
171 default_poll_event, /* poll_event */
172 pipe_server_get_fd_type, /* get_fd_type */
173 no_fd_read, /* read */
174 no_fd_write, /* write */
175 pipe_server_flush, /* flush */
176 pipe_server_ioctl, /* ioctl */
177 default_fd_queue_async, /* queue_async */
178 default_fd_reselect_async, /* reselect_async */
179 default_fd_cancel_async, /* cancel_async */
182 /* client end functions */
183 static void pipe_client_dump( struct object *obj, int verbose );
184 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry );
185 static struct fd *pipe_client_get_fd( struct object *obj );
186 static void pipe_client_destroy( struct object *obj );
187 static obj_handle_t pipe_client_flush( struct fd *fd, const async_data_t *async, int blocking );
188 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd );
190 static const struct object_ops pipe_client_ops =
192 sizeof(struct pipe_client), /* size */
193 pipe_client_dump, /* dump */
194 no_get_type, /* get_type */
195 add_queue, /* add_queue */
196 remove_queue, /* remove_queue */
197 pipe_client_signaled, /* signaled */
198 no_satisfied, /* satisfied */
199 no_signal, /* signal */
200 pipe_client_get_fd, /* get_fd */
201 default_fd_map_access, /* map_access */
202 default_get_sd, /* get_sd */
203 default_set_sd, /* set_sd */
204 no_lookup_name, /* lookup_name */
205 no_open_file, /* open_file */
206 fd_close_handle, /* close_handle */
207 pipe_client_destroy /* destroy */
210 static const struct fd_ops pipe_client_fd_ops =
212 default_fd_get_poll_events, /* get_poll_events */
213 default_poll_event, /* poll_event */
214 pipe_client_get_fd_type, /* get_fd_type */
215 no_fd_read, /* read */
216 no_fd_write, /* write */
217 pipe_client_flush, /* flush */
218 default_fd_ioctl, /* ioctl */
219 default_fd_queue_async, /* queue_async */
220 default_fd_reselect_async, /* reselect_async */
221 default_fd_cancel_async /* cancel_async */
224 static void named_pipe_device_dump( struct object *obj, int verbose );
225 static struct object_type *named_pipe_device_get_type( struct object *obj );
226 static struct fd *named_pipe_device_get_fd( struct object *obj );
227 static struct object *named_pipe_device_lookup_name( struct object *obj,
228 struct unicode_str *name, unsigned int attr );
229 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
230 unsigned int sharing, unsigned int options );
231 static void named_pipe_device_destroy( struct object *obj );
232 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
233 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code,
234 const async_data_t *async_data, int blocking );
236 static const struct object_ops named_pipe_device_ops =
238 sizeof(struct named_pipe_device), /* size */
239 named_pipe_device_dump, /* dump */
240 named_pipe_device_get_type, /* get_type */
241 no_add_queue, /* add_queue */
242 NULL, /* remove_queue */
243 NULL, /* signaled */
244 no_satisfied, /* satisfied */
245 no_signal, /* signal */
246 named_pipe_device_get_fd, /* get_fd */
247 no_map_access, /* map_access */
248 default_get_sd, /* get_sd */
249 default_set_sd, /* set_sd */
250 named_pipe_device_lookup_name, /* lookup_name */
251 named_pipe_device_open_file, /* open_file */
252 fd_close_handle, /* close_handle */
253 named_pipe_device_destroy /* destroy */
256 static const struct fd_ops named_pipe_device_fd_ops =
258 default_fd_get_poll_events, /* get_poll_events */
259 default_poll_event, /* poll_event */
260 named_pipe_device_get_fd_type, /* get_fd_type */
261 no_fd_read, /* read */
262 no_fd_write, /* write */
263 no_fd_flush, /* flush */
264 named_pipe_device_ioctl, /* ioctl */
265 default_fd_queue_async, /* queue_async */
266 default_fd_reselect_async, /* reselect_async */
267 default_fd_cancel_async /* cancel_async */
270 static void named_pipe_dump( struct object *obj, int verbose )
272 struct named_pipe *pipe = (struct named_pipe *) obj;
273 assert( obj->ops == &named_pipe_ops );
274 fprintf( stderr, "Named pipe " );
275 dump_object_name( &pipe->obj );
276 fprintf( stderr, "\n" );
279 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
281 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
282 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
283 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
284 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
285 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
288 static void pipe_server_dump( struct object *obj, int verbose )
290 struct pipe_server *server = (struct pipe_server *) obj;
291 assert( obj->ops == &pipe_server_ops );
292 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
295 static void pipe_client_dump( struct object *obj, int verbose )
297 struct pipe_client *client = (struct pipe_client *) obj;
298 assert( obj->ops == &pipe_client_ops );
299 fprintf( stderr, "Named pipe client server=%p\n", client->server );
302 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry )
304 struct pipe_client *client = (struct pipe_client *) obj;
306 return client->fd && is_fd_signaled(client->fd);
309 static void named_pipe_destroy( struct object *obj)
311 struct named_pipe *pipe = (struct named_pipe *) obj;
313 assert( list_empty( &pipe->servers ) );
314 assert( !pipe->instances );
315 free_async_queue( pipe->waiters );
318 static struct fd *pipe_client_get_fd( struct object *obj )
320 struct pipe_client *client = (struct pipe_client *) obj;
321 if (client->fd)
322 return (struct fd *) grab_object( client->fd );
323 set_error( STATUS_PIPE_DISCONNECTED );
324 return NULL;
327 static void set_server_state( struct pipe_server *server, enum pipe_state state )
329 server->state = state;
331 switch(state)
333 case ps_connected_server:
334 case ps_wait_disconnect:
335 assert( server->fd );
336 break;
337 case ps_wait_open:
338 case ps_idle_server:
339 assert( !server->fd );
340 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
341 break;
342 case ps_disconnected_server:
343 case ps_wait_connect:
344 assert( !server->fd );
345 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
346 break;
350 static struct fd *pipe_server_get_fd( struct object *obj )
352 struct pipe_server *server = (struct pipe_server *) obj;
354 return (struct fd *)grab_object( server->fd ? server->fd : server->ioctl_fd );
358 static void notify_empty( struct pipe_server *server )
360 if (!server->flush_poll)
361 return;
362 assert( server->state == ps_connected_server );
363 remove_timeout_user( server->flush_poll );
364 server->flush_poll = NULL;
365 fd_async_wake_up( server->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
368 static void do_disconnect( struct pipe_server *server )
370 /* we may only have a server fd, if the client disconnected */
371 if (server->client)
373 assert( server->client->server == server );
374 assert( server->client->fd );
375 release_object( server->client->fd );
376 server->client->fd = NULL;
378 assert( server->fd );
379 shutdown( get_unix_fd( server->fd ), SHUT_RDWR );
380 release_object( server->fd );
381 server->fd = NULL;
384 static void pipe_server_destroy( struct object *obj)
386 struct pipe_server *server = (struct pipe_server *)obj;
388 assert( obj->ops == &pipe_server_ops );
390 if (server->fd)
392 notify_empty( server );
393 do_disconnect( server );
396 if (server->client)
398 server->client->server = NULL;
399 server->client = NULL;
402 assert( server->pipe->instances );
403 server->pipe->instances--;
405 if (server->ioctl_fd) release_object( server->ioctl_fd );
406 list_remove( &server->entry );
407 release_object( server->pipe );
410 static void pipe_client_destroy( struct object *obj)
412 struct pipe_client *client = (struct pipe_client *)obj;
413 struct pipe_server *server = client->server;
415 assert( obj->ops == &pipe_client_ops );
417 if (server)
419 notify_empty( server );
421 switch(server->state)
423 case ps_connected_server:
424 /* Don't destroy the server's fd here as we can't
425 do a successful flush without it. */
426 set_server_state( server, ps_wait_disconnect );
427 break;
428 case ps_disconnected_server:
429 set_server_state( server, ps_wait_connect );
430 break;
431 case ps_idle_server:
432 case ps_wait_open:
433 case ps_wait_disconnect:
434 case ps_wait_connect:
435 assert( 0 );
437 assert( server->client );
438 server->client = NULL;
439 client->server = NULL;
441 if (client->fd) release_object( client->fd );
444 static void named_pipe_device_dump( struct object *obj, int verbose )
446 assert( obj->ops == &named_pipe_device_ops );
447 fprintf( stderr, "Named pipe device\n" );
450 static struct object_type *named_pipe_device_get_type( struct object *obj )
452 static const WCHAR name[] = {'D','e','v','i','c','e'};
453 static const struct unicode_str str = { name, sizeof(name) };
454 return get_object_type( &str );
457 static struct fd *named_pipe_device_get_fd( struct object *obj )
459 struct named_pipe_device *device = (struct named_pipe_device *)obj;
460 return (struct fd *)grab_object( device->fd );
463 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
464 unsigned int attr )
466 struct named_pipe_device *device = (struct named_pipe_device*)obj;
467 struct object *found;
469 assert( obj->ops == &named_pipe_device_ops );
470 assert( device->pipes );
472 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
473 name->len = 0;
475 return found;
478 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
479 unsigned int sharing, unsigned int options )
481 return grab_object( obj );
484 static void named_pipe_device_destroy( struct object *obj )
486 struct named_pipe_device *device = (struct named_pipe_device*)obj;
487 assert( obj->ops == &named_pipe_device_ops );
488 if (device->fd) release_object( device->fd );
489 free( device->pipes );
492 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
494 return FD_TYPE_DEVICE;
497 void create_named_pipe_device( struct directory *root, const struct unicode_str *name )
499 struct named_pipe_device *dev;
501 if ((dev = create_named_object_dir( root, name, 0, &named_pipe_device_ops )) &&
502 get_error() != STATUS_OBJECT_NAME_EXISTS)
504 dev->pipes = NULL;
505 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
506 !(dev->pipes = create_namespace( 7 )))
508 release_object( dev );
509 dev = NULL;
512 if (dev) make_object_static( &dev->obj );
515 static int pipe_data_remaining( struct pipe_server *server )
517 struct pollfd pfd;
518 int fd;
520 assert( server->client );
522 fd = get_unix_fd( server->client->fd );
523 if (fd < 0)
524 return 0;
525 pfd.fd = fd;
526 pfd.events = POLLIN;
527 pfd.revents = 0;
529 if (0 > poll( &pfd, 1, 0 ))
530 return 0;
532 return pfd.revents&POLLIN;
535 static void check_flushed( void *arg )
537 struct pipe_server *server = (struct pipe_server*) arg;
539 if (pipe_data_remaining( server ))
541 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
543 else
545 server->flush_poll = NULL;
546 fd_async_wake_up( server->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
550 static obj_handle_t pipe_server_flush( struct fd *fd, const async_data_t *async_data, int blocking )
552 struct pipe_server *server = get_fd_user( fd );
553 obj_handle_t handle = 0;
554 struct async *async;
556 if (!server || server->state != ps_connected_server) return 0;
558 if (!pipe_data_remaining( server )) return 0;
560 if ((async = fd_queue_async( server->fd, async_data, ASYNC_TYPE_WAIT )))
562 /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
563 if (!server->flush_poll)
564 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
565 if (blocking) handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
566 release_object( async );
567 set_error( STATUS_PENDING );
569 return handle;
572 static obj_handle_t pipe_client_flush( struct fd *fd, const async_data_t *async, int blocking )
574 /* FIXME: what do we have to do for this? */
575 return 0;
578 static inline int is_overlapped( unsigned int options )
580 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
583 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
585 return FD_TYPE_PIPE;
588 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
590 return FD_TYPE_PIPE;
593 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
594 int blocking )
596 struct pipe_server *server = get_fd_user( fd );
597 struct async *async;
598 obj_handle_t wait_handle = 0;
600 switch(code)
602 case FSCTL_PIPE_LISTEN:
603 switch(server->state)
605 case ps_idle_server:
606 case ps_wait_connect:
607 if ((async = fd_queue_async( server->ioctl_fd, async_data, ASYNC_TYPE_WAIT )))
609 if (blocking) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
610 set_server_state( server, ps_wait_open );
611 if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
612 release_object( async );
613 set_error( STATUS_PENDING );
614 return wait_handle;
616 break;
617 case ps_connected_server:
618 set_error( STATUS_PIPE_CONNECTED );
619 break;
620 case ps_disconnected_server:
621 set_error( STATUS_PIPE_BUSY );
622 break;
623 case ps_wait_disconnect:
624 set_error( STATUS_NO_DATA_DETECTED );
625 break;
626 case ps_wait_open:
627 set_error( STATUS_INVALID_HANDLE );
628 break;
630 return 0;
632 case FSCTL_PIPE_DISCONNECT:
633 switch(server->state)
635 case ps_connected_server:
636 assert( server->client );
637 assert( server->client->fd );
639 notify_empty( server );
641 /* dump the client and server fds, but keep the pointers
642 around - client loses all waiting data */
643 do_disconnect( server );
644 set_server_state( server, ps_disconnected_server );
645 break;
646 case ps_wait_disconnect:
647 assert( !server->client );
648 do_disconnect( server );
649 set_server_state( server, ps_wait_connect );
650 break;
651 case ps_idle_server:
652 case ps_wait_open:
653 set_error( STATUS_PIPE_LISTENING );
654 break;
655 case ps_disconnected_server:
656 case ps_wait_connect:
657 set_error( STATUS_PIPE_DISCONNECTED );
658 break;
660 return 0;
662 default:
663 return default_fd_ioctl( fd, code, async_data, blocking );
667 static struct named_pipe *create_named_pipe( struct directory *root, const struct unicode_str *name,
668 unsigned int attr, const struct security_descriptor *sd )
670 struct object *obj;
671 struct named_pipe *pipe = NULL;
672 struct unicode_str new_name;
674 if (!name || !name->len) return alloc_object( &named_pipe_ops );
676 if (!(obj = find_object_dir( root, name, attr, &new_name )))
678 set_error( STATUS_OBJECT_NAME_INVALID );
679 return NULL;
681 if (!new_name.len)
683 if (attr & OBJ_OPENIF && obj->ops == &named_pipe_ops)
684 set_error( STATUS_OBJECT_NAME_EXISTS );
685 else
687 release_object( obj );
688 obj = NULL;
689 if (attr & OBJ_OPENIF)
690 set_error( STATUS_OBJECT_TYPE_MISMATCH );
691 else
692 set_error( STATUS_OBJECT_NAME_COLLISION );
694 return (struct named_pipe *)obj;
697 if (obj->ops != &named_pipe_device_ops)
698 set_error( STATUS_OBJECT_NAME_INVALID );
699 else
701 struct named_pipe_device *dev = (struct named_pipe_device *)obj;
702 if ((pipe = create_object( dev->pipes, &named_pipe_ops, &new_name, NULL )))
703 clear_error();
706 release_object( obj );
707 return pipe;
710 static struct pipe_server *get_pipe_server_obj( struct process *process,
711 obj_handle_t handle, unsigned int access )
713 struct object *obj;
714 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
715 return (struct pipe_server *) obj;
718 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
719 unsigned int pipe_flags )
721 struct pipe_server *server;
723 server = alloc_object( &pipe_server_ops );
724 if (!server)
725 return NULL;
727 server->fd = NULL;
728 server->pipe = pipe;
729 server->client = NULL;
730 server->flush_poll = NULL;
731 server->options = options;
732 server->pipe_flags = pipe_flags;
734 list_add_head( &pipe->servers, &server->entry );
735 grab_object( pipe );
736 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->obj, options )))
738 release_object( server );
739 return NULL;
741 set_server_state( server, ps_idle_server );
742 return server;
745 static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags )
747 struct pipe_client *client;
749 client = alloc_object( &pipe_client_ops );
750 if (!client)
751 return NULL;
753 client->fd = NULL;
754 client->server = NULL;
755 client->flags = flags;
756 client->pipe_flags = pipe_flags;
758 return client;
761 static struct pipe_server *find_available_server( struct named_pipe *pipe )
763 struct pipe_server *server;
765 /* look for pipe servers that are listening */
766 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
768 if (server->state == ps_wait_open)
769 return (struct pipe_server *)grab_object( server );
772 /* fall back to pipe servers that are idle */
773 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
775 if (server->state == ps_idle_server)
776 return (struct pipe_server *)grab_object( server );
779 return NULL;
782 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
783 unsigned int sharing, unsigned int options )
785 struct named_pipe *pipe = (struct named_pipe *)obj;
786 struct pipe_server *server;
787 struct pipe_client *client;
788 unsigned int pipe_sharing;
789 int fds[2];
791 if (!(server = find_available_server( pipe )))
793 set_error( STATUS_PIPE_NOT_AVAILABLE );
794 return NULL;
797 pipe_sharing = server->pipe->sharing;
798 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
799 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
801 set_error( STATUS_ACCESS_DENIED );
802 release_object( server );
803 return NULL;
806 if ((client = create_pipe_client( options, pipe->flags )))
808 if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
810 assert( !server->fd );
812 /* for performance reasons, only set nonblocking mode when using
813 * overlapped I/O. Otherwise, we will be doing too much busy
814 * looping */
815 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
816 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
818 if (pipe->insize)
820 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
821 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
823 if (pipe->outsize)
825 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
826 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
829 client->fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->obj, options );
830 server->fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->obj, server->options );
831 if (client->fd && server->fd)
833 allow_fd_caching( client->fd );
834 allow_fd_caching( server->fd );
835 fd_copy_completion( server->ioctl_fd, server->fd );
836 if (server->state == ps_wait_open)
837 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
838 set_server_state( server, ps_connected_server );
839 server->client = client;
840 client->server = server;
842 else
844 release_object( client );
845 client = NULL;
848 else
850 file_set_error();
851 release_object( client );
852 client = NULL;
855 release_object( server );
856 return &client->obj;
859 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code,
860 const async_data_t *async_data, int blocking )
862 struct named_pipe_device *device = get_fd_user( fd );
864 switch(code)
866 case FSCTL_PIPE_WAIT:
868 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
869 data_size_t size = get_req_data_size();
870 obj_handle_t wait_handle = 0;
871 struct named_pipe *pipe;
872 struct pipe_server *server;
873 struct unicode_str name;
875 if (size < sizeof(*buffer) ||
876 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
878 set_error( STATUS_INVALID_PARAMETER );
879 return 0;
881 name.str = buffer->Name;
882 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
883 if (!(pipe = (struct named_pipe *)find_object( device->pipes, &name, OBJ_CASE_INSENSITIVE )))
885 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
886 return 0;
888 if (!(server = find_available_server( pipe )))
890 struct async *async;
892 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
894 if ((async = create_async( current, pipe->waiters, async_data )))
896 timeout_t when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
897 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
898 if (blocking) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
899 release_object( async );
900 set_error( STATUS_PENDING );
903 else release_object( server );
905 done:
906 release_object( pipe );
907 return wait_handle;
910 default:
911 return default_fd_ioctl( fd, code, async_data, blocking );
916 DECL_HANDLER(create_named_pipe)
918 struct named_pipe *pipe;
919 struct pipe_server *server;
920 struct unicode_str name;
921 struct directory *root = NULL;
922 const struct object_attributes *objattr = get_req_data();
923 const struct security_descriptor *sd;
925 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
926 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
928 set_error( STATUS_INVALID_PARAMETER );
929 return;
932 reply->handle = 0;
934 if (!objattr_is_valid( objattr, get_req_data_size() ))
935 return;
937 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
938 objattr_get_name( objattr, &name );
940 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
941 return;
943 pipe = create_named_pipe( root, &name, req->attributes | OBJ_OPENIF, sd );
945 if (root) release_object( root );
946 if (!pipe) return;
948 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
950 /* initialize it if it didn't already exist */
951 pipe->instances = 0;
952 pipe->waiters = NULL;
953 list_init( &pipe->servers );
954 pipe->insize = req->insize;
955 pipe->outsize = req->outsize;
956 pipe->maxinstances = req->maxinstances;
957 pipe->timeout = req->timeout;
958 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
959 pipe->sharing = req->sharing;
961 else
963 if (pipe->maxinstances <= pipe->instances)
965 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
966 release_object( pipe );
967 return;
969 if (pipe->sharing != req->sharing)
971 set_error( STATUS_ACCESS_DENIED );
972 release_object( pipe );
973 return;
975 clear_error(); /* clear the name collision */
978 server = create_pipe_server( pipe, req->options, req->flags );
979 if (server)
981 reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
982 server->pipe->instances++;
983 if (sd) default_set_sd( &server->obj, sd, OWNER_SECURITY_INFORMATION |
984 GROUP_SECURITY_INFORMATION |
985 DACL_SECURITY_INFORMATION |
986 SACL_SECURITY_INFORMATION );
987 release_object( server );
990 release_object( pipe );
993 DECL_HANDLER(get_named_pipe_info)
995 struct pipe_server *server;
996 struct pipe_client *client = NULL;
998 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
999 if (!server)
1001 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1002 return;
1004 clear_error();
1005 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1006 0, &pipe_client_ops );
1007 if (!client) return;
1008 server = client->server;
1011 reply->flags = client ? client->pipe_flags : server->pipe_flags;
1012 reply->sharing = server->pipe->sharing;
1013 reply->maxinstances = server->pipe->maxinstances;
1014 reply->instances = server->pipe->instances;
1015 reply->insize = server->pipe->insize;
1016 reply->outsize = server->pipe->outsize;
1018 if (client)
1019 release_object(client);
1020 else
1022 reply->flags |= NAMED_PIPE_SERVER_END;
1023 release_object(server);
1027 DECL_HANDLER(set_named_pipe_info)
1029 struct pipe_server *server;
1030 struct pipe_client *client = NULL;
1032 server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
1033 if (!server)
1035 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1036 return;
1038 clear_error();
1039 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1040 0, &pipe_client_ops );
1041 if (!client) return;
1042 server = client->server;
1045 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1046 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1048 set_error( STATUS_INVALID_PARAMETER );
1050 else if (client)
1052 client->pipe_flags = server->pipe->flags | req->flags;
1054 else
1056 server->pipe_flags = server->pipe->flags | req->flags;
1059 if (client)
1060 release_object(client);
1061 else
1062 release_object(server);