wined3d: Add basic support for rendering to 3D textures.
[wine.git] / server / named_pipe.c
blobbbdf3bc823c0588787622167a0a2668e021ac9be
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 obj_handle_t pipe_end_read( struct fd *fd, struct async *async, int blocking, file_pos_t pos );
157 static obj_handle_t pipe_end_write( struct fd *fd, struct async *async_data, int blocking, file_pos_t pos );
158 static void pipe_end_queue_async( struct fd *fd, struct async *async, int type, int count );
159 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue );
161 /* server end functions */
162 static void pipe_server_dump( struct object *obj, int verbose );
163 static struct fd *pipe_server_get_fd( struct object *obj );
164 static void pipe_server_destroy( struct object *obj);
165 static obj_handle_t pipe_server_flush( struct fd *fd, struct async *async, int blocking );
166 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd );
167 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async,
168 int blocking );
170 static const struct object_ops pipe_server_ops =
172 sizeof(struct pipe_server), /* size */
173 pipe_server_dump, /* dump */
174 no_get_type, /* get_type */
175 add_queue, /* add_queue */
176 remove_queue, /* remove_queue */
177 default_fd_signaled, /* signaled */
178 no_satisfied, /* satisfied */
179 no_signal, /* signal */
180 pipe_server_get_fd, /* get_fd */
181 default_fd_map_access, /* map_access */
182 default_get_sd, /* get_sd */
183 default_set_sd, /* set_sd */
184 no_lookup_name, /* lookup_name */
185 no_link_name, /* link_name */
186 NULL, /* unlink_name */
187 no_open_file, /* open_file */
188 fd_close_handle, /* close_handle */
189 pipe_server_destroy /* destroy */
192 static const struct fd_ops pipe_server_fd_ops =
194 default_fd_get_poll_events, /* get_poll_events */
195 default_poll_event, /* poll_event */
196 pipe_server_get_fd_type, /* get_fd_type */
197 pipe_end_read, /* read */
198 pipe_end_write, /* write */
199 pipe_server_flush, /* flush */
200 pipe_server_ioctl, /* ioctl */
201 pipe_end_queue_async, /* queue_async */
202 pipe_end_reselect_async /* reselect_async */
205 /* client end functions */
206 static void pipe_client_dump( struct object *obj, int verbose );
207 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry );
208 static struct fd *pipe_client_get_fd( struct object *obj );
209 static void pipe_client_destroy( struct object *obj );
210 static obj_handle_t pipe_client_flush( struct fd *fd, struct async *async, int blocking );
211 static obj_handle_t pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async,
212 int blocking );
213 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd );
215 static const struct object_ops pipe_client_ops =
217 sizeof(struct pipe_client), /* size */
218 pipe_client_dump, /* dump */
219 no_get_type, /* get_type */
220 add_queue, /* add_queue */
221 remove_queue, /* remove_queue */
222 pipe_client_signaled, /* signaled */
223 no_satisfied, /* satisfied */
224 no_signal, /* signal */
225 pipe_client_get_fd, /* get_fd */
226 default_fd_map_access, /* map_access */
227 default_get_sd, /* get_sd */
228 default_set_sd, /* set_sd */
229 no_lookup_name, /* lookup_name */
230 no_link_name, /* link_name */
231 NULL, /* unlink_name */
232 no_open_file, /* open_file */
233 fd_close_handle, /* close_handle */
234 pipe_client_destroy /* destroy */
237 static const struct fd_ops pipe_client_fd_ops =
239 default_fd_get_poll_events, /* get_poll_events */
240 default_poll_event, /* poll_event */
241 pipe_client_get_fd_type, /* get_fd_type */
242 pipe_end_read, /* read */
243 pipe_end_write, /* write */
244 pipe_client_flush, /* flush */
245 pipe_client_ioctl, /* ioctl */
246 pipe_end_queue_async, /* queue_async */
247 pipe_end_reselect_async /* reselect_async */
250 static void named_pipe_device_dump( struct object *obj, int verbose );
251 static struct object_type *named_pipe_device_get_type( struct object *obj );
252 static struct fd *named_pipe_device_get_fd( struct object *obj );
253 static struct object *named_pipe_device_lookup_name( struct object *obj,
254 struct unicode_str *name, unsigned int attr );
255 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
256 unsigned int sharing, unsigned int options );
257 static void named_pipe_device_destroy( struct object *obj );
258 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
259 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code,
260 struct async *async, int blocking );
262 static const struct object_ops named_pipe_device_ops =
264 sizeof(struct named_pipe_device), /* size */
265 named_pipe_device_dump, /* dump */
266 named_pipe_device_get_type, /* get_type */
267 no_add_queue, /* add_queue */
268 NULL, /* remove_queue */
269 NULL, /* signaled */
270 no_satisfied, /* satisfied */
271 no_signal, /* signal */
272 named_pipe_device_get_fd, /* get_fd */
273 no_map_access, /* map_access */
274 default_get_sd, /* get_sd */
275 default_set_sd, /* set_sd */
276 named_pipe_device_lookup_name, /* lookup_name */
277 directory_link_name, /* link_name */
278 default_unlink_name, /* unlink_name */
279 named_pipe_device_open_file, /* open_file */
280 fd_close_handle, /* close_handle */
281 named_pipe_device_destroy /* destroy */
284 static const struct fd_ops named_pipe_device_fd_ops =
286 default_fd_get_poll_events, /* get_poll_events */
287 default_poll_event, /* poll_event */
288 named_pipe_device_get_fd_type, /* get_fd_type */
289 no_fd_read, /* read */
290 no_fd_write, /* write */
291 no_fd_flush, /* flush */
292 named_pipe_device_ioctl, /* ioctl */
293 default_fd_queue_async, /* queue_async */
294 default_fd_reselect_async /* reselect_async */
297 /* Returns if we handle I/O via server calls. Currently message-mode pipes are handled this way. */
298 static int use_server_io( struct pipe_end *pipe_end )
300 return pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
303 static void named_pipe_dump( struct object *obj, int verbose )
305 fputs( "Named pipe\n", stderr );
308 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
310 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
311 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
312 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
313 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
314 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
317 static void pipe_server_dump( struct object *obj, int verbose )
319 struct pipe_server *server = (struct pipe_server *) obj;
320 assert( obj->ops == &pipe_server_ops );
321 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
324 static void pipe_client_dump( struct object *obj, int verbose )
326 struct pipe_client *client = (struct pipe_client *) obj;
327 assert( obj->ops == &pipe_client_ops );
328 fprintf( stderr, "Named pipe client server=%p\n", client->server );
331 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry )
333 struct pipe_client *client = (struct pipe_client *) obj;
335 return client->pipe_end.fd && is_fd_signaled(client->pipe_end.fd);
338 static void named_pipe_destroy( struct object *obj)
340 struct named_pipe *pipe = (struct named_pipe *) obj;
342 assert( list_empty( &pipe->servers ) );
343 assert( !pipe->instances );
344 free_async_queue( pipe->waiters );
347 static struct fd *pipe_client_get_fd( struct object *obj )
349 struct pipe_client *client = (struct pipe_client *) obj;
350 if (client->pipe_end.fd)
351 return (struct fd *) grab_object( client->pipe_end.fd );
352 set_error( STATUS_PIPE_DISCONNECTED );
353 return NULL;
356 static void set_server_state( struct pipe_server *server, enum pipe_state state )
358 server->state = state;
360 switch(state)
362 case ps_connected_server:
363 case ps_wait_disconnect:
364 assert( server->pipe_end.fd );
365 break;
366 case ps_wait_open:
367 case ps_idle_server:
368 assert( !server->pipe_end.fd );
369 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
370 break;
371 case ps_wait_connect:
372 assert( !server->pipe_end.fd );
373 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
374 break;
378 static struct fd *pipe_server_get_fd( struct object *obj )
380 struct pipe_server *server = (struct pipe_server *) obj;
382 return (struct fd *)grab_object( server->pipe_end.fd ? server->pipe_end.fd : server->ioctl_fd );
386 static void notify_empty( struct pipe_server *server )
388 if (!server->flush_poll)
389 return;
390 assert( server->state == ps_connected_server );
391 remove_timeout_user( server->flush_poll );
392 server->flush_poll = NULL;
393 fd_async_wake_up( server->pipe_end.fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
396 static void wake_message( struct pipe_message *message )
398 struct async *async = message->async;
400 message->async = NULL;
401 message->iosb->status = STATUS_SUCCESS;
402 message->iosb->result = message->iosb->in_size;
403 if (async)
405 async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
406 release_object( async );
410 static void free_message( struct pipe_message *message )
412 list_remove( &message->entry );
413 if (message->iosb) release_object( message->iosb );
414 free( message );
417 static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
419 struct pipe_end *connection = pipe_end->connection;
421 pipe_end->connection = NULL;
423 if (use_server_io( pipe_end ))
425 struct pipe_message *message, *next;
426 struct async *async;
427 if (pipe_end->fd) fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
428 async_wake_up( pipe_end->read_q, status );
429 LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
431 async = message->async;
432 if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
433 if (!async) continue;
434 async_terminate( async, status );
435 release_object( async );
437 if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
439 if (connection)
441 connection->connection = NULL;
442 pipe_end_disconnect( connection, status );
446 static void do_disconnect( struct pipe_server *server )
448 /* we may only have a server fd, if the client disconnected */
449 if (server->client)
451 assert( server->client->server == server );
452 assert( server->client->pipe_end.fd );
453 if (!use_server_io( &server->pipe_end ))
455 release_object( server->client->pipe_end.fd );
456 server->client->pipe_end.fd = NULL;
459 assert( server->pipe_end.fd );
460 if (!use_server_io( &server->pipe_end ))
461 shutdown( get_unix_fd( server->pipe_end.fd ), SHUT_RDWR );
462 release_object( server->pipe_end.fd );
463 server->pipe_end.fd = NULL;
466 static void pipe_end_destroy( struct pipe_end *pipe_end )
468 struct pipe_message *message;
470 while (!list_empty( &pipe_end->message_queue ))
472 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
473 assert( !message->async );
474 free_message( message );
477 free_async_queue( pipe_end->read_q );
478 free_async_queue( pipe_end->write_q );
481 static void pipe_server_destroy( struct object *obj)
483 struct pipe_server *server = (struct pipe_server *)obj;
485 assert( obj->ops == &pipe_server_ops );
487 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_BROKEN );
489 if (server->pipe_end.fd)
491 notify_empty( server );
492 do_disconnect( server );
495 pipe_end_destroy( &server->pipe_end );
496 if (server->client)
498 server->client->server = NULL;
499 server->client = NULL;
502 assert( server->pipe->instances );
503 server->pipe->instances--;
505 if (server->ioctl_fd) release_object( server->ioctl_fd );
506 list_remove( &server->entry );
507 release_object( server->pipe );
510 static void pipe_client_destroy( struct object *obj)
512 struct pipe_client *client = (struct pipe_client *)obj;
513 struct pipe_server *server = client->server;
515 assert( obj->ops == &pipe_client_ops );
517 pipe_end_disconnect( &client->pipe_end, STATUS_PIPE_BROKEN );
519 if (server)
521 notify_empty( server );
523 switch(server->state)
525 case ps_connected_server:
526 /* Don't destroy the server's fd here as we can't
527 do a successful flush without it. */
528 set_server_state( server, ps_wait_disconnect );
529 break;
530 case ps_idle_server:
531 case ps_wait_open:
532 case ps_wait_disconnect:
533 case ps_wait_connect:
534 assert( 0 );
536 assert( server->client );
537 server->client = NULL;
538 client->server = NULL;
541 pipe_end_destroy( &client->pipe_end );
542 if (client->pipe_end.fd) release_object( client->pipe_end.fd );
545 static void named_pipe_device_dump( struct object *obj, int verbose )
547 fputs( "Named pipe device\n", stderr );
550 static struct object_type *named_pipe_device_get_type( struct object *obj )
552 static const WCHAR name[] = {'D','e','v','i','c','e'};
553 static const struct unicode_str str = { name, sizeof(name) };
554 return get_object_type( &str );
557 static struct fd *named_pipe_device_get_fd( struct object *obj )
559 struct named_pipe_device *device = (struct named_pipe_device *)obj;
560 return (struct fd *)grab_object( device->fd );
563 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
564 unsigned int attr )
566 struct named_pipe_device *device = (struct named_pipe_device*)obj;
567 struct object *found;
569 assert( obj->ops == &named_pipe_device_ops );
570 assert( device->pipes );
572 if (!name) return NULL; /* open the device itself */
574 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
575 name->len = 0;
577 return found;
580 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
581 unsigned int sharing, unsigned int options )
583 return grab_object( obj );
586 static void named_pipe_device_destroy( struct object *obj )
588 struct named_pipe_device *device = (struct named_pipe_device*)obj;
589 assert( obj->ops == &named_pipe_device_ops );
590 if (device->fd) release_object( device->fd );
591 free( device->pipes );
594 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
596 return FD_TYPE_DEVICE;
599 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name )
601 struct named_pipe_device *dev;
603 if ((dev = create_named_object( root, &named_pipe_device_ops, name, 0, NULL )) &&
604 get_error() != STATUS_OBJECT_NAME_EXISTS)
606 dev->pipes = NULL;
607 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
608 !(dev->pipes = create_namespace( 7 )))
610 release_object( dev );
611 dev = NULL;
614 return &dev->obj;
617 static int pipe_data_remaining( struct pipe_server *server )
619 struct pollfd pfd;
620 int fd;
622 assert( server->client );
624 if (use_server_io( &server->pipe_end ))
625 return !list_empty( &server->client->pipe_end.message_queue );
627 fd = get_unix_fd( server->client->pipe_end.fd );
628 if (fd < 0)
629 return 0;
630 pfd.fd = fd;
631 pfd.events = POLLIN;
632 pfd.revents = 0;
634 if (0 > poll( &pfd, 1, 0 ))
635 return 0;
637 return pfd.revents&POLLIN;
640 static void check_flushed( void *arg )
642 struct pipe_server *server = (struct pipe_server*) arg;
644 if (pipe_data_remaining( server ))
646 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
648 else
650 server->flush_poll = NULL;
651 fd_async_wake_up( server->pipe_end.fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
655 static obj_handle_t pipe_end_flush( struct pipe_end *pipe_end, struct async *async, int blocking )
657 obj_handle_t handle = 0;
659 if (use_server_io( pipe_end ) && (!pipe_end->connection || list_empty( &pipe_end->connection->message_queue )))
660 return 0;
662 if (!fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT )) return 0;
664 if (!blocking || (handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
665 set_error( STATUS_PENDING );
666 return handle;
669 static obj_handle_t pipe_server_flush( struct fd *fd, struct async *async, int blocking )
671 struct pipe_server *server = get_fd_user( fd );
672 obj_handle_t handle;
674 if (!server || server->state != ps_connected_server) return 0;
676 if (!pipe_data_remaining( server )) return 0;
678 handle = pipe_end_flush( &server->pipe_end, async, blocking );
680 /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
681 if (handle && !use_server_io( &server->pipe_end ) && !server->flush_poll)
682 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
683 return handle;
686 static obj_handle_t pipe_client_flush( struct fd *fd, struct async *async, int blocking )
688 struct pipe_end *pipe_end = get_fd_user( fd );
689 /* FIXME: Support byte mode. */
690 return use_server_io( pipe_end ) ? pipe_end_flush( pipe_end, async, blocking ) : 0;
693 static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
695 struct pipe_message *message;
697 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
699 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
700 iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
701 iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
702 ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
704 else
706 data_size_t avail = 0;
707 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
709 avail += message->iosb->in_size - message->read_pos;
710 if (avail >= iosb->out_size) break;
712 iosb->out_size = min( iosb->out_size, avail );
713 iosb->status = STATUS_SUCCESS;
716 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
717 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
719 iosb->out_data = message->iosb->in_data;
720 message->iosb->in_data = NULL;
721 wake_message( message );
722 free_message( message );
724 else
726 data_size_t write_pos = 0, writing;
727 char *buf = NULL;
729 if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
731 iosb->out_size = 0;
732 iosb->status = STATUS_NO_MEMORY;
733 return;
738 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
739 writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
740 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
741 write_pos += writing;
742 message->read_pos += writing;
743 if (message->read_pos == message->iosb->in_size)
745 wake_message(message);
746 free_message(message);
748 } while (write_pos < iosb->out_size);
750 iosb->result = iosb->out_size;
753 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
754 * We're not interested in such reselect calls, so we ignore them. */
755 static int ignore_reselect;
757 static void reselect_write_queue( struct pipe_end *pipe_end );
759 static void reselect_read_queue( struct pipe_end *pipe_end )
761 struct async *async;
762 struct iosb *iosb;
763 int read_done = 0;
765 ignore_reselect = 1;
766 while (!list_empty( &pipe_end->message_queue) && (async = find_pending_async( pipe_end->read_q )))
768 iosb = async_get_iosb( async );
769 message_queue_read( pipe_end, iosb );
770 async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
771 release_object( async );
772 release_object( iosb );
773 read_done = 1;
775 ignore_reselect = 0;
777 if (pipe_end->connection)
779 if (list_empty( &pipe_end->message_queue ))
780 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
781 else if (read_done)
782 reselect_write_queue( pipe_end->connection );
786 static void reselect_write_queue( struct pipe_end *pipe_end )
788 struct pipe_message *message, *next;
789 struct pipe_end *reader = pipe_end->connection;
790 data_size_t avail = 0;
792 if (!reader) return;
794 ignore_reselect = 1;
796 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
798 if (message->async && message->iosb->status != STATUS_PENDING)
800 release_object( message->async );
801 message->async = NULL;
802 free_message( message );
804 else
806 avail += message->iosb->in_size - message->read_pos;
807 if (message->iosb->status == STATUS_PENDING && (avail <= reader->buffer_size || !message->iosb->in_size))
808 wake_message( message );
812 ignore_reselect = 0;
813 reselect_read_queue( reader );
816 static obj_handle_t pipe_end_read( struct fd *fd, struct async *async, int blocking, file_pos_t pos )
818 struct pipe_end *pipe_end = get_fd_user( fd );
819 obj_handle_t handle = 0;
821 if (!use_server_io( pipe_end )) return no_fd_read( fd, async, blocking, pos );
823 if (!pipe_end->connection && list_empty( &pipe_end->message_queue ))
825 set_error( STATUS_PIPE_BROKEN );
826 return 0;
829 if (!pipe_end->read_q && !(pipe_end->read_q = create_async_queue( fd ))) return 0;
830 if (!(handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 ))) return 0;
832 queue_async( pipe_end->read_q, async );
833 reselect_read_queue( pipe_end );
834 set_error( STATUS_PENDING );
836 if (!blocking)
838 struct iosb *iosb;
839 iosb = async_get_iosb( async );
840 if (iosb->status == STATUS_PENDING)
842 close_handle( current->process, handle );
843 handle = 0;
845 release_object( iosb );
847 return handle;
850 static obj_handle_t pipe_end_write( struct fd *fd, struct async *async, int blocking, file_pos_t pos )
852 struct pipe_end *write_end = get_fd_user( fd );
853 struct pipe_end *read_end = write_end->connection;
854 struct pipe_message *message;
855 obj_handle_t handle = 0;
857 if (!use_server_io( write_end )) return no_fd_write( fd, async, blocking, pos );
859 if (!read_end)
861 set_error( STATUS_PIPE_DISCONNECTED );
862 return 0;
865 if (!write_end->write_q && !(write_end->write_q = create_async_queue( fd ))) return 0;
866 if (!(handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 ))) return 0;
868 if (!(message = mem_alloc( sizeof(*message) )))
870 close_handle( current->process, handle );
871 return 0;
873 message->async = (struct async *)grab_object( async );
874 message->iosb = async_get_iosb( async );
875 message->read_pos = 0;
876 list_add_tail( &read_end->message_queue, &message->entry );
878 queue_async( write_end->write_q, async );
879 reselect_write_queue( write_end );
880 set_error( STATUS_PENDING );
882 if (!blocking)
884 struct iosb *iosb;
885 iosb = async_get_iosb( async );
886 if (iosb->status == STATUS_PENDING)
888 close_handle( current->process, handle );
889 handle = 0;
891 release_object( iosb );
893 return handle;
896 static void pipe_end_queue_async( struct fd *fd, struct async *async, int type, int count )
898 struct pipe_end *pipe_end = get_fd_user( fd );
899 if (use_server_io( pipe_end )) no_fd_queue_async( fd, async, type, count );
900 else default_fd_queue_async( fd, async, type, count );
903 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
905 struct pipe_end *pipe_end = get_fd_user( fd );
907 if (ignore_reselect) return;
909 if (!use_server_io( pipe_end ))
910 default_fd_reselect_async( fd, queue );
911 else if (pipe_end->write_q && pipe_end->write_q == queue)
912 reselect_write_queue( pipe_end );
913 else if (pipe_end->read_q && pipe_end->read_q == queue)
914 reselect_read_queue( pipe_end );
917 static inline int is_overlapped( unsigned int options )
919 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
922 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
924 return FD_TYPE_PIPE;
927 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
929 return FD_TYPE_PIPE;
932 static void pipe_end_peek( struct pipe_end *pipe_end )
934 unsigned reply_size = get_reply_max_size();
935 FILE_PIPE_PEEK_BUFFER *buffer;
936 struct pipe_message *message;
937 data_size_t avail = 0;
939 if (!use_server_io( pipe_end ))
941 set_error( STATUS_NOT_SUPPORTED );
942 return;
945 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
947 set_error( STATUS_INFO_LENGTH_MISMATCH );
948 return;
950 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
952 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
953 avail += message->iosb->in_size - message->read_pos;
955 if (avail)
957 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
958 reply_size = min( reply_size, message->iosb->in_size - message->read_pos );
960 else reply_size = 0;
962 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return;
963 buffer->NamedPipeState = 0; /* FIXME */
964 buffer->ReadDataAvailable = avail;
965 buffer->NumberOfMessages = 0; /* FIXME */
966 buffer->MessageLength = 0; /* FIXME */
967 if (reply_size) memcpy( buffer->Data, (const char *)message->iosb->in_data + message->read_pos, reply_size );
970 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async,
971 int blocking )
973 struct pipe_server *server = get_fd_user( fd );
974 obj_handle_t wait_handle = 0;
976 switch(code)
978 case FSCTL_PIPE_LISTEN:
979 switch(server->state)
981 case ps_idle_server:
982 case ps_wait_connect:
983 if (fd_queue_async( server->ioctl_fd, async, ASYNC_TYPE_WAIT ))
985 if (blocking) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
986 set_server_state( server, ps_wait_open );
987 if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
988 set_error( STATUS_PENDING );
989 return wait_handle;
991 break;
992 case ps_connected_server:
993 set_error( STATUS_PIPE_CONNECTED );
994 break;
995 case ps_wait_disconnect:
996 set_error( STATUS_NO_DATA_DETECTED );
997 break;
998 case ps_wait_open:
999 set_error( STATUS_INVALID_HANDLE );
1000 break;
1002 return 0;
1004 case FSCTL_PIPE_DISCONNECT:
1005 switch(server->state)
1007 case ps_connected_server:
1008 assert( server->client );
1009 assert( server->client->pipe_end.fd );
1011 notify_empty( server );
1013 /* dump the client and server fds - client loses all waiting data */
1014 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1015 do_disconnect( server );
1016 server->client->server = NULL;
1017 server->client = NULL;
1018 set_server_state( server, ps_wait_connect );
1019 break;
1020 case ps_wait_disconnect:
1021 assert( !server->client );
1022 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1023 do_disconnect( server );
1024 set_server_state( server, ps_wait_connect );
1025 break;
1026 case ps_idle_server:
1027 case ps_wait_open:
1028 set_error( STATUS_PIPE_LISTENING );
1029 break;
1030 case ps_wait_connect:
1031 set_error( STATUS_PIPE_DISCONNECTED );
1032 break;
1034 return 0;
1036 case FSCTL_PIPE_PEEK:
1037 pipe_end_peek( &server->pipe_end );
1038 return 0;
1040 default:
1041 return default_fd_ioctl( fd, code, async, blocking );
1045 static obj_handle_t pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async,
1046 int blocking )
1048 struct pipe_client *client = get_fd_user( fd );
1050 switch(code)
1052 case FSCTL_PIPE_PEEK:
1053 pipe_end_peek( &client->pipe_end );
1054 return 0;
1056 default:
1057 return default_fd_ioctl( fd, code, async, blocking );
1061 static struct pipe_server *get_pipe_server_obj( struct process *process,
1062 obj_handle_t handle, unsigned int access )
1064 struct object *obj;
1065 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
1066 return (struct pipe_server *) obj;
1069 static void init_pipe_end( struct pipe_end *pipe_end, unsigned int pipe_flags, data_size_t buffer_size )
1071 pipe_end->fd = NULL;
1072 pipe_end->flags = pipe_flags;
1073 pipe_end->connection = NULL;
1074 pipe_end->buffer_size = buffer_size;
1075 pipe_end->read_q = NULL;
1076 pipe_end->write_q = NULL;
1077 list_init( &pipe_end->message_queue );
1080 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
1081 unsigned int pipe_flags )
1083 struct pipe_server *server;
1085 server = alloc_object( &pipe_server_ops );
1086 if (!server)
1087 return NULL;
1089 server->pipe = pipe;
1090 server->client = NULL;
1091 server->flush_poll = NULL;
1092 server->options = options;
1093 init_pipe_end( &server->pipe_end, pipe_flags, pipe->insize );
1095 list_add_head( &pipe->servers, &server->entry );
1096 grab_object( pipe );
1097 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1099 release_object( server );
1100 return NULL;
1102 set_fd_signaled( server->ioctl_fd, 1 );
1103 set_server_state( server, ps_idle_server );
1104 return server;
1107 static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags, data_size_t buffer_size )
1109 struct pipe_client *client;
1111 client = alloc_object( &pipe_client_ops );
1112 if (!client)
1113 return NULL;
1115 client->server = NULL;
1116 client->flags = flags;
1117 init_pipe_end( &client->pipe_end, pipe_flags, buffer_size );
1119 return client;
1122 static struct pipe_server *find_available_server( struct named_pipe *pipe )
1124 struct pipe_server *server;
1126 /* look for pipe servers that are listening */
1127 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1129 if (server->state == ps_wait_open)
1130 return (struct pipe_server *)grab_object( server );
1133 /* fall back to pipe servers that are idle */
1134 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1136 if (server->state == ps_idle_server)
1137 return (struct pipe_server *)grab_object( server );
1140 return NULL;
1143 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1145 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1147 if (parent->ops != &named_pipe_device_ops)
1149 set_error( STATUS_OBJECT_NAME_INVALID );
1150 return 0;
1152 namespace_add( dev->pipes, name );
1153 name->parent = grab_object( parent );
1154 return 1;
1157 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1158 unsigned int sharing, unsigned int options )
1160 struct named_pipe *pipe = (struct named_pipe *)obj;
1161 struct pipe_server *server;
1162 struct pipe_client *client;
1163 unsigned int pipe_sharing;
1164 int fds[2];
1166 if (!(server = find_available_server( pipe )))
1168 set_error( STATUS_PIPE_NOT_AVAILABLE );
1169 return NULL;
1172 pipe_sharing = server->pipe->sharing;
1173 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1174 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1176 set_error( STATUS_ACCESS_DENIED );
1177 release_object( server );
1178 return NULL;
1181 if ((client = create_pipe_client( options, pipe->flags, pipe->outsize )))
1183 if (use_server_io( &server->pipe_end ))
1185 client->pipe_end.fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->pipe_end.obj, options );
1186 if (client->pipe_end.fd)
1188 set_fd_signaled( client->pipe_end.fd, 1 );
1189 server->pipe_end.fd = (struct fd *)grab_object( server->ioctl_fd );
1190 set_no_fd_status( server->ioctl_fd, STATUS_BAD_DEVICE_TYPE );
1192 else
1194 release_object( client );
1195 client = NULL;
1198 else if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
1200 assert( !server->pipe_end.fd );
1202 /* for performance reasons, only set nonblocking mode when using
1203 * overlapped I/O. Otherwise, we will be doing too much busy
1204 * looping */
1205 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
1206 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
1208 if (pipe->insize)
1210 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
1211 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
1213 if (pipe->outsize)
1215 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
1216 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
1219 client->pipe_end.fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->pipe_end.obj, options );
1220 server->pipe_end.fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->pipe_end.obj, server->options );
1221 if (client->pipe_end.fd && server->pipe_end.fd)
1223 fd_copy_completion( server->ioctl_fd, server->pipe_end.fd );
1225 else
1227 release_object( client );
1228 client = NULL;
1231 else
1233 file_set_error();
1234 release_object( client );
1235 client = NULL;
1237 if (client)
1239 allow_fd_caching( client->pipe_end.fd );
1240 allow_fd_caching( server->pipe_end.fd );
1241 if (server->state == ps_wait_open)
1242 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
1243 set_server_state( server, ps_connected_server );
1244 server->client = client;
1245 client->server = server;
1246 server->pipe_end.connection = &client->pipe_end;
1247 client->pipe_end.connection = &server->pipe_end;
1250 release_object( server );
1251 return &client->pipe_end.obj;
1254 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code,
1255 struct async *async, int blocking )
1257 struct named_pipe_device *device = get_fd_user( fd );
1259 switch(code)
1261 case FSCTL_PIPE_WAIT:
1263 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1264 data_size_t size = get_req_data_size();
1265 obj_handle_t wait_handle = 0;
1266 struct named_pipe *pipe;
1267 struct pipe_server *server;
1268 struct unicode_str name;
1269 timeout_t when;
1271 if (size < sizeof(*buffer) ||
1272 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1274 set_error( STATUS_INVALID_PARAMETER );
1275 return 0;
1277 name.str = buffer->Name;
1278 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1279 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;
1281 if (!(server = find_available_server( pipe )))
1283 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
1285 queue_async( pipe->waiters, async );
1286 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1287 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1288 if (blocking) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
1289 set_error( STATUS_PENDING );
1291 else release_object( server );
1293 done:
1294 release_object( pipe );
1295 return wait_handle;
1298 default:
1299 return default_fd_ioctl( fd, code, async, blocking );
1304 DECL_HANDLER(create_named_pipe)
1306 struct named_pipe *pipe;
1307 struct pipe_server *server;
1308 struct unicode_str name;
1309 struct object *root;
1310 const struct security_descriptor *sd;
1311 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1313 if (!objattr) return;
1315 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1316 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1318 if (root) release_object( root );
1319 set_error( STATUS_INVALID_PARAMETER );
1320 return;
1323 if (!name.len) /* pipes need a root directory even without a name */
1325 if (!objattr->rootdir)
1327 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1328 return;
1330 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
1333 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1335 if (root) release_object( root );
1336 if (!pipe) return;
1338 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1340 /* initialize it if it didn't already exist */
1341 pipe->instances = 0;
1342 pipe->waiters = NULL;
1343 list_init( &pipe->servers );
1344 pipe->insize = req->insize;
1345 pipe->outsize = req->outsize;
1346 pipe->maxinstances = req->maxinstances;
1347 pipe->timeout = req->timeout;
1348 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
1349 pipe->sharing = req->sharing;
1351 else
1353 if (pipe->maxinstances <= pipe->instances)
1355 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1356 release_object( pipe );
1357 return;
1359 if (pipe->sharing != req->sharing)
1361 set_error( STATUS_ACCESS_DENIED );
1362 release_object( pipe );
1363 return;
1365 clear_error(); /* clear the name collision */
1368 server = create_pipe_server( pipe, req->options, req->flags );
1369 if (server)
1371 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1372 server->pipe->instances++;
1373 if (sd) default_set_sd( &server->pipe_end.obj, sd, OWNER_SECURITY_INFORMATION |
1374 GROUP_SECURITY_INFORMATION |
1375 DACL_SECURITY_INFORMATION |
1376 SACL_SECURITY_INFORMATION );
1377 release_object( server );
1380 release_object( pipe );
1383 DECL_HANDLER(get_named_pipe_info)
1385 struct pipe_server *server;
1386 struct pipe_client *client = NULL;
1388 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1389 if (!server)
1391 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1392 return;
1394 clear_error();
1395 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1396 0, &pipe_client_ops );
1397 if (!client) return;
1398 server = client->server;
1401 reply->flags = client ? client->pipe_end.flags : server->pipe_end.flags;
1402 if (server)
1404 reply->sharing = server->pipe->sharing;
1405 reply->maxinstances = server->pipe->maxinstances;
1406 reply->instances = server->pipe->instances;
1407 reply->insize = server->pipe->insize;
1408 reply->outsize = server->pipe->outsize;
1411 if (client)
1412 release_object(client);
1413 else
1415 reply->flags |= NAMED_PIPE_SERVER_END;
1416 release_object(server);
1420 DECL_HANDLER(set_named_pipe_info)
1422 struct pipe_server *server;
1423 struct pipe_client *client = NULL;
1425 server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
1426 if (!server)
1428 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1429 return;
1431 clear_error();
1432 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1433 0, &pipe_client_ops );
1434 if (!client) return;
1435 if (!(server = client->server))
1437 release_object( client );
1438 return;
1442 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1443 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1445 set_error( STATUS_INVALID_PARAMETER );
1447 else if (client)
1449 client->pipe_end.flags = server->pipe->flags | req->flags;
1451 else
1453 server->pipe_end.flags = server->pipe->flags | req->flags;
1456 if (client)
1457 release_object(client);
1458 else
1459 release_object(server);