regedit/tests: Correct out of sequence test numbering.
[wine.git] / server / named_pipe.c
blobc0bfc2a27b68e1fc20e8402750afebba6d3598e5
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 int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos );
158 static int 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 int pipe_server_flush( struct fd *fd, struct async *async );
167 static int 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 int pipe_client_flush( struct fd *fd, struct async *async );
210 static int 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 int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
258 static const struct object_ops named_pipe_device_ops =
260 sizeof(struct named_pipe_device), /* size */
261 named_pipe_device_dump, /* dump */
262 named_pipe_device_get_type, /* get_type */
263 no_add_queue, /* add_queue */
264 NULL, /* remove_queue */
265 NULL, /* signaled */
266 no_satisfied, /* satisfied */
267 no_signal, /* signal */
268 named_pipe_device_get_fd, /* get_fd */
269 no_map_access, /* map_access */
270 default_get_sd, /* get_sd */
271 default_set_sd, /* set_sd */
272 named_pipe_device_lookup_name, /* lookup_name */
273 directory_link_name, /* link_name */
274 default_unlink_name, /* unlink_name */
275 named_pipe_device_open_file, /* open_file */
276 fd_close_handle, /* close_handle */
277 named_pipe_device_destroy /* destroy */
280 static const struct fd_ops named_pipe_device_fd_ops =
282 default_fd_get_poll_events, /* get_poll_events */
283 default_poll_event, /* poll_event */
284 named_pipe_device_get_fd_type, /* get_fd_type */
285 no_fd_read, /* read */
286 no_fd_write, /* write */
287 no_fd_flush, /* flush */
288 named_pipe_device_ioctl, /* ioctl */
289 default_fd_queue_async, /* queue_async */
290 default_fd_reselect_async /* reselect_async */
293 /* Returns if we handle I/O via server calls. Currently message-mode pipes are handled this way. */
294 static int use_server_io( struct pipe_end *pipe_end )
296 return pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
299 static void named_pipe_dump( struct object *obj, int verbose )
301 fputs( "Named pipe\n", stderr );
304 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
306 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
307 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
308 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
309 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
310 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
313 static void pipe_server_dump( struct object *obj, int verbose )
315 struct pipe_server *server = (struct pipe_server *) obj;
316 assert( obj->ops == &pipe_server_ops );
317 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
320 static void pipe_client_dump( struct object *obj, int verbose )
322 struct pipe_client *client = (struct pipe_client *) obj;
323 assert( obj->ops == &pipe_client_ops );
324 fprintf( stderr, "Named pipe client server=%p\n", client->server );
327 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry )
329 struct pipe_client *client = (struct pipe_client *) obj;
331 return client->pipe_end.fd && is_fd_signaled(client->pipe_end.fd);
334 static void named_pipe_destroy( struct object *obj)
336 struct named_pipe *pipe = (struct named_pipe *) obj;
338 assert( list_empty( &pipe->servers ) );
339 assert( !pipe->instances );
340 free_async_queue( pipe->waiters );
343 static struct fd *pipe_client_get_fd( struct object *obj )
345 struct pipe_client *client = (struct pipe_client *) obj;
346 if (client->pipe_end.fd)
347 return (struct fd *) grab_object( client->pipe_end.fd );
348 set_error( STATUS_PIPE_DISCONNECTED );
349 return NULL;
352 static void set_server_state( struct pipe_server *server, enum pipe_state state )
354 server->state = state;
356 switch(state)
358 case ps_connected_server:
359 case ps_wait_disconnect:
360 assert( server->pipe_end.fd );
361 break;
362 case ps_wait_open:
363 case ps_idle_server:
364 assert( !server->pipe_end.fd );
365 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
366 break;
367 case ps_wait_connect:
368 assert( !server->pipe_end.fd );
369 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
370 break;
374 static struct fd *pipe_server_get_fd( struct object *obj )
376 struct pipe_server *server = (struct pipe_server *) obj;
378 return (struct fd *)grab_object( server->pipe_end.fd ? server->pipe_end.fd : server->ioctl_fd );
382 static void notify_empty( struct pipe_server *server )
384 if (!server->flush_poll)
385 return;
386 assert( server->state == ps_connected_server );
387 remove_timeout_user( server->flush_poll );
388 server->flush_poll = NULL;
389 fd_async_wake_up( server->pipe_end.fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
392 static void wake_message( struct pipe_message *message )
394 struct async *async = message->async;
396 message->async = NULL;
397 message->iosb->status = STATUS_SUCCESS;
398 message->iosb->result = message->iosb->in_size;
399 if (async)
401 async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
402 release_object( async );
406 static void free_message( struct pipe_message *message )
408 list_remove( &message->entry );
409 if (message->iosb) release_object( message->iosb );
410 free( message );
413 static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
415 struct pipe_end *connection = pipe_end->connection;
417 pipe_end->connection = NULL;
419 if (use_server_io( pipe_end ))
421 struct pipe_message *message, *next;
422 struct async *async;
423 if (pipe_end->fd) fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
424 async_wake_up( pipe_end->read_q, status );
425 LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
427 async = message->async;
428 if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
429 if (!async) continue;
430 async_terminate( async, status );
431 release_object( async );
433 if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
435 if (connection)
437 connection->connection = NULL;
438 pipe_end_disconnect( connection, status );
442 static void do_disconnect( struct pipe_server *server )
444 /* we may only have a server fd, if the client disconnected */
445 if (server->client)
447 assert( server->client->server == server );
448 assert( server->client->pipe_end.fd );
449 if (!use_server_io( &server->pipe_end ))
451 release_object( server->client->pipe_end.fd );
452 server->client->pipe_end.fd = NULL;
455 assert( server->pipe_end.fd );
456 if (!use_server_io( &server->pipe_end ))
457 shutdown( get_unix_fd( server->pipe_end.fd ), SHUT_RDWR );
458 release_object( server->pipe_end.fd );
459 server->pipe_end.fd = NULL;
462 static void pipe_end_destroy( struct pipe_end *pipe_end )
464 struct pipe_message *message;
466 while (!list_empty( &pipe_end->message_queue ))
468 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
469 assert( !message->async );
470 free_message( message );
473 free_async_queue( pipe_end->read_q );
474 free_async_queue( pipe_end->write_q );
477 static void pipe_server_destroy( struct object *obj)
479 struct pipe_server *server = (struct pipe_server *)obj;
481 assert( obj->ops == &pipe_server_ops );
483 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_BROKEN );
485 if (server->pipe_end.fd)
487 notify_empty( server );
488 do_disconnect( server );
491 pipe_end_destroy( &server->pipe_end );
492 if (server->client)
494 server->client->server = NULL;
495 server->client = NULL;
498 assert( server->pipe->instances );
499 server->pipe->instances--;
501 if (server->ioctl_fd) release_object( server->ioctl_fd );
502 list_remove( &server->entry );
503 release_object( server->pipe );
506 static void pipe_client_destroy( struct object *obj)
508 struct pipe_client *client = (struct pipe_client *)obj;
509 struct pipe_server *server = client->server;
511 assert( obj->ops == &pipe_client_ops );
513 pipe_end_disconnect( &client->pipe_end, STATUS_PIPE_BROKEN );
515 if (server)
517 notify_empty( server );
519 switch(server->state)
521 case ps_connected_server:
522 /* Don't destroy the server's fd here as we can't
523 do a successful flush without it. */
524 set_server_state( server, ps_wait_disconnect );
525 break;
526 case ps_idle_server:
527 case ps_wait_open:
528 case ps_wait_disconnect:
529 case ps_wait_connect:
530 assert( 0 );
532 assert( server->client );
533 server->client = NULL;
534 client->server = NULL;
537 pipe_end_destroy( &client->pipe_end );
538 if (client->pipe_end.fd) release_object( client->pipe_end.fd );
541 static void named_pipe_device_dump( struct object *obj, int verbose )
543 fputs( "Named pipe device\n", stderr );
546 static struct object_type *named_pipe_device_get_type( struct object *obj )
548 static const WCHAR name[] = {'D','e','v','i','c','e'};
549 static const struct unicode_str str = { name, sizeof(name) };
550 return get_object_type( &str );
553 static struct fd *named_pipe_device_get_fd( struct object *obj )
555 struct named_pipe_device *device = (struct named_pipe_device *)obj;
556 return (struct fd *)grab_object( device->fd );
559 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
560 unsigned int attr )
562 struct named_pipe_device *device = (struct named_pipe_device*)obj;
563 struct object *found;
565 assert( obj->ops == &named_pipe_device_ops );
566 assert( device->pipes );
568 if (!name) return NULL; /* open the device itself */
570 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
571 name->len = 0;
573 return found;
576 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
577 unsigned int sharing, unsigned int options )
579 return grab_object( obj );
582 static void named_pipe_device_destroy( struct object *obj )
584 struct named_pipe_device *device = (struct named_pipe_device*)obj;
585 assert( obj->ops == &named_pipe_device_ops );
586 if (device->fd) release_object( device->fd );
587 free( device->pipes );
590 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
592 return FD_TYPE_DEVICE;
595 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name )
597 struct named_pipe_device *dev;
599 if ((dev = create_named_object( root, &named_pipe_device_ops, name, 0, NULL )) &&
600 get_error() != STATUS_OBJECT_NAME_EXISTS)
602 dev->pipes = NULL;
603 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
604 !(dev->pipes = create_namespace( 7 )))
606 release_object( dev );
607 dev = NULL;
610 return &dev->obj;
613 static int pipe_data_remaining( struct pipe_server *server )
615 struct pollfd pfd;
616 int fd;
618 assert( server->client );
620 if (use_server_io( &server->pipe_end ))
621 return !list_empty( &server->client->pipe_end.message_queue );
623 fd = get_unix_fd( server->client->pipe_end.fd );
624 if (fd < 0)
625 return 0;
626 pfd.fd = fd;
627 pfd.events = POLLIN;
628 pfd.revents = 0;
630 if (0 > poll( &pfd, 1, 0 ))
631 return 0;
633 return pfd.revents&POLLIN;
636 static void check_flushed( void *arg )
638 struct pipe_server *server = (struct pipe_server*) arg;
640 if (pipe_data_remaining( server ))
642 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
644 else
646 server->flush_poll = NULL;
647 fd_async_wake_up( server->pipe_end.fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
651 static int pipe_end_flush( struct pipe_end *pipe_end, struct async *async )
653 if (use_server_io( pipe_end ) && (!pipe_end->connection || list_empty( &pipe_end->connection->message_queue )))
654 return 1;
656 if (!fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT )) return 0;
658 set_error( STATUS_PENDING );
659 return 1;
662 static int pipe_server_flush( struct fd *fd, struct async *async )
664 struct pipe_server *server = get_fd_user( fd );
665 obj_handle_t handle;
667 if (!server || server->state != ps_connected_server) return 1;
669 if (!pipe_data_remaining( server )) return 1;
671 handle = pipe_end_flush( &server->pipe_end, async );
673 /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
674 if (handle && !use_server_io( &server->pipe_end ) && !server->flush_poll)
675 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
676 return handle;
679 static int pipe_client_flush( struct fd *fd, struct async *async )
681 struct pipe_end *pipe_end = get_fd_user( fd );
682 /* FIXME: Support byte mode. */
683 return use_server_io( pipe_end ) ? pipe_end_flush( pipe_end, async ) : 1;
686 static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
688 struct pipe_message *message;
690 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
692 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
693 iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
694 iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
695 ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
697 else
699 data_size_t avail = 0;
700 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
702 avail += message->iosb->in_size - message->read_pos;
703 if (avail >= iosb->out_size) break;
705 iosb->out_size = min( iosb->out_size, avail );
706 iosb->status = STATUS_SUCCESS;
709 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
710 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
712 iosb->out_data = message->iosb->in_data;
713 message->iosb->in_data = NULL;
714 wake_message( message );
715 free_message( message );
717 else
719 data_size_t write_pos = 0, writing;
720 char *buf = NULL;
722 if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
724 iosb->out_size = 0;
725 iosb->status = STATUS_NO_MEMORY;
726 return;
731 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
732 writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
733 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
734 write_pos += writing;
735 message->read_pos += writing;
736 if (message->read_pos == message->iosb->in_size)
738 wake_message(message);
739 free_message(message);
741 } while (write_pos < iosb->out_size);
743 iosb->result = iosb->out_size;
746 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
747 * We're not interested in such reselect calls, so we ignore them. */
748 static int ignore_reselect;
750 static void reselect_write_queue( struct pipe_end *pipe_end );
752 static void reselect_read_queue( struct pipe_end *pipe_end )
754 struct async *async;
755 struct iosb *iosb;
756 int read_done = 0;
758 ignore_reselect = 1;
759 while (!list_empty( &pipe_end->message_queue) && (async = find_pending_async( pipe_end->read_q )))
761 iosb = async_get_iosb( async );
762 message_queue_read( pipe_end, iosb );
763 async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
764 release_object( async );
765 release_object( iosb );
766 read_done = 1;
768 ignore_reselect = 0;
770 if (pipe_end->connection)
772 if (list_empty( &pipe_end->message_queue ))
773 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
774 else if (read_done)
775 reselect_write_queue( pipe_end->connection );
779 static void reselect_write_queue( struct pipe_end *pipe_end )
781 struct pipe_message *message, *next;
782 struct pipe_end *reader = pipe_end->connection;
783 data_size_t avail = 0;
785 if (!reader) return;
787 ignore_reselect = 1;
789 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
791 if (message->async && message->iosb->status != STATUS_PENDING)
793 release_object( message->async );
794 message->async = NULL;
795 free_message( message );
797 else
799 avail += message->iosb->in_size - message->read_pos;
800 if (message->iosb->status == STATUS_PENDING && (avail <= reader->buffer_size || !message->iosb->in_size))
801 wake_message( message );
805 ignore_reselect = 0;
806 reselect_read_queue( reader );
809 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
811 struct pipe_end *pipe_end = get_fd_user( fd );
813 if (!use_server_io( pipe_end )) return no_fd_read( fd, async, pos );
815 if (!pipe_end->connection && list_empty( &pipe_end->message_queue ))
817 set_error( STATUS_PIPE_BROKEN );
818 return 0;
821 if (!pipe_end->read_q && !(pipe_end->read_q = create_async_queue( fd ))) return 0;
823 queue_async( pipe_end->read_q, async );
824 reselect_read_queue( pipe_end );
825 set_error( STATUS_PENDING );
826 return 1;
829 static int pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
831 struct pipe_end *write_end = get_fd_user( fd );
832 struct pipe_end *read_end = write_end->connection;
833 struct pipe_message *message;
835 if (!use_server_io( write_end )) return no_fd_write( fd, async, pos );
837 if (!read_end)
839 set_error( STATUS_PIPE_DISCONNECTED );
840 return 0;
843 if (!write_end->write_q && !(write_end->write_q = create_async_queue( fd ))) return 0;
845 if (!(message = mem_alloc( sizeof(*message) ))) return 0;
846 message->async = (struct async *)grab_object( async );
847 message->iosb = async_get_iosb( async );
848 message->read_pos = 0;
849 list_add_tail( &read_end->message_queue, &message->entry );
851 queue_async( write_end->write_q, async );
852 reselect_write_queue( write_end );
853 set_error( STATUS_PENDING );
854 return 1;
857 static void pipe_end_queue_async( struct fd *fd, struct async *async, int type, int count )
859 struct pipe_end *pipe_end = get_fd_user( fd );
860 if (use_server_io( pipe_end )) no_fd_queue_async( fd, async, type, count );
861 else default_fd_queue_async( fd, async, type, count );
864 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
866 struct pipe_end *pipe_end = get_fd_user( fd );
868 if (ignore_reselect) return;
870 if (!use_server_io( pipe_end ))
871 default_fd_reselect_async( fd, queue );
872 else if (pipe_end->write_q && pipe_end->write_q == queue)
873 reselect_write_queue( pipe_end );
874 else if (pipe_end->read_q && pipe_end->read_q == queue)
875 reselect_read_queue( pipe_end );
878 static inline int is_overlapped( unsigned int options )
880 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
883 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
885 return FD_TYPE_PIPE;
888 static int pipe_end_peek( struct pipe_end *pipe_end )
890 unsigned reply_size = get_reply_max_size();
891 FILE_PIPE_PEEK_BUFFER *buffer;
892 struct pipe_message *message;
893 data_size_t avail = 0;
894 data_size_t message_length = 0;
896 if (!use_server_io( pipe_end ))
898 set_error( STATUS_NOT_SUPPORTED );
899 return 0;
902 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
904 set_error( STATUS_INFO_LENGTH_MISMATCH );
905 return 0;
907 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
909 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
910 avail += message->iosb->in_size - message->read_pos;
912 if (avail)
914 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
915 message_length = message->iosb->in_size - message->read_pos;
916 reply_size = min( reply_size, message_length );
918 else reply_size = 0;
920 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return 0;
921 buffer->NamedPipeState = 0; /* FIXME */
922 buffer->ReadDataAvailable = avail;
923 buffer->NumberOfMessages = 0; /* FIXME */
924 buffer->MessageLength = message_length;
925 if (reply_size) memcpy( buffer->Data, (const char *)message->iosb->in_data + message->read_pos, reply_size );
926 return 1;
929 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
931 struct pipe_server *server = get_fd_user( fd );
933 switch(code)
935 case FSCTL_PIPE_LISTEN:
936 switch(server->state)
938 case ps_idle_server:
939 case ps_wait_connect:
940 if (fd_queue_async( server->ioctl_fd, async, ASYNC_TYPE_WAIT ))
942 set_server_state( server, ps_wait_open );
943 if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
944 set_error( STATUS_PENDING );
945 return 1;
947 break;
948 case ps_connected_server:
949 set_error( STATUS_PIPE_CONNECTED );
950 break;
951 case ps_wait_disconnect:
952 set_error( STATUS_NO_DATA_DETECTED );
953 break;
954 case ps_wait_open:
955 set_error( STATUS_INVALID_HANDLE );
956 break;
958 return 0;
960 case FSCTL_PIPE_DISCONNECT:
961 switch(server->state)
963 case ps_connected_server:
964 assert( server->client );
965 assert( server->client->pipe_end.fd );
967 notify_empty( server );
969 /* dump the client and server fds - client loses all waiting data */
970 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
971 do_disconnect( server );
972 server->client->server = NULL;
973 server->client = NULL;
974 set_server_state( server, ps_wait_connect );
975 break;
976 case ps_wait_disconnect:
977 assert( !server->client );
978 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
979 do_disconnect( server );
980 set_server_state( server, ps_wait_connect );
981 break;
982 case ps_idle_server:
983 case ps_wait_open:
984 set_error( STATUS_PIPE_LISTENING );
985 return 0;
986 case ps_wait_connect:
987 set_error( STATUS_PIPE_DISCONNECTED );
988 return 0;
990 return 1;
992 case FSCTL_PIPE_PEEK:
993 return pipe_end_peek( &server->pipe_end );
995 default:
996 return default_fd_ioctl( fd, code, async );
1000 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1002 struct pipe_client *client = get_fd_user( fd );
1004 switch(code)
1006 case FSCTL_PIPE_PEEK:
1007 return pipe_end_peek( &client->pipe_end );
1009 default:
1010 return default_fd_ioctl( fd, code, async );
1014 static struct pipe_server *get_pipe_server_obj( struct process *process,
1015 obj_handle_t handle, unsigned int access )
1017 struct object *obj;
1018 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
1019 return (struct pipe_server *) obj;
1022 static void init_pipe_end( struct pipe_end *pipe_end, unsigned int pipe_flags, data_size_t buffer_size )
1024 pipe_end->fd = NULL;
1025 pipe_end->flags = pipe_flags;
1026 pipe_end->connection = NULL;
1027 pipe_end->buffer_size = buffer_size;
1028 pipe_end->read_q = NULL;
1029 pipe_end->write_q = NULL;
1030 list_init( &pipe_end->message_queue );
1033 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
1034 unsigned int pipe_flags )
1036 struct pipe_server *server;
1038 server = alloc_object( &pipe_server_ops );
1039 if (!server)
1040 return NULL;
1042 server->pipe = pipe;
1043 server->client = NULL;
1044 server->flush_poll = NULL;
1045 server->options = options;
1046 init_pipe_end( &server->pipe_end, pipe_flags, pipe->insize );
1048 list_add_head( &pipe->servers, &server->entry );
1049 grab_object( pipe );
1050 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1052 release_object( server );
1053 return NULL;
1055 set_fd_signaled( server->ioctl_fd, 1 );
1056 set_server_state( server, ps_idle_server );
1057 return server;
1060 static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags, data_size_t buffer_size )
1062 struct pipe_client *client;
1064 client = alloc_object( &pipe_client_ops );
1065 if (!client)
1066 return NULL;
1068 client->server = NULL;
1069 client->flags = flags;
1070 init_pipe_end( &client->pipe_end, pipe_flags, buffer_size );
1072 return client;
1075 static struct pipe_server *find_available_server( struct named_pipe *pipe )
1077 struct pipe_server *server;
1079 /* look for pipe servers that are listening */
1080 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1082 if (server->state == ps_wait_open)
1083 return (struct pipe_server *)grab_object( server );
1086 /* fall back to pipe servers that are idle */
1087 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1089 if (server->state == ps_idle_server)
1090 return (struct pipe_server *)grab_object( server );
1093 return NULL;
1096 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1098 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1100 if (parent->ops != &named_pipe_device_ops)
1102 set_error( STATUS_OBJECT_NAME_INVALID );
1103 return 0;
1105 namespace_add( dev->pipes, name );
1106 name->parent = grab_object( parent );
1107 return 1;
1110 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1111 unsigned int sharing, unsigned int options )
1113 struct named_pipe *pipe = (struct named_pipe *)obj;
1114 struct pipe_server *server;
1115 struct pipe_client *client;
1116 unsigned int pipe_sharing;
1117 int fds[2];
1119 if (!(server = find_available_server( pipe )))
1121 set_error( STATUS_PIPE_NOT_AVAILABLE );
1122 return NULL;
1125 pipe_sharing = server->pipe->sharing;
1126 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1127 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1129 set_error( STATUS_ACCESS_DENIED );
1130 release_object( server );
1131 return NULL;
1134 if ((client = create_pipe_client( options, pipe->flags, pipe->outsize )))
1136 if (use_server_io( &server->pipe_end ))
1138 client->pipe_end.fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->pipe_end.obj, options );
1139 if (client->pipe_end.fd)
1141 set_fd_signaled( client->pipe_end.fd, 1 );
1142 server->pipe_end.fd = (struct fd *)grab_object( server->ioctl_fd );
1143 set_no_fd_status( server->ioctl_fd, STATUS_BAD_DEVICE_TYPE );
1145 else
1147 release_object( client );
1148 client = NULL;
1151 else if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
1153 assert( !server->pipe_end.fd );
1155 /* for performance reasons, only set nonblocking mode when using
1156 * overlapped I/O. Otherwise, we will be doing too much busy
1157 * looping */
1158 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
1159 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
1161 if (pipe->insize)
1163 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
1164 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
1166 if (pipe->outsize)
1168 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
1169 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
1172 client->pipe_end.fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->pipe_end.obj, options );
1173 server->pipe_end.fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->pipe_end.obj, server->options );
1174 if (client->pipe_end.fd && server->pipe_end.fd)
1176 fd_copy_completion( server->ioctl_fd, server->pipe_end.fd );
1178 else
1180 release_object( client );
1181 client = NULL;
1184 else
1186 file_set_error();
1187 release_object( client );
1188 client = NULL;
1190 if (client)
1192 allow_fd_caching( client->pipe_end.fd );
1193 allow_fd_caching( server->pipe_end.fd );
1194 if (server->state == ps_wait_open)
1195 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
1196 set_server_state( server, ps_connected_server );
1197 server->client = client;
1198 client->server = server;
1199 server->pipe_end.connection = &client->pipe_end;
1200 client->pipe_end.connection = &server->pipe_end;
1203 release_object( server );
1204 return &client->pipe_end.obj;
1207 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1209 struct named_pipe_device *device = get_fd_user( fd );
1211 switch(code)
1213 case FSCTL_PIPE_WAIT:
1215 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1216 data_size_t size = get_req_data_size();
1217 struct named_pipe *pipe;
1218 struct pipe_server *server;
1219 struct unicode_str name;
1220 timeout_t when;
1222 if (size < sizeof(*buffer) ||
1223 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1225 set_error( STATUS_INVALID_PARAMETER );
1226 return 0;
1228 name.str = buffer->Name;
1229 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1230 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;
1232 if (!(server = find_available_server( pipe )))
1234 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
1236 queue_async( pipe->waiters, async );
1237 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1238 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1239 release_object( pipe );
1240 set_error( STATUS_PENDING );
1241 return 1;
1243 else release_object( server );
1245 done:
1246 release_object( pipe );
1247 return 0;
1250 default:
1251 return default_fd_ioctl( fd, code, async );
1256 DECL_HANDLER(create_named_pipe)
1258 struct named_pipe *pipe;
1259 struct pipe_server *server;
1260 struct unicode_str name;
1261 struct object *root;
1262 const struct security_descriptor *sd;
1263 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1265 if (!objattr) return;
1267 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1268 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1270 if (root) release_object( root );
1271 set_error( STATUS_INVALID_PARAMETER );
1272 return;
1275 if (!name.len) /* pipes need a root directory even without a name */
1277 if (!objattr->rootdir)
1279 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1280 return;
1282 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
1285 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1287 if (root) release_object( root );
1288 if (!pipe) return;
1290 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1292 /* initialize it if it didn't already exist */
1293 pipe->instances = 0;
1294 pipe->waiters = NULL;
1295 list_init( &pipe->servers );
1296 pipe->insize = req->insize;
1297 pipe->outsize = req->outsize;
1298 pipe->maxinstances = req->maxinstances;
1299 pipe->timeout = req->timeout;
1300 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
1301 pipe->sharing = req->sharing;
1303 else
1305 if (pipe->maxinstances <= pipe->instances)
1307 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1308 release_object( pipe );
1309 return;
1311 if (pipe->sharing != req->sharing)
1313 set_error( STATUS_ACCESS_DENIED );
1314 release_object( pipe );
1315 return;
1317 clear_error(); /* clear the name collision */
1320 server = create_pipe_server( pipe, req->options, req->flags );
1321 if (server)
1323 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1324 server->pipe->instances++;
1325 if (sd) default_set_sd( &server->pipe_end.obj, sd, OWNER_SECURITY_INFORMATION |
1326 GROUP_SECURITY_INFORMATION |
1327 DACL_SECURITY_INFORMATION |
1328 SACL_SECURITY_INFORMATION );
1329 release_object( server );
1332 release_object( pipe );
1335 DECL_HANDLER(get_named_pipe_info)
1337 struct pipe_server *server;
1338 struct pipe_client *client = NULL;
1340 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1341 if (!server)
1343 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1344 return;
1346 clear_error();
1347 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1348 0, &pipe_client_ops );
1349 if (!client) return;
1350 server = client->server;
1353 reply->flags = client ? client->pipe_end.flags : server->pipe_end.flags;
1354 if (server)
1356 reply->sharing = server->pipe->sharing;
1357 reply->maxinstances = server->pipe->maxinstances;
1358 reply->instances = server->pipe->instances;
1359 reply->insize = server->pipe->insize;
1360 reply->outsize = server->pipe->outsize;
1363 if (client)
1364 release_object(client);
1365 else
1367 reply->flags |= NAMED_PIPE_SERVER_END;
1368 release_object(server);
1372 DECL_HANDLER(set_named_pipe_info)
1374 struct pipe_server *server;
1375 struct pipe_client *client = NULL;
1377 server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
1378 if (!server)
1380 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1381 return;
1383 clear_error();
1384 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1385 0, &pipe_client_ops );
1386 if (!client) return;
1387 if (!(server = client->server))
1389 release_object( client );
1390 return;
1394 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1395 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1397 set_error( STATUS_INVALID_PARAMETER );
1399 else if (client)
1401 client->pipe_end.flags = server->pipe->flags | req->flags;
1403 else
1405 server->pipe_end.flags = server->pipe->flags | req->flags;
1408 if (client)
1409 release_object(client);
1410 else
1411 release_object(server);