ntdll: Fallback to server calls for read and write on objects without a file descriptor.
[wine.git] / server / named_pipe.c
blob154eeb9c3f4065e683140262d384a001020be980
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 struct event *event;
80 unsigned int options; /* pipe options */
81 unsigned int pipe_flags;
84 struct pipe_client
86 struct object obj; /* object header */
87 struct fd *fd; /* pipe file descriptor */
88 struct pipe_server *server; /* server that this client is connected to */
89 unsigned int flags; /* file flags */
90 unsigned int pipe_flags;
93 struct named_pipe
95 struct object obj; /* object header */
96 unsigned int flags;
97 unsigned int sharing;
98 unsigned int maxinstances;
99 unsigned int outsize;
100 unsigned int insize;
101 unsigned int instances;
102 timeout_t timeout;
103 struct list servers; /* list of servers using this pipe */
104 struct async_queue *waiters; /* list of clients waiting to connect */
107 struct named_pipe_device
109 struct object obj; /* object header */
110 struct fd *fd; /* pseudo-fd for ioctls */
111 struct namespace *pipes; /* named pipe namespace */
114 static void named_pipe_dump( struct object *obj, int verbose );
115 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
116 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
117 unsigned int sharing, unsigned int options );
118 static void named_pipe_destroy( struct object *obj );
120 static const struct object_ops named_pipe_ops =
122 sizeof(struct named_pipe), /* size */
123 named_pipe_dump, /* dump */
124 no_get_type, /* get_type */
125 no_add_queue, /* add_queue */
126 NULL, /* remove_queue */
127 NULL, /* signaled */
128 NULL, /* satisfied */
129 no_signal, /* signal */
130 no_get_fd, /* get_fd */
131 named_pipe_map_access, /* map_access */
132 default_get_sd, /* get_sd */
133 default_set_sd, /* set_sd */
134 no_lookup_name, /* lookup_name */
135 named_pipe_open_file, /* open_file */
136 no_close_handle, /* close_handle */
137 named_pipe_destroy /* destroy */
140 /* server end functions */
141 static void pipe_server_dump( struct object *obj, int verbose );
142 static struct fd *pipe_server_get_fd( struct object *obj );
143 static void pipe_server_destroy( struct object *obj);
144 static obj_handle_t pipe_server_flush( struct fd *fd, const async_data_t *async, int blocking );
145 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd );
146 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
147 int blocking );
149 static const struct object_ops pipe_server_ops =
151 sizeof(struct pipe_server), /* size */
152 pipe_server_dump, /* dump */
153 no_get_type, /* get_type */
154 add_queue, /* add_queue */
155 remove_queue, /* remove_queue */
156 default_fd_signaled, /* signaled */
157 no_satisfied, /* satisfied */
158 no_signal, /* signal */
159 pipe_server_get_fd, /* get_fd */
160 default_fd_map_access, /* map_access */
161 default_get_sd, /* get_sd */
162 default_set_sd, /* set_sd */
163 no_lookup_name, /* lookup_name */
164 no_open_file, /* open_file */
165 fd_close_handle, /* close_handle */
166 pipe_server_destroy /* destroy */
169 static const struct fd_ops pipe_server_fd_ops =
171 default_fd_get_poll_events, /* get_poll_events */
172 default_poll_event, /* poll_event */
173 pipe_server_get_fd_type, /* get_fd_type */
174 no_fd_read, /* read */
175 no_fd_write, /* write */
176 pipe_server_flush, /* flush */
177 pipe_server_ioctl, /* ioctl */
178 default_fd_queue_async, /* queue_async */
179 default_fd_reselect_async, /* reselect_async */
180 default_fd_cancel_async, /* cancel_async */
183 /* client end functions */
184 static void pipe_client_dump( struct object *obj, int verbose );
185 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry );
186 static struct fd *pipe_client_get_fd( struct object *obj );
187 static void pipe_client_destroy( struct object *obj );
188 static obj_handle_t pipe_client_flush( struct fd *fd, const async_data_t *async, int blocking );
189 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd );
191 static const struct object_ops pipe_client_ops =
193 sizeof(struct pipe_client), /* size */
194 pipe_client_dump, /* dump */
195 no_get_type, /* get_type */
196 add_queue, /* add_queue */
197 remove_queue, /* remove_queue */
198 pipe_client_signaled, /* signaled */
199 no_satisfied, /* satisfied */
200 no_signal, /* signal */
201 pipe_client_get_fd, /* get_fd */
202 default_fd_map_access, /* map_access */
203 default_get_sd, /* get_sd */
204 default_set_sd, /* set_sd */
205 no_lookup_name, /* lookup_name */
206 no_open_file, /* open_file */
207 fd_close_handle, /* close_handle */
208 pipe_client_destroy /* destroy */
211 static const struct fd_ops pipe_client_fd_ops =
213 default_fd_get_poll_events, /* get_poll_events */
214 default_poll_event, /* poll_event */
215 pipe_client_get_fd_type, /* get_fd_type */
216 no_fd_read, /* read */
217 no_fd_write, /* write */
218 pipe_client_flush, /* flush */
219 default_fd_ioctl, /* ioctl */
220 default_fd_queue_async, /* queue_async */
221 default_fd_reselect_async, /* reselect_async */
222 default_fd_cancel_async /* cancel_async */
225 static void named_pipe_device_dump( struct object *obj, int verbose );
226 static struct object_type *named_pipe_device_get_type( struct object *obj );
227 static struct fd *named_pipe_device_get_fd( struct object *obj );
228 static struct object *named_pipe_device_lookup_name( struct object *obj,
229 struct unicode_str *name, unsigned int attr );
230 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
231 unsigned int sharing, unsigned int options );
232 static void named_pipe_device_destroy( struct object *obj );
233 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
234 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code,
235 const async_data_t *async_data, int blocking );
237 static const struct object_ops named_pipe_device_ops =
239 sizeof(struct named_pipe_device), /* size */
240 named_pipe_device_dump, /* dump */
241 named_pipe_device_get_type, /* get_type */
242 no_add_queue, /* add_queue */
243 NULL, /* remove_queue */
244 NULL, /* signaled */
245 no_satisfied, /* satisfied */
246 no_signal, /* signal */
247 named_pipe_device_get_fd, /* get_fd */
248 no_map_access, /* map_access */
249 default_get_sd, /* get_sd */
250 default_set_sd, /* set_sd */
251 named_pipe_device_lookup_name, /* lookup_name */
252 named_pipe_device_open_file, /* open_file */
253 fd_close_handle, /* close_handle */
254 named_pipe_device_destroy /* destroy */
257 static const struct fd_ops named_pipe_device_fd_ops =
259 default_fd_get_poll_events, /* get_poll_events */
260 default_poll_event, /* poll_event */
261 named_pipe_device_get_fd_type, /* get_fd_type */
262 no_fd_read, /* read */
263 no_fd_write, /* write */
264 no_fd_flush, /* flush */
265 named_pipe_device_ioctl, /* ioctl */
266 default_fd_queue_async, /* queue_async */
267 default_fd_reselect_async, /* reselect_async */
268 default_fd_cancel_async /* cancel_async */
271 static void named_pipe_dump( struct object *obj, int verbose )
273 struct named_pipe *pipe = (struct named_pipe *) obj;
274 assert( obj->ops == &named_pipe_ops );
275 fprintf( stderr, "Named pipe " );
276 dump_object_name( &pipe->obj );
277 fprintf( stderr, "\n" );
280 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
282 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
283 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
284 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
285 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
286 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
289 static void pipe_server_dump( struct object *obj, int verbose )
291 struct pipe_server *server = (struct pipe_server *) obj;
292 assert( obj->ops == &pipe_server_ops );
293 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
296 static void pipe_client_dump( struct object *obj, int verbose )
298 struct pipe_client *client = (struct pipe_client *) obj;
299 assert( obj->ops == &pipe_client_ops );
300 fprintf( stderr, "Named pipe client server=%p\n", client->server );
303 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry )
305 struct pipe_client *client = (struct pipe_client *) obj;
307 return client->fd && is_fd_signaled(client->fd);
310 static void named_pipe_destroy( struct object *obj)
312 struct named_pipe *pipe = (struct named_pipe *) obj;
314 assert( list_empty( &pipe->servers ) );
315 assert( !pipe->instances );
316 free_async_queue( pipe->waiters );
319 static struct fd *pipe_client_get_fd( struct object *obj )
321 struct pipe_client *client = (struct pipe_client *) obj;
322 if (client->fd)
323 return (struct fd *) grab_object( client->fd );
324 set_error( STATUS_PIPE_DISCONNECTED );
325 return NULL;
328 static void set_server_state( struct pipe_server *server, enum pipe_state state )
330 server->state = state;
332 switch(state)
334 case ps_connected_server:
335 case ps_wait_disconnect:
336 assert( server->fd );
337 break;
338 case ps_wait_open:
339 case ps_idle_server:
340 assert( !server->fd );
341 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
342 break;
343 case ps_disconnected_server:
344 case ps_wait_connect:
345 assert( !server->fd );
346 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
347 break;
351 static struct fd *pipe_server_get_fd( struct object *obj )
353 struct pipe_server *server = (struct pipe_server *) obj;
355 return (struct fd *)grab_object( server->fd ? server->fd : server->ioctl_fd );
359 static void notify_empty( struct pipe_server *server )
361 if (!server->flush_poll)
362 return;
363 assert( server->state == ps_connected_server );
364 assert( server->event );
365 remove_timeout_user( server->flush_poll );
366 server->flush_poll = NULL;
367 set_event( server->event );
368 release_object( server->event );
369 server->event = NULL;
372 static void do_disconnect( struct pipe_server *server )
374 /* we may only have a server fd, if the client disconnected */
375 if (server->client)
377 assert( server->client->server == server );
378 assert( server->client->fd );
379 release_object( server->client->fd );
380 server->client->fd = NULL;
382 assert( server->fd );
383 shutdown( get_unix_fd( server->fd ), SHUT_RDWR );
384 release_object( server->fd );
385 server->fd = NULL;
388 static void pipe_server_destroy( struct object *obj)
390 struct pipe_server *server = (struct pipe_server *)obj;
392 assert( obj->ops == &pipe_server_ops );
394 if (server->fd)
396 notify_empty( server );
397 do_disconnect( server );
400 if (server->client)
402 server->client->server = NULL;
403 server->client = NULL;
406 assert( server->pipe->instances );
407 server->pipe->instances--;
409 if (server->ioctl_fd) release_object( server->ioctl_fd );
410 list_remove( &server->entry );
411 release_object( server->pipe );
414 static void pipe_client_destroy( struct object *obj)
416 struct pipe_client *client = (struct pipe_client *)obj;
417 struct pipe_server *server = client->server;
419 assert( obj->ops == &pipe_client_ops );
421 if (server)
423 notify_empty( server );
425 switch(server->state)
427 case ps_connected_server:
428 /* Don't destroy the server's fd here as we can't
429 do a successful flush without it. */
430 set_server_state( server, ps_wait_disconnect );
431 break;
432 case ps_disconnected_server:
433 set_server_state( server, ps_wait_connect );
434 break;
435 case ps_idle_server:
436 case ps_wait_open:
437 case ps_wait_disconnect:
438 case ps_wait_connect:
439 assert( 0 );
441 assert( server->client );
442 server->client = NULL;
443 client->server = NULL;
445 if (client->fd) release_object( client->fd );
448 static void named_pipe_device_dump( struct object *obj, int verbose )
450 assert( obj->ops == &named_pipe_device_ops );
451 fprintf( stderr, "Named pipe device\n" );
454 static struct object_type *named_pipe_device_get_type( struct object *obj )
456 static const WCHAR name[] = {'D','e','v','i','c','e'};
457 static const struct unicode_str str = { name, sizeof(name) };
458 return get_object_type( &str );
461 static struct fd *named_pipe_device_get_fd( struct object *obj )
463 struct named_pipe_device *device = (struct named_pipe_device *)obj;
464 return (struct fd *)grab_object( device->fd );
467 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
468 unsigned int attr )
470 struct named_pipe_device *device = (struct named_pipe_device*)obj;
471 struct object *found;
473 assert( obj->ops == &named_pipe_device_ops );
474 assert( device->pipes );
476 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
477 name->len = 0;
479 return found;
482 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
483 unsigned int sharing, unsigned int options )
485 return grab_object( obj );
488 static void named_pipe_device_destroy( struct object *obj )
490 struct named_pipe_device *device = (struct named_pipe_device*)obj;
491 assert( obj->ops == &named_pipe_device_ops );
492 if (device->fd) release_object( device->fd );
493 free( device->pipes );
496 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
498 return FD_TYPE_DEVICE;
501 void create_named_pipe_device( struct directory *root, const struct unicode_str *name )
503 struct named_pipe_device *dev;
505 if ((dev = create_named_object_dir( root, name, 0, &named_pipe_device_ops )) &&
506 get_error() != STATUS_OBJECT_NAME_EXISTS)
508 dev->pipes = NULL;
509 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
510 !(dev->pipes = create_namespace( 7 )))
512 release_object( dev );
513 dev = NULL;
516 if (dev) make_object_static( &dev->obj );
519 static int pipe_data_remaining( struct pipe_server *server )
521 struct pollfd pfd;
522 int fd;
524 assert( server->client );
526 fd = get_unix_fd( server->client->fd );
527 if (fd < 0)
528 return 0;
529 pfd.fd = fd;
530 pfd.events = POLLIN;
531 pfd.revents = 0;
533 if (0 > poll( &pfd, 1, 0 ))
534 return 0;
536 return pfd.revents&POLLIN;
539 static void check_flushed( void *arg )
541 struct pipe_server *server = (struct pipe_server*) arg;
543 assert( server->event );
544 if (pipe_data_remaining( server ))
546 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
548 else
550 /* notify_empty( server ); */
551 server->flush_poll = NULL;
552 set_event( server->event );
553 release_object( server->event );
554 server->event = NULL;
558 static obj_handle_t pipe_server_flush( struct fd *fd, const async_data_t *async, int blocking )
560 struct pipe_server *server = get_fd_user( fd );
561 obj_handle_t handle = 0;
563 if (!server || server->state != ps_connected_server) return 0;
565 /* FIXME: if multiple threads flush the same pipe,
566 maybe should create a list of processes to notify */
567 if (server->flush_poll) return 0;
569 if (pipe_data_remaining( server ))
571 /* this kind of sux -
572 there's no unix way to be alerted when a pipe becomes empty */
573 server->event = create_event( NULL, NULL, 0, 0, 0, NULL );
574 if (!server->event) return 0;
575 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
576 handle = alloc_handle( current->process, server->event, SYNCHRONIZE, 0 );
577 set_error( STATUS_PENDING );
579 return handle;
582 static obj_handle_t pipe_client_flush( struct fd *fd, const async_data_t *async, int blocking )
584 /* FIXME: what do we have to do for this? */
585 return 0;
588 static inline int is_overlapped( unsigned int options )
590 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
593 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
595 return FD_TYPE_PIPE;
598 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
600 return FD_TYPE_PIPE;
603 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
604 int blocking )
606 struct pipe_server *server = get_fd_user( fd );
607 struct async *async;
608 obj_handle_t wait_handle = 0;
610 switch(code)
612 case FSCTL_PIPE_LISTEN:
613 switch(server->state)
615 case ps_idle_server:
616 case ps_wait_connect:
617 if (blocking)
619 async_data_t new_data = *async_data;
620 if (!(wait_handle = alloc_wait_event( current->process ))) break;
621 new_data.event = wait_handle;
622 if (!(async = fd_queue_async( server->ioctl_fd, &new_data, ASYNC_TYPE_WAIT )))
624 close_handle( current->process, wait_handle );
625 break;
628 else async = fd_queue_async( server->ioctl_fd, async_data, ASYNC_TYPE_WAIT );
630 if (async)
632 set_server_state( server, ps_wait_open );
633 if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
634 release_object( async );
635 set_error( STATUS_PENDING );
636 return wait_handle;
638 break;
639 case ps_connected_server:
640 set_error( STATUS_PIPE_CONNECTED );
641 break;
642 case ps_disconnected_server:
643 set_error( STATUS_PIPE_BUSY );
644 break;
645 case ps_wait_disconnect:
646 set_error( STATUS_NO_DATA_DETECTED );
647 break;
648 case ps_wait_open:
649 set_error( STATUS_INVALID_HANDLE );
650 break;
652 return 0;
654 case FSCTL_PIPE_DISCONNECT:
655 switch(server->state)
657 case ps_connected_server:
658 assert( server->client );
659 assert( server->client->fd );
661 notify_empty( server );
663 /* dump the client and server fds, but keep the pointers
664 around - client loses all waiting data */
665 do_disconnect( server );
666 set_server_state( server, ps_disconnected_server );
667 break;
668 case ps_wait_disconnect:
669 assert( !server->client );
670 do_disconnect( server );
671 set_server_state( server, ps_wait_connect );
672 break;
673 case ps_idle_server:
674 case ps_wait_open:
675 set_error( STATUS_PIPE_LISTENING );
676 break;
677 case ps_disconnected_server:
678 case ps_wait_connect:
679 set_error( STATUS_PIPE_DISCONNECTED );
680 break;
682 return 0;
684 default:
685 return default_fd_ioctl( fd, code, async_data, blocking );
689 static struct named_pipe *create_named_pipe( struct directory *root, const struct unicode_str *name,
690 unsigned int attr, const struct security_descriptor *sd )
692 struct object *obj;
693 struct named_pipe *pipe = NULL;
694 struct unicode_str new_name;
696 if (!name || !name->len) return alloc_object( &named_pipe_ops );
698 if (!(obj = find_object_dir( root, name, attr, &new_name )))
700 set_error( STATUS_OBJECT_NAME_INVALID );
701 return NULL;
703 if (!new_name.len)
705 if (attr & OBJ_OPENIF && obj->ops == &named_pipe_ops)
706 set_error( STATUS_OBJECT_NAME_EXISTS );
707 else
709 release_object( obj );
710 obj = NULL;
711 if (attr & OBJ_OPENIF)
712 set_error( STATUS_OBJECT_TYPE_MISMATCH );
713 else
714 set_error( STATUS_OBJECT_NAME_COLLISION );
716 return (struct named_pipe *)obj;
719 if (obj->ops != &named_pipe_device_ops)
720 set_error( STATUS_OBJECT_NAME_INVALID );
721 else
723 struct named_pipe_device *dev = (struct named_pipe_device *)obj;
724 if ((pipe = create_object( dev->pipes, &named_pipe_ops, &new_name, NULL )))
725 clear_error();
728 release_object( obj );
729 return pipe;
732 static struct pipe_server *get_pipe_server_obj( struct process *process,
733 obj_handle_t handle, unsigned int access )
735 struct object *obj;
736 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
737 return (struct pipe_server *) obj;
740 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
741 unsigned int pipe_flags )
743 struct pipe_server *server;
745 server = alloc_object( &pipe_server_ops );
746 if (!server)
747 return NULL;
749 server->fd = NULL;
750 server->pipe = pipe;
751 server->client = NULL;
752 server->flush_poll = NULL;
753 server->options = options;
754 server->pipe_flags = pipe_flags;
756 list_add_head( &pipe->servers, &server->entry );
757 grab_object( pipe );
758 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->obj, options )))
760 release_object( server );
761 return NULL;
763 set_server_state( server, ps_idle_server );
764 return server;
767 static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags )
769 struct pipe_client *client;
771 client = alloc_object( &pipe_client_ops );
772 if (!client)
773 return NULL;
775 client->fd = NULL;
776 client->server = NULL;
777 client->flags = flags;
778 client->pipe_flags = pipe_flags;
780 return client;
783 static struct pipe_server *find_available_server( struct named_pipe *pipe )
785 struct pipe_server *server;
787 /* look for pipe servers that are listening */
788 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
790 if (server->state == ps_wait_open)
791 return (struct pipe_server *)grab_object( server );
794 /* fall back to pipe servers that are idle */
795 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
797 if (server->state == ps_idle_server)
798 return (struct pipe_server *)grab_object( server );
801 return NULL;
804 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
805 unsigned int sharing, unsigned int options )
807 struct named_pipe *pipe = (struct named_pipe *)obj;
808 struct pipe_server *server;
809 struct pipe_client *client;
810 unsigned int pipe_sharing;
811 int fds[2];
813 if (!(server = find_available_server( pipe )))
815 set_error( STATUS_PIPE_NOT_AVAILABLE );
816 return NULL;
819 pipe_sharing = server->pipe->sharing;
820 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
821 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
823 set_error( STATUS_ACCESS_DENIED );
824 release_object( server );
825 return NULL;
828 if ((client = create_pipe_client( options, pipe->flags )))
830 if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
832 assert( !server->fd );
834 /* for performance reasons, only set nonblocking mode when using
835 * overlapped I/O. Otherwise, we will be doing too much busy
836 * looping */
837 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
838 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
840 if (pipe->insize)
842 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
843 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
845 if (pipe->outsize)
847 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
848 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
851 client->fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->obj, options );
852 server->fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->obj, server->options );
853 if (client->fd && server->fd)
855 allow_fd_caching( client->fd );
856 allow_fd_caching( server->fd );
857 fd_copy_completion( server->ioctl_fd, server->fd );
858 if (server->state == ps_wait_open)
859 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
860 set_server_state( server, ps_connected_server );
861 server->client = client;
862 client->server = server;
864 else
866 release_object( client );
867 client = NULL;
870 else
872 file_set_error();
873 release_object( client );
874 client = NULL;
877 release_object( server );
878 return &client->obj;
881 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code,
882 const async_data_t *async_data, int blocking )
884 struct named_pipe_device *device = get_fd_user( fd );
886 switch(code)
888 case FSCTL_PIPE_WAIT:
890 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
891 data_size_t size = get_req_data_size();
892 obj_handle_t wait_handle = 0;
893 struct named_pipe *pipe;
894 struct pipe_server *server;
895 struct unicode_str name;
897 if (size < sizeof(*buffer) ||
898 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
900 set_error( STATUS_INVALID_PARAMETER );
901 return 0;
903 name.str = buffer->Name;
904 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
905 if (!(pipe = (struct named_pipe *)find_object( device->pipes, &name, OBJ_CASE_INSENSITIVE )))
907 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
908 return 0;
910 if (!(server = find_available_server( pipe )))
912 struct async *async;
914 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
916 if (blocking)
918 async_data_t new_data = *async_data;
919 if (!(wait_handle = alloc_wait_event( current->process ))) goto done;
920 new_data.event = wait_handle;
921 if (!(async = create_async( current, pipe->waiters, &new_data )))
923 close_handle( current->process, wait_handle );
924 wait_handle = 0;
927 else async = create_async( current, pipe->waiters, async_data );
929 if (async)
931 timeout_t when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
932 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
933 release_object( async );
934 set_error( STATUS_PENDING );
937 else release_object( server );
939 done:
940 release_object( pipe );
941 return wait_handle;
944 default:
945 return default_fd_ioctl( fd, code, async_data, blocking );
950 DECL_HANDLER(create_named_pipe)
952 struct named_pipe *pipe;
953 struct pipe_server *server;
954 struct unicode_str name;
955 struct directory *root = NULL;
956 const struct object_attributes *objattr = get_req_data();
957 const struct security_descriptor *sd;
959 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
960 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
962 set_error( STATUS_INVALID_PARAMETER );
963 return;
966 reply->handle = 0;
968 if (!objattr_is_valid( objattr, get_req_data_size() ))
969 return;
971 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
972 objattr_get_name( objattr, &name );
974 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
975 return;
977 pipe = create_named_pipe( root, &name, req->attributes | OBJ_OPENIF, sd );
979 if (root) release_object( root );
980 if (!pipe) return;
982 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
984 /* initialize it if it didn't already exist */
985 pipe->instances = 0;
986 pipe->waiters = NULL;
987 list_init( &pipe->servers );
988 pipe->insize = req->insize;
989 pipe->outsize = req->outsize;
990 pipe->maxinstances = req->maxinstances;
991 pipe->timeout = req->timeout;
992 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
993 pipe->sharing = req->sharing;
995 else
997 if (pipe->maxinstances <= pipe->instances)
999 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1000 release_object( pipe );
1001 return;
1003 if (pipe->sharing != req->sharing)
1005 set_error( STATUS_ACCESS_DENIED );
1006 release_object( pipe );
1007 return;
1009 clear_error(); /* clear the name collision */
1012 server = create_pipe_server( pipe, req->options, req->flags );
1013 if (server)
1015 reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
1016 server->pipe->instances++;
1017 if (sd) default_set_sd( &server->obj, sd, OWNER_SECURITY_INFORMATION |
1018 GROUP_SECURITY_INFORMATION |
1019 DACL_SECURITY_INFORMATION |
1020 SACL_SECURITY_INFORMATION );
1021 release_object( server );
1024 release_object( pipe );
1027 DECL_HANDLER(get_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_READ_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 reply->flags = client ? client->pipe_flags : server->pipe_flags;
1046 reply->sharing = server->pipe->sharing;
1047 reply->maxinstances = server->pipe->maxinstances;
1048 reply->instances = server->pipe->instances;
1049 reply->insize = server->pipe->insize;
1050 reply->outsize = server->pipe->outsize;
1052 if (client)
1053 release_object(client);
1054 else
1056 reply->flags |= NAMED_PIPE_SERVER_END;
1057 release_object(server);
1061 DECL_HANDLER(set_named_pipe_info)
1063 struct pipe_server *server;
1064 struct pipe_client *client = NULL;
1066 server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
1067 if (!server)
1069 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1070 return;
1072 clear_error();
1073 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1074 0, &pipe_client_ops );
1075 if (!client) return;
1076 server = client->server;
1079 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1080 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1082 set_error( STATUS_INVALID_PARAMETER );
1084 else if (client)
1086 client->pipe_flags = server->pipe->flags | req->flags;
1088 else
1090 server->pipe_flags = server->pipe->flags | req->flags;
1093 if (client)
1094 release_object(client);
1095 else
1096 release_object(server);