shell32/tests: Fix tests of IShellLinkA_SetPath to conform on both Windows 98 and XP.
[wine.git] / server / named_pipe.c
blob39dd10a8d48475881ee931cafaa8b067b7521608
1 /*
2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2001 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * TODO:
22 * message mode
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
38 #endif
39 #include <time.h>
40 #include <unistd.h>
41 #ifdef HAVE_POLL_H
42 #include <poll.h>
43 #endif
45 #include "ntstatus.h"
46 #define WIN32_NO_STATUS
47 #include "windef.h"
48 #include "winternl.h"
49 #include "winioctl.h"
51 #include "file.h"
52 #include "handle.h"
53 #include "thread.h"
54 #include "request.h"
56 enum pipe_state
58 ps_idle_server,
59 ps_wait_open,
60 ps_connected_server,
61 ps_wait_disconnect,
62 ps_disconnected_server,
63 ps_wait_connect
66 struct named_pipe;
68 struct pipe_server
70 struct object obj; /* object header */
71 struct fd *fd; /* pipe file descriptor */
72 struct fd *ioctl_fd; /* file descriptor for ioctls when not connected */
73 struct list entry; /* entry in named pipe servers list */
74 enum pipe_state state; /* server state */
75 struct pipe_client *client; /* client that this server is connected to */
76 struct named_pipe *pipe;
77 struct timeout_user *flush_poll;
78 struct event *event;
79 unsigned int options; /* pipe options */
82 struct pipe_client
84 struct object obj; /* object header */
85 struct fd *fd; /* pipe file descriptor */
86 struct pipe_server *server; /* server that this client is connected to */
87 unsigned int flags; /* file flags */
90 struct named_pipe
92 struct object obj; /* object header */
93 unsigned int flags;
94 unsigned int maxinstances;
95 unsigned int outsize;
96 unsigned int insize;
97 unsigned int instances;
98 timeout_t timeout;
99 struct list servers; /* list of servers using this pipe */
100 struct async_queue *waiters; /* list of clients waiting to connect */
103 struct named_pipe_device
105 struct object obj; /* object header */
106 struct fd *fd; /* pseudo-fd for ioctls */
107 struct namespace *pipes; /* named pipe namespace */
110 static void named_pipe_dump( struct object *obj, int verbose );
111 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
112 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
113 unsigned int sharing, unsigned int options );
114 static void named_pipe_destroy( struct object *obj );
116 static const struct object_ops named_pipe_ops =
118 sizeof(struct named_pipe), /* size */
119 named_pipe_dump, /* dump */
120 no_add_queue, /* add_queue */
121 NULL, /* remove_queue */
122 NULL, /* signaled */
123 NULL, /* satisfied */
124 no_signal, /* signal */
125 no_get_fd, /* get_fd */
126 named_pipe_map_access, /* map_access */
127 no_lookup_name, /* lookup_name */
128 named_pipe_open_file, /* open_file */
129 no_close_handle, /* close_handle */
130 named_pipe_destroy /* destroy */
133 /* functions common to server and client */
134 static unsigned int pipe_map_access( struct object *obj, unsigned int access );
136 /* server end functions */
137 static void pipe_server_dump( struct object *obj, int verbose );
138 static struct fd *pipe_server_get_fd( struct object *obj );
139 static void pipe_server_destroy( struct object *obj);
140 static void pipe_server_flush( struct fd *fd, struct event **event );
141 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd );
142 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
143 const void *data, data_size_t size );
145 static const struct object_ops pipe_server_ops =
147 sizeof(struct pipe_server), /* size */
148 pipe_server_dump, /* dump */
149 add_queue, /* add_queue */
150 remove_queue, /* remove_queue */
151 default_fd_signaled, /* signaled */
152 no_satisfied, /* satisfied */
153 no_signal, /* signal */
154 pipe_server_get_fd, /* get_fd */
155 pipe_map_access, /* map_access */
156 no_lookup_name, /* lookup_name */
157 no_open_file, /* open_file */
158 fd_close_handle, /* close_handle */
159 pipe_server_destroy /* destroy */
162 static const struct fd_ops pipe_server_fd_ops =
164 default_fd_get_poll_events, /* get_poll_events */
165 default_poll_event, /* poll_event */
166 pipe_server_flush, /* flush */
167 pipe_server_get_fd_type, /* get_fd_type */
168 pipe_server_ioctl, /* ioctl */
169 default_fd_queue_async, /* queue_async */
170 default_fd_reselect_async, /* reselect_async */
171 default_fd_cancel_async, /* cancel_async */
174 /* client end functions */
175 static void pipe_client_dump( struct object *obj, int verbose );
176 static struct fd *pipe_client_get_fd( struct object *obj );
177 static void pipe_client_destroy( struct object *obj );
178 static void pipe_client_flush( struct fd *fd, struct event **event );
179 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd );
181 static const struct object_ops pipe_client_ops =
183 sizeof(struct pipe_client), /* size */
184 pipe_client_dump, /* dump */
185 add_queue, /* add_queue */
186 remove_queue, /* remove_queue */
187 default_fd_signaled, /* signaled */
188 no_satisfied, /* satisfied */
189 no_signal, /* signal */
190 pipe_client_get_fd, /* get_fd */
191 pipe_map_access, /* map_access */
192 no_lookup_name, /* lookup_name */
193 no_open_file, /* open_file */
194 fd_close_handle, /* close_handle */
195 pipe_client_destroy /* destroy */
198 static const struct fd_ops pipe_client_fd_ops =
200 default_fd_get_poll_events, /* get_poll_events */
201 default_poll_event, /* poll_event */
202 pipe_client_flush, /* flush */
203 pipe_client_get_fd_type, /* get_fd_type */
204 default_fd_ioctl, /* ioctl */
205 default_fd_queue_async, /* queue_async */
206 default_fd_reselect_async, /* reselect_async */
207 default_fd_cancel_async /* cancel_async */
210 static void named_pipe_device_dump( struct object *obj, int verbose );
211 static struct fd *named_pipe_device_get_fd( struct object *obj );
212 static struct object *named_pipe_device_lookup_name( struct object *obj,
213 struct unicode_str *name, unsigned int attr );
214 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
215 unsigned int sharing, unsigned int options );
216 static void named_pipe_device_destroy( struct object *obj );
217 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
218 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
219 const void *data, data_size_t size );
221 static const struct object_ops named_pipe_device_ops =
223 sizeof(struct named_pipe_device), /* size */
224 named_pipe_device_dump, /* dump */
225 no_add_queue, /* add_queue */
226 NULL, /* remove_queue */
227 NULL, /* signaled */
228 no_satisfied, /* satisfied */
229 no_signal, /* signal */
230 named_pipe_device_get_fd, /* get_fd */
231 no_map_access, /* map_access */
232 named_pipe_device_lookup_name, /* lookup_name */
233 named_pipe_device_open_file, /* open_file */
234 fd_close_handle, /* close_handle */
235 named_pipe_device_destroy /* destroy */
238 static const struct fd_ops named_pipe_device_fd_ops =
240 default_fd_get_poll_events, /* get_poll_events */
241 default_poll_event, /* poll_event */
242 no_flush, /* flush */
243 named_pipe_device_get_fd_type, /* get_fd_type */
244 named_pipe_device_ioctl, /* ioctl */
245 default_fd_queue_async, /* queue_async */
246 default_fd_reselect_async, /* reselect_async */
247 default_fd_cancel_async /* cancel_async */
250 static void named_pipe_dump( struct object *obj, int verbose )
252 struct named_pipe *pipe = (struct named_pipe *) obj;
253 assert( obj->ops == &named_pipe_ops );
254 fprintf( stderr, "Named pipe " );
255 dump_object_name( &pipe->obj );
256 fprintf( stderr, "\n" );
259 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
261 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
262 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
263 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
264 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
265 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
268 static void pipe_server_dump( struct object *obj, int verbose )
270 struct pipe_server *server = (struct pipe_server *) obj;
271 assert( obj->ops == &pipe_server_ops );
272 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
275 static void pipe_client_dump( struct object *obj, int verbose )
277 struct pipe_client *client = (struct pipe_client *) obj;
278 assert( obj->ops == &pipe_client_ops );
279 fprintf( stderr, "Named pipe client server=%p\n", client->server );
282 static void named_pipe_destroy( struct object *obj)
284 struct named_pipe *pipe = (struct named_pipe *) obj;
286 assert( list_empty( &pipe->servers ) );
287 assert( !pipe->instances );
288 free_async_queue( pipe->waiters );
291 static struct fd *pipe_client_get_fd( struct object *obj )
293 struct pipe_client *client = (struct pipe_client *) obj;
294 if (client->fd)
295 return (struct fd *) grab_object( client->fd );
296 set_error( STATUS_PIPE_DISCONNECTED );
297 return NULL;
300 static void set_server_state( struct pipe_server *server, enum pipe_state state )
302 server->state = state;
304 switch(state)
306 case ps_connected_server:
307 case ps_wait_disconnect:
308 assert( server->fd );
309 break;
310 case ps_wait_open:
311 case ps_idle_server:
312 assert( !server->fd );
313 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
314 break;
315 case ps_disconnected_server:
316 case ps_wait_connect:
317 assert( !server->fd );
318 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
319 break;
323 static struct fd *pipe_server_get_fd( struct object *obj )
325 struct pipe_server *server = (struct pipe_server *) obj;
327 return (struct fd *)grab_object( server->fd ? server->fd : server->ioctl_fd );
331 static void notify_empty( struct pipe_server *server )
333 if (!server->flush_poll)
334 return;
335 assert( server->state == ps_connected_server );
336 assert( server->event );
337 remove_timeout_user( server->flush_poll );
338 server->flush_poll = NULL;
339 set_event( server->event );
340 release_object( server->event );
341 server->event = NULL;
344 static void do_disconnect( struct pipe_server *server )
346 /* we may only have a server fd, if the client disconnected */
347 if (server->client)
349 assert( server->client->server == server );
350 assert( server->client->fd );
351 release_object( server->client->fd );
352 server->client->fd = NULL;
354 assert( server->fd );
355 shutdown( get_unix_fd( server->fd ), SHUT_RDWR );
356 release_object( server->fd );
357 server->fd = NULL;
360 static unsigned int pipe_map_access( struct object *obj, unsigned int access )
362 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
363 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
364 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
365 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
366 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
369 static void pipe_server_destroy( struct object *obj)
371 struct pipe_server *server = (struct pipe_server *)obj;
373 assert( obj->ops == &pipe_server_ops );
375 if (server->fd)
377 notify_empty( server );
378 do_disconnect( server );
381 if (server->client)
383 server->client->server = NULL;
384 server->client = NULL;
387 assert( server->pipe->instances );
388 server->pipe->instances--;
390 if (server->ioctl_fd) release_object( server->ioctl_fd );
391 list_remove( &server->entry );
392 release_object( server->pipe );
395 static void pipe_client_destroy( struct object *obj)
397 struct pipe_client *client = (struct pipe_client *)obj;
398 struct pipe_server *server = client->server;
400 assert( obj->ops == &pipe_client_ops );
402 if (server)
404 notify_empty( server );
406 switch(server->state)
408 case ps_connected_server:
409 /* Don't destroy the server's fd here as we can't
410 do a successful flush without it. */
411 set_server_state( server, ps_wait_disconnect );
412 break;
413 case ps_disconnected_server:
414 set_server_state( server, ps_wait_connect );
415 break;
416 case ps_idle_server:
417 case ps_wait_open:
418 case ps_wait_disconnect:
419 case ps_wait_connect:
420 assert( 0 );
422 assert( server->client );
423 server->client = NULL;
424 client->server = NULL;
426 if (client->fd) release_object( client->fd );
429 static void named_pipe_device_dump( struct object *obj, int verbose )
431 assert( obj->ops == &named_pipe_device_ops );
432 fprintf( stderr, "Named pipe device\n" );
435 static struct fd *named_pipe_device_get_fd( struct object *obj )
437 struct named_pipe_device *device = (struct named_pipe_device *)obj;
438 return (struct fd *)grab_object( device->fd );
441 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
442 unsigned int attr )
444 struct named_pipe_device *device = (struct named_pipe_device*)obj;
445 struct object *found;
447 assert( obj->ops == &named_pipe_device_ops );
448 assert( device->pipes );
450 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
451 name->len = 0;
453 return found;
456 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
457 unsigned int sharing, unsigned int options )
459 return grab_object( obj );
462 static void named_pipe_device_destroy( struct object *obj )
464 struct named_pipe_device *device = (struct named_pipe_device*)obj;
465 assert( obj->ops == &named_pipe_device_ops );
466 if (device->fd) release_object( device->fd );
467 free( device->pipes );
470 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
472 return FD_TYPE_DEVICE;
475 void create_named_pipe_device( struct directory *root, const struct unicode_str *name )
477 struct named_pipe_device *dev;
479 if ((dev = create_named_object_dir( root, name, 0, &named_pipe_device_ops )) &&
480 get_error() != STATUS_OBJECT_NAME_EXISTS)
482 dev->pipes = NULL;
483 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
484 !(dev->pipes = create_namespace( 7 )))
486 release_object( dev );
487 dev = NULL;
490 if (dev) make_object_static( &dev->obj );
493 static int pipe_data_remaining( struct pipe_server *server )
495 struct pollfd pfd;
496 int fd;
498 assert( server->client );
500 fd = get_unix_fd( server->client->fd );
501 if (fd < 0)
502 return 0;
503 pfd.fd = fd;
504 pfd.events = POLLIN;
505 pfd.revents = 0;
507 if (0 > poll( &pfd, 1, 0 ))
508 return 0;
510 return pfd.revents&POLLIN;
513 static void check_flushed( void *arg )
515 struct pipe_server *server = (struct pipe_server*) arg;
517 assert( server->event );
518 if (pipe_data_remaining( server ))
520 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
522 else
524 /* notify_empty( server ); */
525 server->flush_poll = NULL;
526 set_event( server->event );
527 release_object( server->event );
528 server->event = NULL;
532 static void pipe_server_flush( struct fd *fd, struct event **event )
534 struct pipe_server *server = get_fd_user( fd );
536 if (!server || server->state != ps_connected_server) return;
538 /* FIXME: if multiple threads flush the same pipe,
539 maybe should create a list of processes to notify */
540 if (server->flush_poll) return;
542 if (pipe_data_remaining( server ))
544 /* this kind of sux -
545 there's no unix way to be alerted when a pipe becomes empty */
546 server->event = create_event( NULL, NULL, 0, 0, 0 );
547 if (!server->event) return;
548 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
549 *event = server->event;
553 static void pipe_client_flush( struct fd *fd, struct event **event )
555 /* FIXME: what do we have to do for this? */
558 static inline int is_overlapped( unsigned int options )
560 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
563 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
565 return FD_TYPE_PIPE;
568 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
570 return FD_TYPE_PIPE;
573 static obj_handle_t alloc_wait_event( struct process *process )
575 obj_handle_t handle = 0;
576 struct event *event = create_event( NULL, NULL, 0, 1, 0 );
578 if (event)
580 handle = alloc_handle( process, event, EVENT_ALL_ACCESS, 0 );
581 release_object( event );
583 return handle;
586 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
587 const void *data, data_size_t size )
589 struct pipe_server *server = get_fd_user( fd );
590 struct async *async;
591 obj_handle_t wait_handle = 0;
593 switch(code)
595 case FSCTL_PIPE_LISTEN:
596 switch(server->state)
598 case ps_idle_server:
599 case ps_wait_connect:
600 if (!async_data->event && !async_data->apc)
602 async_data_t new_data = *async_data;
603 if (!(wait_handle = alloc_wait_event( current->process ))) break;
604 new_data.event = wait_handle;
605 if (!(async = fd_queue_async( server->ioctl_fd, &new_data, ASYNC_TYPE_WAIT, 0 )))
607 close_handle( current->process, wait_handle );
608 break;
611 else async = fd_queue_async( server->ioctl_fd, async_data, ASYNC_TYPE_WAIT, 0 );
613 if (async)
615 set_server_state( server, ps_wait_open );
616 if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
617 release_object( async );
618 set_error( STATUS_PENDING );
619 return wait_handle;
621 break;
622 case ps_connected_server:
623 set_error( STATUS_PIPE_CONNECTED );
624 break;
625 case ps_disconnected_server:
626 set_error( STATUS_PIPE_BUSY );
627 break;
628 case ps_wait_disconnect:
629 set_error( STATUS_NO_DATA_DETECTED );
630 break;
631 case ps_wait_open:
632 set_error( STATUS_INVALID_HANDLE );
633 break;
635 return 0;
637 case FSCTL_PIPE_DISCONNECT:
638 switch(server->state)
640 case ps_connected_server:
641 assert( server->client );
642 assert( server->client->fd );
644 notify_empty( server );
646 /* dump the client and server fds, but keep the pointers
647 around - client loses all waiting data */
648 do_disconnect( server );
649 set_server_state( server, ps_disconnected_server );
650 break;
651 case ps_wait_disconnect:
652 assert( !server->client );
653 do_disconnect( server );
654 set_server_state( server, ps_wait_connect );
655 break;
656 case ps_idle_server:
657 case ps_wait_open:
658 set_error( STATUS_PIPE_LISTENING );
659 break;
660 case ps_disconnected_server:
661 case ps_wait_connect:
662 set_error( STATUS_PIPE_DISCONNECTED );
663 break;
665 return 0;
667 default:
668 return default_fd_ioctl( fd, code, async_data, data, size );
672 static struct named_pipe *create_named_pipe( struct directory *root, const struct unicode_str *name,
673 unsigned int attr )
675 struct object *obj;
676 struct named_pipe *pipe = NULL;
677 struct unicode_str new_name;
679 if (!name || !name->len) return alloc_object( &named_pipe_ops );
681 if (!(obj = find_object_dir( root, name, attr, &new_name )))
683 set_error( STATUS_OBJECT_NAME_INVALID );
684 return NULL;
686 if (!new_name.len)
688 if (attr & OBJ_OPENIF && obj->ops == &named_pipe_ops)
689 set_error( STATUS_OBJECT_NAME_EXISTS );
690 else
692 release_object( obj );
693 obj = NULL;
694 if (attr & OBJ_OPENIF)
695 set_error( STATUS_OBJECT_TYPE_MISMATCH );
696 else
697 set_error( STATUS_OBJECT_NAME_COLLISION );
699 return (struct named_pipe *)obj;
702 if (obj->ops != &named_pipe_device_ops)
703 set_error( STATUS_OBJECT_NAME_INVALID );
704 else
706 struct named_pipe_device *dev = (struct named_pipe_device *)obj;
707 if ((pipe = create_object( dev->pipes, &named_pipe_ops, &new_name, NULL )))
708 clear_error();
711 release_object( obj );
712 return pipe;
715 static struct pipe_server *get_pipe_server_obj( struct process *process,
716 obj_handle_t handle, unsigned int access )
718 struct object *obj;
719 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
720 return (struct pipe_server *) obj;
723 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options )
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;
737 list_add_head( &pipe->servers, &server->entry );
738 grab_object( pipe );
739 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->obj, options )))
741 release_object( server );
742 return NULL;
744 set_server_state( server, ps_idle_server );
745 return server;
748 static struct pipe_client *create_pipe_client( unsigned int flags )
750 struct pipe_client *client;
752 client = alloc_object( &pipe_client_ops );
753 if (!client)
754 return NULL;
756 client->fd = NULL;
757 client->server = NULL;
758 client->flags = flags;
760 return client;
763 static struct pipe_server *find_available_server( struct named_pipe *pipe )
765 struct pipe_server *server;
767 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
769 if (server->state == ps_idle_server || server->state == ps_wait_open)
770 return (struct pipe_server *)grab_object( server );
772 return NULL;
775 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
776 unsigned int sharing, unsigned int options )
778 struct named_pipe *pipe = (struct named_pipe *)obj;
779 struct pipe_server *server;
780 struct pipe_client *client;
781 int fds[2];
783 if (!(server = find_available_server( pipe )))
785 set_error( STATUS_PIPE_NOT_AVAILABLE );
786 return NULL;
789 if ((client = create_pipe_client( options )))
791 if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
793 assert( !server->fd );
795 /* for performance reasons, only set nonblocking mode when using
796 * overlapped I/O. Otherwise, we will be doing too much busy
797 * looping */
798 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
799 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
801 if (pipe->insize)
803 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
804 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
806 if (pipe->outsize)
808 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
809 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
812 client->fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->obj, options );
813 server->fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->obj, server->options );
814 if (client->fd && server->fd)
816 if (server->state == ps_wait_open)
817 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
818 set_server_state( server, ps_connected_server );
819 server->client = client;
820 client->server = server;
822 else
824 release_object( client );
825 client = NULL;
828 else
830 file_set_error();
831 release_object( client );
832 client = NULL;
835 release_object( server );
836 return &client->obj;
839 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
840 const void *data, data_size_t size )
842 struct named_pipe_device *device = get_fd_user( fd );
844 switch(code)
846 case FSCTL_PIPE_WAIT:
848 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = data;
849 obj_handle_t wait_handle = 0;
850 struct named_pipe *pipe;
851 struct pipe_server *server;
852 struct unicode_str name;
854 if (size < sizeof(*buffer) ||
855 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
857 set_error( STATUS_INVALID_PARAMETER );
858 return 0;
860 name.str = buffer->Name;
861 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
862 if (!(pipe = (struct named_pipe *)find_object( device->pipes, &name, OBJ_CASE_INSENSITIVE )))
864 set_error( STATUS_PIPE_NOT_AVAILABLE );
865 return 0;
867 if (!(server = find_available_server( pipe )))
869 struct async *async;
871 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
873 if (!async_data->event && !async_data->apc)
875 async_data_t new_data = *async_data;
876 if (!(wait_handle = alloc_wait_event( current->process ))) goto done;
877 new_data.event = wait_handle;
878 if (!(async = create_async( current, pipe->waiters, &new_data )))
880 close_handle( current->process, wait_handle );
881 wait_handle = 0;
884 else async = create_async( current, pipe->waiters, async_data );
886 if (async)
888 timeout_t when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
889 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
890 release_object( async );
891 set_error( STATUS_PENDING );
894 else release_object( server );
896 done:
897 release_object( pipe );
898 return wait_handle;
901 default:
902 return default_fd_ioctl( fd, code, async_data, data, size );
907 DECL_HANDLER(create_named_pipe)
909 struct named_pipe *pipe;
910 struct pipe_server *server;
911 struct unicode_str name;
912 struct directory *root = NULL;
914 reply->handle = 0;
915 get_req_unicode_str( &name );
916 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
917 return;
919 pipe = create_named_pipe( root, &name, req->attributes | OBJ_OPENIF );
921 if (root) release_object( root );
922 if (!pipe) return;
924 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
926 /* initialize it if it didn't already exist */
927 pipe->instances = 0;
928 pipe->waiters = NULL;
929 list_init( &pipe->servers );
930 pipe->insize = req->insize;
931 pipe->outsize = req->outsize;
932 pipe->maxinstances = req->maxinstances;
933 pipe->timeout = req->timeout;
934 pipe->flags = req->flags;
936 else
938 if (pipe->maxinstances <= pipe->instances)
940 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
941 release_object( pipe );
942 return;
944 if ((pipe->maxinstances != req->maxinstances) ||
945 (pipe->timeout != req->timeout) ||
946 (pipe->flags != req->flags))
948 set_error( STATUS_ACCESS_DENIED );
949 release_object( pipe );
950 return;
952 clear_error(); /* clear the name collision */
955 server = create_pipe_server( pipe, req->options );
956 if (server)
958 reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
959 server->pipe->instances++;
960 release_object( server );
963 release_object( pipe );
966 DECL_HANDLER(get_named_pipe_info)
968 struct pipe_server *server;
969 struct pipe_client *client = NULL;
971 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
972 if (!server)
974 clear_error();
975 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
976 FILE_READ_ATTRIBUTES, &pipe_client_ops );
977 if (!client) return;
978 server = client->server;
981 reply->flags = server->pipe->flags;
982 reply->maxinstances = server->pipe->maxinstances;
983 reply->instances = server->pipe->instances;
984 reply->insize = server->pipe->insize;
985 reply->outsize = server->pipe->outsize;
987 if (client)
988 release_object(client);
989 else
991 reply->flags |= NAMED_PIPE_SERVER_END;
992 release_object(server);