kernel32: Add support for FILE_FLAG_WRITE_THROUGH to CreateFile.
[wine.git] / server / named_pipe.c
bloba1f3723729565b474af6af5137120b1263749442
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 <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winternl.h"
36 #include "winioctl.h"
38 #include "file.h"
39 #include "handle.h"
40 #include "thread.h"
41 #include "request.h"
42 #include "security.h"
43 #include "process.h"
45 enum pipe_state
47 ps_idle_server,
48 ps_wait_open,
49 ps_connected_server,
50 ps_wait_disconnect,
51 ps_wait_connect
54 struct named_pipe;
56 struct pipe_message
58 struct list entry; /* entry in message queue */
59 data_size_t read_pos; /* already read bytes */
60 struct iosb *iosb; /* message iosb */
61 struct async *async; /* async of pending write */
64 struct pipe_end
66 struct object obj; /* object header */
67 struct fd *fd; /* pipe file descriptor */
68 unsigned int flags; /* pipe flags */
69 struct pipe_end *connection; /* the other end of the pipe */
70 process_id_t client_pid; /* process that created the client */
71 process_id_t server_pid; /* process that created the server */
72 data_size_t buffer_size;/* size of buffered data that doesn't block caller */
73 struct list message_queue;
74 struct async_queue read_q; /* read queue */
75 struct async_queue write_q; /* write queue */
78 struct pipe_server
80 struct pipe_end pipe_end; /* common header for pipe_client and pipe_server */
81 struct list entry; /* entry in named pipe servers list */
82 enum pipe_state state; /* server state */
83 struct pipe_client *client; /* client that this server is connected to */
84 struct named_pipe *pipe;
85 unsigned int options; /* pipe options */
88 struct pipe_client
90 struct pipe_end pipe_end; /* common header for pipe_client and pipe_server */
91 struct pipe_server *server; /* server that this client is connected to */
92 unsigned int flags; /* file flags */
95 struct named_pipe
97 struct object obj; /* object header */
98 unsigned int flags;
99 unsigned int sharing;
100 unsigned int maxinstances;
101 unsigned int outsize;
102 unsigned int insize;
103 unsigned int instances;
104 timeout_t timeout;
105 struct list servers; /* list of servers using this pipe */
106 struct async_queue waiters; /* list of clients waiting to connect */
109 struct named_pipe_device
111 struct object obj; /* object header */
112 struct fd *fd; /* pseudo-fd for ioctls */
113 struct namespace *pipes; /* named pipe namespace */
116 static void named_pipe_dump( struct object *obj, int verbose );
117 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
118 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent );
119 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
120 unsigned int sharing, unsigned int options );
121 static void named_pipe_destroy( struct object *obj );
123 static const struct object_ops named_pipe_ops =
125 sizeof(struct named_pipe), /* size */
126 named_pipe_dump, /* dump */
127 no_get_type, /* get_type */
128 no_add_queue, /* add_queue */
129 NULL, /* remove_queue */
130 NULL, /* signaled */
131 NULL, /* satisfied */
132 no_signal, /* signal */
133 no_get_fd, /* get_fd */
134 named_pipe_map_access, /* map_access */
135 default_get_sd, /* get_sd */
136 default_set_sd, /* set_sd */
137 no_lookup_name, /* lookup_name */
138 named_pipe_link_name, /* link_name */
139 default_unlink_name, /* unlink_name */
140 named_pipe_open_file, /* open_file */
141 no_close_handle, /* close_handle */
142 named_pipe_destroy /* destroy */
145 /* common server and client pipe end functions */
146 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd );
147 static struct fd *pipe_end_get_fd( struct object *obj );
148 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos );
149 static int pipe_end_write( struct fd *fd, struct async *async_data, file_pos_t pos );
150 static int pipe_end_flush( struct fd *fd, struct async *async );
151 static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class );
152 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue );
154 /* server end functions */
155 static void pipe_server_dump( struct object *obj, int verbose );
156 static struct security_descriptor *pipe_server_get_sd( struct object *obj );
157 static int pipe_server_set_sd( struct object *obj, const struct security_descriptor *sd,
158 unsigned int set_info );
159 static void pipe_server_destroy( struct object *obj);
160 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
161 static void pipe_server_get_file_info( struct fd *fd, unsigned int info_class );
163 static const struct object_ops pipe_server_ops =
165 sizeof(struct pipe_server), /* size */
166 pipe_server_dump, /* dump */
167 no_get_type, /* get_type */
168 add_queue, /* add_queue */
169 remove_queue, /* remove_queue */
170 default_fd_signaled, /* signaled */
171 no_satisfied, /* satisfied */
172 no_signal, /* signal */
173 pipe_end_get_fd, /* get_fd */
174 default_fd_map_access, /* map_access */
175 pipe_server_get_sd, /* get_sd */
176 pipe_server_set_sd, /* set_sd */
177 no_lookup_name, /* lookup_name */
178 no_link_name, /* link_name */
179 NULL, /* unlink_name */
180 no_open_file, /* open_file */
181 fd_close_handle, /* close_handle */
182 pipe_server_destroy /* destroy */
185 static const struct fd_ops pipe_server_fd_ops =
187 default_fd_get_poll_events, /* get_poll_events */
188 default_poll_event, /* poll_event */
189 pipe_end_get_fd_type, /* get_fd_type */
190 pipe_end_read, /* read */
191 pipe_end_write, /* write */
192 pipe_end_flush, /* flush */
193 pipe_server_get_file_info, /* get_file_info */
194 pipe_end_get_volume_info, /* get_volume_info */
195 pipe_server_ioctl, /* ioctl */
196 no_fd_queue_async, /* queue_async */
197 pipe_end_reselect_async /* reselect_async */
200 /* client end functions */
201 static void pipe_client_dump( struct object *obj, int verbose );
202 static struct security_descriptor *pipe_client_get_sd( struct object *obj );
203 static int pipe_client_set_sd( struct object *obj, const struct security_descriptor *sd,
204 unsigned int set_info );
205 static void pipe_client_destroy( struct object *obj );
206 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
207 static void pipe_client_get_file_info( struct fd *fd, unsigned int info_class );
209 static const struct object_ops pipe_client_ops =
211 sizeof(struct pipe_client), /* size */
212 pipe_client_dump, /* dump */
213 no_get_type, /* get_type */
214 add_queue, /* add_queue */
215 remove_queue, /* remove_queue */
216 default_fd_signaled, /* signaled */
217 no_satisfied, /* satisfied */
218 no_signal, /* signal */
219 pipe_end_get_fd, /* get_fd */
220 default_fd_map_access, /* map_access */
221 pipe_client_get_sd, /* get_sd */
222 pipe_client_set_sd, /* set_sd */
223 no_lookup_name, /* lookup_name */
224 no_link_name, /* link_name */
225 NULL, /* unlink_name */
226 no_open_file, /* open_file */
227 fd_close_handle, /* close_handle */
228 pipe_client_destroy /* destroy */
231 static const struct fd_ops pipe_client_fd_ops =
233 default_fd_get_poll_events, /* get_poll_events */
234 default_poll_event, /* poll_event */
235 pipe_end_get_fd_type, /* get_fd_type */
236 pipe_end_read, /* read */
237 pipe_end_write, /* write */
238 pipe_end_flush, /* flush */
239 pipe_client_get_file_info, /* get_file_info */
240 pipe_end_get_volume_info, /* get_volume_info */
241 pipe_client_ioctl, /* ioctl */
242 no_fd_queue_async, /* queue_async */
243 pipe_end_reselect_async /* reselect_async */
246 static void named_pipe_device_dump( struct object *obj, int verbose );
247 static struct object_type *named_pipe_device_get_type( struct object *obj );
248 static struct fd *named_pipe_device_get_fd( struct object *obj );
249 static struct object *named_pipe_device_lookup_name( struct object *obj,
250 struct unicode_str *name, unsigned int attr );
251 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
252 unsigned int sharing, unsigned int options );
253 static void named_pipe_device_destroy( struct object *obj );
254 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
255 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
257 static const struct object_ops named_pipe_device_ops =
259 sizeof(struct named_pipe_device), /* size */
260 named_pipe_device_dump, /* dump */
261 named_pipe_device_get_type, /* get_type */
262 no_add_queue, /* add_queue */
263 NULL, /* remove_queue */
264 NULL, /* signaled */
265 no_satisfied, /* satisfied */
266 no_signal, /* signal */
267 named_pipe_device_get_fd, /* get_fd */
268 no_map_access, /* map_access */
269 default_get_sd, /* get_sd */
270 default_set_sd, /* set_sd */
271 named_pipe_device_lookup_name, /* lookup_name */
272 directory_link_name, /* link_name */
273 default_unlink_name, /* unlink_name */
274 named_pipe_device_open_file, /* open_file */
275 fd_close_handle, /* close_handle */
276 named_pipe_device_destroy /* destroy */
279 static const struct fd_ops named_pipe_device_fd_ops =
281 default_fd_get_poll_events, /* get_poll_events */
282 default_poll_event, /* poll_event */
283 named_pipe_device_get_fd_type, /* get_fd_type */
284 no_fd_read, /* read */
285 no_fd_write, /* write */
286 no_fd_flush, /* flush */
287 no_fd_get_file_info, /* get_file_info */
288 no_fd_get_volume_info, /* get_volume_info */
289 named_pipe_device_ioctl, /* ioctl */
290 default_fd_queue_async, /* queue_async */
291 default_fd_reselect_async /* reselect_async */
294 static void named_pipe_dump( struct object *obj, int verbose )
296 fputs( "Named pipe\n", stderr );
299 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
301 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
302 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
303 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
304 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
305 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
308 static void pipe_server_dump( struct object *obj, int verbose )
310 struct pipe_server *server = (struct pipe_server *) obj;
311 assert( obj->ops == &pipe_server_ops );
312 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
315 static void pipe_client_dump( struct object *obj, int verbose )
317 struct pipe_client *client = (struct pipe_client *) obj;
318 assert( obj->ops == &pipe_client_ops );
319 fprintf( stderr, "Named pipe client server=%p\n", client->server );
322 static void named_pipe_destroy( struct object *obj)
324 struct named_pipe *pipe = (struct named_pipe *) obj;
326 assert( list_empty( &pipe->servers ) );
327 assert( !pipe->instances );
328 free_async_queue( &pipe->waiters );
331 static struct fd *pipe_end_get_fd( struct object *obj )
333 struct pipe_end *pipe_end = (struct pipe_end *) obj;
334 return (struct fd *) grab_object( pipe_end->fd );
337 static void set_server_state( struct pipe_server *server, enum pipe_state state )
339 server->state = state;
341 switch(state)
343 case ps_connected_server:
344 case ps_wait_disconnect:
345 break;
346 case ps_wait_open:
347 case ps_idle_server:
348 set_no_fd_status( server->pipe_end.fd, STATUS_PIPE_LISTENING );
349 break;
350 case ps_wait_connect:
351 set_no_fd_status( server->pipe_end.fd, STATUS_PIPE_DISCONNECTED );
352 break;
357 static struct pipe_message *queue_message( struct pipe_end *pipe_end, struct iosb *iosb )
359 struct pipe_message *message;
361 if (!(message = mem_alloc( sizeof(*message) ))) return NULL;
362 message->iosb = (struct iosb *)grab_object( iosb );
363 message->async = NULL;
364 message->read_pos = 0;
365 list_add_tail( &pipe_end->message_queue, &message->entry );
366 return message;
369 static void wake_message( struct pipe_message *message )
371 struct async *async = message->async;
373 message->async = NULL;
374 if (!async) return;
376 message->iosb->status = STATUS_SUCCESS;
377 message->iosb->result = message->iosb->in_size;
378 async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
379 release_object( async );
382 static void free_message( struct pipe_message *message )
384 list_remove( &message->entry );
385 if (message->iosb) release_object( message->iosb );
386 free( message );
389 static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
391 struct pipe_end *connection = pipe_end->connection;
392 struct pipe_message *message, *next;
393 struct async *async;
395 pipe_end->connection = NULL;
397 fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
398 async_wake_up( &pipe_end->read_q, status );
399 LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
401 async = message->async;
402 if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
403 if (!async) continue;
404 async_terminate( async, status );
405 release_object( async );
407 if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
409 if (connection)
411 connection->connection = NULL;
412 pipe_end_disconnect( connection, status );
416 static void pipe_end_destroy( struct pipe_end *pipe_end )
418 struct pipe_message *message;
420 while (!list_empty( &pipe_end->message_queue ))
422 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
423 assert( !message->async );
424 free_message( message );
427 free_async_queue( &pipe_end->read_q );
428 free_async_queue( &pipe_end->write_q );
429 if (pipe_end->fd) release_object( pipe_end->fd );
432 static void pipe_server_destroy( struct object *obj)
434 struct pipe_server *server = (struct pipe_server *)obj;
436 assert( obj->ops == &pipe_server_ops );
438 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_BROKEN );
440 pipe_end_destroy( &server->pipe_end );
441 if (server->client)
443 server->client->server = NULL;
444 server->client = NULL;
447 assert( server->pipe->instances );
448 server->pipe->instances--;
450 list_remove( &server->entry );
451 release_object( server->pipe );
454 static void pipe_client_destroy( struct object *obj)
456 struct pipe_client *client = (struct pipe_client *)obj;
457 struct pipe_server *server = client->server;
459 assert( obj->ops == &pipe_client_ops );
461 pipe_end_disconnect( &client->pipe_end, STATUS_PIPE_BROKEN );
463 if (server)
465 switch(server->state)
467 case ps_connected_server:
468 /* Don't destroy the server's fd here as we can't
469 do a successful flush without it. */
470 set_server_state( server, ps_wait_disconnect );
471 break;
472 case ps_idle_server:
473 case ps_wait_open:
474 case ps_wait_disconnect:
475 case ps_wait_connect:
476 assert( 0 );
478 assert( server->client );
479 server->client = NULL;
480 client->server = NULL;
483 pipe_end_destroy( &client->pipe_end );
486 static void named_pipe_device_dump( struct object *obj, int verbose )
488 fputs( "Named pipe device\n", stderr );
491 static struct object_type *named_pipe_device_get_type( struct object *obj )
493 static const WCHAR name[] = {'D','e','v','i','c','e'};
494 static const struct unicode_str str = { name, sizeof(name) };
495 return get_object_type( &str );
498 static struct fd *named_pipe_device_get_fd( struct object *obj )
500 struct named_pipe_device *device = (struct named_pipe_device *)obj;
501 return (struct fd *)grab_object( device->fd );
504 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
505 unsigned int attr )
507 struct named_pipe_device *device = (struct named_pipe_device*)obj;
508 struct object *found;
510 assert( obj->ops == &named_pipe_device_ops );
511 assert( device->pipes );
513 if (!name) return NULL; /* open the device itself */
515 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
516 name->len = 0;
518 return found;
521 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
522 unsigned int sharing, unsigned int options )
524 return grab_object( obj );
527 static void named_pipe_device_destroy( struct object *obj )
529 struct named_pipe_device *device = (struct named_pipe_device*)obj;
530 assert( obj->ops == &named_pipe_device_ops );
531 if (device->fd) release_object( device->fd );
532 free( device->pipes );
535 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
537 return FD_TYPE_DEVICE;
540 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name )
542 struct named_pipe_device *dev;
544 if ((dev = create_named_object( root, &named_pipe_device_ops, name, 0, NULL )) &&
545 get_error() != STATUS_OBJECT_NAME_EXISTS)
547 dev->pipes = NULL;
548 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
549 !(dev->pipes = create_namespace( 7 )))
551 release_object( dev );
552 dev = NULL;
555 return &dev->obj;
558 static int pipe_end_flush( struct fd *fd, struct async *async )
560 struct pipe_end *pipe_end = get_fd_user( fd );
562 if (pipe_end->connection && !list_empty( &pipe_end->connection->message_queue ))
564 fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT );
565 set_error( STATUS_PENDING );
567 return 1;
570 static void pipe_end_get_file_info( struct fd *fd, struct named_pipe *pipe, unsigned int info_class )
572 switch (info_class)
574 case FileNameInformation:
576 FILE_NAME_INFORMATION *name_info;
577 data_size_t name_len, reply_size;
578 const WCHAR *name;
580 if (get_reply_max_size() < sizeof(*name_info))
582 set_error( STATUS_INFO_LENGTH_MISMATCH );
583 return;
586 name = get_object_name( &pipe->obj, &name_len );
587 reply_size = offsetof( FILE_NAME_INFORMATION, FileName[name_len/sizeof(WCHAR) + 1] );
588 if (reply_size > get_reply_max_size())
590 reply_size = get_reply_max_size();
591 set_error( STATUS_BUFFER_OVERFLOW );
594 if (!(name_info = set_reply_data_size( reply_size ))) return;
595 name_info->FileNameLength = name_len + sizeof(WCHAR);
596 name_info->FileName[0] = '\\';
597 reply_size -= offsetof( FILE_NAME_INFORMATION, FileName[1] );
598 if (reply_size) memcpy( &name_info->FileName[1], name, reply_size );
599 break;
601 default:
602 no_fd_get_file_info( fd, info_class );
606 static struct security_descriptor *pipe_server_get_sd( struct object *obj )
608 struct pipe_server *server = (struct pipe_server *) obj;
609 return default_get_sd( &server->pipe->obj );
612 static struct security_descriptor *pipe_client_get_sd( struct object *obj )
614 struct pipe_client *client = (struct pipe_client *) obj;
615 if (client->server) return default_get_sd( &client->server->pipe->obj );
616 set_error( STATUS_PIPE_DISCONNECTED );
617 return NULL;
620 static int pipe_server_set_sd( struct object *obj, const struct security_descriptor *sd,
621 unsigned int set_info )
623 struct pipe_server *server = (struct pipe_server *) obj;
624 return default_set_sd( &server->pipe->obj, sd, set_info );
627 static int pipe_client_set_sd( struct object *obj, const struct security_descriptor *sd,
628 unsigned int set_info )
630 struct pipe_client *client = (struct pipe_client *) obj;
631 if (client->server) return default_set_sd( &client->server->pipe->obj, sd, set_info );
632 set_error( STATUS_PIPE_DISCONNECTED );
633 return 0;
636 static void pipe_server_get_file_info( struct fd *fd, unsigned int info_class )
638 struct pipe_server *server = get_fd_user( fd );
639 pipe_end_get_file_info( fd, server->pipe, info_class );
642 static void pipe_client_get_file_info( struct fd *fd, unsigned int info_class )
644 struct pipe_client *client = get_fd_user( fd );
645 if (client->server) pipe_end_get_file_info( fd, client->server->pipe, info_class );
646 else set_error( STATUS_PIPE_DISCONNECTED );
649 static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class )
651 switch (info_class)
653 case FileFsDeviceInformation:
655 static const FILE_FS_DEVICE_INFORMATION device_info =
657 FILE_DEVICE_NAMED_PIPE,
658 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
660 if (get_reply_max_size() >= sizeof(device_info))
661 set_reply_data( &device_info, sizeof(device_info) );
662 else
663 set_error( STATUS_BUFFER_TOO_SMALL );
664 break;
666 default:
667 set_error( STATUS_NOT_IMPLEMENTED );
671 static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
673 struct pipe_message *message;
675 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
677 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
678 iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
679 iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
680 ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
682 else
684 data_size_t avail = 0;
685 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
687 avail += message->iosb->in_size - message->read_pos;
688 if (avail >= iosb->out_size) break;
690 iosb->out_size = min( iosb->out_size, avail );
691 iosb->status = STATUS_SUCCESS;
694 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
695 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
697 iosb->out_data = message->iosb->in_data;
698 message->iosb->in_data = NULL;
699 wake_message( message );
700 free_message( message );
702 else
704 data_size_t write_pos = 0, writing;
705 char *buf = NULL;
707 if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
709 iosb->out_size = 0;
710 iosb->status = STATUS_NO_MEMORY;
711 return;
716 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
717 writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
718 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
719 write_pos += writing;
720 message->read_pos += writing;
721 if (message->read_pos == message->iosb->in_size)
723 wake_message(message);
724 free_message(message);
726 } while (write_pos < iosb->out_size);
728 iosb->result = iosb->out_size;
731 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
732 * We're not interested in such reselect calls, so we ignore them. */
733 static int ignore_reselect;
735 static void reselect_write_queue( struct pipe_end *pipe_end );
737 static void reselect_read_queue( struct pipe_end *pipe_end )
739 struct async *async;
740 struct iosb *iosb;
741 int read_done = 0;
743 ignore_reselect = 1;
744 while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
746 iosb = async_get_iosb( async );
747 message_queue_read( pipe_end, iosb );
748 async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
749 release_object( async );
750 release_object( iosb );
751 read_done = 1;
753 ignore_reselect = 0;
755 if (pipe_end->connection)
757 if (list_empty( &pipe_end->message_queue ))
758 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
759 else if (read_done)
760 reselect_write_queue( pipe_end->connection );
764 static void reselect_write_queue( struct pipe_end *pipe_end )
766 struct pipe_message *message, *next;
767 struct pipe_end *reader = pipe_end->connection;
768 data_size_t avail = 0;
770 if (!reader) return;
772 ignore_reselect = 1;
774 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
776 if (message->async && message->iosb->status != STATUS_PENDING)
778 release_object( message->async );
779 message->async = NULL;
780 free_message( message );
782 else
784 avail += message->iosb->in_size - message->read_pos;
785 if (message->async && (avail <= reader->buffer_size || !message->iosb->in_size))
786 wake_message( message );
790 ignore_reselect = 0;
791 reselect_read_queue( reader );
794 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
796 struct pipe_end *pipe_end = get_fd_user( fd );
798 if (!pipe_end->connection && list_empty( &pipe_end->message_queue ))
800 set_error( STATUS_PIPE_BROKEN );
801 return 0;
804 queue_async( &pipe_end->read_q, async );
805 reselect_read_queue( pipe_end );
806 set_error( STATUS_PENDING );
807 return 1;
810 static int pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
812 struct pipe_end *pipe_end = get_fd_user( fd );
813 struct pipe_message *message;
814 struct iosb *iosb;
816 if (!pipe_end->connection)
818 set_error( STATUS_PIPE_DISCONNECTED );
819 return 0;
822 if (!(pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && !get_req_data_size()) return 1;
824 iosb = async_get_iosb( async );
825 message = queue_message( pipe_end->connection, iosb );
826 release_object( iosb );
827 if (!message) return 0;
829 message->async = (struct async *)grab_object( async );
830 queue_async( &pipe_end->write_q, async );
831 reselect_write_queue( pipe_end );
832 set_error( STATUS_PENDING );
833 return 1;
836 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
838 struct pipe_end *pipe_end = get_fd_user( fd );
840 if (ignore_reselect) return;
842 if (&pipe_end->write_q == queue)
843 reselect_write_queue( pipe_end );
844 else if (&pipe_end->read_q == queue)
845 reselect_read_queue( pipe_end );
848 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
850 return FD_TYPE_PIPE;
853 static int pipe_end_peek( struct pipe_end *pipe_end )
855 unsigned reply_size = get_reply_max_size();
856 FILE_PIPE_PEEK_BUFFER *buffer;
857 struct pipe_message *message;
858 data_size_t avail = 0;
859 data_size_t message_length = 0;
861 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
863 set_error( STATUS_INFO_LENGTH_MISMATCH );
864 return 0;
866 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
868 if (!pipe_end->connection && list_empty( &pipe_end->message_queue ))
870 set_error( STATUS_PIPE_BROKEN );
871 return 0;
874 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
875 avail += message->iosb->in_size - message->read_pos;
876 reply_size = min( reply_size, avail );
878 if (avail && (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE))
880 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
881 message_length = message->iosb->in_size - message->read_pos;
882 reply_size = min( reply_size, message_length );
885 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return 0;
886 buffer->NamedPipeState = 0; /* FIXME */
887 buffer->ReadDataAvailable = avail;
888 buffer->NumberOfMessages = 0; /* FIXME */
889 buffer->MessageLength = message_length;
891 if (reply_size)
893 data_size_t write_pos = 0, writing;
894 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
896 writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
897 memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
898 writing );
899 write_pos += writing;
900 if (write_pos == reply_size) break;
903 return 1;
906 static int pipe_end_transceive( struct pipe_end *pipe_end, struct async *async )
908 struct pipe_message *message;
909 struct iosb *iosb;
911 if ((pipe_end->flags & (NAMED_PIPE_MESSAGE_STREAM_WRITE | NAMED_PIPE_MESSAGE_STREAM_READ))
912 != (NAMED_PIPE_MESSAGE_STREAM_WRITE | NAMED_PIPE_MESSAGE_STREAM_READ))
914 set_error( STATUS_INVALID_READ_MODE );
915 return 0;
918 if (!pipe_end->connection)
920 set_error( STATUS_PIPE_BROKEN );
921 return 0;
924 /* not allowed if we already have read data buffered */
925 if (!list_empty( &pipe_end->message_queue ))
927 set_error( STATUS_PIPE_BUSY );
928 return 0;
931 iosb = async_get_iosb( async );
932 /* ignore output buffer copy transferred because of METHOD_NEITHER */
933 iosb->in_size -= iosb->out_size;
934 /* transaction never blocks on write, so just queue a message without async */
935 message = queue_message( pipe_end->connection, iosb );
936 release_object( iosb );
937 if (!message) return 0;
938 reselect_read_queue( pipe_end->connection );
940 queue_async( &pipe_end->read_q, async );
941 reselect_read_queue( pipe_end );
942 set_error( STATUS_PENDING );
943 return 1;
946 static int pipe_end_get_connection_attribute( struct pipe_end *pipe_end )
948 const char *attr = get_req_data();
949 data_size_t value_size, attr_size = get_req_data_size();
950 void *value;
952 if (attr_size == sizeof("ClientProcessId") && !memcmp( attr, "ClientProcessId", attr_size ))
954 value = &pipe_end->client_pid;
955 value_size = sizeof(pipe_end->client_pid);
957 else if (attr_size == sizeof("ServerProcessId") && !memcmp( attr, "ServerProcessId", attr_size ))
959 value = &pipe_end->server_pid;
960 value_size = sizeof(pipe_end->server_pid);
962 else
964 set_error( STATUS_ILLEGAL_FUNCTION );
965 return 0;
968 if (get_reply_max_size() < value_size)
970 set_error( STATUS_INFO_LENGTH_MISMATCH );
971 return 0;
974 set_reply_data( value, value_size );
975 return 1;
978 static int pipe_end_ioctl( struct pipe_end *pipe_end, ioctl_code_t code, struct async *async )
980 switch(code)
982 case FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE:
983 return pipe_end_get_connection_attribute( pipe_end );
985 case FSCTL_PIPE_PEEK:
986 return pipe_end_peek( pipe_end );
988 case FSCTL_PIPE_TRANSCEIVE:
989 return pipe_end_transceive( pipe_end, async );
991 default:
992 return default_fd_ioctl( pipe_end->fd, code, async );
996 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
998 struct pipe_server *server = get_fd_user( fd );
1000 switch(code)
1002 case FSCTL_PIPE_LISTEN:
1003 switch(server->state)
1005 case ps_idle_server:
1006 case ps_wait_connect:
1007 fd_queue_async( server->pipe_end.fd, async, ASYNC_TYPE_WAIT );
1008 set_server_state( server, ps_wait_open );
1009 async_wake_up( &server->pipe->waiters, STATUS_SUCCESS );
1010 set_error( STATUS_PENDING );
1011 return 1;
1012 case ps_connected_server:
1013 set_error( STATUS_PIPE_CONNECTED );
1014 break;
1015 case ps_wait_disconnect:
1016 set_error( STATUS_NO_DATA_DETECTED );
1017 break;
1018 case ps_wait_open:
1019 set_error( STATUS_INVALID_HANDLE );
1020 break;
1022 return 0;
1024 case FSCTL_PIPE_DISCONNECT:
1025 switch(server->state)
1027 case ps_connected_server:
1028 assert( server->client );
1030 /* dump the client and server fds - client loses all waiting data */
1031 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1032 server->client->server = NULL;
1033 server->client = NULL;
1034 set_server_state( server, ps_wait_connect );
1035 break;
1036 case ps_wait_disconnect:
1037 assert( !server->client );
1038 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1039 set_server_state( server, ps_wait_connect );
1040 break;
1041 case ps_idle_server:
1042 case ps_wait_open:
1043 set_error( STATUS_PIPE_LISTENING );
1044 return 0;
1045 case ps_wait_connect:
1046 set_error( STATUS_PIPE_DISCONNECTED );
1047 return 0;
1049 return 1;
1051 default:
1052 return pipe_end_ioctl( &server->pipe_end, code, async );
1056 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1058 struct pipe_client *client = get_fd_user( fd );
1060 switch(code)
1062 case FSCTL_PIPE_LISTEN:
1063 set_error( STATUS_ILLEGAL_FUNCTION );
1064 return 0;
1066 default:
1067 return pipe_end_ioctl( &client->pipe_end, code, async );
1071 static struct pipe_server *get_pipe_server_obj( struct process *process,
1072 obj_handle_t handle, unsigned int access )
1074 struct object *obj;
1075 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
1076 return (struct pipe_server *) obj;
1079 static void init_pipe_end( struct pipe_end *pipe_end, unsigned int pipe_flags, data_size_t buffer_size )
1081 pipe_end->fd = NULL;
1082 pipe_end->flags = pipe_flags;
1083 pipe_end->connection = NULL;
1084 pipe_end->buffer_size = buffer_size;
1085 init_async_queue( &pipe_end->read_q );
1086 init_async_queue( &pipe_end->write_q );
1087 list_init( &pipe_end->message_queue );
1090 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
1091 unsigned int pipe_flags )
1093 struct pipe_server *server;
1095 server = alloc_object( &pipe_server_ops );
1096 if (!server)
1097 return NULL;
1099 server->pipe = pipe;
1100 server->client = NULL;
1101 server->options = options;
1102 init_pipe_end( &server->pipe_end, pipe_flags, pipe->insize );
1103 server->pipe_end.server_pid = get_process_id( current->process );
1105 list_add_head( &pipe->servers, &server->entry );
1106 grab_object( pipe );
1107 if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1109 release_object( server );
1110 return NULL;
1112 set_fd_signaled( server->pipe_end.fd, 1 );
1113 set_server_state( server, ps_idle_server );
1114 return server;
1117 static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags,
1118 data_size_t buffer_size, unsigned int options )
1120 struct pipe_client *client;
1122 client = alloc_object( &pipe_client_ops );
1123 if (!client)
1124 return NULL;
1126 client->server = NULL;
1127 client->flags = flags;
1128 init_pipe_end( &client->pipe_end, pipe_flags, buffer_size );
1129 client->pipe_end.client_pid = get_process_id( current->process );
1131 client->pipe_end.fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->pipe_end.obj, options );
1132 if (!client->pipe_end.fd)
1134 release_object( client );
1135 return NULL;
1137 allow_fd_caching( client->pipe_end.fd );
1138 set_fd_signaled( client->pipe_end.fd, 1 );
1140 return client;
1143 static struct pipe_server *find_available_server( struct named_pipe *pipe )
1145 struct pipe_server *server;
1147 /* look for pipe servers that are listening */
1148 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1150 if (server->state == ps_wait_open)
1151 return (struct pipe_server *)grab_object( server );
1154 /* fall back to pipe servers that are idle */
1155 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1157 if (server->state == ps_idle_server)
1158 return (struct pipe_server *)grab_object( server );
1161 return NULL;
1164 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1166 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1168 if (parent->ops != &named_pipe_device_ops)
1170 set_error( STATUS_OBJECT_NAME_INVALID );
1171 return 0;
1173 namespace_add( dev->pipes, name );
1174 name->parent = grab_object( parent );
1175 return 1;
1178 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1179 unsigned int sharing, unsigned int options )
1181 struct named_pipe *pipe = (struct named_pipe *)obj;
1182 struct pipe_server *server;
1183 struct pipe_client *client;
1184 unsigned int pipe_sharing;
1186 if (!(server = find_available_server( pipe )))
1188 set_error( STATUS_PIPE_NOT_AVAILABLE );
1189 return NULL;
1192 pipe_sharing = server->pipe->sharing;
1193 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1194 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1196 set_error( STATUS_ACCESS_DENIED );
1197 release_object( server );
1198 return NULL;
1201 if ((client = create_pipe_client( options, pipe->flags, pipe->outsize, options )))
1203 set_no_fd_status( server->pipe_end.fd, STATUS_BAD_DEVICE_TYPE );
1204 allow_fd_caching( server->pipe_end.fd );
1205 if (server->state == ps_wait_open)
1206 fd_async_wake_up( server->pipe_end.fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
1207 set_server_state( server, ps_connected_server );
1208 server->client = client;
1209 client->server = server;
1210 server->pipe_end.connection = &client->pipe_end;
1211 client->pipe_end.connection = &server->pipe_end;
1212 server->pipe_end.client_pid = client->pipe_end.client_pid;
1213 client->pipe_end.server_pid = server->pipe_end.server_pid;
1215 release_object( server );
1216 return &client->pipe_end.obj;
1219 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1221 struct named_pipe_device *device = get_fd_user( fd );
1223 switch(code)
1225 case FSCTL_PIPE_WAIT:
1227 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1228 data_size_t size = get_req_data_size();
1229 struct named_pipe *pipe;
1230 struct pipe_server *server;
1231 struct unicode_str name;
1232 timeout_t when;
1234 if (size < sizeof(*buffer) ||
1235 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1237 set_error( STATUS_INVALID_PARAMETER );
1238 return 0;
1240 name.str = buffer->Name;
1241 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1242 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;
1244 if (!(server = find_available_server( pipe )))
1246 queue_async( &pipe->waiters, async );
1247 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1248 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1249 release_object( pipe );
1250 set_error( STATUS_PENDING );
1251 return 1;
1254 release_object( server );
1255 release_object( pipe );
1256 return 0;
1259 default:
1260 return default_fd_ioctl( fd, code, async );
1265 DECL_HANDLER(create_named_pipe)
1267 struct named_pipe *pipe;
1268 struct pipe_server *server;
1269 struct unicode_str name;
1270 struct object *root;
1271 const struct security_descriptor *sd;
1272 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1274 if (!objattr) return;
1276 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1277 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1279 if (root) release_object( root );
1280 set_error( STATUS_INVALID_PARAMETER );
1281 return;
1284 if (!name.len) /* pipes need a root directory even without a name */
1286 if (!objattr->rootdir)
1288 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1289 return;
1291 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
1294 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1296 if (root) release_object( root );
1297 if (!pipe) return;
1299 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1301 /* initialize it if it didn't already exist */
1302 pipe->instances = 0;
1303 init_async_queue( &pipe->waiters );
1304 list_init( &pipe->servers );
1305 pipe->insize = req->insize;
1306 pipe->outsize = req->outsize;
1307 pipe->maxinstances = req->maxinstances;
1308 pipe->timeout = req->timeout;
1309 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
1310 pipe->sharing = req->sharing;
1311 if (sd) default_set_sd( &pipe->obj, sd, OWNER_SECURITY_INFORMATION |
1312 GROUP_SECURITY_INFORMATION |
1313 DACL_SECURITY_INFORMATION |
1314 SACL_SECURITY_INFORMATION );
1316 else
1318 if (pipe->maxinstances <= pipe->instances)
1320 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1321 release_object( pipe );
1322 return;
1324 if (pipe->sharing != req->sharing)
1326 set_error( STATUS_ACCESS_DENIED );
1327 release_object( pipe );
1328 return;
1330 clear_error(); /* clear the name collision */
1333 server = create_pipe_server( pipe, req->options, req->flags );
1334 if (server)
1336 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1337 server->pipe->instances++;
1338 release_object( server );
1341 release_object( pipe );
1344 DECL_HANDLER(get_named_pipe_info)
1346 struct pipe_server *server;
1347 struct pipe_client *client = NULL;
1349 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1350 if (!server)
1352 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1353 return;
1355 clear_error();
1356 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1357 0, &pipe_client_ops );
1358 if (!client) return;
1359 server = client->server;
1362 reply->flags = client ? client->pipe_end.flags : server->pipe_end.flags;
1363 if (server)
1365 reply->sharing = server->pipe->sharing;
1366 reply->maxinstances = server->pipe->maxinstances;
1367 reply->instances = server->pipe->instances;
1368 reply->insize = server->pipe->insize;
1369 reply->outsize = server->pipe->outsize;
1372 if (client)
1373 release_object(client);
1374 else
1376 reply->flags |= NAMED_PIPE_SERVER_END;
1377 release_object(server);
1381 DECL_HANDLER(set_named_pipe_info)
1383 struct pipe_server *server;
1384 struct pipe_client *client = NULL;
1386 server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
1387 if (!server)
1389 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1390 return;
1392 clear_error();
1393 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1394 0, &pipe_client_ops );
1395 if (!client) return;
1396 if (!(server = client->server))
1398 release_object( client );
1399 return;
1403 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1404 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1406 set_error( STATUS_INVALID_PARAMETER );
1408 else if (client)
1410 client->pipe_end.flags = server->pipe->flags | req->flags;
1412 else
1414 server->pipe_end.flags = server->pipe->flags | req->flags;
1417 if (client)
1418 release_object(client);
1419 else
1420 release_object(server);