opengl32: Introduce wrappers for glGetIntegerv and glGetStringi. (v3).
[wine/multimedia.git] / server / named_pipe.c
blob706cce888d81dcf2e7065b64a33cf77726de36e7
1 /*
2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2001 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * TODO:
22 * message mode
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
38 #endif
39 #include <time.h>
40 #include <unistd.h>
41 #ifdef HAVE_POLL_H
42 #include <poll.h>
43 #endif
45 #include "ntstatus.h"
46 #define WIN32_NO_STATUS
47 #include "windef.h"
48 #include "winternl.h"
49 #include "winioctl.h"
51 #include "file.h"
52 #include "handle.h"
53 #include "thread.h"
54 #include "request.h"
55 #include "security.h"
57 enum pipe_state
59 ps_idle_server,
60 ps_wait_open,
61 ps_connected_server,
62 ps_wait_disconnect,
63 ps_disconnected_server,
64 ps_wait_connect
67 struct named_pipe;
69 struct pipe_server
71 struct object obj; /* object header */
72 struct fd *fd; /* pipe file descriptor */
73 struct fd *ioctl_fd; /* file descriptor for ioctls when not connected */
74 struct list entry; /* entry in named pipe servers list */
75 enum pipe_state state; /* server state */
76 struct pipe_client *client; /* client that this server is connected to */
77 struct named_pipe *pipe;
78 struct timeout_user *flush_poll;
79 struct event *event;
80 unsigned int options; /* pipe options */
81 unsigned int pipe_flags;
84 struct pipe_client
86 struct object obj; /* object header */
87 struct fd *fd; /* pipe file descriptor */
88 struct pipe_server *server; /* server that this client is connected to */
89 unsigned int flags; /* file flags */
90 unsigned int pipe_flags;
93 struct named_pipe
95 struct object obj; /* object header */
96 unsigned int flags;
97 unsigned int sharing;
98 unsigned int maxinstances;
99 unsigned int outsize;
100 unsigned int insize;
101 unsigned int instances;
102 timeout_t timeout;
103 struct list servers; /* list of servers using this pipe */
104 struct async_queue *waiters; /* list of clients waiting to connect */
107 struct named_pipe_device
109 struct object obj; /* object header */
110 struct fd *fd; /* pseudo-fd for ioctls */
111 struct namespace *pipes; /* named pipe namespace */
114 static void named_pipe_dump( struct object *obj, int verbose );
115 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
116 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
117 unsigned int sharing, unsigned int options );
118 static void named_pipe_destroy( struct object *obj );
120 static const struct object_ops named_pipe_ops =
122 sizeof(struct named_pipe), /* size */
123 named_pipe_dump, /* dump */
124 no_get_type, /* get_type */
125 no_add_queue, /* add_queue */
126 NULL, /* remove_queue */
127 NULL, /* signaled */
128 NULL, /* satisfied */
129 no_signal, /* signal */
130 no_get_fd, /* get_fd */
131 named_pipe_map_access, /* map_access */
132 default_get_sd, /* get_sd */
133 default_set_sd, /* set_sd */
134 no_lookup_name, /* lookup_name */
135 named_pipe_open_file, /* open_file */
136 no_close_handle, /* close_handle */
137 named_pipe_destroy /* destroy */
140 /* server end functions */
141 static void pipe_server_dump( struct object *obj, int verbose );
142 static struct fd *pipe_server_get_fd( struct object *obj );
143 static void pipe_server_destroy( struct object *obj);
144 static void pipe_server_flush( struct fd *fd, struct event **event );
145 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd );
146 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
147 int blocking, const void *data, data_size_t size );
149 static const struct object_ops pipe_server_ops =
151 sizeof(struct pipe_server), /* size */
152 pipe_server_dump, /* dump */
153 no_get_type, /* get_type */
154 add_queue, /* add_queue */
155 remove_queue, /* remove_queue */
156 default_fd_signaled, /* signaled */
157 no_satisfied, /* satisfied */
158 no_signal, /* signal */
159 pipe_server_get_fd, /* get_fd */
160 default_fd_map_access, /* map_access */
161 default_get_sd, /* get_sd */
162 default_set_sd, /* set_sd */
163 no_lookup_name, /* lookup_name */
164 no_open_file, /* open_file */
165 fd_close_handle, /* close_handle */
166 pipe_server_destroy /* destroy */
169 static const struct fd_ops pipe_server_fd_ops =
171 default_fd_get_poll_events, /* get_poll_events */
172 default_poll_event, /* poll_event */
173 pipe_server_flush, /* flush */
174 pipe_server_get_fd_type, /* get_fd_type */
175 pipe_server_ioctl, /* ioctl */
176 default_fd_queue_async, /* queue_async */
177 default_fd_reselect_async, /* reselect_async */
178 default_fd_cancel_async, /* cancel_async */
181 /* client end functions */
182 static void pipe_client_dump( struct object *obj, int verbose );
183 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry );
184 static struct fd *pipe_client_get_fd( struct object *obj );
185 static void pipe_client_destroy( struct object *obj );
186 static void pipe_client_flush( struct fd *fd, struct event **event );
187 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd );
189 static const struct object_ops pipe_client_ops =
191 sizeof(struct pipe_client), /* size */
192 pipe_client_dump, /* dump */
193 no_get_type, /* get_type */
194 add_queue, /* add_queue */
195 remove_queue, /* remove_queue */
196 pipe_client_signaled, /* signaled */
197 no_satisfied, /* satisfied */
198 no_signal, /* signal */
199 pipe_client_get_fd, /* get_fd */
200 default_fd_map_access, /* map_access */
201 default_get_sd, /* get_sd */
202 default_set_sd, /* set_sd */
203 no_lookup_name, /* lookup_name */
204 no_open_file, /* open_file */
205 fd_close_handle, /* close_handle */
206 pipe_client_destroy /* destroy */
209 static const struct fd_ops pipe_client_fd_ops =
211 default_fd_get_poll_events, /* get_poll_events */
212 default_poll_event, /* poll_event */
213 pipe_client_flush, /* flush */
214 pipe_client_get_fd_type, /* get_fd_type */
215 default_fd_ioctl, /* ioctl */
216 default_fd_queue_async, /* queue_async */
217 default_fd_reselect_async, /* reselect_async */
218 default_fd_cancel_async /* cancel_async */
221 static void named_pipe_device_dump( struct object *obj, int verbose );
222 static struct object_type *named_pipe_device_get_type( struct object *obj );
223 static struct fd *named_pipe_device_get_fd( struct object *obj );
224 static struct object *named_pipe_device_lookup_name( struct object *obj,
225 struct unicode_str *name, unsigned int attr );
226 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
227 unsigned int sharing, unsigned int options );
228 static void named_pipe_device_destroy( struct object *obj );
229 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
230 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
231 int blocking, const void *data, data_size_t size );
233 static const struct object_ops named_pipe_device_ops =
235 sizeof(struct named_pipe_device), /* size */
236 named_pipe_device_dump, /* dump */
237 named_pipe_device_get_type, /* get_type */
238 no_add_queue, /* add_queue */
239 NULL, /* remove_queue */
240 NULL, /* signaled */
241 no_satisfied, /* satisfied */
242 no_signal, /* signal */
243 named_pipe_device_get_fd, /* get_fd */
244 no_map_access, /* map_access */
245 default_get_sd, /* get_sd */
246 default_set_sd, /* set_sd */
247 named_pipe_device_lookup_name, /* lookup_name */
248 named_pipe_device_open_file, /* open_file */
249 fd_close_handle, /* close_handle */
250 named_pipe_device_destroy /* destroy */
253 static const struct fd_ops named_pipe_device_fd_ops =
255 default_fd_get_poll_events, /* get_poll_events */
256 default_poll_event, /* poll_event */
257 no_flush, /* flush */
258 named_pipe_device_get_fd_type, /* get_fd_type */
259 named_pipe_device_ioctl, /* ioctl */
260 default_fd_queue_async, /* queue_async */
261 default_fd_reselect_async, /* reselect_async */
262 default_fd_cancel_async /* cancel_async */
265 static void named_pipe_dump( struct object *obj, int verbose )
267 struct named_pipe *pipe = (struct named_pipe *) obj;
268 assert( obj->ops == &named_pipe_ops );
269 fprintf( stderr, "Named pipe " );
270 dump_object_name( &pipe->obj );
271 fprintf( stderr, "\n" );
274 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
276 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
277 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
278 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
279 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
280 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
283 static void pipe_server_dump( struct object *obj, int verbose )
285 struct pipe_server *server = (struct pipe_server *) obj;
286 assert( obj->ops == &pipe_server_ops );
287 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
290 static void pipe_client_dump( struct object *obj, int verbose )
292 struct pipe_client *client = (struct pipe_client *) obj;
293 assert( obj->ops == &pipe_client_ops );
294 fprintf( stderr, "Named pipe client server=%p\n", client->server );
297 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry )
299 struct pipe_client *client = (struct pipe_client *) obj;
301 return client->fd && is_fd_signaled(client->fd);
304 static void named_pipe_destroy( struct object *obj)
306 struct named_pipe *pipe = (struct named_pipe *) obj;
308 assert( list_empty( &pipe->servers ) );
309 assert( !pipe->instances );
310 free_async_queue( pipe->waiters );
313 static struct fd *pipe_client_get_fd( struct object *obj )
315 struct pipe_client *client = (struct pipe_client *) obj;
316 if (client->fd)
317 return (struct fd *) grab_object( client->fd );
318 set_error( STATUS_PIPE_DISCONNECTED );
319 return NULL;
322 static void set_server_state( struct pipe_server *server, enum pipe_state state )
324 server->state = state;
326 switch(state)
328 case ps_connected_server:
329 case ps_wait_disconnect:
330 assert( server->fd );
331 break;
332 case ps_wait_open:
333 case ps_idle_server:
334 assert( !server->fd );
335 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
336 break;
337 case ps_disconnected_server:
338 case ps_wait_connect:
339 assert( !server->fd );
340 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
341 break;
345 static struct fd *pipe_server_get_fd( struct object *obj )
347 struct pipe_server *server = (struct pipe_server *) obj;
349 return (struct fd *)grab_object( server->fd ? server->fd : server->ioctl_fd );
353 static void notify_empty( struct pipe_server *server )
355 if (!server->flush_poll)
356 return;
357 assert( server->state == ps_connected_server );
358 assert( server->event );
359 remove_timeout_user( server->flush_poll );
360 server->flush_poll = NULL;
361 set_event( server->event );
362 release_object( server->event );
363 server->event = NULL;
366 static void do_disconnect( struct pipe_server *server )
368 /* we may only have a server fd, if the client disconnected */
369 if (server->client)
371 assert( server->client->server == server );
372 assert( server->client->fd );
373 release_object( server->client->fd );
374 server->client->fd = NULL;
376 assert( server->fd );
377 shutdown( get_unix_fd( server->fd ), SHUT_RDWR );
378 release_object( server->fd );
379 server->fd = NULL;
382 static void pipe_server_destroy( struct object *obj)
384 struct pipe_server *server = (struct pipe_server *)obj;
386 assert( obj->ops == &pipe_server_ops );
388 if (server->fd)
390 notify_empty( server );
391 do_disconnect( server );
394 if (server->client)
396 server->client->server = NULL;
397 server->client = NULL;
400 assert( server->pipe->instances );
401 server->pipe->instances--;
403 if (server->ioctl_fd) release_object( server->ioctl_fd );
404 list_remove( &server->entry );
405 release_object( server->pipe );
408 static void pipe_client_destroy( struct object *obj)
410 struct pipe_client *client = (struct pipe_client *)obj;
411 struct pipe_server *server = client->server;
413 assert( obj->ops == &pipe_client_ops );
415 if (server)
417 notify_empty( server );
419 switch(server->state)
421 case ps_connected_server:
422 /* Don't destroy the server's fd here as we can't
423 do a successful flush without it. */
424 set_server_state( server, ps_wait_disconnect );
425 break;
426 case ps_disconnected_server:
427 set_server_state( server, ps_wait_connect );
428 break;
429 case ps_idle_server:
430 case ps_wait_open:
431 case ps_wait_disconnect:
432 case ps_wait_connect:
433 assert( 0 );
435 assert( server->client );
436 server->client = NULL;
437 client->server = NULL;
439 if (client->fd) release_object( client->fd );
442 static void named_pipe_device_dump( struct object *obj, int verbose )
444 assert( obj->ops == &named_pipe_device_ops );
445 fprintf( stderr, "Named pipe device\n" );
448 static struct object_type *named_pipe_device_get_type( struct object *obj )
450 static const WCHAR name[] = {'D','e','v','i','c','e'};
451 static const struct unicode_str str = { name, sizeof(name) };
452 return get_object_type( &str );
455 static struct fd *named_pipe_device_get_fd( struct object *obj )
457 struct named_pipe_device *device = (struct named_pipe_device *)obj;
458 return (struct fd *)grab_object( device->fd );
461 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
462 unsigned int attr )
464 struct named_pipe_device *device = (struct named_pipe_device*)obj;
465 struct object *found;
467 assert( obj->ops == &named_pipe_device_ops );
468 assert( device->pipes );
470 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
471 name->len = 0;
473 return found;
476 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
477 unsigned int sharing, unsigned int options )
479 return grab_object( obj );
482 static void named_pipe_device_destroy( struct object *obj )
484 struct named_pipe_device *device = (struct named_pipe_device*)obj;
485 assert( obj->ops == &named_pipe_device_ops );
486 if (device->fd) release_object( device->fd );
487 free( device->pipes );
490 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
492 return FD_TYPE_DEVICE;
495 void create_named_pipe_device( struct directory *root, const struct unicode_str *name )
497 struct named_pipe_device *dev;
499 if ((dev = create_named_object_dir( root, name, 0, &named_pipe_device_ops )) &&
500 get_error() != STATUS_OBJECT_NAME_EXISTS)
502 dev->pipes = NULL;
503 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
504 !(dev->pipes = create_namespace( 7 )))
506 release_object( dev );
507 dev = NULL;
510 if (dev) make_object_static( &dev->obj );
513 static int pipe_data_remaining( struct pipe_server *server )
515 struct pollfd pfd;
516 int fd;
518 assert( server->client );
520 fd = get_unix_fd( server->client->fd );
521 if (fd < 0)
522 return 0;
523 pfd.fd = fd;
524 pfd.events = POLLIN;
525 pfd.revents = 0;
527 if (0 > poll( &pfd, 1, 0 ))
528 return 0;
530 return pfd.revents&POLLIN;
533 static void check_flushed( void *arg )
535 struct pipe_server *server = (struct pipe_server*) arg;
537 assert( server->event );
538 if (pipe_data_remaining( server ))
540 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
542 else
544 /* notify_empty( server ); */
545 server->flush_poll = NULL;
546 set_event( server->event );
547 release_object( server->event );
548 server->event = NULL;
552 static void pipe_server_flush( struct fd *fd, struct event **event )
554 struct pipe_server *server = get_fd_user( fd );
556 if (!server || server->state != ps_connected_server) return;
558 /* FIXME: if multiple threads flush the same pipe,
559 maybe should create a list of processes to notify */
560 if (server->flush_poll) return;
562 if (pipe_data_remaining( server ))
564 /* this kind of sux -
565 there's no unix way to be alerted when a pipe becomes empty */
566 server->event = create_event( NULL, NULL, 0, 0, 0, NULL );
567 if (!server->event) return;
568 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
569 *event = server->event;
573 static void pipe_client_flush( struct fd *fd, struct event **event )
575 /* FIXME: what do we have to do for this? */
578 static inline int is_overlapped( unsigned int options )
580 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
583 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
585 return FD_TYPE_PIPE;
588 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
590 return FD_TYPE_PIPE;
593 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
594 int blocking, const void *data, data_size_t size )
596 struct pipe_server *server = get_fd_user( fd );
597 struct async *async;
598 obj_handle_t wait_handle = 0;
600 switch(code)
602 case FSCTL_PIPE_LISTEN:
603 switch(server->state)
605 case ps_idle_server:
606 case ps_wait_connect:
607 if (blocking)
609 async_data_t new_data = *async_data;
610 if (!(wait_handle = alloc_wait_event( current->process ))) break;
611 new_data.event = wait_handle;
612 if (!(async = fd_queue_async( server->ioctl_fd, &new_data, ASYNC_TYPE_WAIT )))
614 close_handle( current->process, wait_handle );
615 break;
618 else async = fd_queue_async( server->ioctl_fd, async_data, ASYNC_TYPE_WAIT );
620 if (async)
622 set_server_state( server, ps_wait_open );
623 if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
624 release_object( async );
625 set_error( STATUS_PENDING );
626 return wait_handle;
628 break;
629 case ps_connected_server:
630 set_error( STATUS_PIPE_CONNECTED );
631 break;
632 case ps_disconnected_server:
633 set_error( STATUS_PIPE_BUSY );
634 break;
635 case ps_wait_disconnect:
636 set_error( STATUS_NO_DATA_DETECTED );
637 break;
638 case ps_wait_open:
639 set_error( STATUS_INVALID_HANDLE );
640 break;
642 return 0;
644 case FSCTL_PIPE_DISCONNECT:
645 switch(server->state)
647 case ps_connected_server:
648 assert( server->client );
649 assert( server->client->fd );
651 notify_empty( server );
653 /* dump the client and server fds, but keep the pointers
654 around - client loses all waiting data */
655 do_disconnect( server );
656 set_server_state( server, ps_disconnected_server );
657 break;
658 case ps_wait_disconnect:
659 assert( !server->client );
660 do_disconnect( server );
661 set_server_state( server, ps_wait_connect );
662 break;
663 case ps_idle_server:
664 case ps_wait_open:
665 set_error( STATUS_PIPE_LISTENING );
666 break;
667 case ps_disconnected_server:
668 case ps_wait_connect:
669 set_error( STATUS_PIPE_DISCONNECTED );
670 break;
672 return 0;
674 default:
675 return default_fd_ioctl( fd, code, async_data, blocking, data, size );
679 static struct named_pipe *create_named_pipe( struct directory *root, const struct unicode_str *name,
680 unsigned int attr, const struct security_descriptor *sd )
682 struct object *obj;
683 struct named_pipe *pipe = NULL;
684 struct unicode_str new_name;
686 if (!name || !name->len) return alloc_object( &named_pipe_ops );
688 if (!(obj = find_object_dir( root, name, attr, &new_name )))
690 set_error( STATUS_OBJECT_NAME_INVALID );
691 return NULL;
693 if (!new_name.len)
695 if (attr & OBJ_OPENIF && obj->ops == &named_pipe_ops)
696 set_error( STATUS_OBJECT_NAME_EXISTS );
697 else
699 release_object( obj );
700 obj = NULL;
701 if (attr & OBJ_OPENIF)
702 set_error( STATUS_OBJECT_TYPE_MISMATCH );
703 else
704 set_error( STATUS_OBJECT_NAME_COLLISION );
706 return (struct named_pipe *)obj;
709 if (obj->ops != &named_pipe_device_ops)
710 set_error( STATUS_OBJECT_NAME_INVALID );
711 else
713 struct named_pipe_device *dev = (struct named_pipe_device *)obj;
714 if ((pipe = create_object( dev->pipes, &named_pipe_ops, &new_name, NULL )))
715 clear_error();
718 release_object( obj );
719 return pipe;
722 static struct pipe_server *get_pipe_server_obj( struct process *process,
723 obj_handle_t handle, unsigned int access )
725 struct object *obj;
726 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
727 return (struct pipe_server *) obj;
730 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
731 unsigned int pipe_flags )
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;
744 server->pipe_flags = pipe_flags;
746 list_add_head( &pipe->servers, &server->entry );
747 grab_object( pipe );
748 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->obj, options )))
750 release_object( server );
751 return NULL;
753 set_server_state( server, ps_idle_server );
754 return server;
757 static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags )
759 struct pipe_client *client;
761 client = alloc_object( &pipe_client_ops );
762 if (!client)
763 return NULL;
765 client->fd = NULL;
766 client->server = NULL;
767 client->flags = flags;
768 client->pipe_flags = pipe_flags;
770 return client;
773 static struct pipe_server *find_available_server( struct named_pipe *pipe )
775 struct pipe_server *server;
777 /* look for pipe servers that are listening */
778 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
780 if (server->state == ps_wait_open)
781 return (struct pipe_server *)grab_object( server );
784 /* fall back to pipe servers that are idle */
785 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
787 if (server->state == ps_idle_server)
788 return (struct pipe_server *)grab_object( server );
791 return NULL;
794 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
795 unsigned int sharing, unsigned int options )
797 struct named_pipe *pipe = (struct named_pipe *)obj;
798 struct pipe_server *server;
799 struct pipe_client *client;
800 unsigned int pipe_sharing;
801 int fds[2];
803 if (!(server = find_available_server( pipe )))
805 set_error( STATUS_PIPE_NOT_AVAILABLE );
806 return NULL;
809 pipe_sharing = server->pipe->sharing;
810 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
811 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
813 set_error( STATUS_ACCESS_DENIED );
814 release_object( server );
815 return NULL;
818 if ((client = create_pipe_client( options, pipe->flags )))
820 if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
822 assert( !server->fd );
824 /* for performance reasons, only set nonblocking mode when using
825 * overlapped I/O. Otherwise, we will be doing too much busy
826 * looping */
827 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
828 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
830 if (pipe->insize)
832 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
833 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
835 if (pipe->outsize)
837 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
838 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
841 client->fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->obj, options );
842 server->fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->obj, server->options );
843 if (client->fd && server->fd)
845 allow_fd_caching( client->fd );
846 allow_fd_caching( server->fd );
847 fd_copy_completion( server->ioctl_fd, server->fd );
848 if (server->state == ps_wait_open)
849 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
850 set_server_state( server, ps_connected_server );
851 server->client = client;
852 client->server = server;
854 else
856 release_object( client );
857 client = NULL;
860 else
862 file_set_error();
863 release_object( client );
864 client = NULL;
867 release_object( server );
868 return &client->obj;
871 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
872 int blocking, const void *data, data_size_t size )
874 struct named_pipe_device *device = get_fd_user( fd );
876 switch(code)
878 case FSCTL_PIPE_WAIT:
880 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = data;
881 obj_handle_t wait_handle = 0;
882 struct named_pipe *pipe;
883 struct pipe_server *server;
884 struct unicode_str name;
886 if (size < sizeof(*buffer) ||
887 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
889 set_error( STATUS_INVALID_PARAMETER );
890 return 0;
892 name.str = buffer->Name;
893 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
894 if (!(pipe = (struct named_pipe *)find_object( device->pipes, &name, OBJ_CASE_INSENSITIVE )))
896 set_error( STATUS_OBJECT_NAME_NOT_FOUND );
897 return 0;
899 if (!(server = find_available_server( pipe )))
901 struct async *async;
903 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
905 if (blocking)
907 async_data_t new_data = *async_data;
908 if (!(wait_handle = alloc_wait_event( current->process ))) goto done;
909 new_data.event = wait_handle;
910 if (!(async = create_async( current, pipe->waiters, &new_data )))
912 close_handle( current->process, wait_handle );
913 wait_handle = 0;
916 else async = create_async( current, pipe->waiters, async_data );
918 if (async)
920 timeout_t when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
921 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
922 release_object( async );
923 set_error( STATUS_PENDING );
926 else release_object( server );
928 done:
929 release_object( pipe );
930 return wait_handle;
933 default:
934 return default_fd_ioctl( fd, code, async_data, blocking, data, size );
939 DECL_HANDLER(create_named_pipe)
941 struct named_pipe *pipe;
942 struct pipe_server *server;
943 struct unicode_str name;
944 struct directory *root = NULL;
945 const struct object_attributes *objattr = get_req_data();
946 const struct security_descriptor *sd;
948 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
949 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
951 set_error( STATUS_INVALID_PARAMETER );
952 return;
955 reply->handle = 0;
957 if (!objattr_is_valid( objattr, get_req_data_size() ))
958 return;
960 sd = objattr->sd_len ? (const struct security_descriptor *)(objattr + 1) : NULL;
961 objattr_get_name( objattr, &name );
963 if (objattr->rootdir && !(root = get_directory_obj( current->process, objattr->rootdir, 0 )))
964 return;
966 pipe = create_named_pipe( root, &name, req->attributes | OBJ_OPENIF, sd );
968 if (root) release_object( root );
969 if (!pipe) return;
971 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
973 /* initialize it if it didn't already exist */
974 pipe->instances = 0;
975 pipe->waiters = NULL;
976 list_init( &pipe->servers );
977 pipe->insize = req->insize;
978 pipe->outsize = req->outsize;
979 pipe->maxinstances = req->maxinstances;
980 pipe->timeout = req->timeout;
981 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
982 pipe->sharing = req->sharing;
984 else
986 if (pipe->maxinstances <= pipe->instances)
988 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
989 release_object( pipe );
990 return;
992 if (pipe->sharing != req->sharing)
994 set_error( STATUS_ACCESS_DENIED );
995 release_object( pipe );
996 return;
998 clear_error(); /* clear the name collision */
1001 server = create_pipe_server( pipe, req->options, req->flags );
1002 if (server)
1004 reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
1005 server->pipe->instances++;
1006 if (sd) default_set_sd( &server->obj, sd, OWNER_SECURITY_INFORMATION |
1007 GROUP_SECURITY_INFORMATION |
1008 DACL_SECURITY_INFORMATION |
1009 SACL_SECURITY_INFORMATION );
1010 release_object( server );
1013 release_object( pipe );
1016 DECL_HANDLER(get_named_pipe_info)
1018 struct pipe_server *server;
1019 struct pipe_client *client = NULL;
1021 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1022 if (!server)
1024 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1025 return;
1027 clear_error();
1028 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1029 0, &pipe_client_ops );
1030 if (!client) return;
1031 server = client->server;
1034 reply->flags = client ? client->pipe_flags : server->pipe_flags;
1035 reply->sharing = server->pipe->sharing;
1036 reply->maxinstances = server->pipe->maxinstances;
1037 reply->instances = server->pipe->instances;
1038 reply->insize = server->pipe->insize;
1039 reply->outsize = server->pipe->outsize;
1041 if (client)
1042 release_object(client);
1043 else
1045 reply->flags |= NAMED_PIPE_SERVER_END;
1046 release_object(server);
1050 DECL_HANDLER(set_named_pipe_info)
1052 struct pipe_server *server;
1053 struct pipe_client *client = NULL;
1055 server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
1056 if (!server)
1058 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1059 return;
1061 clear_error();
1062 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1063 0, &pipe_client_ops );
1064 if (!client) return;
1065 server = client->server;
1068 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1069 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1071 set_error( STATUS_INVALID_PARAMETER );
1073 else if (client)
1075 client->pipe_flags = server->pipe->flags | req->flags;
1077 else
1079 server->pipe_flags = server->pipe->flags | req->flags;
1082 if (client)
1083 release_object(client);
1084 else
1085 release_object(server);