wordpad: Move printing functions to a separate file.
[wine.git] / server / named_pipe.c
blobd277cdcf96a72ce1735bcee4bcb21aedc5f5a723
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 default_get_sd, /* get_sd */
128 default_set_sd, /* set_sd */
129 no_lookup_name, /* lookup_name */
130 named_pipe_open_file, /* open_file */
131 no_close_handle, /* close_handle */
132 named_pipe_destroy /* destroy */
135 /* server end functions */
136 static void pipe_server_dump( struct object *obj, int verbose );
137 static struct fd *pipe_server_get_fd( struct object *obj );
138 static void pipe_server_destroy( struct object *obj);
139 static void pipe_server_flush( struct fd *fd, struct event **event );
140 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd );
141 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
142 const void *data, data_size_t size );
144 static const struct object_ops pipe_server_ops =
146 sizeof(struct pipe_server), /* size */
147 pipe_server_dump, /* dump */
148 add_queue, /* add_queue */
149 remove_queue, /* remove_queue */
150 default_fd_signaled, /* signaled */
151 no_satisfied, /* satisfied */
152 no_signal, /* signal */
153 pipe_server_get_fd, /* get_fd */
154 default_fd_map_access, /* map_access */
155 default_get_sd, /* get_sd */
156 default_set_sd, /* set_sd */
157 no_lookup_name, /* lookup_name */
158 no_open_file, /* open_file */
159 fd_close_handle, /* close_handle */
160 pipe_server_destroy /* destroy */
163 static const struct fd_ops pipe_server_fd_ops =
165 default_fd_get_poll_events, /* get_poll_events */
166 default_poll_event, /* poll_event */
167 pipe_server_flush, /* flush */
168 pipe_server_get_fd_type, /* get_fd_type */
169 pipe_server_ioctl, /* ioctl */
170 default_fd_queue_async, /* queue_async */
171 default_fd_reselect_async, /* reselect_async */
172 default_fd_cancel_async, /* cancel_async */
175 /* client end functions */
176 static void pipe_client_dump( struct object *obj, int verbose );
177 static struct fd *pipe_client_get_fd( struct object *obj );
178 static void pipe_client_destroy( struct object *obj );
179 static void pipe_client_flush( struct fd *fd, struct event **event );
180 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd );
182 static const struct object_ops pipe_client_ops =
184 sizeof(struct pipe_client), /* size */
185 pipe_client_dump, /* dump */
186 add_queue, /* add_queue */
187 remove_queue, /* remove_queue */
188 default_fd_signaled, /* signaled */
189 no_satisfied, /* satisfied */
190 no_signal, /* signal */
191 pipe_client_get_fd, /* get_fd */
192 default_fd_map_access, /* map_access */
193 default_get_sd, /* get_sd */
194 default_set_sd, /* set_sd */
195 no_lookup_name, /* lookup_name */
196 no_open_file, /* open_file */
197 fd_close_handle, /* close_handle */
198 pipe_client_destroy /* destroy */
201 static const struct fd_ops pipe_client_fd_ops =
203 default_fd_get_poll_events, /* get_poll_events */
204 default_poll_event, /* poll_event */
205 pipe_client_flush, /* flush */
206 pipe_client_get_fd_type, /* get_fd_type */
207 default_fd_ioctl, /* ioctl */
208 default_fd_queue_async, /* queue_async */
209 default_fd_reselect_async, /* reselect_async */
210 default_fd_cancel_async /* cancel_async */
213 static void named_pipe_device_dump( struct object *obj, int verbose );
214 static struct fd *named_pipe_device_get_fd( struct object *obj );
215 static struct object *named_pipe_device_lookup_name( struct object *obj,
216 struct unicode_str *name, unsigned int attr );
217 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
218 unsigned int sharing, unsigned int options );
219 static void named_pipe_device_destroy( struct object *obj );
220 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
221 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
222 const void *data, data_size_t size );
224 static const struct object_ops named_pipe_device_ops =
226 sizeof(struct named_pipe_device), /* size */
227 named_pipe_device_dump, /* dump */
228 no_add_queue, /* add_queue */
229 NULL, /* remove_queue */
230 NULL, /* signaled */
231 no_satisfied, /* satisfied */
232 no_signal, /* signal */
233 named_pipe_device_get_fd, /* get_fd */
234 no_map_access, /* map_access */
235 default_get_sd, /* get_sd */
236 default_set_sd, /* set_sd */
237 named_pipe_device_lookup_name, /* lookup_name */
238 named_pipe_device_open_file, /* open_file */
239 fd_close_handle, /* close_handle */
240 named_pipe_device_destroy /* destroy */
243 static const struct fd_ops named_pipe_device_fd_ops =
245 default_fd_get_poll_events, /* get_poll_events */
246 default_poll_event, /* poll_event */
247 no_flush, /* flush */
248 named_pipe_device_get_fd_type, /* get_fd_type */
249 named_pipe_device_ioctl, /* ioctl */
250 default_fd_queue_async, /* queue_async */
251 default_fd_reselect_async, /* reselect_async */
252 default_fd_cancel_async /* cancel_async */
255 static void named_pipe_dump( struct object *obj, int verbose )
257 struct named_pipe *pipe = (struct named_pipe *) obj;
258 assert( obj->ops == &named_pipe_ops );
259 fprintf( stderr, "Named pipe " );
260 dump_object_name( &pipe->obj );
261 fprintf( stderr, "\n" );
264 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
266 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
267 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
268 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
269 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
270 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
273 static void pipe_server_dump( struct object *obj, int verbose )
275 struct pipe_server *server = (struct pipe_server *) obj;
276 assert( obj->ops == &pipe_server_ops );
277 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
280 static void pipe_client_dump( struct object *obj, int verbose )
282 struct pipe_client *client = (struct pipe_client *) obj;
283 assert( obj->ops == &pipe_client_ops );
284 fprintf( stderr, "Named pipe client server=%p\n", client->server );
287 static void named_pipe_destroy( struct object *obj)
289 struct named_pipe *pipe = (struct named_pipe *) obj;
291 assert( list_empty( &pipe->servers ) );
292 assert( !pipe->instances );
293 free_async_queue( pipe->waiters );
296 static struct fd *pipe_client_get_fd( struct object *obj )
298 struct pipe_client *client = (struct pipe_client *) obj;
299 if (client->fd)
300 return (struct fd *) grab_object( client->fd );
301 set_error( STATUS_PIPE_DISCONNECTED );
302 return NULL;
305 static void set_server_state( struct pipe_server *server, enum pipe_state state )
307 server->state = state;
309 switch(state)
311 case ps_connected_server:
312 case ps_wait_disconnect:
313 assert( server->fd );
314 break;
315 case ps_wait_open:
316 case ps_idle_server:
317 assert( !server->fd );
318 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
319 break;
320 case ps_disconnected_server:
321 case ps_wait_connect:
322 assert( !server->fd );
323 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
324 break;
328 static struct fd *pipe_server_get_fd( struct object *obj )
330 struct pipe_server *server = (struct pipe_server *) obj;
332 return (struct fd *)grab_object( server->fd ? server->fd : server->ioctl_fd );
336 static void notify_empty( struct pipe_server *server )
338 if (!server->flush_poll)
339 return;
340 assert( server->state == ps_connected_server );
341 assert( server->event );
342 remove_timeout_user( server->flush_poll );
343 server->flush_poll = NULL;
344 set_event( server->event );
345 release_object( server->event );
346 server->event = NULL;
349 static void do_disconnect( struct pipe_server *server )
351 /* we may only have a server fd, if the client disconnected */
352 if (server->client)
354 assert( server->client->server == server );
355 assert( server->client->fd );
356 release_object( server->client->fd );
357 server->client->fd = NULL;
359 assert( server->fd );
360 shutdown( get_unix_fd( server->fd ), SHUT_RDWR );
361 release_object( server->fd );
362 server->fd = NULL;
365 static void pipe_server_destroy( struct object *obj)
367 struct pipe_server *server = (struct pipe_server *)obj;
369 assert( obj->ops == &pipe_server_ops );
371 if (server->fd)
373 notify_empty( server );
374 do_disconnect( server );
377 if (server->client)
379 server->client->server = NULL;
380 server->client = NULL;
383 assert( server->pipe->instances );
384 server->pipe->instances--;
386 if (server->ioctl_fd) release_object( server->ioctl_fd );
387 list_remove( &server->entry );
388 release_object( server->pipe );
391 static void pipe_client_destroy( struct object *obj)
393 struct pipe_client *client = (struct pipe_client *)obj;
394 struct pipe_server *server = client->server;
396 assert( obj->ops == &pipe_client_ops );
398 if (server)
400 notify_empty( server );
402 switch(server->state)
404 case ps_connected_server:
405 /* Don't destroy the server's fd here as we can't
406 do a successful flush without it. */
407 set_server_state( server, ps_wait_disconnect );
408 break;
409 case ps_disconnected_server:
410 set_server_state( server, ps_wait_connect );
411 break;
412 case ps_idle_server:
413 case ps_wait_open:
414 case ps_wait_disconnect:
415 case ps_wait_connect:
416 assert( 0 );
418 assert( server->client );
419 server->client = NULL;
420 client->server = NULL;
422 if (client->fd) release_object( client->fd );
425 static void named_pipe_device_dump( struct object *obj, int verbose )
427 assert( obj->ops == &named_pipe_device_ops );
428 fprintf( stderr, "Named pipe device\n" );
431 static struct fd *named_pipe_device_get_fd( struct object *obj )
433 struct named_pipe_device *device = (struct named_pipe_device *)obj;
434 return (struct fd *)grab_object( device->fd );
437 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
438 unsigned int attr )
440 struct named_pipe_device *device = (struct named_pipe_device*)obj;
441 struct object *found;
443 assert( obj->ops == &named_pipe_device_ops );
444 assert( device->pipes );
446 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
447 name->len = 0;
449 return found;
452 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
453 unsigned int sharing, unsigned int options )
455 return grab_object( obj );
458 static void named_pipe_device_destroy( struct object *obj )
460 struct named_pipe_device *device = (struct named_pipe_device*)obj;
461 assert( obj->ops == &named_pipe_device_ops );
462 if (device->fd) release_object( device->fd );
463 free( device->pipes );
466 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
468 return FD_TYPE_DEVICE;
471 void create_named_pipe_device( struct directory *root, const struct unicode_str *name )
473 struct named_pipe_device *dev;
475 if ((dev = create_named_object_dir( root, name, 0, &named_pipe_device_ops )) &&
476 get_error() != STATUS_OBJECT_NAME_EXISTS)
478 dev->pipes = NULL;
479 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
480 !(dev->pipes = create_namespace( 7 )))
482 release_object( dev );
483 dev = NULL;
486 if (dev) make_object_static( &dev->obj );
489 static int pipe_data_remaining( struct pipe_server *server )
491 struct pollfd pfd;
492 int fd;
494 assert( server->client );
496 fd = get_unix_fd( server->client->fd );
497 if (fd < 0)
498 return 0;
499 pfd.fd = fd;
500 pfd.events = POLLIN;
501 pfd.revents = 0;
503 if (0 > poll( &pfd, 1, 0 ))
504 return 0;
506 return pfd.revents&POLLIN;
509 static void check_flushed( void *arg )
511 struct pipe_server *server = (struct pipe_server*) arg;
513 assert( server->event );
514 if (pipe_data_remaining( server ))
516 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
518 else
520 /* notify_empty( server ); */
521 server->flush_poll = NULL;
522 set_event( server->event );
523 release_object( server->event );
524 server->event = NULL;
528 static void pipe_server_flush( struct fd *fd, struct event **event )
530 struct pipe_server *server = get_fd_user( fd );
532 if (!server || server->state != ps_connected_server) return;
534 /* FIXME: if multiple threads flush the same pipe,
535 maybe should create a list of processes to notify */
536 if (server->flush_poll) return;
538 if (pipe_data_remaining( server ))
540 /* this kind of sux -
541 there's no unix way to be alerted when a pipe becomes empty */
542 server->event = create_event( NULL, NULL, 0, 0, 0, NULL );
543 if (!server->event) return;
544 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
545 *event = server->event;
549 static void pipe_client_flush( struct fd *fd, struct event **event )
551 /* FIXME: what do we have to do for this? */
554 static inline int is_overlapped( unsigned int options )
556 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
559 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
561 return FD_TYPE_PIPE;
564 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
566 return FD_TYPE_PIPE;
569 static obj_handle_t alloc_wait_event( struct process *process )
571 obj_handle_t handle = 0;
572 struct event *event = create_event( NULL, NULL, 0, 1, 0, NULL );
574 if (event)
576 handle = alloc_handle( process, event, EVENT_ALL_ACCESS, 0 );
577 release_object( event );
579 return handle;
582 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
583 const void *data, data_size_t size )
585 struct pipe_server *server = get_fd_user( fd );
586 struct async *async;
587 obj_handle_t wait_handle = 0;
589 switch(code)
591 case FSCTL_PIPE_LISTEN:
592 switch(server->state)
594 case ps_idle_server:
595 case ps_wait_connect:
596 if (!async_data->event && !async_data->apc)
598 async_data_t new_data = *async_data;
599 if (!(wait_handle = alloc_wait_event( current->process ))) break;
600 new_data.event = wait_handle;
601 if (!(async = fd_queue_async( server->ioctl_fd, &new_data, ASYNC_TYPE_WAIT, 0 )))
603 close_handle( current->process, wait_handle );
604 break;
607 else async = fd_queue_async( server->ioctl_fd, async_data, ASYNC_TYPE_WAIT, 0 );
609 if (async)
611 set_server_state( server, ps_wait_open );
612 if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
613 release_object( async );
614 set_error( STATUS_PENDING );
615 return wait_handle;
617 break;
618 case ps_connected_server:
619 set_error( STATUS_PIPE_CONNECTED );
620 break;
621 case ps_disconnected_server:
622 set_error( STATUS_PIPE_BUSY );
623 break;
624 case ps_wait_disconnect:
625 set_error( STATUS_NO_DATA_DETECTED );
626 break;
627 case ps_wait_open:
628 set_error( STATUS_INVALID_HANDLE );
629 break;
631 return 0;
633 case FSCTL_PIPE_DISCONNECT:
634 switch(server->state)
636 case ps_connected_server:
637 assert( server->client );
638 assert( server->client->fd );
640 notify_empty( server );
642 /* dump the client and server fds, but keep the pointers
643 around - client loses all waiting data */
644 do_disconnect( server );
645 set_server_state( server, ps_disconnected_server );
646 break;
647 case ps_wait_disconnect:
648 assert( !server->client );
649 do_disconnect( server );
650 set_server_state( server, ps_wait_connect );
651 break;
652 case ps_idle_server:
653 case ps_wait_open:
654 set_error( STATUS_PIPE_LISTENING );
655 break;
656 case ps_disconnected_server:
657 case ps_wait_connect:
658 set_error( STATUS_PIPE_DISCONNECTED );
659 break;
661 return 0;
663 default:
664 return default_fd_ioctl( fd, code, async_data, data, size );
668 static struct named_pipe *create_named_pipe( struct directory *root, const struct unicode_str *name,
669 unsigned int attr )
671 struct object *obj;
672 struct named_pipe *pipe = NULL;
673 struct unicode_str new_name;
675 if (!name || !name->len) return alloc_object( &named_pipe_ops );
677 if (!(obj = find_object_dir( root, name, attr, &new_name )))
679 set_error( STATUS_OBJECT_NAME_INVALID );
680 return NULL;
682 if (!new_name.len)
684 if (attr & OBJ_OPENIF && obj->ops == &named_pipe_ops)
685 set_error( STATUS_OBJECT_NAME_EXISTS );
686 else
688 release_object( obj );
689 obj = NULL;
690 if (attr & OBJ_OPENIF)
691 set_error( STATUS_OBJECT_TYPE_MISMATCH );
692 else
693 set_error( STATUS_OBJECT_NAME_COLLISION );
695 return (struct named_pipe *)obj;
698 if (obj->ops != &named_pipe_device_ops)
699 set_error( STATUS_OBJECT_NAME_INVALID );
700 else
702 struct named_pipe_device *dev = (struct named_pipe_device *)obj;
703 if ((pipe = create_object( dev->pipes, &named_pipe_ops, &new_name, NULL )))
704 clear_error();
707 release_object( obj );
708 return pipe;
711 static struct pipe_server *get_pipe_server_obj( struct process *process,
712 obj_handle_t handle, unsigned int access )
714 struct object *obj;
715 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
716 return (struct pipe_server *) obj;
719 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options )
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;
733 list_add_head( &pipe->servers, &server->entry );
734 grab_object( pipe );
735 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->obj, options )))
737 release_object( server );
738 return NULL;
740 set_server_state( server, ps_idle_server );
741 return server;
744 static struct pipe_client *create_pipe_client( unsigned int flags )
746 struct pipe_client *client;
748 client = alloc_object( &pipe_client_ops );
749 if (!client)
750 return NULL;
752 client->fd = NULL;
753 client->server = NULL;
754 client->flags = flags;
756 return client;
759 static struct pipe_server *find_available_server( struct named_pipe *pipe )
761 struct pipe_server *server;
763 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
765 if (server->state == ps_idle_server || server->state == ps_wait_open)
766 return (struct pipe_server *)grab_object( server );
768 return NULL;
771 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
772 unsigned int sharing, unsigned int options )
774 struct named_pipe *pipe = (struct named_pipe *)obj;
775 struct pipe_server *server;
776 struct pipe_client *client;
777 int fds[2];
779 if (!(server = find_available_server( pipe )))
781 set_error( STATUS_PIPE_NOT_AVAILABLE );
782 return NULL;
785 if ((client = create_pipe_client( options )))
787 if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
789 assert( !server->fd );
791 /* for performance reasons, only set nonblocking mode when using
792 * overlapped I/O. Otherwise, we will be doing too much busy
793 * looping */
794 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
795 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
797 if (pipe->insize)
799 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
800 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
802 if (pipe->outsize)
804 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
805 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
808 client->fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->obj, options );
809 server->fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->obj, server->options );
810 if (client->fd && server->fd)
812 if (server->state == ps_wait_open)
813 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
814 set_server_state( server, ps_connected_server );
815 server->client = client;
816 client->server = server;
818 else
820 release_object( client );
821 client = NULL;
824 else
826 file_set_error();
827 release_object( client );
828 client = NULL;
831 release_object( server );
832 return &client->obj;
835 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
836 const void *data, data_size_t size )
838 struct named_pipe_device *device = get_fd_user( fd );
840 switch(code)
842 case FSCTL_PIPE_WAIT:
844 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = data;
845 obj_handle_t wait_handle = 0;
846 struct named_pipe *pipe;
847 struct pipe_server *server;
848 struct unicode_str name;
850 if (size < sizeof(*buffer) ||
851 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
853 set_error( STATUS_INVALID_PARAMETER );
854 return 0;
856 name.str = buffer->Name;
857 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
858 if (!(pipe = (struct named_pipe *)find_object( device->pipes, &name, OBJ_CASE_INSENSITIVE )))
860 set_error( STATUS_PIPE_NOT_AVAILABLE );
861 return 0;
863 if (!(server = find_available_server( pipe )))
865 struct async *async;
867 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
869 if (!async_data->event && !async_data->apc)
871 async_data_t new_data = *async_data;
872 if (!(wait_handle = alloc_wait_event( current->process ))) goto done;
873 new_data.event = wait_handle;
874 if (!(async = create_async( current, pipe->waiters, &new_data )))
876 close_handle( current->process, wait_handle );
877 wait_handle = 0;
880 else async = create_async( current, pipe->waiters, async_data );
882 if (async)
884 timeout_t when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
885 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
886 release_object( async );
887 set_error( STATUS_PENDING );
890 else release_object( server );
892 done:
893 release_object( pipe );
894 return wait_handle;
897 default:
898 return default_fd_ioctl( fd, code, async_data, data, size );
903 DECL_HANDLER(create_named_pipe)
905 struct named_pipe *pipe;
906 struct pipe_server *server;
907 struct unicode_str name;
908 struct directory *root = NULL;
910 reply->handle = 0;
911 get_req_unicode_str( &name );
912 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
913 return;
915 pipe = create_named_pipe( root, &name, req->attributes | OBJ_OPENIF );
917 if (root) release_object( root );
918 if (!pipe) return;
920 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
922 /* initialize it if it didn't already exist */
923 pipe->instances = 0;
924 pipe->waiters = NULL;
925 list_init( &pipe->servers );
926 pipe->insize = req->insize;
927 pipe->outsize = req->outsize;
928 pipe->maxinstances = req->maxinstances;
929 pipe->timeout = req->timeout;
930 pipe->flags = req->flags;
932 else
934 if (pipe->maxinstances <= pipe->instances)
936 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
937 release_object( pipe );
938 return;
940 if ((pipe->maxinstances != req->maxinstances) ||
941 (pipe->timeout != req->timeout) ||
942 (pipe->flags != req->flags))
944 set_error( STATUS_ACCESS_DENIED );
945 release_object( pipe );
946 return;
948 clear_error(); /* clear the name collision */
951 server = create_pipe_server( pipe, req->options );
952 if (server)
954 reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
955 server->pipe->instances++;
956 release_object( server );
959 release_object( pipe );
962 DECL_HANDLER(get_named_pipe_info)
964 struct pipe_server *server;
965 struct pipe_client *client = NULL;
967 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
968 if (!server)
970 clear_error();
971 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
972 FILE_READ_ATTRIBUTES, &pipe_client_ops );
973 if (!client) return;
974 server = client->server;
977 reply->flags = server->pipe->flags;
978 reply->maxinstances = server->pipe->maxinstances;
979 reply->instances = server->pipe->instances;
980 reply->insize = server->pipe->insize;
981 reply->outsize = server->pipe->outsize;
983 if (client)
984 release_object(client);
985 else
987 reply->flags |= NAMED_PIPE_SERVER_END;
988 release_object(server);