oleaut32: Resize 32-bit typeinfos when loaded in 64-bit.
[wine.git] / server / named_pipe.c
blob590adcab76b91f5f9375efaebc76f55c539bbbdc
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 sharing;
95 unsigned int maxinstances;
96 unsigned int outsize;
97 unsigned int insize;
98 unsigned int instances;
99 timeout_t timeout;
100 struct list servers; /* list of servers using this pipe */
101 struct async_queue *waiters; /* list of clients waiting to connect */
104 struct named_pipe_device
106 struct object obj; /* object header */
107 struct fd *fd; /* pseudo-fd for ioctls */
108 struct namespace *pipes; /* named pipe namespace */
111 static void named_pipe_dump( struct object *obj, int verbose );
112 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
113 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
114 unsigned int sharing, unsigned int options );
115 static void named_pipe_destroy( struct object *obj );
117 static const struct object_ops named_pipe_ops =
119 sizeof(struct named_pipe), /* size */
120 named_pipe_dump, /* dump */
121 no_get_type, /* get_type */
122 no_add_queue, /* add_queue */
123 NULL, /* remove_queue */
124 NULL, /* signaled */
125 NULL, /* satisfied */
126 no_signal, /* signal */
127 no_get_fd, /* get_fd */
128 named_pipe_map_access, /* map_access */
129 default_get_sd, /* get_sd */
130 default_set_sd, /* set_sd */
131 no_lookup_name, /* lookup_name */
132 named_pipe_open_file, /* open_file */
133 no_close_handle, /* close_handle */
134 named_pipe_destroy /* destroy */
137 /* server end functions */
138 static void pipe_server_dump( struct object *obj, int verbose );
139 static struct fd *pipe_server_get_fd( struct object *obj );
140 static void pipe_server_destroy( struct object *obj);
141 static void pipe_server_flush( struct fd *fd, struct event **event );
142 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd );
143 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
144 int blocking, const void *data, data_size_t size );
146 static const struct object_ops pipe_server_ops =
148 sizeof(struct pipe_server), /* size */
149 pipe_server_dump, /* dump */
150 no_get_type, /* get_type */
151 add_queue, /* add_queue */
152 remove_queue, /* remove_queue */
153 default_fd_signaled, /* signaled */
154 no_satisfied, /* satisfied */
155 no_signal, /* signal */
156 pipe_server_get_fd, /* get_fd */
157 default_fd_map_access, /* map_access */
158 default_get_sd, /* get_sd */
159 default_set_sd, /* set_sd */
160 no_lookup_name, /* lookup_name */
161 no_open_file, /* open_file */
162 fd_close_handle, /* close_handle */
163 pipe_server_destroy /* destroy */
166 static const struct fd_ops pipe_server_fd_ops =
168 default_fd_get_poll_events, /* get_poll_events */
169 default_poll_event, /* poll_event */
170 pipe_server_flush, /* flush */
171 pipe_server_get_fd_type, /* get_fd_type */
172 pipe_server_ioctl, /* ioctl */
173 default_fd_queue_async, /* queue_async */
174 default_fd_reselect_async, /* reselect_async */
175 default_fd_cancel_async, /* cancel_async */
178 /* client end functions */
179 static void pipe_client_dump( struct object *obj, int verbose );
180 static int pipe_client_signaled( struct object *obj, struct thread *thread );
181 static struct fd *pipe_client_get_fd( struct object *obj );
182 static void pipe_client_destroy( struct object *obj );
183 static void pipe_client_flush( struct fd *fd, struct event **event );
184 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd );
186 static const struct object_ops pipe_client_ops =
188 sizeof(struct pipe_client), /* size */
189 pipe_client_dump, /* dump */
190 no_get_type, /* get_type */
191 add_queue, /* add_queue */
192 remove_queue, /* remove_queue */
193 pipe_client_signaled, /* signaled */
194 no_satisfied, /* satisfied */
195 no_signal, /* signal */
196 pipe_client_get_fd, /* get_fd */
197 default_fd_map_access, /* map_access */
198 default_get_sd, /* get_sd */
199 default_set_sd, /* set_sd */
200 no_lookup_name, /* lookup_name */
201 no_open_file, /* open_file */
202 fd_close_handle, /* close_handle */
203 pipe_client_destroy /* destroy */
206 static const struct fd_ops pipe_client_fd_ops =
208 default_fd_get_poll_events, /* get_poll_events */
209 default_poll_event, /* poll_event */
210 pipe_client_flush, /* flush */
211 pipe_client_get_fd_type, /* get_fd_type */
212 default_fd_ioctl, /* ioctl */
213 default_fd_queue_async, /* queue_async */
214 default_fd_reselect_async, /* reselect_async */
215 default_fd_cancel_async /* cancel_async */
218 static void named_pipe_device_dump( struct object *obj, int verbose );
219 static struct object_type *named_pipe_device_get_type( struct object *obj );
220 static struct fd *named_pipe_device_get_fd( struct object *obj );
221 static struct object *named_pipe_device_lookup_name( struct object *obj,
222 struct unicode_str *name, unsigned int attr );
223 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
224 unsigned int sharing, unsigned int options );
225 static void named_pipe_device_destroy( struct object *obj );
226 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
227 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
228 int blocking, const void *data, data_size_t size );
230 static const struct object_ops named_pipe_device_ops =
232 sizeof(struct named_pipe_device), /* size */
233 named_pipe_device_dump, /* dump */
234 named_pipe_device_get_type, /* get_type */
235 no_add_queue, /* add_queue */
236 NULL, /* remove_queue */
237 NULL, /* signaled */
238 no_satisfied, /* satisfied */
239 no_signal, /* signal */
240 named_pipe_device_get_fd, /* get_fd */
241 no_map_access, /* map_access */
242 default_get_sd, /* get_sd */
243 default_set_sd, /* set_sd */
244 named_pipe_device_lookup_name, /* lookup_name */
245 named_pipe_device_open_file, /* open_file */
246 fd_close_handle, /* close_handle */
247 named_pipe_device_destroy /* destroy */
250 static const struct fd_ops named_pipe_device_fd_ops =
252 default_fd_get_poll_events, /* get_poll_events */
253 default_poll_event, /* poll_event */
254 no_flush, /* flush */
255 named_pipe_device_get_fd_type, /* get_fd_type */
256 named_pipe_device_ioctl, /* ioctl */
257 default_fd_queue_async, /* queue_async */
258 default_fd_reselect_async, /* reselect_async */
259 default_fd_cancel_async /* cancel_async */
262 static void named_pipe_dump( struct object *obj, int verbose )
264 struct named_pipe *pipe = (struct named_pipe *) obj;
265 assert( obj->ops == &named_pipe_ops );
266 fprintf( stderr, "Named pipe " );
267 dump_object_name( &pipe->obj );
268 fprintf( stderr, "\n" );
271 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
273 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
274 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
275 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
276 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
277 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
280 static void pipe_server_dump( struct object *obj, int verbose )
282 struct pipe_server *server = (struct pipe_server *) obj;
283 assert( obj->ops == &pipe_server_ops );
284 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
287 static void pipe_client_dump( struct object *obj, int verbose )
289 struct pipe_client *client = (struct pipe_client *) obj;
290 assert( obj->ops == &pipe_client_ops );
291 fprintf( stderr, "Named pipe client server=%p\n", client->server );
294 static int pipe_client_signaled( struct object *obj, struct thread *thread )
296 struct pipe_client *client = (struct pipe_client *) obj;
298 return client->fd && is_fd_signaled(client->fd);
301 static void named_pipe_destroy( struct object *obj)
303 struct named_pipe *pipe = (struct named_pipe *) obj;
305 assert( list_empty( &pipe->servers ) );
306 assert( !pipe->instances );
307 free_async_queue( pipe->waiters );
310 static struct fd *pipe_client_get_fd( struct object *obj )
312 struct pipe_client *client = (struct pipe_client *) obj;
313 if (client->fd)
314 return (struct fd *) grab_object( client->fd );
315 set_error( STATUS_PIPE_DISCONNECTED );
316 return NULL;
319 static void set_server_state( struct pipe_server *server, enum pipe_state state )
321 server->state = state;
323 switch(state)
325 case ps_connected_server:
326 case ps_wait_disconnect:
327 assert( server->fd );
328 break;
329 case ps_wait_open:
330 case ps_idle_server:
331 assert( !server->fd );
332 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
333 break;
334 case ps_disconnected_server:
335 case ps_wait_connect:
336 assert( !server->fd );
337 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
338 break;
342 static struct fd *pipe_server_get_fd( struct object *obj )
344 struct pipe_server *server = (struct pipe_server *) obj;
346 return (struct fd *)grab_object( server->fd ? server->fd : server->ioctl_fd );
350 static void notify_empty( struct pipe_server *server )
352 if (!server->flush_poll)
353 return;
354 assert( server->state == ps_connected_server );
355 assert( server->event );
356 remove_timeout_user( server->flush_poll );
357 server->flush_poll = NULL;
358 set_event( server->event );
359 release_object( server->event );
360 server->event = NULL;
363 static void do_disconnect( struct pipe_server *server )
365 /* we may only have a server fd, if the client disconnected */
366 if (server->client)
368 assert( server->client->server == server );
369 assert( server->client->fd );
370 release_object( server->client->fd );
371 server->client->fd = NULL;
373 assert( server->fd );
374 shutdown( get_unix_fd( server->fd ), SHUT_RDWR );
375 release_object( server->fd );
376 server->fd = NULL;
379 static void pipe_server_destroy( struct object *obj)
381 struct pipe_server *server = (struct pipe_server *)obj;
383 assert( obj->ops == &pipe_server_ops );
385 if (server->fd)
387 notify_empty( server );
388 do_disconnect( server );
391 if (server->client)
393 server->client->server = NULL;
394 server->client = NULL;
397 assert( server->pipe->instances );
398 server->pipe->instances--;
400 if (server->ioctl_fd) release_object( server->ioctl_fd );
401 list_remove( &server->entry );
402 release_object( server->pipe );
405 static void pipe_client_destroy( struct object *obj)
407 struct pipe_client *client = (struct pipe_client *)obj;
408 struct pipe_server *server = client->server;
410 assert( obj->ops == &pipe_client_ops );
412 if (server)
414 notify_empty( server );
416 switch(server->state)
418 case ps_connected_server:
419 /* Don't destroy the server's fd here as we can't
420 do a successful flush without it. */
421 set_server_state( server, ps_wait_disconnect );
422 break;
423 case ps_disconnected_server:
424 set_server_state( server, ps_wait_connect );
425 break;
426 case ps_idle_server:
427 case ps_wait_open:
428 case ps_wait_disconnect:
429 case ps_wait_connect:
430 assert( 0 );
432 assert( server->client );
433 server->client = NULL;
434 client->server = NULL;
436 if (client->fd) release_object( client->fd );
439 static void named_pipe_device_dump( struct object *obj, int verbose )
441 assert( obj->ops == &named_pipe_device_ops );
442 fprintf( stderr, "Named pipe device\n" );
445 static struct object_type *named_pipe_device_get_type( struct object *obj )
447 static const WCHAR name[] = {'D','e','v','i','c','e'};
448 static const struct unicode_str str = { name, sizeof(name) };
449 return get_object_type( &str );
452 static struct fd *named_pipe_device_get_fd( struct object *obj )
454 struct named_pipe_device *device = (struct named_pipe_device *)obj;
455 return (struct fd *)grab_object( device->fd );
458 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
459 unsigned int attr )
461 struct named_pipe_device *device = (struct named_pipe_device*)obj;
462 struct object *found;
464 assert( obj->ops == &named_pipe_device_ops );
465 assert( device->pipes );
467 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
468 name->len = 0;
470 return found;
473 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
474 unsigned int sharing, unsigned int options )
476 return grab_object( obj );
479 static void named_pipe_device_destroy( struct object *obj )
481 struct named_pipe_device *device = (struct named_pipe_device*)obj;
482 assert( obj->ops == &named_pipe_device_ops );
483 if (device->fd) release_object( device->fd );
484 free( device->pipes );
487 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
489 return FD_TYPE_DEVICE;
492 void create_named_pipe_device( struct directory *root, const struct unicode_str *name )
494 struct named_pipe_device *dev;
496 if ((dev = create_named_object_dir( root, name, 0, &named_pipe_device_ops )) &&
497 get_error() != STATUS_OBJECT_NAME_EXISTS)
499 dev->pipes = NULL;
500 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
501 !(dev->pipes = create_namespace( 7 )))
503 release_object( dev );
504 dev = NULL;
507 if (dev) make_object_static( &dev->obj );
510 static int pipe_data_remaining( struct pipe_server *server )
512 struct pollfd pfd;
513 int fd;
515 assert( server->client );
517 fd = get_unix_fd( server->client->fd );
518 if (fd < 0)
519 return 0;
520 pfd.fd = fd;
521 pfd.events = POLLIN;
522 pfd.revents = 0;
524 if (0 > poll( &pfd, 1, 0 ))
525 return 0;
527 return pfd.revents&POLLIN;
530 static void check_flushed( void *arg )
532 struct pipe_server *server = (struct pipe_server*) arg;
534 assert( server->event );
535 if (pipe_data_remaining( server ))
537 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
539 else
541 /* notify_empty( server ); */
542 server->flush_poll = NULL;
543 set_event( server->event );
544 release_object( server->event );
545 server->event = NULL;
549 static void pipe_server_flush( struct fd *fd, struct event **event )
551 struct pipe_server *server = get_fd_user( fd );
553 if (!server || server->state != ps_connected_server) return;
555 /* FIXME: if multiple threads flush the same pipe,
556 maybe should create a list of processes to notify */
557 if (server->flush_poll) return;
559 if (pipe_data_remaining( server ))
561 /* this kind of sux -
562 there's no unix way to be alerted when a pipe becomes empty */
563 server->event = create_event( NULL, NULL, 0, 0, 0, NULL );
564 if (!server->event) return;
565 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
566 *event = server->event;
570 static void pipe_client_flush( struct fd *fd, struct event **event )
572 /* FIXME: what do we have to do for this? */
575 static inline int is_overlapped( unsigned int options )
577 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
580 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
582 return FD_TYPE_PIPE;
585 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
587 return FD_TYPE_PIPE;
590 static obj_handle_t alloc_wait_event( struct process *process )
592 obj_handle_t handle = 0;
593 struct event *event = create_event( NULL, NULL, 0, 1, 0, NULL );
595 if (event)
597 handle = alloc_handle( process, event, EVENT_ALL_ACCESS, 0 );
598 release_object( event );
600 return handle;
603 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
604 int blocking, const void *data, data_size_t size )
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, data, size );
689 static struct named_pipe *create_named_pipe( struct directory *root, const struct unicode_str *name,
690 unsigned int attr )
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 )
742 struct pipe_server *server;
744 server = alloc_object( &pipe_server_ops );
745 if (!server)
746 return NULL;
748 server->fd = NULL;
749 server->pipe = pipe;
750 server->client = NULL;
751 server->flush_poll = NULL;
752 server->options = options;
754 list_add_head( &pipe->servers, &server->entry );
755 grab_object( pipe );
756 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->obj, options )))
758 release_object( server );
759 return NULL;
761 set_server_state( server, ps_idle_server );
762 return server;
765 static struct pipe_client *create_pipe_client( unsigned int flags )
767 struct pipe_client *client;
769 client = alloc_object( &pipe_client_ops );
770 if (!client)
771 return NULL;
773 client->fd = NULL;
774 client->server = NULL;
775 client->flags = flags;
777 return client;
780 static struct pipe_server *find_available_server( struct named_pipe *pipe )
782 struct pipe_server *server;
784 /* look for pipe servers that are listening */
785 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
787 if (server->state == ps_wait_open)
788 return (struct pipe_server *)grab_object( server );
791 /* fall back to pipe servers that are idle */
792 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
794 if (server->state == ps_idle_server)
795 return (struct pipe_server *)grab_object( server );
798 return NULL;
801 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
802 unsigned int sharing, unsigned int options )
804 struct named_pipe *pipe = (struct named_pipe *)obj;
805 struct pipe_server *server;
806 struct pipe_client *client;
807 unsigned int pipe_sharing;
808 int fds[2];
810 if (!(server = find_available_server( pipe )))
812 set_error( STATUS_PIPE_NOT_AVAILABLE );
813 return NULL;
816 pipe_sharing = server->pipe->sharing;
817 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
818 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
820 set_error( STATUS_ACCESS_DENIED );
821 release_object( server );
822 return NULL;
825 if ((client = create_pipe_client( options )))
827 if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
829 assert( !server->fd );
831 /* for performance reasons, only set nonblocking mode when using
832 * overlapped I/O. Otherwise, we will be doing too much busy
833 * looping */
834 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
835 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
837 if (pipe->insize)
839 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
840 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
842 if (pipe->outsize)
844 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
845 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
848 client->fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->obj, options );
849 server->fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->obj, server->options );
850 if (client->fd && server->fd)
852 allow_fd_caching( client->fd );
853 allow_fd_caching( server->fd );
854 fd_copy_completion( server->ioctl_fd, server->fd );
855 if (server->state == ps_wait_open)
856 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
857 set_server_state( server, ps_connected_server );
858 server->client = client;
859 client->server = server;
861 else
863 release_object( client );
864 client = NULL;
867 else
869 file_set_error();
870 release_object( client );
871 client = NULL;
874 release_object( server );
875 return &client->obj;
878 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
879 int blocking, const void *data, data_size_t size )
881 struct named_pipe_device *device = get_fd_user( fd );
883 switch(code)
885 case FSCTL_PIPE_WAIT:
887 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = data;
888 obj_handle_t wait_handle = 0;
889 struct named_pipe *pipe;
890 struct pipe_server *server;
891 struct unicode_str name;
893 if (size < sizeof(*buffer) ||
894 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
896 set_error( STATUS_INVALID_PARAMETER );
897 return 0;
899 name.str = buffer->Name;
900 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
901 if (!(pipe = (struct named_pipe *)find_object( device->pipes, &name, OBJ_CASE_INSENSITIVE )))
903 set_error( STATUS_PIPE_NOT_AVAILABLE );
904 return 0;
906 if (!(server = find_available_server( pipe )))
908 struct async *async;
910 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
912 if (blocking)
914 async_data_t new_data = *async_data;
915 if (!(wait_handle = alloc_wait_event( current->process ))) goto done;
916 new_data.event = wait_handle;
917 if (!(async = create_async( current, pipe->waiters, &new_data )))
919 close_handle( current->process, wait_handle );
920 wait_handle = 0;
923 else async = create_async( current, pipe->waiters, async_data );
925 if (async)
927 timeout_t when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
928 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
929 release_object( async );
930 set_error( STATUS_PENDING );
933 else release_object( server );
935 done:
936 release_object( pipe );
937 return wait_handle;
940 default:
941 return default_fd_ioctl( fd, code, async_data, blocking, data, size );
946 DECL_HANDLER(create_named_pipe)
948 struct named_pipe *pipe;
949 struct pipe_server *server;
950 struct unicode_str name;
951 struct directory *root = NULL;
953 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
954 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
956 set_error( STATUS_INVALID_PARAMETER );
957 return;
960 reply->handle = 0;
961 get_req_unicode_str( &name );
962 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
963 return;
965 pipe = create_named_pipe( root, &name, req->attributes | OBJ_OPENIF );
967 if (root) release_object( root );
968 if (!pipe) return;
970 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
972 /* initialize it if it didn't already exist */
973 pipe->instances = 0;
974 pipe->waiters = NULL;
975 list_init( &pipe->servers );
976 pipe->insize = req->insize;
977 pipe->outsize = req->outsize;
978 pipe->maxinstances = req->maxinstances;
979 pipe->timeout = req->timeout;
980 pipe->flags = req->flags;
981 pipe->sharing = req->sharing;
983 else
985 if (pipe->maxinstances <= pipe->instances)
987 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
988 release_object( pipe );
989 return;
991 if (pipe->sharing != req->sharing)
993 set_error( STATUS_ACCESS_DENIED );
994 release_object( pipe );
995 return;
997 clear_error(); /* clear the name collision */
1000 server = create_pipe_server( pipe, req->options );
1001 if (server)
1003 reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
1004 server->pipe->instances++;
1005 release_object( server );
1008 release_object( pipe );
1011 DECL_HANDLER(get_named_pipe_info)
1013 struct pipe_server *server;
1014 struct pipe_client *client = NULL;
1016 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1017 if (!server)
1019 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1020 return;
1022 clear_error();
1023 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1024 0, &pipe_client_ops );
1025 if (!client) return;
1026 server = client->server;
1029 reply->flags = server->pipe->flags;
1030 reply->sharing = server->pipe->sharing;
1031 reply->maxinstances = server->pipe->maxinstances;
1032 reply->instances = server->pipe->instances;
1033 reply->insize = server->pipe->insize;
1034 reply->outsize = server->pipe->outsize;
1036 if (client)
1037 release_object(client);
1038 else
1040 reply->flags |= NAMED_PIPE_SERVER_END;
1041 release_object(server);