d3d11: Implement d3d11_immediate_context_SetPredication().
[wine.git] / server / named_pipe.c
blobfbabc5a53bc39f633b52580291d0c4022b15dd13
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)
676 if ((pipe = alloc_object( &named_pipe_ops ))) clear_error();
677 return pipe;
680 if (!(obj = find_object_dir( root, name, attr, &new_name )))
682 set_error( STATUS_OBJECT_NAME_INVALID );
683 return NULL;
685 if (!new_name.len)
687 if (attr & OBJ_OPENIF && obj->ops == &named_pipe_ops)
688 set_error( STATUS_OBJECT_NAME_EXISTS );
689 else
691 release_object( obj );
692 obj = NULL;
693 if (attr & OBJ_OPENIF)
694 set_error( STATUS_OBJECT_TYPE_MISMATCH );
695 else
696 set_error( STATUS_OBJECT_NAME_COLLISION );
698 return (struct named_pipe *)obj;
701 if (obj->ops != &named_pipe_device_ops)
702 set_error( STATUS_OBJECT_NAME_INVALID );
703 else
705 struct named_pipe_device *dev = (struct named_pipe_device *)obj;
706 if ((pipe = create_object( dev->pipes, &named_pipe_ops, &new_name, NULL )))
707 clear_error();
710 release_object( obj );
711 return pipe;
714 static struct pipe_server *get_pipe_server_obj( struct process *process,
715 obj_handle_t handle, unsigned int access )
717 struct object *obj;
718 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
719 return (struct pipe_server *) obj;
722 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
723 unsigned int pipe_flags )
725 struct pipe_server *server;
727 server = alloc_object( &pipe_server_ops );
728 if (!server)
729 return NULL;
731 server->fd = NULL;
732 server->pipe = pipe;
733 server->client = NULL;
734 server->flush_poll = NULL;
735 server->options = options;
736 server->pipe_flags = pipe_flags;
738 list_add_head( &pipe->servers, &server->entry );
739 grab_object( pipe );
740 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->obj, options )))
742 release_object( server );
743 return NULL;
745 set_server_state( server, ps_idle_server );
746 return server;
749 static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags )
751 struct pipe_client *client;
753 client = alloc_object( &pipe_client_ops );
754 if (!client)
755 return NULL;
757 client->fd = NULL;
758 client->server = NULL;
759 client->flags = flags;
760 client->pipe_flags = pipe_flags;
762 return client;
765 static struct pipe_server *find_available_server( struct named_pipe *pipe )
767 struct pipe_server *server;
769 /* look for pipe servers that are listening */
770 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
772 if (server->state == ps_wait_open)
773 return (struct pipe_server *)grab_object( server );
776 /* fall back to pipe servers that are idle */
777 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
779 if (server->state == ps_idle_server)
780 return (struct pipe_server *)grab_object( server );
783 return NULL;
786 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
787 unsigned int sharing, unsigned int options )
789 struct named_pipe *pipe = (struct named_pipe *)obj;
790 struct pipe_server *server;
791 struct pipe_client *client;
792 unsigned int pipe_sharing;
793 int fds[2];
795 if (!(server = find_available_server( pipe )))
797 set_error( STATUS_PIPE_NOT_AVAILABLE );
798 return NULL;
801 pipe_sharing = server->pipe->sharing;
802 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
803 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
805 set_error( STATUS_ACCESS_DENIED );
806 release_object( server );
807 return NULL;
810 if ((client = create_pipe_client( options, pipe->flags )))
812 if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
814 assert( !server->fd );
816 /* for performance reasons, only set nonblocking mode when using
817 * overlapped I/O. Otherwise, we will be doing too much busy
818 * looping */
819 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
820 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
822 if (pipe->insize)
824 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
825 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
827 if (pipe->outsize)
829 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
830 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
833 client->fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->obj, options );
834 server->fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->obj, server->options );
835 if (client->fd && server->fd)
837 allow_fd_caching( client->fd );
838 allow_fd_caching( server->fd );
839 fd_copy_completion( server->ioctl_fd, server->fd );
840 if (server->state == ps_wait_open)
841 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
842 set_server_state( server, ps_connected_server );
843 server->client = client;
844 client->server = server;
846 else
848 release_object( client );
849 client = NULL;
852 else
854 file_set_error();
855 release_object( client );
856 client = NULL;
859 release_object( server );
860 return &client->obj;
863 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code,
864 const async_data_t *async_data, int blocking )
866 struct named_pipe_device *device = get_fd_user( fd );
868 switch(code)
870 case FSCTL_PIPE_WAIT:
872 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
873 data_size_t size = get_req_data_size();
874 obj_handle_t wait_handle = 0;
875 struct named_pipe *pipe;
876 struct pipe_server *server;
877 struct unicode_str name;
879 if (size < sizeof(*buffer) ||
880 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
882 set_error( STATUS_INVALID_PARAMETER );
883 return 0;
885 name.str = buffer->Name;
886 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
887 if (!(pipe = (struct named_pipe *)find_object( device->pipes, &name, OBJ_CASE_INSENSITIVE )))
889 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
890 return 0;
892 if (!(server = find_available_server( pipe )))
894 struct async *async;
896 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
898 if ((async = create_async( current, pipe->waiters, async_data )))
900 timeout_t when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
901 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
902 if (blocking) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
903 release_object( async );
904 set_error( STATUS_PENDING );
907 else release_object( server );
909 done:
910 release_object( pipe );
911 return wait_handle;
914 default:
915 return default_fd_ioctl( fd, code, async_data, blocking );
920 DECL_HANDLER(create_named_pipe)
922 struct named_pipe *pipe;
923 struct pipe_server *server;
924 struct unicode_str name;
925 struct directory *root = NULL;
926 const struct object_attributes *objattr = get_req_data();
927 const struct security_descriptor *sd;
929 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
930 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
932 set_error( STATUS_INVALID_PARAMETER );
933 return;
936 reply->handle = 0;
938 if (!objattr_is_valid( objattr, get_req_data_size() ))
939 return;
941 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
942 objattr_get_name( objattr, &name );
944 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
945 return;
947 pipe = create_named_pipe( root, &name, req->attributes | OBJ_OPENIF, sd );
949 if (root) release_object( root );
950 if (!pipe) return;
952 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
954 /* initialize it if it didn't already exist */
955 pipe->instances = 0;
956 pipe->waiters = NULL;
957 list_init( &pipe->servers );
958 pipe->insize = req->insize;
959 pipe->outsize = req->outsize;
960 pipe->maxinstances = req->maxinstances;
961 pipe->timeout = req->timeout;
962 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
963 pipe->sharing = req->sharing;
965 else
967 if (pipe->maxinstances <= pipe->instances)
969 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
970 release_object( pipe );
971 return;
973 if (pipe->sharing != req->sharing)
975 set_error( STATUS_ACCESS_DENIED );
976 release_object( pipe );
977 return;
979 clear_error(); /* clear the name collision */
982 server = create_pipe_server( pipe, req->options, req->flags );
983 if (server)
985 reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
986 server->pipe->instances++;
987 if (sd) default_set_sd( &server->obj, sd, OWNER_SECURITY_INFORMATION |
988 GROUP_SECURITY_INFORMATION |
989 DACL_SECURITY_INFORMATION |
990 SACL_SECURITY_INFORMATION );
991 release_object( server );
994 release_object( pipe );
997 DECL_HANDLER(get_named_pipe_info)
999 struct pipe_server *server;
1000 struct pipe_client *client = NULL;
1002 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1003 if (!server)
1005 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1006 return;
1008 clear_error();
1009 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1010 0, &pipe_client_ops );
1011 if (!client) return;
1012 server = client->server;
1015 reply->flags = client ? client->pipe_flags : server->pipe_flags;
1016 reply->sharing = server->pipe->sharing;
1017 reply->maxinstances = server->pipe->maxinstances;
1018 reply->instances = server->pipe->instances;
1019 reply->insize = server->pipe->insize;
1020 reply->outsize = server->pipe->outsize;
1022 if (client)
1023 release_object(client);
1024 else
1026 reply->flags |= NAMED_PIPE_SERVER_END;
1027 release_object(server);
1031 DECL_HANDLER(set_named_pipe_info)
1033 struct pipe_server *server;
1034 struct pipe_client *client = NULL;
1036 server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
1037 if (!server)
1039 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1040 return;
1042 clear_error();
1043 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1044 0, &pipe_client_ops );
1045 if (!client) return;
1046 server = client->server;
1049 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1050 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1052 set_error( STATUS_INVALID_PARAMETER );
1054 else if (client)
1056 client->pipe_flags = server->pipe->flags | req->flags;
1058 else
1060 server->pipe_flags = server->pipe->flags | req->flags;
1063 if (client)
1064 release_object(client);
1065 else
1066 release_object(server);