webservices: Add support for dictionary strings in the reader.
[wine.git] / server / named_pipe.c
blob0c314bd2e18974a0798a020831e71164d5eb8bcd
1 /*
2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2001 Mike McCormack
6 * Copyright 2016 Jacek Caban for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
36 #endif
37 #include <time.h>
38 #include <unistd.h>
39 #ifdef HAVE_POLL_H
40 #include <poll.h>
41 #endif
43 #include "ntstatus.h"
44 #define WIN32_NO_STATUS
45 #include "windef.h"
46 #include "winternl.h"
47 #include "winioctl.h"
49 #include "file.h"
50 #include "handle.h"
51 #include "thread.h"
52 #include "request.h"
53 #include "security.h"
55 enum pipe_state
57 ps_idle_server,
58 ps_wait_open,
59 ps_connected_server,
60 ps_wait_disconnect,
61 ps_wait_connect
64 struct named_pipe;
66 struct pipe_message
68 struct list entry; /* entry in message queue */
69 data_size_t read_pos; /* already read bytes */
70 struct iosb *iosb; /* message iosb */
71 struct async *async; /* async of pending write */
74 struct pipe_end
76 struct object obj; /* object header */
77 struct fd *fd; /* pipe file descriptor */
78 unsigned int flags; /* pipe flags */
79 struct pipe_end *connection; /* the other end of the pipe */
80 data_size_t buffer_size;/* size of buffered data that doesn't block caller */
81 struct list message_queue;
82 struct async_queue *read_q; /* read queue */
83 struct async_queue *write_q; /* write queue */
86 struct pipe_server
88 struct pipe_end pipe_end; /* common header for pipe_client and pipe_server */
89 struct fd *ioctl_fd; /* file descriptor for ioctls when not connected */
90 struct list entry; /* entry in named pipe servers list */
91 enum pipe_state state; /* server state */
92 struct pipe_client *client; /* client that this server is connected to */
93 struct named_pipe *pipe;
94 struct timeout_user *flush_poll;
95 unsigned int options; /* pipe options */
98 struct pipe_client
100 struct pipe_end pipe_end; /* common header for pipe_client and pipe_server */
101 struct pipe_server *server; /* server that this client is connected to */
102 unsigned int flags; /* file flags */
105 struct named_pipe
107 struct object obj; /* object header */
108 unsigned int flags;
109 unsigned int sharing;
110 unsigned int maxinstances;
111 unsigned int outsize;
112 unsigned int insize;
113 unsigned int instances;
114 timeout_t timeout;
115 struct list servers; /* list of servers using this pipe */
116 struct async_queue *waiters; /* list of clients waiting to connect */
119 struct named_pipe_device
121 struct object obj; /* object header */
122 struct fd *fd; /* pseudo-fd for ioctls */
123 struct namespace *pipes; /* named pipe namespace */
126 static void named_pipe_dump( struct object *obj, int verbose );
127 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
128 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent );
129 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
130 unsigned int sharing, unsigned int options );
131 static void named_pipe_destroy( struct object *obj );
133 static const struct object_ops named_pipe_ops =
135 sizeof(struct named_pipe), /* size */
136 named_pipe_dump, /* dump */
137 no_get_type, /* get_type */
138 no_add_queue, /* add_queue */
139 NULL, /* remove_queue */
140 NULL, /* signaled */
141 NULL, /* satisfied */
142 no_signal, /* signal */
143 no_get_fd, /* get_fd */
144 named_pipe_map_access, /* map_access */
145 default_get_sd, /* get_sd */
146 default_set_sd, /* set_sd */
147 no_lookup_name, /* lookup_name */
148 named_pipe_link_name, /* link_name */
149 default_unlink_name, /* unlink_name */
150 named_pipe_open_file, /* open_file */
151 no_close_handle, /* close_handle */
152 named_pipe_destroy /* destroy */
155 /* common server and client pipe end functions */
156 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd );
157 static obj_handle_t pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos );
158 static obj_handle_t pipe_end_write( struct fd *fd, struct async *async_data, file_pos_t pos );
159 static void pipe_end_queue_async( struct fd *fd, struct async *async, int type, int count );
160 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue );
162 /* server end functions */
163 static void pipe_server_dump( struct object *obj, int verbose );
164 static struct fd *pipe_server_get_fd( struct object *obj );
165 static void pipe_server_destroy( struct object *obj);
166 static obj_handle_t pipe_server_flush( struct fd *fd, struct async *async );
167 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
169 static const struct object_ops pipe_server_ops =
171 sizeof(struct pipe_server), /* size */
172 pipe_server_dump, /* dump */
173 no_get_type, /* get_type */
174 add_queue, /* add_queue */
175 remove_queue, /* remove_queue */
176 default_fd_signaled, /* signaled */
177 no_satisfied, /* satisfied */
178 no_signal, /* signal */
179 pipe_server_get_fd, /* get_fd */
180 default_fd_map_access, /* map_access */
181 default_get_sd, /* get_sd */
182 default_set_sd, /* set_sd */
183 no_lookup_name, /* lookup_name */
184 no_link_name, /* link_name */
185 NULL, /* unlink_name */
186 no_open_file, /* open_file */
187 fd_close_handle, /* close_handle */
188 pipe_server_destroy /* destroy */
191 static const struct fd_ops pipe_server_fd_ops =
193 default_fd_get_poll_events, /* get_poll_events */
194 default_poll_event, /* poll_event */
195 pipe_end_get_fd_type, /* get_fd_type */
196 pipe_end_read, /* read */
197 pipe_end_write, /* write */
198 pipe_server_flush, /* flush */
199 pipe_server_ioctl, /* ioctl */
200 pipe_end_queue_async, /* queue_async */
201 pipe_end_reselect_async /* reselect_async */
204 /* client end functions */
205 static void pipe_client_dump( struct object *obj, int verbose );
206 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry );
207 static struct fd *pipe_client_get_fd( struct object *obj );
208 static void pipe_client_destroy( struct object *obj );
209 static obj_handle_t pipe_client_flush( struct fd *fd, struct async *async );
210 static obj_handle_t pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
212 static const struct object_ops pipe_client_ops =
214 sizeof(struct pipe_client), /* size */
215 pipe_client_dump, /* dump */
216 no_get_type, /* get_type */
217 add_queue, /* add_queue */
218 remove_queue, /* remove_queue */
219 pipe_client_signaled, /* signaled */
220 no_satisfied, /* satisfied */
221 no_signal, /* signal */
222 pipe_client_get_fd, /* get_fd */
223 default_fd_map_access, /* map_access */
224 default_get_sd, /* get_sd */
225 default_set_sd, /* set_sd */
226 no_lookup_name, /* lookup_name */
227 no_link_name, /* link_name */
228 NULL, /* unlink_name */
229 no_open_file, /* open_file */
230 fd_close_handle, /* close_handle */
231 pipe_client_destroy /* destroy */
234 static const struct fd_ops pipe_client_fd_ops =
236 default_fd_get_poll_events, /* get_poll_events */
237 default_poll_event, /* poll_event */
238 pipe_end_get_fd_type, /* get_fd_type */
239 pipe_end_read, /* read */
240 pipe_end_write, /* write */
241 pipe_client_flush, /* flush */
242 pipe_client_ioctl, /* ioctl */
243 pipe_end_queue_async, /* queue_async */
244 pipe_end_reselect_async /* reselect_async */
247 static void named_pipe_device_dump( struct object *obj, int verbose );
248 static struct object_type *named_pipe_device_get_type( struct object *obj );
249 static struct fd *named_pipe_device_get_fd( struct object *obj );
250 static struct object *named_pipe_device_lookup_name( struct object *obj,
251 struct unicode_str *name, unsigned int attr );
252 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
253 unsigned int sharing, unsigned int options );
254 static void named_pipe_device_destroy( struct object *obj );
255 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
256 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code,
257 struct async *async );
259 static const struct object_ops named_pipe_device_ops =
261 sizeof(struct named_pipe_device), /* size */
262 named_pipe_device_dump, /* dump */
263 named_pipe_device_get_type, /* get_type */
264 no_add_queue, /* add_queue */
265 NULL, /* remove_queue */
266 NULL, /* signaled */
267 no_satisfied, /* satisfied */
268 no_signal, /* signal */
269 named_pipe_device_get_fd, /* get_fd */
270 no_map_access, /* map_access */
271 default_get_sd, /* get_sd */
272 default_set_sd, /* set_sd */
273 named_pipe_device_lookup_name, /* lookup_name */
274 directory_link_name, /* link_name */
275 default_unlink_name, /* unlink_name */
276 named_pipe_device_open_file, /* open_file */
277 fd_close_handle, /* close_handle */
278 named_pipe_device_destroy /* destroy */
281 static const struct fd_ops named_pipe_device_fd_ops =
283 default_fd_get_poll_events, /* get_poll_events */
284 default_poll_event, /* poll_event */
285 named_pipe_device_get_fd_type, /* get_fd_type */
286 no_fd_read, /* read */
287 no_fd_write, /* write */
288 no_fd_flush, /* flush */
289 named_pipe_device_ioctl, /* ioctl */
290 default_fd_queue_async, /* queue_async */
291 default_fd_reselect_async /* reselect_async */
294 /* Returns if we handle I/O via server calls. Currently message-mode pipes are handled this way. */
295 static int use_server_io( struct pipe_end *pipe_end )
297 return pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
300 static void named_pipe_dump( struct object *obj, int verbose )
302 fputs( "Named pipe\n", stderr );
305 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
307 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
308 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
309 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
310 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
311 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
314 static void pipe_server_dump( struct object *obj, int verbose )
316 struct pipe_server *server = (struct pipe_server *) obj;
317 assert( obj->ops == &pipe_server_ops );
318 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
321 static void pipe_client_dump( struct object *obj, int verbose )
323 struct pipe_client *client = (struct pipe_client *) obj;
324 assert( obj->ops == &pipe_client_ops );
325 fprintf( stderr, "Named pipe client server=%p\n", client->server );
328 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry )
330 struct pipe_client *client = (struct pipe_client *) obj;
332 return client->pipe_end.fd && is_fd_signaled(client->pipe_end.fd);
335 static void named_pipe_destroy( struct object *obj)
337 struct named_pipe *pipe = (struct named_pipe *) obj;
339 assert( list_empty( &pipe->servers ) );
340 assert( !pipe->instances );
341 free_async_queue( pipe->waiters );
344 static struct fd *pipe_client_get_fd( struct object *obj )
346 struct pipe_client *client = (struct pipe_client *) obj;
347 if (client->pipe_end.fd)
348 return (struct fd *) grab_object( client->pipe_end.fd );
349 set_error( STATUS_PIPE_DISCONNECTED );
350 return NULL;
353 static void set_server_state( struct pipe_server *server, enum pipe_state state )
355 server->state = state;
357 switch(state)
359 case ps_connected_server:
360 case ps_wait_disconnect:
361 assert( server->pipe_end.fd );
362 break;
363 case ps_wait_open:
364 case ps_idle_server:
365 assert( !server->pipe_end.fd );
366 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
367 break;
368 case ps_wait_connect:
369 assert( !server->pipe_end.fd );
370 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
371 break;
375 static struct fd *pipe_server_get_fd( struct object *obj )
377 struct pipe_server *server = (struct pipe_server *) obj;
379 return (struct fd *)grab_object( server->pipe_end.fd ? server->pipe_end.fd : server->ioctl_fd );
383 static void notify_empty( struct pipe_server *server )
385 if (!server->flush_poll)
386 return;
387 assert( server->state == ps_connected_server );
388 remove_timeout_user( server->flush_poll );
389 server->flush_poll = NULL;
390 fd_async_wake_up( server->pipe_end.fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
393 static void wake_message( struct pipe_message *message )
395 struct async *async = message->async;
397 message->async = NULL;
398 message->iosb->status = STATUS_SUCCESS;
399 message->iosb->result = message->iosb->in_size;
400 if (async)
402 async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
403 release_object( async );
407 static void free_message( struct pipe_message *message )
409 list_remove( &message->entry );
410 if (message->iosb) release_object( message->iosb );
411 free( message );
414 static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
416 struct pipe_end *connection = pipe_end->connection;
418 pipe_end->connection = NULL;
420 if (use_server_io( pipe_end ))
422 struct pipe_message *message, *next;
423 struct async *async;
424 if (pipe_end->fd) fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
425 async_wake_up( pipe_end->read_q, status );
426 LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
428 async = message->async;
429 if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
430 if (!async) continue;
431 async_terminate( async, status );
432 release_object( async );
434 if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
436 if (connection)
438 connection->connection = NULL;
439 pipe_end_disconnect( connection, status );
443 static void do_disconnect( struct pipe_server *server )
445 /* we may only have a server fd, if the client disconnected */
446 if (server->client)
448 assert( server->client->server == server );
449 assert( server->client->pipe_end.fd );
450 if (!use_server_io( &server->pipe_end ))
452 release_object( server->client->pipe_end.fd );
453 server->client->pipe_end.fd = NULL;
456 assert( server->pipe_end.fd );
457 if (!use_server_io( &server->pipe_end ))
458 shutdown( get_unix_fd( server->pipe_end.fd ), SHUT_RDWR );
459 release_object( server->pipe_end.fd );
460 server->pipe_end.fd = NULL;
463 static void pipe_end_destroy( struct pipe_end *pipe_end )
465 struct pipe_message *message;
467 while (!list_empty( &pipe_end->message_queue ))
469 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
470 assert( !message->async );
471 free_message( message );
474 free_async_queue( pipe_end->read_q );
475 free_async_queue( pipe_end->write_q );
478 static void pipe_server_destroy( struct object *obj)
480 struct pipe_server *server = (struct pipe_server *)obj;
482 assert( obj->ops == &pipe_server_ops );
484 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_BROKEN );
486 if (server->pipe_end.fd)
488 notify_empty( server );
489 do_disconnect( server );
492 pipe_end_destroy( &server->pipe_end );
493 if (server->client)
495 server->client->server = NULL;
496 server->client = NULL;
499 assert( server->pipe->instances );
500 server->pipe->instances--;
502 if (server->ioctl_fd) release_object( server->ioctl_fd );
503 list_remove( &server->entry );
504 release_object( server->pipe );
507 static void pipe_client_destroy( struct object *obj)
509 struct pipe_client *client = (struct pipe_client *)obj;
510 struct pipe_server *server = client->server;
512 assert( obj->ops == &pipe_client_ops );
514 pipe_end_disconnect( &client->pipe_end, STATUS_PIPE_BROKEN );
516 if (server)
518 notify_empty( server );
520 switch(server->state)
522 case ps_connected_server:
523 /* Don't destroy the server's fd here as we can't
524 do a successful flush without it. */
525 set_server_state( server, ps_wait_disconnect );
526 break;
527 case ps_idle_server:
528 case ps_wait_open:
529 case ps_wait_disconnect:
530 case ps_wait_connect:
531 assert( 0 );
533 assert( server->client );
534 server->client = NULL;
535 client->server = NULL;
538 pipe_end_destroy( &client->pipe_end );
539 if (client->pipe_end.fd) release_object( client->pipe_end.fd );
542 static void named_pipe_device_dump( struct object *obj, int verbose )
544 fputs( "Named pipe device\n", stderr );
547 static struct object_type *named_pipe_device_get_type( struct object *obj )
549 static const WCHAR name[] = {'D','e','v','i','c','e'};
550 static const struct unicode_str str = { name, sizeof(name) };
551 return get_object_type( &str );
554 static struct fd *named_pipe_device_get_fd( struct object *obj )
556 struct named_pipe_device *device = (struct named_pipe_device *)obj;
557 return (struct fd *)grab_object( device->fd );
560 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
561 unsigned int attr )
563 struct named_pipe_device *device = (struct named_pipe_device*)obj;
564 struct object *found;
566 assert( obj->ops == &named_pipe_device_ops );
567 assert( device->pipes );
569 if (!name) return NULL; /* open the device itself */
571 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
572 name->len = 0;
574 return found;
577 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
578 unsigned int sharing, unsigned int options )
580 return grab_object( obj );
583 static void named_pipe_device_destroy( struct object *obj )
585 struct named_pipe_device *device = (struct named_pipe_device*)obj;
586 assert( obj->ops == &named_pipe_device_ops );
587 if (device->fd) release_object( device->fd );
588 free( device->pipes );
591 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
593 return FD_TYPE_DEVICE;
596 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name )
598 struct named_pipe_device *dev;
600 if ((dev = create_named_object( root, &named_pipe_device_ops, name, 0, NULL )) &&
601 get_error() != STATUS_OBJECT_NAME_EXISTS)
603 dev->pipes = NULL;
604 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
605 !(dev->pipes = create_namespace( 7 )))
607 release_object( dev );
608 dev = NULL;
611 return &dev->obj;
614 static int pipe_data_remaining( struct pipe_server *server )
616 struct pollfd pfd;
617 int fd;
619 assert( server->client );
621 if (use_server_io( &server->pipe_end ))
622 return !list_empty( &server->client->pipe_end.message_queue );
624 fd = get_unix_fd( server->client->pipe_end.fd );
625 if (fd < 0)
626 return 0;
627 pfd.fd = fd;
628 pfd.events = POLLIN;
629 pfd.revents = 0;
631 if (0 > poll( &pfd, 1, 0 ))
632 return 0;
634 return pfd.revents&POLLIN;
637 static void check_flushed( void *arg )
639 struct pipe_server *server = (struct pipe_server*) arg;
641 if (pipe_data_remaining( server ))
643 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
645 else
647 server->flush_poll = NULL;
648 fd_async_wake_up( server->pipe_end.fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
652 static obj_handle_t pipe_end_flush( struct pipe_end *pipe_end, struct async *async )
654 obj_handle_t handle = 0;
656 if (use_server_io( pipe_end ) && (!pipe_end->connection || list_empty( &pipe_end->connection->message_queue )))
657 return 0;
659 if (!fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT )) return 0;
661 if (!async_is_blocking( async ) || (handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
662 set_error( STATUS_PENDING );
663 return handle;
666 static obj_handle_t pipe_server_flush( struct fd *fd, struct async *async )
668 struct pipe_server *server = get_fd_user( fd );
669 obj_handle_t handle;
671 if (!server || server->state != ps_connected_server) return 0;
673 if (!pipe_data_remaining( server )) return 0;
675 handle = pipe_end_flush( &server->pipe_end, async );
677 /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
678 if (handle && !use_server_io( &server->pipe_end ) && !server->flush_poll)
679 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
680 return handle;
683 static obj_handle_t pipe_client_flush( struct fd *fd, struct async *async )
685 struct pipe_end *pipe_end = get_fd_user( fd );
686 /* FIXME: Support byte mode. */
687 return use_server_io( pipe_end ) ? pipe_end_flush( pipe_end, async ) : 0;
690 static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
692 struct pipe_message *message;
694 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
696 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
697 iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
698 iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
699 ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
701 else
703 data_size_t avail = 0;
704 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
706 avail += message->iosb->in_size - message->read_pos;
707 if (avail >= iosb->out_size) break;
709 iosb->out_size = min( iosb->out_size, avail );
710 iosb->status = STATUS_SUCCESS;
713 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
714 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
716 iosb->out_data = message->iosb->in_data;
717 message->iosb->in_data = NULL;
718 wake_message( message );
719 free_message( message );
721 else
723 data_size_t write_pos = 0, writing;
724 char *buf = NULL;
726 if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
728 iosb->out_size = 0;
729 iosb->status = STATUS_NO_MEMORY;
730 return;
735 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
736 writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
737 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
738 write_pos += writing;
739 message->read_pos += writing;
740 if (message->read_pos == message->iosb->in_size)
742 wake_message(message);
743 free_message(message);
745 } while (write_pos < iosb->out_size);
747 iosb->result = iosb->out_size;
750 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
751 * We're not interested in such reselect calls, so we ignore them. */
752 static int ignore_reselect;
754 static void reselect_write_queue( struct pipe_end *pipe_end );
756 static void reselect_read_queue( struct pipe_end *pipe_end )
758 struct async *async;
759 struct iosb *iosb;
760 int read_done = 0;
762 ignore_reselect = 1;
763 while (!list_empty( &pipe_end->message_queue) && (async = find_pending_async( pipe_end->read_q )))
765 iosb = async_get_iosb( async );
766 message_queue_read( pipe_end, iosb );
767 async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
768 release_object( async );
769 release_object( iosb );
770 read_done = 1;
772 ignore_reselect = 0;
774 if (pipe_end->connection)
776 if (list_empty( &pipe_end->message_queue ))
777 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
778 else if (read_done)
779 reselect_write_queue( pipe_end->connection );
783 static void reselect_write_queue( struct pipe_end *pipe_end )
785 struct pipe_message *message, *next;
786 struct pipe_end *reader = pipe_end->connection;
787 data_size_t avail = 0;
789 if (!reader) return;
791 ignore_reselect = 1;
793 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
795 if (message->async && message->iosb->status != STATUS_PENDING)
797 release_object( message->async );
798 message->async = NULL;
799 free_message( message );
801 else
803 avail += message->iosb->in_size - message->read_pos;
804 if (message->iosb->status == STATUS_PENDING && (avail <= reader->buffer_size || !message->iosb->in_size))
805 wake_message( message );
809 ignore_reselect = 0;
810 reselect_read_queue( reader );
813 static obj_handle_t pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
815 struct pipe_end *pipe_end = get_fd_user( fd );
816 obj_handle_t handle = 0;
818 if (!use_server_io( pipe_end )) return no_fd_read( fd, async, pos );
820 if (!pipe_end->connection && list_empty( &pipe_end->message_queue ))
822 set_error( STATUS_PIPE_BROKEN );
823 return 0;
826 if (!pipe_end->read_q && !(pipe_end->read_q = create_async_queue( fd ))) return 0;
827 if (!(handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 ))) return 0;
829 queue_async( pipe_end->read_q, async );
830 reselect_read_queue( pipe_end );
831 set_error( STATUS_PENDING );
833 if (!async_is_blocking( async ))
835 struct iosb *iosb;
836 iosb = async_get_iosb( async );
837 if (iosb->status == STATUS_PENDING)
839 close_handle( current->process, handle );
840 handle = 0;
842 release_object( iosb );
844 return handle;
847 static obj_handle_t pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
849 struct pipe_end *write_end = get_fd_user( fd );
850 struct pipe_end *read_end = write_end->connection;
851 struct pipe_message *message;
852 obj_handle_t handle = 0;
854 if (!use_server_io( write_end )) return no_fd_write( fd, async, pos );
856 if (!read_end)
858 set_error( STATUS_PIPE_DISCONNECTED );
859 return 0;
862 if (!write_end->write_q && !(write_end->write_q = create_async_queue( fd ))) return 0;
863 if (!(handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 ))) return 0;
865 if (!(message = mem_alloc( sizeof(*message) )))
867 close_handle( current->process, handle );
868 return 0;
870 message->async = (struct async *)grab_object( async );
871 message->iosb = async_get_iosb( async );
872 message->read_pos = 0;
873 list_add_tail( &read_end->message_queue, &message->entry );
875 queue_async( write_end->write_q, async );
876 reselect_write_queue( write_end );
877 set_error( STATUS_PENDING );
879 if (!async_is_blocking( async ))
881 struct iosb *iosb;
882 iosb = async_get_iosb( async );
883 if (iosb->status == STATUS_PENDING)
885 close_handle( current->process, handle );
886 handle = 0;
888 release_object( iosb );
890 return handle;
893 static void pipe_end_queue_async( struct fd *fd, struct async *async, int type, int count )
895 struct pipe_end *pipe_end = get_fd_user( fd );
896 if (use_server_io( pipe_end )) no_fd_queue_async( fd, async, type, count );
897 else default_fd_queue_async( fd, async, type, count );
900 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
902 struct pipe_end *pipe_end = get_fd_user( fd );
904 if (ignore_reselect) return;
906 if (!use_server_io( pipe_end ))
907 default_fd_reselect_async( fd, queue );
908 else if (pipe_end->write_q && pipe_end->write_q == queue)
909 reselect_write_queue( pipe_end );
910 else if (pipe_end->read_q && pipe_end->read_q == queue)
911 reselect_read_queue( pipe_end );
914 static inline int is_overlapped( unsigned int options )
916 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
919 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
921 return FD_TYPE_PIPE;
924 static void pipe_end_peek( struct pipe_end *pipe_end )
926 unsigned reply_size = get_reply_max_size();
927 FILE_PIPE_PEEK_BUFFER *buffer;
928 struct pipe_message *message;
929 data_size_t avail = 0;
930 data_size_t message_length = 0;
932 if (!use_server_io( pipe_end ))
934 set_error( STATUS_NOT_SUPPORTED );
935 return;
938 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
940 set_error( STATUS_INFO_LENGTH_MISMATCH );
941 return;
943 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
945 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
946 avail += message->iosb->in_size - message->read_pos;
948 if (avail)
950 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
951 message_length = message->iosb->in_size - message->read_pos;
952 reply_size = min( reply_size, message_length );
954 else reply_size = 0;
956 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return;
957 buffer->NamedPipeState = 0; /* FIXME */
958 buffer->ReadDataAvailable = avail;
959 buffer->NumberOfMessages = 0; /* FIXME */
960 buffer->MessageLength = message_length;
961 if (reply_size) memcpy( buffer->Data, (const char *)message->iosb->in_data + message->read_pos, reply_size );
964 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
966 struct pipe_server *server = get_fd_user( fd );
967 obj_handle_t wait_handle = 0;
969 switch(code)
971 case FSCTL_PIPE_LISTEN:
972 switch(server->state)
974 case ps_idle_server:
975 case ps_wait_connect:
976 if (fd_queue_async( server->ioctl_fd, async, ASYNC_TYPE_WAIT ))
978 if (async_is_blocking( async )) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
979 set_server_state( server, ps_wait_open );
980 if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
981 set_error( STATUS_PENDING );
982 return wait_handle;
984 break;
985 case ps_connected_server:
986 set_error( STATUS_PIPE_CONNECTED );
987 break;
988 case ps_wait_disconnect:
989 set_error( STATUS_NO_DATA_DETECTED );
990 break;
991 case ps_wait_open:
992 set_error( STATUS_INVALID_HANDLE );
993 break;
995 return 0;
997 case FSCTL_PIPE_DISCONNECT:
998 switch(server->state)
1000 case ps_connected_server:
1001 assert( server->client );
1002 assert( server->client->pipe_end.fd );
1004 notify_empty( server );
1006 /* dump the client and server fds - client loses all waiting data */
1007 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1008 do_disconnect( server );
1009 server->client->server = NULL;
1010 server->client = NULL;
1011 set_server_state( server, ps_wait_connect );
1012 break;
1013 case ps_wait_disconnect:
1014 assert( !server->client );
1015 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1016 do_disconnect( server );
1017 set_server_state( server, ps_wait_connect );
1018 break;
1019 case ps_idle_server:
1020 case ps_wait_open:
1021 set_error( STATUS_PIPE_LISTENING );
1022 break;
1023 case ps_wait_connect:
1024 set_error( STATUS_PIPE_DISCONNECTED );
1025 break;
1027 return 0;
1029 case FSCTL_PIPE_PEEK:
1030 pipe_end_peek( &server->pipe_end );
1031 return 0;
1033 default:
1034 return default_fd_ioctl( fd, code, async );
1038 static obj_handle_t pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1040 struct pipe_client *client = get_fd_user( fd );
1042 switch(code)
1044 case FSCTL_PIPE_PEEK:
1045 pipe_end_peek( &client->pipe_end );
1046 return 0;
1048 default:
1049 return default_fd_ioctl( fd, code, async );
1053 static struct pipe_server *get_pipe_server_obj( struct process *process,
1054 obj_handle_t handle, unsigned int access )
1056 struct object *obj;
1057 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
1058 return (struct pipe_server *) obj;
1061 static void init_pipe_end( struct pipe_end *pipe_end, unsigned int pipe_flags, data_size_t buffer_size )
1063 pipe_end->fd = NULL;
1064 pipe_end->flags = pipe_flags;
1065 pipe_end->connection = NULL;
1066 pipe_end->buffer_size = buffer_size;
1067 pipe_end->read_q = NULL;
1068 pipe_end->write_q = NULL;
1069 list_init( &pipe_end->message_queue );
1072 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
1073 unsigned int pipe_flags )
1075 struct pipe_server *server;
1077 server = alloc_object( &pipe_server_ops );
1078 if (!server)
1079 return NULL;
1081 server->pipe = pipe;
1082 server->client = NULL;
1083 server->flush_poll = NULL;
1084 server->options = options;
1085 init_pipe_end( &server->pipe_end, pipe_flags, pipe->insize );
1087 list_add_head( &pipe->servers, &server->entry );
1088 grab_object( pipe );
1089 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1091 release_object( server );
1092 return NULL;
1094 set_fd_signaled( server->ioctl_fd, 1 );
1095 set_server_state( server, ps_idle_server );
1096 return server;
1099 static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags, data_size_t buffer_size )
1101 struct pipe_client *client;
1103 client = alloc_object( &pipe_client_ops );
1104 if (!client)
1105 return NULL;
1107 client->server = NULL;
1108 client->flags = flags;
1109 init_pipe_end( &client->pipe_end, pipe_flags, buffer_size );
1111 return client;
1114 static struct pipe_server *find_available_server( struct named_pipe *pipe )
1116 struct pipe_server *server;
1118 /* look for pipe servers that are listening */
1119 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1121 if (server->state == ps_wait_open)
1122 return (struct pipe_server *)grab_object( server );
1125 /* fall back to pipe servers that are idle */
1126 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1128 if (server->state == ps_idle_server)
1129 return (struct pipe_server *)grab_object( server );
1132 return NULL;
1135 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1137 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1139 if (parent->ops != &named_pipe_device_ops)
1141 set_error( STATUS_OBJECT_NAME_INVALID );
1142 return 0;
1144 namespace_add( dev->pipes, name );
1145 name->parent = grab_object( parent );
1146 return 1;
1149 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1150 unsigned int sharing, unsigned int options )
1152 struct named_pipe *pipe = (struct named_pipe *)obj;
1153 struct pipe_server *server;
1154 struct pipe_client *client;
1155 unsigned int pipe_sharing;
1156 int fds[2];
1158 if (!(server = find_available_server( pipe )))
1160 set_error( STATUS_PIPE_NOT_AVAILABLE );
1161 return NULL;
1164 pipe_sharing = server->pipe->sharing;
1165 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1166 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1168 set_error( STATUS_ACCESS_DENIED );
1169 release_object( server );
1170 return NULL;
1173 if ((client = create_pipe_client( options, pipe->flags, pipe->outsize )))
1175 if (use_server_io( &server->pipe_end ))
1177 client->pipe_end.fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->pipe_end.obj, options );
1178 if (client->pipe_end.fd)
1180 set_fd_signaled( client->pipe_end.fd, 1 );
1181 server->pipe_end.fd = (struct fd *)grab_object( server->ioctl_fd );
1182 set_no_fd_status( server->ioctl_fd, STATUS_BAD_DEVICE_TYPE );
1184 else
1186 release_object( client );
1187 client = NULL;
1190 else if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
1192 assert( !server->pipe_end.fd );
1194 /* for performance reasons, only set nonblocking mode when using
1195 * overlapped I/O. Otherwise, we will be doing too much busy
1196 * looping */
1197 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
1198 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
1200 if (pipe->insize)
1202 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
1203 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
1205 if (pipe->outsize)
1207 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
1208 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
1211 client->pipe_end.fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->pipe_end.obj, options );
1212 server->pipe_end.fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->pipe_end.obj, server->options );
1213 if (client->pipe_end.fd && server->pipe_end.fd)
1215 fd_copy_completion( server->ioctl_fd, server->pipe_end.fd );
1217 else
1219 release_object( client );
1220 client = NULL;
1223 else
1225 file_set_error();
1226 release_object( client );
1227 client = NULL;
1229 if (client)
1231 allow_fd_caching( client->pipe_end.fd );
1232 allow_fd_caching( server->pipe_end.fd );
1233 if (server->state == ps_wait_open)
1234 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
1235 set_server_state( server, ps_connected_server );
1236 server->client = client;
1237 client->server = server;
1238 server->pipe_end.connection = &client->pipe_end;
1239 client->pipe_end.connection = &server->pipe_end;
1242 release_object( server );
1243 return &client->pipe_end.obj;
1246 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1248 struct named_pipe_device *device = get_fd_user( fd );
1250 switch(code)
1252 case FSCTL_PIPE_WAIT:
1254 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1255 data_size_t size = get_req_data_size();
1256 obj_handle_t wait_handle = 0;
1257 struct named_pipe *pipe;
1258 struct pipe_server *server;
1259 struct unicode_str name;
1260 timeout_t when;
1262 if (size < sizeof(*buffer) ||
1263 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1265 set_error( STATUS_INVALID_PARAMETER );
1266 return 0;
1268 name.str = buffer->Name;
1269 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1270 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;
1272 if (!(server = find_available_server( pipe )))
1274 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
1276 queue_async( pipe->waiters, async );
1277 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1278 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1279 if (async_is_blocking( async )) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
1280 set_error( STATUS_PENDING );
1282 else release_object( server );
1284 done:
1285 release_object( pipe );
1286 return wait_handle;
1289 default:
1290 return default_fd_ioctl( fd, code, async );
1295 DECL_HANDLER(create_named_pipe)
1297 struct named_pipe *pipe;
1298 struct pipe_server *server;
1299 struct unicode_str name;
1300 struct object *root;
1301 const struct security_descriptor *sd;
1302 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1304 if (!objattr) return;
1306 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1307 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1309 if (root) release_object( root );
1310 set_error( STATUS_INVALID_PARAMETER );
1311 return;
1314 if (!name.len) /* pipes need a root directory even without a name */
1316 if (!objattr->rootdir)
1318 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1319 return;
1321 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
1324 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1326 if (root) release_object( root );
1327 if (!pipe) return;
1329 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1331 /* initialize it if it didn't already exist */
1332 pipe->instances = 0;
1333 pipe->waiters = NULL;
1334 list_init( &pipe->servers );
1335 pipe->insize = req->insize;
1336 pipe->outsize = req->outsize;
1337 pipe->maxinstances = req->maxinstances;
1338 pipe->timeout = req->timeout;
1339 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
1340 pipe->sharing = req->sharing;
1342 else
1344 if (pipe->maxinstances <= pipe->instances)
1346 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1347 release_object( pipe );
1348 return;
1350 if (pipe->sharing != req->sharing)
1352 set_error( STATUS_ACCESS_DENIED );
1353 release_object( pipe );
1354 return;
1356 clear_error(); /* clear the name collision */
1359 server = create_pipe_server( pipe, req->options, req->flags );
1360 if (server)
1362 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1363 server->pipe->instances++;
1364 if (sd) default_set_sd( &server->pipe_end.obj, sd, OWNER_SECURITY_INFORMATION |
1365 GROUP_SECURITY_INFORMATION |
1366 DACL_SECURITY_INFORMATION |
1367 SACL_SECURITY_INFORMATION );
1368 release_object( server );
1371 release_object( pipe );
1374 DECL_HANDLER(get_named_pipe_info)
1376 struct pipe_server *server;
1377 struct pipe_client *client = NULL;
1379 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1380 if (!server)
1382 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1383 return;
1385 clear_error();
1386 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1387 0, &pipe_client_ops );
1388 if (!client) return;
1389 server = client->server;
1392 reply->flags = client ? client->pipe_end.flags : server->pipe_end.flags;
1393 if (server)
1395 reply->sharing = server->pipe->sharing;
1396 reply->maxinstances = server->pipe->maxinstances;
1397 reply->instances = server->pipe->instances;
1398 reply->insize = server->pipe->insize;
1399 reply->outsize = server->pipe->outsize;
1402 if (client)
1403 release_object(client);
1404 else
1406 reply->flags |= NAMED_PIPE_SERVER_END;
1407 release_object(server);
1411 DECL_HANDLER(set_named_pipe_info)
1413 struct pipe_server *server;
1414 struct pipe_client *client = NULL;
1416 server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
1417 if (!server)
1419 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1420 return;
1422 clear_error();
1423 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1424 0, &pipe_client_ops );
1425 if (!client) return;
1426 if (!(server = client->server))
1428 release_object( client );
1429 return;
1433 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1434 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1436 set_error( STATUS_INVALID_PARAMETER );
1438 else if (client)
1440 client->pipe_end.flags = server->pipe->flags | req->flags;
1442 else
1444 server->pipe_end.flags = server->pipe->flags | req->flags;
1447 if (client)
1448 release_object(client);
1449 else
1450 release_object(server);