shell32: Add eject icon.
[wine.git] / server / named_pipe.c
blobc006acb3734fbb1faa29d834c516061616530ff4
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 struct named_pipe;
47 struct pipe_message
49 struct list entry; /* entry in message queue */
50 data_size_t read_pos; /* already read bytes */
51 struct iosb *iosb; /* message iosb */
52 struct async *async; /* async of pending write */
55 struct pipe_end
57 struct object obj; /* object header */
58 struct fd *fd; /* pipe file descriptor */
59 unsigned int flags; /* pipe flags */
60 unsigned int state; /* pipe state */
61 struct named_pipe *pipe;
62 struct pipe_end *connection; /* the other end of the pipe */
63 process_id_t client_pid; /* process that created the client */
64 process_id_t server_pid; /* process that created the server */
65 data_size_t buffer_size;/* size of buffered data that doesn't block caller */
66 struct list message_queue;
67 struct async_queue read_q; /* read queue */
68 struct async_queue write_q; /* write queue */
71 struct pipe_server
73 struct pipe_end pipe_end; /* common header for pipe_client and pipe_server */
74 struct list entry; /* entry in named pipe servers list */
75 unsigned int options; /* pipe options */
76 struct async_queue listen_q; /* listen queue */
79 struct pipe_client
81 struct pipe_end pipe_end; /* common header for pipe_client and pipe_server */
82 unsigned int flags; /* file flags */
85 struct named_pipe
87 struct object obj; /* object header */
88 unsigned int flags;
89 unsigned int sharing;
90 unsigned int maxinstances;
91 unsigned int outsize;
92 unsigned int insize;
93 unsigned int instances;
94 timeout_t timeout;
95 struct list servers; /* list of servers using this pipe */
96 struct async_queue waiters; /* list of clients waiting to connect */
99 struct named_pipe_device
101 struct object obj; /* object header */
102 struct fd *fd; /* pseudo-fd for ioctls */
103 struct namespace *pipes; /* named pipe namespace */
106 static void named_pipe_dump( struct object *obj, int verbose );
107 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
108 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent );
109 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
110 unsigned int sharing, unsigned int options );
111 static void named_pipe_destroy( struct object *obj );
113 static const struct object_ops named_pipe_ops =
115 sizeof(struct named_pipe), /* size */
116 named_pipe_dump, /* dump */
117 no_get_type, /* get_type */
118 no_add_queue, /* add_queue */
119 NULL, /* remove_queue */
120 NULL, /* signaled */
121 NULL, /* satisfied */
122 no_signal, /* signal */
123 no_get_fd, /* get_fd */
124 named_pipe_map_access, /* map_access */
125 default_get_sd, /* get_sd */
126 default_set_sd, /* set_sd */
127 no_lookup_name, /* lookup_name */
128 named_pipe_link_name, /* link_name */
129 default_unlink_name, /* unlink_name */
130 named_pipe_open_file, /* open_file */
131 no_close_handle, /* close_handle */
132 named_pipe_destroy /* destroy */
135 /* common server and client pipe end functions */
136 static void pipe_end_destroy( struct object *obj );
137 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd );
138 static struct fd *pipe_end_get_fd( struct object *obj );
139 static struct security_descriptor *pipe_end_get_sd( struct object *obj );
140 static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
141 unsigned int set_info );
142 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos );
143 static int pipe_end_write( struct fd *fd, struct async *async_data, file_pos_t pos );
144 static int pipe_end_flush( struct fd *fd, struct async *async );
145 static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class );
146 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue );
147 static void pipe_end_get_file_info( struct fd *fd, unsigned int info_class );
149 /* server end functions */
150 static void pipe_server_dump( struct object *obj, int verbose );
151 static void pipe_server_destroy( struct object *obj);
152 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
154 static const struct object_ops pipe_server_ops =
156 sizeof(struct pipe_server), /* size */
157 pipe_server_dump, /* dump */
158 no_get_type, /* get_type */
159 add_queue, /* add_queue */
160 remove_queue, /* remove_queue */
161 default_fd_signaled, /* signaled */
162 no_satisfied, /* satisfied */
163 no_signal, /* signal */
164 pipe_end_get_fd, /* get_fd */
165 default_fd_map_access, /* map_access */
166 pipe_end_get_sd, /* get_sd */
167 pipe_end_set_sd, /* set_sd */
168 no_lookup_name, /* lookup_name */
169 no_link_name, /* link_name */
170 NULL, /* unlink_name */
171 no_open_file, /* open_file */
172 fd_close_handle, /* close_handle */
173 pipe_server_destroy /* destroy */
176 static const struct fd_ops pipe_server_fd_ops =
178 default_fd_get_poll_events, /* get_poll_events */
179 default_poll_event, /* poll_event */
180 pipe_end_get_fd_type, /* get_fd_type */
181 pipe_end_read, /* read */
182 pipe_end_write, /* write */
183 pipe_end_flush, /* flush */
184 pipe_end_get_file_info, /* get_file_info */
185 pipe_end_get_volume_info, /* get_volume_info */
186 pipe_server_ioctl, /* ioctl */
187 no_fd_queue_async, /* queue_async */
188 pipe_end_reselect_async /* reselect_async */
191 /* client end functions */
192 static void pipe_client_dump( struct object *obj, int verbose );
193 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
195 static const struct object_ops pipe_client_ops =
197 sizeof(struct pipe_client), /* size */
198 pipe_client_dump, /* dump */
199 no_get_type, /* get_type */
200 add_queue, /* add_queue */
201 remove_queue, /* remove_queue */
202 default_fd_signaled, /* signaled */
203 no_satisfied, /* satisfied */
204 no_signal, /* signal */
205 pipe_end_get_fd, /* get_fd */
206 default_fd_map_access, /* map_access */
207 pipe_end_get_sd, /* get_sd */
208 pipe_end_set_sd, /* set_sd */
209 no_lookup_name, /* lookup_name */
210 no_link_name, /* link_name */
211 NULL, /* unlink_name */
212 no_open_file, /* open_file */
213 fd_close_handle, /* close_handle */
214 pipe_end_destroy /* destroy */
217 static const struct fd_ops pipe_client_fd_ops =
219 default_fd_get_poll_events, /* get_poll_events */
220 default_poll_event, /* poll_event */
221 pipe_end_get_fd_type, /* get_fd_type */
222 pipe_end_read, /* read */
223 pipe_end_write, /* write */
224 pipe_end_flush, /* flush */
225 pipe_end_get_file_info, /* get_file_info */
226 pipe_end_get_volume_info, /* get_volume_info */
227 pipe_client_ioctl, /* ioctl */
228 no_fd_queue_async, /* queue_async */
229 pipe_end_reselect_async /* reselect_async */
232 static void named_pipe_device_dump( struct object *obj, int verbose );
233 static struct object_type *named_pipe_device_get_type( struct object *obj );
234 static struct fd *named_pipe_device_get_fd( struct object *obj );
235 static struct object *named_pipe_device_lookup_name( struct object *obj,
236 struct unicode_str *name, unsigned int attr );
237 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
238 unsigned int sharing, unsigned int options );
239 static void named_pipe_device_destroy( struct object *obj );
240 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
241 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
243 static const struct object_ops named_pipe_device_ops =
245 sizeof(struct named_pipe_device), /* size */
246 named_pipe_device_dump, /* dump */
247 named_pipe_device_get_type, /* get_type */
248 no_add_queue, /* add_queue */
249 NULL, /* remove_queue */
250 NULL, /* signaled */
251 no_satisfied, /* satisfied */
252 no_signal, /* signal */
253 named_pipe_device_get_fd, /* get_fd */
254 no_map_access, /* map_access */
255 default_get_sd, /* get_sd */
256 default_set_sd, /* set_sd */
257 named_pipe_device_lookup_name, /* lookup_name */
258 directory_link_name, /* link_name */
259 default_unlink_name, /* unlink_name */
260 named_pipe_device_open_file, /* open_file */
261 fd_close_handle, /* close_handle */
262 named_pipe_device_destroy /* destroy */
265 static const struct fd_ops named_pipe_device_fd_ops =
267 default_fd_get_poll_events, /* get_poll_events */
268 default_poll_event, /* poll_event */
269 named_pipe_device_get_fd_type, /* get_fd_type */
270 no_fd_read, /* read */
271 no_fd_write, /* write */
272 no_fd_flush, /* flush */
273 no_fd_get_file_info, /* get_file_info */
274 no_fd_get_volume_info, /* get_volume_info */
275 named_pipe_device_ioctl, /* ioctl */
276 default_fd_queue_async, /* queue_async */
277 default_fd_reselect_async /* reselect_async */
280 static void named_pipe_dump( struct object *obj, int verbose )
282 fputs( "Named pipe\n", stderr );
285 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
287 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
288 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
289 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
290 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
291 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
294 static void pipe_server_dump( struct object *obj, int verbose )
296 struct pipe_server *server = (struct pipe_server *) obj;
297 assert( obj->ops == &pipe_server_ops );
298 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe_end.pipe,
299 server->pipe_end.state );
302 static void pipe_client_dump( struct object *obj, int verbose )
304 struct pipe_client *client = (struct pipe_client *) obj;
305 assert( obj->ops == &pipe_client_ops );
306 fprintf( stderr, "Named pipe client server=%p\n", client->pipe_end.connection );
309 static void named_pipe_destroy( struct object *obj)
311 struct named_pipe *pipe = (struct named_pipe *) obj;
313 assert( list_empty( &pipe->servers ) );
314 assert( !pipe->instances );
315 free_async_queue( &pipe->waiters );
318 static struct fd *pipe_end_get_fd( struct object *obj )
320 struct pipe_end *pipe_end = (struct pipe_end *) obj;
321 return (struct fd *) grab_object( pipe_end->fd );
324 static struct pipe_message *queue_message( struct pipe_end *pipe_end, struct iosb *iosb )
326 struct pipe_message *message;
328 if (!(message = mem_alloc( sizeof(*message) ))) return NULL;
329 message->iosb = (struct iosb *)grab_object( iosb );
330 message->async = NULL;
331 message->read_pos = 0;
332 list_add_tail( &pipe_end->message_queue, &message->entry );
333 return message;
336 static void wake_message( struct pipe_message *message )
338 struct async *async = message->async;
340 message->async = NULL;
341 if (!async) return;
343 message->iosb->status = STATUS_SUCCESS;
344 message->iosb->result = message->iosb->in_size;
345 async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
346 release_object( async );
349 static void free_message( struct pipe_message *message )
351 list_remove( &message->entry );
352 if (message->iosb) release_object( message->iosb );
353 free( message );
356 static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
358 struct pipe_end *connection = pipe_end->connection;
359 struct pipe_message *message, *next;
360 struct async *async;
362 pipe_end->connection = NULL;
364 pipe_end->state = status == STATUS_PIPE_DISCONNECTED
365 ? FILE_PIPE_DISCONNECTED_STATE : FILE_PIPE_CLOSING_STATE;
366 fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
367 async_wake_up( &pipe_end->read_q, status );
368 LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
370 async = message->async;
371 if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
372 if (!async) continue;
373 async_terminate( async, status );
374 release_object( async );
376 if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
378 if (connection)
380 connection->connection = NULL;
381 pipe_end_disconnect( connection, status );
385 static void pipe_end_destroy( struct object *obj )
387 struct pipe_end *pipe_end = (struct pipe_end *)obj;
388 struct pipe_message *message;
390 pipe_end_disconnect( pipe_end, STATUS_PIPE_BROKEN );
392 while (!list_empty( &pipe_end->message_queue ))
394 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
395 assert( !message->async );
396 free_message( message );
399 free_async_queue( &pipe_end->read_q );
400 free_async_queue( &pipe_end->write_q );
401 if (pipe_end->fd) release_object( pipe_end->fd );
402 if (pipe_end->pipe) release_object( pipe_end->pipe );
405 static void pipe_server_destroy( struct object *obj )
407 struct pipe_server *server = (struct pipe_server *)obj;
408 struct named_pipe *pipe = server->pipe_end.pipe;
410 assert( obj->ops == &pipe_server_ops );
412 assert( pipe->instances );
413 if (!--pipe->instances) unlink_named_object( &pipe->obj );
414 list_remove( &server->entry );
416 free_async_queue( &server->listen_q );
417 pipe_end_destroy( obj );
420 static void named_pipe_device_dump( struct object *obj, int verbose )
422 fputs( "Named pipe device\n", stderr );
425 static struct object_type *named_pipe_device_get_type( struct object *obj )
427 static const WCHAR name[] = {'D','e','v','i','c','e'};
428 static const struct unicode_str str = { name, sizeof(name) };
429 return get_object_type( &str );
432 static struct fd *named_pipe_device_get_fd( struct object *obj )
434 struct named_pipe_device *device = (struct named_pipe_device *)obj;
435 return (struct fd *)grab_object( device->fd );
438 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
439 unsigned int attr )
441 struct named_pipe_device *device = (struct named_pipe_device*)obj;
442 struct object *found;
444 assert( obj->ops == &named_pipe_device_ops );
445 assert( device->pipes );
447 if (!name) return NULL; /* open the device itself */
449 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
450 name->len = 0;
452 return found;
455 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
456 unsigned int sharing, unsigned int options )
458 return grab_object( obj );
461 static void named_pipe_device_destroy( struct object *obj )
463 struct named_pipe_device *device = (struct named_pipe_device*)obj;
464 assert( obj->ops == &named_pipe_device_ops );
465 if (device->fd) release_object( device->fd );
466 free( device->pipes );
469 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
471 return FD_TYPE_DEVICE;
474 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name )
476 struct named_pipe_device *dev;
478 if ((dev = create_named_object( root, &named_pipe_device_ops, name, 0, NULL )) &&
479 get_error() != STATUS_OBJECT_NAME_EXISTS)
481 dev->pipes = NULL;
482 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
483 !(dev->pipes = create_namespace( 7 )))
485 release_object( dev );
486 dev = NULL;
489 return &dev->obj;
492 static int pipe_end_flush( struct fd *fd, struct async *async )
494 struct pipe_end *pipe_end = get_fd_user( fd );
496 if (pipe_end->connection && !list_empty( &pipe_end->connection->message_queue ))
498 fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT );
499 set_error( STATUS_PENDING );
501 return 1;
504 static void pipe_end_get_file_info( struct fd *fd, unsigned int info_class )
506 struct pipe_end *pipe_end = get_fd_user( fd );
507 struct named_pipe *pipe = pipe_end->pipe;
509 if (!pipe)
511 set_error( STATUS_PIPE_DISCONNECTED );
512 return;
515 switch (info_class)
517 case FileNameInformation:
519 FILE_NAME_INFORMATION *name_info;
520 data_size_t name_len, reply_size;
521 const WCHAR *name;
523 if (get_reply_max_size() < sizeof(*name_info))
525 set_error( STATUS_INFO_LENGTH_MISMATCH );
526 return;
529 name = get_object_name( &pipe->obj, &name_len );
530 /* FIXME: We should be able to return on unlinked pipe */
531 if (!name)
533 set_error( STATUS_PIPE_DISCONNECTED );
534 return;
536 reply_size = offsetof( FILE_NAME_INFORMATION, FileName[name_len/sizeof(WCHAR) + 1] );
537 if (reply_size > get_reply_max_size())
539 reply_size = get_reply_max_size();
540 set_error( STATUS_BUFFER_OVERFLOW );
543 if (!(name_info = set_reply_data_size( reply_size ))) return;
544 name_info->FileNameLength = name_len + sizeof(WCHAR);
545 name_info->FileName[0] = '\\';
546 reply_size -= offsetof( FILE_NAME_INFORMATION, FileName[1] );
547 if (reply_size) memcpy( &name_info->FileName[1], name, reply_size );
548 break;
550 default:
551 no_fd_get_file_info( fd, info_class );
555 static struct security_descriptor *pipe_end_get_sd( struct object *obj )
557 struct pipe_end *pipe_end = (struct pipe_end *) obj;
558 if (pipe_end->pipe) return default_get_sd( &pipe_end->pipe->obj );
559 set_error( STATUS_PIPE_DISCONNECTED );
560 return NULL;
563 static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
564 unsigned int set_info )
566 struct pipe_end *pipe_end = (struct pipe_end *) obj;
567 if (pipe_end->pipe) return default_set_sd( &pipe_end->pipe->obj, sd, set_info );
568 set_error( STATUS_PIPE_DISCONNECTED );
569 return 0;
572 static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class )
574 switch (info_class)
576 case FileFsDeviceInformation:
578 static const FILE_FS_DEVICE_INFORMATION device_info =
580 FILE_DEVICE_NAMED_PIPE,
581 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
583 if (get_reply_max_size() >= sizeof(device_info))
584 set_reply_data( &device_info, sizeof(device_info) );
585 else
586 set_error( STATUS_BUFFER_TOO_SMALL );
587 break;
589 default:
590 set_error( STATUS_NOT_IMPLEMENTED );
594 static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
596 struct pipe_message *message;
598 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
600 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
601 iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
602 iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
603 ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
605 else
607 data_size_t avail = 0;
608 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
610 avail += message->iosb->in_size - message->read_pos;
611 if (avail >= iosb->out_size) break;
613 iosb->out_size = min( iosb->out_size, avail );
614 iosb->status = STATUS_SUCCESS;
617 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
618 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
620 iosb->out_data = message->iosb->in_data;
621 message->iosb->in_data = NULL;
622 wake_message( message );
623 free_message( message );
625 else
627 data_size_t write_pos = 0, writing;
628 char *buf = NULL;
630 if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
632 iosb->out_size = 0;
633 iosb->status = STATUS_NO_MEMORY;
634 return;
639 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
640 writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
641 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
642 write_pos += writing;
643 message->read_pos += writing;
644 if (message->read_pos == message->iosb->in_size)
646 wake_message(message);
647 free_message(message);
649 } while (write_pos < iosb->out_size);
651 iosb->result = iosb->out_size;
654 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
655 * We're not interested in such reselect calls, so we ignore them. */
656 static int ignore_reselect;
658 static void reselect_write_queue( struct pipe_end *pipe_end );
660 static void reselect_read_queue( struct pipe_end *pipe_end )
662 struct async *async;
663 struct iosb *iosb;
664 int read_done = 0;
666 ignore_reselect = 1;
667 while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
669 iosb = async_get_iosb( async );
670 message_queue_read( pipe_end, iosb );
671 async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
672 release_object( async );
673 release_object( iosb );
674 read_done = 1;
676 ignore_reselect = 0;
678 if (pipe_end->connection)
680 if (list_empty( &pipe_end->message_queue ))
681 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
682 else if (read_done)
683 reselect_write_queue( pipe_end->connection );
687 static void reselect_write_queue( struct pipe_end *pipe_end )
689 struct pipe_message *message, *next;
690 struct pipe_end *reader = pipe_end->connection;
691 data_size_t avail = 0;
693 if (!reader) return;
695 ignore_reselect = 1;
697 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
699 if (message->async && message->iosb->status != STATUS_PENDING)
701 release_object( message->async );
702 message->async = NULL;
703 free_message( message );
705 else
707 avail += message->iosb->in_size - message->read_pos;
708 if (message->async && (avail <= reader->buffer_size || !message->iosb->in_size))
709 wake_message( message );
713 ignore_reselect = 0;
714 reselect_read_queue( reader );
717 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
719 struct pipe_end *pipe_end = get_fd_user( fd );
721 switch (pipe_end->state)
723 case FILE_PIPE_CONNECTED_STATE:
724 break;
725 case FILE_PIPE_DISCONNECTED_STATE:
726 set_error( STATUS_PIPE_DISCONNECTED );
727 return 0;
728 case FILE_PIPE_LISTENING_STATE:
729 set_error( STATUS_PIPE_LISTENING );
730 return 0;
731 case FILE_PIPE_CLOSING_STATE:
732 if (!list_empty( &pipe_end->message_queue )) break;
733 set_error( STATUS_PIPE_BROKEN );
734 return 0;
737 queue_async( &pipe_end->read_q, async );
738 reselect_read_queue( pipe_end );
739 set_error( STATUS_PENDING );
740 return 1;
743 static int pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
745 struct pipe_end *pipe_end = get_fd_user( fd );
746 struct pipe_message *message;
747 struct iosb *iosb;
749 switch (pipe_end->state)
751 case FILE_PIPE_CONNECTED_STATE:
752 break;
753 case FILE_PIPE_DISCONNECTED_STATE:
754 set_error( STATUS_PIPE_DISCONNECTED );
755 return 0;
756 case FILE_PIPE_LISTENING_STATE:
757 set_error( STATUS_PIPE_LISTENING );
758 return 0;
759 case FILE_PIPE_CLOSING_STATE:
760 set_error( STATUS_PIPE_CLOSING );
761 return 0;
764 if (!(pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && !get_req_data_size()) return 1;
766 iosb = async_get_iosb( async );
767 message = queue_message( pipe_end->connection, iosb );
768 release_object( iosb );
769 if (!message) return 0;
771 message->async = (struct async *)grab_object( async );
772 queue_async( &pipe_end->write_q, async );
773 reselect_write_queue( pipe_end );
774 set_error( STATUS_PENDING );
775 return 1;
778 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
780 struct pipe_end *pipe_end = get_fd_user( fd );
782 if (ignore_reselect) return;
784 if (&pipe_end->write_q == queue)
785 reselect_write_queue( pipe_end );
786 else if (&pipe_end->read_q == queue)
787 reselect_read_queue( pipe_end );
790 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
792 return FD_TYPE_PIPE;
795 static int pipe_end_peek( struct pipe_end *pipe_end )
797 unsigned reply_size = get_reply_max_size();
798 FILE_PIPE_PEEK_BUFFER *buffer;
799 struct pipe_message *message;
800 data_size_t avail = 0;
801 data_size_t message_length = 0;
803 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
805 set_error( STATUS_INFO_LENGTH_MISMATCH );
806 return 0;
808 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
810 switch (pipe_end->state)
812 case FILE_PIPE_CONNECTED_STATE:
813 break;
814 case FILE_PIPE_CLOSING_STATE:
815 if (!list_empty( &pipe_end->message_queue )) break;
816 set_error( STATUS_PIPE_BROKEN );
817 return 0;
818 default:
819 set_error( STATUS_INVALID_PIPE_STATE );
820 return 0;
823 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
824 avail += message->iosb->in_size - message->read_pos;
825 reply_size = min( reply_size, avail );
827 if (avail && (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE))
829 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
830 message_length = message->iosb->in_size - message->read_pos;
831 reply_size = min( reply_size, message_length );
834 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return 0;
835 buffer->NamedPipeState = pipe_end->state;
836 buffer->ReadDataAvailable = avail;
837 buffer->NumberOfMessages = 0; /* FIXME */
838 buffer->MessageLength = message_length;
840 if (reply_size)
842 data_size_t write_pos = 0, writing;
843 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
845 writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
846 memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
847 writing );
848 write_pos += writing;
849 if (write_pos == reply_size) break;
852 return 1;
855 static int pipe_end_transceive( struct pipe_end *pipe_end, struct async *async )
857 struct pipe_message *message;
858 struct iosb *iosb;
860 if (!pipe_end->connection)
862 set_error( STATUS_INVALID_PIPE_STATE );
863 return 0;
866 if ((pipe_end->flags & (NAMED_PIPE_MESSAGE_STREAM_WRITE | NAMED_PIPE_MESSAGE_STREAM_READ))
867 != (NAMED_PIPE_MESSAGE_STREAM_WRITE | NAMED_PIPE_MESSAGE_STREAM_READ))
869 set_error( STATUS_INVALID_READ_MODE );
870 return 0;
873 /* not allowed if we already have read data buffered */
874 if (!list_empty( &pipe_end->message_queue ))
876 set_error( STATUS_PIPE_BUSY );
877 return 0;
880 iosb = async_get_iosb( async );
881 /* ignore output buffer copy transferred because of METHOD_NEITHER */
882 iosb->in_size -= iosb->out_size;
883 /* transaction never blocks on write, so just queue a message without async */
884 message = queue_message( pipe_end->connection, iosb );
885 release_object( iosb );
886 if (!message) return 0;
887 reselect_read_queue( pipe_end->connection );
889 queue_async( &pipe_end->read_q, async );
890 reselect_read_queue( pipe_end );
891 set_error( STATUS_PENDING );
892 return 1;
895 static int pipe_end_get_connection_attribute( struct pipe_end *pipe_end )
897 const char *attr = get_req_data();
898 data_size_t value_size, attr_size = get_req_data_size();
899 void *value;
901 if (attr_size == sizeof("ClientProcessId") && !memcmp( attr, "ClientProcessId", attr_size ))
903 value = &pipe_end->client_pid;
904 value_size = sizeof(pipe_end->client_pid);
906 else if (attr_size == sizeof("ServerProcessId") && !memcmp( attr, "ServerProcessId", attr_size ))
908 value = &pipe_end->server_pid;
909 value_size = sizeof(pipe_end->server_pid);
911 else
913 set_error( STATUS_ILLEGAL_FUNCTION );
914 return 0;
917 if (get_reply_max_size() < value_size)
919 set_error( STATUS_INFO_LENGTH_MISMATCH );
920 return 0;
923 set_reply_data( value, value_size );
924 return 1;
927 static int pipe_end_ioctl( struct pipe_end *pipe_end, ioctl_code_t code, struct async *async )
929 switch(code)
931 case FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE:
932 return pipe_end_get_connection_attribute( pipe_end );
934 case FSCTL_PIPE_PEEK:
935 return pipe_end_peek( pipe_end );
937 case FSCTL_PIPE_TRANSCEIVE:
938 return pipe_end_transceive( pipe_end, async );
940 default:
941 return default_fd_ioctl( pipe_end->fd, code, async );
945 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
947 struct pipe_server *server = get_fd_user( fd );
949 switch(code)
951 case FSCTL_PIPE_LISTEN:
952 switch(server->pipe_end.state)
954 case FILE_PIPE_LISTENING_STATE:
955 case FILE_PIPE_DISCONNECTED_STATE:
956 break;
957 case FILE_PIPE_CONNECTED_STATE:
958 set_error( STATUS_PIPE_CONNECTED );
959 return 0;
960 case FILE_PIPE_CLOSING_STATE:
961 set_error( STATUS_PIPE_CLOSING );
962 return 0;
965 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
966 queue_async( &server->listen_q, async );
967 async_wake_up( &server->pipe_end.pipe->waiters, STATUS_SUCCESS );
968 set_error( STATUS_PENDING );
969 return 1;
971 case FSCTL_PIPE_DISCONNECT:
972 switch(server->pipe_end.state)
974 case FILE_PIPE_CONNECTED_STATE:
975 /* dump the client connection - all data is lost */
976 assert( server->pipe_end.connection );
977 release_object( server->pipe_end.connection->pipe );
978 server->pipe_end.connection->pipe = NULL;
979 break;
980 case FILE_PIPE_CLOSING_STATE:
981 break;
982 case FILE_PIPE_LISTENING_STATE:
983 set_error( STATUS_PIPE_LISTENING );
984 return 0;
985 case FILE_PIPE_DISCONNECTED_STATE:
986 set_error( STATUS_PIPE_DISCONNECTED );
987 return 0;
990 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
991 return 1;
993 default:
994 return pipe_end_ioctl( &server->pipe_end, code, async );
998 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1000 struct pipe_client *client = get_fd_user( fd );
1002 switch(code)
1004 case FSCTL_PIPE_LISTEN:
1005 set_error( STATUS_ILLEGAL_FUNCTION );
1006 return 0;
1008 default:
1009 return pipe_end_ioctl( &client->pipe_end, code, async );
1013 static void init_pipe_end( struct pipe_end *pipe_end, struct named_pipe *pipe,
1014 unsigned int pipe_flags, data_size_t buffer_size )
1016 pipe_end->pipe = (struct named_pipe *)grab_object( pipe );
1017 pipe_end->fd = NULL;
1018 pipe_end->flags = pipe_flags;
1019 pipe_end->connection = NULL;
1020 pipe_end->buffer_size = buffer_size;
1021 init_async_queue( &pipe_end->read_q );
1022 init_async_queue( &pipe_end->write_q );
1023 list_init( &pipe_end->message_queue );
1026 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
1027 unsigned int pipe_flags )
1029 struct pipe_server *server;
1031 server = alloc_object( &pipe_server_ops );
1032 if (!server)
1033 return NULL;
1035 server->options = options;
1036 init_pipe_end( &server->pipe_end, pipe, pipe_flags, pipe->insize );
1037 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1038 server->pipe_end.server_pid = get_process_id( current->process );
1040 list_add_head( &pipe->servers, &server->entry );
1041 if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1043 release_object( server );
1044 return NULL;
1046 allow_fd_caching( server->pipe_end.fd );
1047 set_fd_signaled( server->pipe_end.fd, 1 );
1048 init_async_queue( &server->listen_q );
1049 return server;
1052 static struct pipe_client *create_pipe_client( unsigned int flags, struct named_pipe *pipe,
1053 data_size_t buffer_size, unsigned int options )
1055 struct pipe_client *client;
1057 client = alloc_object( &pipe_client_ops );
1058 if (!client)
1059 return NULL;
1061 client->flags = flags;
1062 init_pipe_end( &client->pipe_end, pipe, pipe->flags, buffer_size );
1063 client->pipe_end.state = FILE_PIPE_CONNECTED_STATE;
1064 client->pipe_end.client_pid = get_process_id( current->process );
1066 client->pipe_end.fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->pipe_end.obj, options );
1067 if (!client->pipe_end.fd)
1069 release_object( client );
1070 return NULL;
1072 allow_fd_caching( client->pipe_end.fd );
1073 set_fd_signaled( client->pipe_end.fd, 1 );
1075 return client;
1078 static struct pipe_server *find_available_server( struct named_pipe *pipe )
1080 struct pipe_server *server;
1082 /* look for pipe servers that are listening */
1083 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1085 if (server->pipe_end.state == FILE_PIPE_LISTENING_STATE && async_queued( &server->listen_q ))
1086 return (struct pipe_server *)grab_object( server );
1089 /* fall back to pipe servers that are idle */
1090 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1092 if (server->pipe_end.state == FILE_PIPE_LISTENING_STATE )
1093 return (struct pipe_server *)grab_object( server );
1096 return NULL;
1099 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1101 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1103 if (parent->ops != &named_pipe_device_ops)
1105 set_error( STATUS_OBJECT_NAME_INVALID );
1106 return 0;
1108 namespace_add( dev->pipes, name );
1109 name->parent = grab_object( parent );
1110 return 1;
1113 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1114 unsigned int sharing, unsigned int options )
1116 struct named_pipe *pipe = (struct named_pipe *)obj;
1117 struct pipe_server *server;
1118 struct pipe_client *client;
1119 unsigned int pipe_sharing;
1121 if (!(server = find_available_server( pipe )))
1123 set_error( STATUS_PIPE_NOT_AVAILABLE );
1124 return NULL;
1127 pipe_sharing = pipe->sharing;
1128 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1129 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1131 set_error( STATUS_ACCESS_DENIED );
1132 release_object( server );
1133 return NULL;
1136 if ((client = create_pipe_client( options, pipe, pipe->outsize, options )))
1138 async_wake_up( &server->listen_q, STATUS_SUCCESS );
1139 server->pipe_end.state = FILE_PIPE_CONNECTED_STATE;
1140 server->pipe_end.connection = &client->pipe_end;
1141 client->pipe_end.connection = &server->pipe_end;
1142 server->pipe_end.client_pid = client->pipe_end.client_pid;
1143 client->pipe_end.server_pid = server->pipe_end.server_pid;
1145 release_object( server );
1146 return &client->pipe_end.obj;
1149 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1151 struct named_pipe_device *device = get_fd_user( fd );
1153 switch(code)
1155 case FSCTL_PIPE_WAIT:
1157 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1158 data_size_t size = get_req_data_size();
1159 struct named_pipe *pipe;
1160 struct pipe_server *server;
1161 struct unicode_str name;
1162 timeout_t when;
1164 if (size < sizeof(*buffer) ||
1165 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1167 set_error( STATUS_INVALID_PARAMETER );
1168 return 0;
1170 name.str = buffer->Name;
1171 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1172 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;
1174 if (!(server = find_available_server( pipe )))
1176 queue_async( &pipe->waiters, async );
1177 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1178 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1179 release_object( pipe );
1180 set_error( STATUS_PENDING );
1181 return 1;
1184 release_object( server );
1185 release_object( pipe );
1186 return 0;
1189 default:
1190 return default_fd_ioctl( fd, code, async );
1195 DECL_HANDLER(create_named_pipe)
1197 struct named_pipe *pipe;
1198 struct pipe_server *server;
1199 struct unicode_str name;
1200 struct object *root;
1201 const struct security_descriptor *sd;
1202 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1204 if (!objattr) return;
1206 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1207 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1209 if (root) release_object( root );
1210 set_error( STATUS_INVALID_PARAMETER );
1211 return;
1214 if (!name.len) /* pipes need a root directory even without a name */
1216 if (!objattr->rootdir)
1218 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1219 return;
1221 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
1224 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1226 if (root) release_object( root );
1227 if (!pipe) return;
1229 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1231 /* initialize it if it didn't already exist */
1232 pipe->instances = 0;
1233 init_async_queue( &pipe->waiters );
1234 list_init( &pipe->servers );
1235 pipe->insize = req->insize;
1236 pipe->outsize = req->outsize;
1237 pipe->maxinstances = req->maxinstances;
1238 pipe->timeout = req->timeout;
1239 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
1240 pipe->sharing = req->sharing;
1241 if (sd) default_set_sd( &pipe->obj, sd, OWNER_SECURITY_INFORMATION |
1242 GROUP_SECURITY_INFORMATION |
1243 DACL_SECURITY_INFORMATION |
1244 SACL_SECURITY_INFORMATION );
1246 else
1248 if (pipe->maxinstances <= pipe->instances)
1250 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1251 release_object( pipe );
1252 return;
1254 if (pipe->sharing != req->sharing)
1256 set_error( STATUS_ACCESS_DENIED );
1257 release_object( pipe );
1258 return;
1260 clear_error(); /* clear the name collision */
1263 server = create_pipe_server( pipe, req->options, req->flags );
1264 if (server)
1266 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1267 pipe->instances++;
1268 release_object( server );
1271 release_object( pipe );
1274 DECL_HANDLER(get_named_pipe_info)
1276 struct pipe_end *pipe_end;
1278 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1279 FILE_READ_ATTRIBUTES, &pipe_server_ops );
1280 if (!pipe_end)
1282 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1283 return;
1285 clear_error();
1286 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1287 FILE_READ_ATTRIBUTES, &pipe_client_ops );
1288 if (!pipe_end) return;
1291 if (pipe_end->pipe)
1293 reply->flags = pipe_end->flags;
1294 reply->sharing = pipe_end->pipe->sharing;
1295 reply->maxinstances = pipe_end->pipe->maxinstances;
1296 reply->instances = pipe_end->pipe->instances;
1297 reply->insize = pipe_end->pipe->insize;
1298 reply->outsize = pipe_end->pipe->outsize;
1300 if (pipe_end->obj.ops == &pipe_server_ops) reply->flags |= NAMED_PIPE_SERVER_END;
1302 else set_error( STATUS_PIPE_DISCONNECTED );
1304 release_object( pipe_end );
1307 DECL_HANDLER(set_named_pipe_info)
1309 struct pipe_end *pipe_end;
1311 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1312 FILE_WRITE_ATTRIBUTES, &pipe_server_ops );
1313 if (!pipe_end)
1315 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1316 return;
1318 clear_error();
1319 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1320 0, &pipe_client_ops );
1321 if (!pipe_end) return;
1324 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1325 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(pipe_end->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1327 set_error( STATUS_INVALID_PARAMETER );
1329 else
1331 pipe_end->flags = pipe_end->pipe->flags | req->flags;
1334 release_object( pipe_end );