server: Also crop the update region of children when moving a window.
[wine.git] / server / named_pipe.c
blob6db9a7a05c2252525293b6b5c26b4cc37fc957aa
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_get_type, /* get_type */
121 no_add_queue, /* add_queue */
122 NULL, /* remove_queue */
123 NULL, /* signaled */
124 NULL, /* satisfied */
125 no_signal, /* signal */
126 no_get_fd, /* get_fd */
127 named_pipe_map_access, /* map_access */
128 default_get_sd, /* get_sd */
129 default_set_sd, /* set_sd */
130 no_lookup_name, /* lookup_name */
131 named_pipe_open_file, /* open_file */
132 no_close_handle, /* close_handle */
133 named_pipe_destroy /* destroy */
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 no_get_type, /* get_type */
150 add_queue, /* add_queue */
151 remove_queue, /* remove_queue */
152 default_fd_signaled, /* signaled */
153 no_satisfied, /* satisfied */
154 no_signal, /* signal */
155 pipe_server_get_fd, /* get_fd */
156 default_fd_map_access, /* map_access */
157 default_get_sd, /* get_sd */
158 default_set_sd, /* set_sd */
159 no_lookup_name, /* lookup_name */
160 no_open_file, /* open_file */
161 fd_close_handle, /* close_handle */
162 pipe_server_destroy /* destroy */
165 static const struct fd_ops pipe_server_fd_ops =
167 default_fd_get_poll_events, /* get_poll_events */
168 default_poll_event, /* poll_event */
169 pipe_server_flush, /* flush */
170 pipe_server_get_fd_type, /* get_fd_type */
171 pipe_server_ioctl, /* ioctl */
172 default_fd_queue_async, /* queue_async */
173 default_fd_reselect_async, /* reselect_async */
174 default_fd_cancel_async, /* cancel_async */
177 /* client end functions */
178 static void pipe_client_dump( struct object *obj, int verbose );
179 static struct fd *pipe_client_get_fd( struct object *obj );
180 static void pipe_client_destroy( struct object *obj );
181 static void pipe_client_flush( struct fd *fd, struct event **event );
182 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd );
184 static const struct object_ops pipe_client_ops =
186 sizeof(struct pipe_client), /* size */
187 pipe_client_dump, /* dump */
188 no_get_type, /* get_type */
189 add_queue, /* add_queue */
190 remove_queue, /* remove_queue */
191 default_fd_signaled, /* signaled */
192 no_satisfied, /* satisfied */
193 no_signal, /* signal */
194 pipe_client_get_fd, /* get_fd */
195 default_fd_map_access, /* map_access */
196 default_get_sd, /* get_sd */
197 default_set_sd, /* set_sd */
198 no_lookup_name, /* lookup_name */
199 no_open_file, /* open_file */
200 fd_close_handle, /* close_handle */
201 pipe_client_destroy /* destroy */
204 static const struct fd_ops pipe_client_fd_ops =
206 default_fd_get_poll_events, /* get_poll_events */
207 default_poll_event, /* poll_event */
208 pipe_client_flush, /* flush */
209 pipe_client_get_fd_type, /* get_fd_type */
210 default_fd_ioctl, /* ioctl */
211 default_fd_queue_async, /* queue_async */
212 default_fd_reselect_async, /* reselect_async */
213 default_fd_cancel_async /* cancel_async */
216 static void named_pipe_device_dump( struct object *obj, int verbose );
217 static struct object_type *named_pipe_device_get_type( struct object *obj );
218 static struct fd *named_pipe_device_get_fd( struct object *obj );
219 static struct object *named_pipe_device_lookup_name( struct object *obj,
220 struct unicode_str *name, unsigned int attr );
221 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
222 unsigned int sharing, unsigned int options );
223 static void named_pipe_device_destroy( struct object *obj );
224 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
225 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
226 const void *data, data_size_t size );
228 static const struct object_ops named_pipe_device_ops =
230 sizeof(struct named_pipe_device), /* size */
231 named_pipe_device_dump, /* dump */
232 named_pipe_device_get_type, /* get_type */
233 no_add_queue, /* add_queue */
234 NULL, /* remove_queue */
235 NULL, /* signaled */
236 no_satisfied, /* satisfied */
237 no_signal, /* signal */
238 named_pipe_device_get_fd, /* get_fd */
239 no_map_access, /* map_access */
240 default_get_sd, /* get_sd */
241 default_set_sd, /* set_sd */
242 named_pipe_device_lookup_name, /* lookup_name */
243 named_pipe_device_open_file, /* open_file */
244 fd_close_handle, /* close_handle */
245 named_pipe_device_destroy /* destroy */
248 static const struct fd_ops named_pipe_device_fd_ops =
250 default_fd_get_poll_events, /* get_poll_events */
251 default_poll_event, /* poll_event */
252 no_flush, /* flush */
253 named_pipe_device_get_fd_type, /* get_fd_type */
254 named_pipe_device_ioctl, /* ioctl */
255 default_fd_queue_async, /* queue_async */
256 default_fd_reselect_async, /* reselect_async */
257 default_fd_cancel_async /* cancel_async */
260 static void named_pipe_dump( struct object *obj, int verbose )
262 struct named_pipe *pipe = (struct named_pipe *) obj;
263 assert( obj->ops == &named_pipe_ops );
264 fprintf( stderr, "Named pipe " );
265 dump_object_name( &pipe->obj );
266 fprintf( stderr, "\n" );
269 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
271 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
272 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
273 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
274 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
275 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
278 static void pipe_server_dump( struct object *obj, int verbose )
280 struct pipe_server *server = (struct pipe_server *) obj;
281 assert( obj->ops == &pipe_server_ops );
282 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
285 static void pipe_client_dump( struct object *obj, int verbose )
287 struct pipe_client *client = (struct pipe_client *) obj;
288 assert( obj->ops == &pipe_client_ops );
289 fprintf( stderr, "Named pipe client server=%p\n", client->server );
292 static void named_pipe_destroy( struct object *obj)
294 struct named_pipe *pipe = (struct named_pipe *) obj;
296 assert( list_empty( &pipe->servers ) );
297 assert( !pipe->instances );
298 free_async_queue( pipe->waiters );
301 static struct fd *pipe_client_get_fd( struct object *obj )
303 struct pipe_client *client = (struct pipe_client *) obj;
304 if (client->fd)
305 return (struct fd *) grab_object( client->fd );
306 set_error( STATUS_PIPE_DISCONNECTED );
307 return NULL;
310 static void set_server_state( struct pipe_server *server, enum pipe_state state )
312 server->state = state;
314 switch(state)
316 case ps_connected_server:
317 case ps_wait_disconnect:
318 assert( server->fd );
319 break;
320 case ps_wait_open:
321 case ps_idle_server:
322 assert( !server->fd );
323 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
324 break;
325 case ps_disconnected_server:
326 case ps_wait_connect:
327 assert( !server->fd );
328 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
329 break;
333 static struct fd *pipe_server_get_fd( struct object *obj )
335 struct pipe_server *server = (struct pipe_server *) obj;
337 return (struct fd *)grab_object( server->fd ? server->fd : server->ioctl_fd );
341 static void notify_empty( struct pipe_server *server )
343 if (!server->flush_poll)
344 return;
345 assert( server->state == ps_connected_server );
346 assert( server->event );
347 remove_timeout_user( server->flush_poll );
348 server->flush_poll = NULL;
349 set_event( server->event );
350 release_object( server->event );
351 server->event = NULL;
354 static void do_disconnect( struct pipe_server *server )
356 /* we may only have a server fd, if the client disconnected */
357 if (server->client)
359 assert( server->client->server == server );
360 assert( server->client->fd );
361 release_object( server->client->fd );
362 server->client->fd = NULL;
364 assert( server->fd );
365 shutdown( get_unix_fd( server->fd ), SHUT_RDWR );
366 release_object( server->fd );
367 server->fd = NULL;
370 static void pipe_server_destroy( struct object *obj)
372 struct pipe_server *server = (struct pipe_server *)obj;
374 assert( obj->ops == &pipe_server_ops );
376 if (server->fd)
378 notify_empty( server );
379 do_disconnect( server );
382 if (server->client)
384 server->client->server = NULL;
385 server->client = NULL;
388 assert( server->pipe->instances );
389 server->pipe->instances--;
391 if (server->ioctl_fd) release_object( server->ioctl_fd );
392 list_remove( &server->entry );
393 release_object( server->pipe );
396 static void pipe_client_destroy( struct object *obj)
398 struct pipe_client *client = (struct pipe_client *)obj;
399 struct pipe_server *server = client->server;
401 assert( obj->ops == &pipe_client_ops );
403 if (server)
405 notify_empty( server );
407 switch(server->state)
409 case ps_connected_server:
410 /* Don't destroy the server's fd here as we can't
411 do a successful flush without it. */
412 set_server_state( server, ps_wait_disconnect );
413 break;
414 case ps_disconnected_server:
415 set_server_state( server, ps_wait_connect );
416 break;
417 case ps_idle_server:
418 case ps_wait_open:
419 case ps_wait_disconnect:
420 case ps_wait_connect:
421 assert( 0 );
423 assert( server->client );
424 server->client = NULL;
425 client->server = NULL;
427 if (client->fd) release_object( client->fd );
430 static void named_pipe_device_dump( struct object *obj, int verbose )
432 assert( obj->ops == &named_pipe_device_ops );
433 fprintf( stderr, "Named pipe device\n" );
436 static struct object_type *named_pipe_device_get_type( struct object *obj )
438 static const WCHAR name[] = {'D','e','v','i','c','e'};
439 static const struct unicode_str str = { name, sizeof(name) };
440 return get_object_type( &str );
443 static struct fd *named_pipe_device_get_fd( struct object *obj )
445 struct named_pipe_device *device = (struct named_pipe_device *)obj;
446 return (struct fd *)grab_object( device->fd );
449 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
450 unsigned int attr )
452 struct named_pipe_device *device = (struct named_pipe_device*)obj;
453 struct object *found;
455 assert( obj->ops == &named_pipe_device_ops );
456 assert( device->pipes );
458 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
459 name->len = 0;
461 return found;
464 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
465 unsigned int sharing, unsigned int options )
467 return grab_object( obj );
470 static void named_pipe_device_destroy( struct object *obj )
472 struct named_pipe_device *device = (struct named_pipe_device*)obj;
473 assert( obj->ops == &named_pipe_device_ops );
474 if (device->fd) release_object( device->fd );
475 free( device->pipes );
478 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
480 return FD_TYPE_DEVICE;
483 void create_named_pipe_device( struct directory *root, const struct unicode_str *name )
485 struct named_pipe_device *dev;
487 if ((dev = create_named_object_dir( root, name, 0, &named_pipe_device_ops )) &&
488 get_error() != STATUS_OBJECT_NAME_EXISTS)
490 dev->pipes = NULL;
491 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
492 !(dev->pipes = create_namespace( 7 )))
494 release_object( dev );
495 dev = NULL;
498 if (dev) make_object_static( &dev->obj );
501 static int pipe_data_remaining( struct pipe_server *server )
503 struct pollfd pfd;
504 int fd;
506 assert( server->client );
508 fd = get_unix_fd( server->client->fd );
509 if (fd < 0)
510 return 0;
511 pfd.fd = fd;
512 pfd.events = POLLIN;
513 pfd.revents = 0;
515 if (0 > poll( &pfd, 1, 0 ))
516 return 0;
518 return pfd.revents&POLLIN;
521 static void check_flushed( void *arg )
523 struct pipe_server *server = (struct pipe_server*) arg;
525 assert( server->event );
526 if (pipe_data_remaining( server ))
528 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
530 else
532 /* notify_empty( server ); */
533 server->flush_poll = NULL;
534 set_event( server->event );
535 release_object( server->event );
536 server->event = NULL;
540 static void pipe_server_flush( struct fd *fd, struct event **event )
542 struct pipe_server *server = get_fd_user( fd );
544 if (!server || server->state != ps_connected_server) return;
546 /* FIXME: if multiple threads flush the same pipe,
547 maybe should create a list of processes to notify */
548 if (server->flush_poll) return;
550 if (pipe_data_remaining( server ))
552 /* this kind of sux -
553 there's no unix way to be alerted when a pipe becomes empty */
554 server->event = create_event( NULL, NULL, 0, 0, 0, NULL );
555 if (!server->event) return;
556 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
557 *event = server->event;
561 static void pipe_client_flush( struct fd *fd, struct event **event )
563 /* FIXME: what do we have to do for this? */
566 static inline int is_overlapped( unsigned int options )
568 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
571 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
573 return FD_TYPE_PIPE;
576 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
578 return FD_TYPE_PIPE;
581 static obj_handle_t alloc_wait_event( struct process *process )
583 obj_handle_t handle = 0;
584 struct event *event = create_event( NULL, NULL, 0, 1, 0, NULL );
586 if (event)
588 handle = alloc_handle( process, event, EVENT_ALL_ACCESS, 0 );
589 release_object( event );
591 return handle;
594 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
595 const void *data, data_size_t size )
597 struct pipe_server *server = get_fd_user( fd );
598 struct async *async;
599 obj_handle_t wait_handle = 0;
601 switch(code)
603 case FSCTL_PIPE_LISTEN:
604 switch(server->state)
606 case ps_idle_server:
607 case ps_wait_connect:
608 if (!async_data->event && !async_data->apc)
610 async_data_t new_data = *async_data;
611 if (!(wait_handle = alloc_wait_event( current->process ))) break;
612 new_data.event = wait_handle;
613 if (!(async = fd_queue_async( server->ioctl_fd, &new_data, ASYNC_TYPE_WAIT, 0 )))
615 close_handle( current->process, wait_handle );
616 break;
619 else async = fd_queue_async( server->ioctl_fd, async_data, ASYNC_TYPE_WAIT, 0 );
621 if (async)
623 set_server_state( server, ps_wait_open );
624 if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
625 release_object( async );
626 set_error( STATUS_PENDING );
627 return wait_handle;
629 break;
630 case ps_connected_server:
631 set_error( STATUS_PIPE_CONNECTED );
632 break;
633 case ps_disconnected_server:
634 set_error( STATUS_PIPE_BUSY );
635 break;
636 case ps_wait_disconnect:
637 set_error( STATUS_NO_DATA_DETECTED );
638 break;
639 case ps_wait_open:
640 set_error( STATUS_INVALID_HANDLE );
641 break;
643 return 0;
645 case FSCTL_PIPE_DISCONNECT:
646 switch(server->state)
648 case ps_connected_server:
649 assert( server->client );
650 assert( server->client->fd );
652 notify_empty( server );
654 /* dump the client and server fds, but keep the pointers
655 around - client loses all waiting data */
656 do_disconnect( server );
657 set_server_state( server, ps_disconnected_server );
658 break;
659 case ps_wait_disconnect:
660 assert( !server->client );
661 do_disconnect( server );
662 set_server_state( server, ps_wait_connect );
663 break;
664 case ps_idle_server:
665 case ps_wait_open:
666 set_error( STATUS_PIPE_LISTENING );
667 break;
668 case ps_disconnected_server:
669 case ps_wait_connect:
670 set_error( STATUS_PIPE_DISCONNECTED );
671 break;
673 return 0;
675 default:
676 return default_fd_ioctl( fd, code, async_data, data, size );
680 static struct named_pipe *create_named_pipe( struct directory *root, const struct unicode_str *name,
681 unsigned int attr )
683 struct object *obj;
684 struct named_pipe *pipe = NULL;
685 struct unicode_str new_name;
687 if (!name || !name->len) return alloc_object( &named_pipe_ops );
689 if (!(obj = find_object_dir( root, name, attr, &new_name )))
691 set_error( STATUS_OBJECT_NAME_INVALID );
692 return NULL;
694 if (!new_name.len)
696 if (attr & OBJ_OPENIF && obj->ops == &named_pipe_ops)
697 set_error( STATUS_OBJECT_NAME_EXISTS );
698 else
700 release_object( obj );
701 obj = NULL;
702 if (attr & OBJ_OPENIF)
703 set_error( STATUS_OBJECT_TYPE_MISMATCH );
704 else
705 set_error( STATUS_OBJECT_NAME_COLLISION );
707 return (struct named_pipe *)obj;
710 if (obj->ops != &named_pipe_device_ops)
711 set_error( STATUS_OBJECT_NAME_INVALID );
712 else
714 struct named_pipe_device *dev = (struct named_pipe_device *)obj;
715 if ((pipe = create_object( dev->pipes, &named_pipe_ops, &new_name, NULL )))
716 clear_error();
719 release_object( obj );
720 return pipe;
723 static struct pipe_server *get_pipe_server_obj( struct process *process,
724 obj_handle_t handle, unsigned int access )
726 struct object *obj;
727 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
728 return (struct pipe_server *) obj;
731 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options )
733 struct pipe_server *server;
735 server = alloc_object( &pipe_server_ops );
736 if (!server)
737 return NULL;
739 server->fd = NULL;
740 server->pipe = pipe;
741 server->client = NULL;
742 server->flush_poll = NULL;
743 server->options = options;
745 list_add_head( &pipe->servers, &server->entry );
746 grab_object( pipe );
747 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->obj, options )))
749 release_object( server );
750 return NULL;
752 set_server_state( server, ps_idle_server );
753 return server;
756 static struct pipe_client *create_pipe_client( unsigned int flags )
758 struct pipe_client *client;
760 client = alloc_object( &pipe_client_ops );
761 if (!client)
762 return NULL;
764 client->fd = NULL;
765 client->server = NULL;
766 client->flags = flags;
768 return client;
771 static struct pipe_server *find_available_server( struct named_pipe *pipe )
773 struct pipe_server *server;
775 /* look for pipe servers that are listening */
776 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
778 if (server->state == ps_wait_open)
779 return (struct pipe_server *)grab_object( server );
782 /* fall back to pipe servers that are idle */
783 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
785 if (server->state == ps_idle_server)
786 return (struct pipe_server *)grab_object( server );
789 return NULL;
792 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
793 unsigned int sharing, unsigned int options )
795 struct named_pipe *pipe = (struct named_pipe *)obj;
796 struct pipe_server *server;
797 struct pipe_client *client;
798 int fds[2];
800 if (!(server = find_available_server( pipe )))
802 set_error( STATUS_PIPE_NOT_AVAILABLE );
803 return NULL;
806 if ((client = create_pipe_client( options )))
808 if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
810 assert( !server->fd );
812 /* for performance reasons, only set nonblocking mode when using
813 * overlapped I/O. Otherwise, we will be doing too much busy
814 * looping */
815 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
816 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
818 if (pipe->insize)
820 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
821 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
823 if (pipe->outsize)
825 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
826 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
829 client->fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->obj, options );
830 server->fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->obj, server->options );
831 if (client->fd && server->fd)
833 if (server->state == ps_wait_open)
834 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
835 set_server_state( server, ps_connected_server );
836 server->client = client;
837 client->server = server;
839 else
841 release_object( client );
842 client = NULL;
845 else
847 file_set_error();
848 release_object( client );
849 client = NULL;
852 release_object( server );
853 return &client->obj;
856 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
857 const void *data, data_size_t size )
859 struct named_pipe_device *device = get_fd_user( fd );
861 switch(code)
863 case FSCTL_PIPE_WAIT:
865 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = data;
866 obj_handle_t wait_handle = 0;
867 struct named_pipe *pipe;
868 struct pipe_server *server;
869 struct unicode_str name;
871 if (size < sizeof(*buffer) ||
872 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
874 set_error( STATUS_INVALID_PARAMETER );
875 return 0;
877 name.str = buffer->Name;
878 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
879 if (!(pipe = (struct named_pipe *)find_object( device->pipes, &name, OBJ_CASE_INSENSITIVE )))
881 set_error( STATUS_PIPE_NOT_AVAILABLE );
882 return 0;
884 if (!(server = find_available_server( pipe )))
886 struct async *async;
888 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
890 if (!async_data->event && !async_data->apc)
892 async_data_t new_data = *async_data;
893 if (!(wait_handle = alloc_wait_event( current->process ))) goto done;
894 new_data.event = wait_handle;
895 if (!(async = create_async( current, pipe->waiters, &new_data )))
897 close_handle( current->process, wait_handle );
898 wait_handle = 0;
901 else async = create_async( current, pipe->waiters, async_data );
903 if (async)
905 timeout_t when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
906 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
907 release_object( async );
908 set_error( STATUS_PENDING );
911 else release_object( server );
913 done:
914 release_object( pipe );
915 return wait_handle;
918 default:
919 return default_fd_ioctl( fd, code, async_data, data, size );
924 DECL_HANDLER(create_named_pipe)
926 struct named_pipe *pipe;
927 struct pipe_server *server;
928 struct unicode_str name;
929 struct directory *root = NULL;
931 reply->handle = 0;
932 get_req_unicode_str( &name );
933 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
934 return;
936 pipe = create_named_pipe( root, &name, req->attributes | OBJ_OPENIF );
938 if (root) release_object( root );
939 if (!pipe) return;
941 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
943 /* initialize it if it didn't already exist */
944 pipe->instances = 0;
945 pipe->waiters = NULL;
946 list_init( &pipe->servers );
947 pipe->insize = req->insize;
948 pipe->outsize = req->outsize;
949 pipe->maxinstances = req->maxinstances;
950 pipe->timeout = req->timeout;
951 pipe->flags = req->flags;
953 else
955 if (pipe->maxinstances <= pipe->instances)
957 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
958 release_object( pipe );
959 return;
961 if ((pipe->maxinstances != req->maxinstances) ||
962 (pipe->timeout != req->timeout) ||
963 (pipe->flags != req->flags))
965 set_error( STATUS_ACCESS_DENIED );
966 release_object( pipe );
967 return;
969 clear_error(); /* clear the name collision */
972 server = create_pipe_server( pipe, req->options );
973 if (server)
975 reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
976 server->pipe->instances++;
977 release_object( server );
980 release_object( pipe );
983 DECL_HANDLER(get_named_pipe_info)
985 struct pipe_server *server;
986 struct pipe_client *client = NULL;
988 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
989 if (!server)
991 clear_error();
992 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
993 FILE_READ_ATTRIBUTES, &pipe_client_ops );
994 if (!client) return;
995 server = client->server;
998 reply->flags = server->pipe->flags;
999 reply->maxinstances = server->pipe->maxinstances;
1000 reply->instances = server->pipe->instances;
1001 reply->insize = server->pipe->insize;
1002 reply->outsize = server->pipe->outsize;
1004 if (client)
1005 release_object(client);
1006 else
1008 reply->flags |= NAMED_PIPE_SERVER_END;
1009 release_object(server);