msvcp100: Add _Concurrent_queue_base_v4 destructor implementation.
[wine.git] / server / named_pipe.c
blobc6b37cef06f3d3042b29e04034ea67cc1ff75412
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"
44 enum pipe_state
46 ps_idle_server,
47 ps_wait_open,
48 ps_connected_server,
49 ps_wait_disconnect,
50 ps_wait_connect
53 struct named_pipe;
55 struct pipe_message
57 struct list entry; /* entry in message queue */
58 data_size_t read_pos; /* already read bytes */
59 struct iosb *iosb; /* message iosb */
60 struct async *async; /* async of pending write */
63 struct pipe_end
65 struct object obj; /* object header */
66 struct fd *fd; /* pipe file descriptor */
67 unsigned int flags; /* pipe flags */
68 struct pipe_end *connection; /* the other end of the pipe */
69 data_size_t buffer_size;/* size of buffered data that doesn't block caller */
70 struct list message_queue;
71 struct async_queue read_q; /* read queue */
72 struct async_queue write_q; /* write queue */
75 struct pipe_server
77 struct pipe_end pipe_end; /* common header for pipe_client and pipe_server */
78 struct list entry; /* entry in named pipe servers list */
79 enum pipe_state state; /* server state */
80 struct pipe_client *client; /* client that this server is connected to */
81 struct named_pipe *pipe;
82 unsigned int options; /* pipe options */
85 struct pipe_client
87 struct pipe_end pipe_end; /* common header for pipe_client and pipe_server */
88 struct pipe_server *server; /* server that this client is connected to */
89 unsigned int flags; /* file flags */
92 struct named_pipe
94 struct object obj; /* object header */
95 unsigned int flags;
96 unsigned int sharing;
97 unsigned int maxinstances;
98 unsigned int outsize;
99 unsigned int insize;
100 unsigned int instances;
101 timeout_t timeout;
102 struct list servers; /* list of servers using this pipe */
103 struct async_queue waiters; /* list of clients waiting to connect */
106 struct named_pipe_device
108 struct object obj; /* object header */
109 struct fd *fd; /* pseudo-fd for ioctls */
110 struct namespace *pipes; /* named pipe namespace */
113 static void named_pipe_dump( struct object *obj, int verbose );
114 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
115 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent );
116 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
117 unsigned int sharing, unsigned int options );
118 static void named_pipe_destroy( struct object *obj );
120 static const struct object_ops named_pipe_ops =
122 sizeof(struct named_pipe), /* size */
123 named_pipe_dump, /* dump */
124 no_get_type, /* get_type */
125 no_add_queue, /* add_queue */
126 NULL, /* remove_queue */
127 NULL, /* signaled */
128 NULL, /* satisfied */
129 no_signal, /* signal */
130 no_get_fd, /* get_fd */
131 named_pipe_map_access, /* map_access */
132 default_get_sd, /* get_sd */
133 default_set_sd, /* set_sd */
134 no_lookup_name, /* lookup_name */
135 named_pipe_link_name, /* link_name */
136 default_unlink_name, /* unlink_name */
137 named_pipe_open_file, /* open_file */
138 no_close_handle, /* close_handle */
139 named_pipe_destroy /* destroy */
142 /* common server and client pipe end functions */
143 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd );
144 static struct fd *pipe_end_get_fd( struct object *obj );
145 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos );
146 static int pipe_end_write( struct fd *fd, struct async *async_data, file_pos_t pos );
147 static int pipe_end_flush( struct fd *fd, struct async *async );
148 static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class );
149 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue );
151 /* server end functions */
152 static void pipe_server_dump( struct object *obj, int verbose );
153 static void pipe_server_destroy( struct object *obj);
154 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
155 static void pipe_server_get_file_info( struct fd *fd, unsigned int info_class );
157 static const struct object_ops pipe_server_ops =
159 sizeof(struct pipe_server), /* size */
160 pipe_server_dump, /* dump */
161 no_get_type, /* get_type */
162 add_queue, /* add_queue */
163 remove_queue, /* remove_queue */
164 default_fd_signaled, /* signaled */
165 no_satisfied, /* satisfied */
166 no_signal, /* signal */
167 pipe_end_get_fd, /* get_fd */
168 default_fd_map_access, /* map_access */
169 default_get_sd, /* get_sd */
170 default_set_sd, /* set_sd */
171 no_lookup_name, /* lookup_name */
172 no_link_name, /* link_name */
173 NULL, /* unlink_name */
174 no_open_file, /* open_file */
175 fd_close_handle, /* close_handle */
176 pipe_server_destroy /* destroy */
179 static const struct fd_ops pipe_server_fd_ops =
181 default_fd_get_poll_events, /* get_poll_events */
182 default_poll_event, /* poll_event */
183 pipe_end_get_fd_type, /* get_fd_type */
184 pipe_end_read, /* read */
185 pipe_end_write, /* write */
186 pipe_end_flush, /* flush */
187 pipe_server_get_file_info, /* get_file_info */
188 pipe_end_get_volume_info, /* get_volume_info */
189 pipe_server_ioctl, /* ioctl */
190 no_fd_queue_async, /* queue_async */
191 pipe_end_reselect_async /* reselect_async */
194 /* client end functions */
195 static void pipe_client_dump( struct object *obj, int verbose );
196 static void pipe_client_destroy( struct object *obj );
197 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
198 static void pipe_client_get_file_info( struct fd *fd, unsigned int info_class );
200 static const struct object_ops pipe_client_ops =
202 sizeof(struct pipe_client), /* size */
203 pipe_client_dump, /* dump */
204 no_get_type, /* get_type */
205 add_queue, /* add_queue */
206 remove_queue, /* remove_queue */
207 default_fd_signaled, /* signaled */
208 no_satisfied, /* satisfied */
209 no_signal, /* signal */
210 pipe_end_get_fd, /* get_fd */
211 default_fd_map_access, /* map_access */
212 default_get_sd, /* get_sd */
213 default_set_sd, /* set_sd */
214 no_lookup_name, /* lookup_name */
215 no_link_name, /* link_name */
216 NULL, /* unlink_name */
217 no_open_file, /* open_file */
218 fd_close_handle, /* close_handle */
219 pipe_client_destroy /* destroy */
222 static const struct fd_ops pipe_client_fd_ops =
224 default_fd_get_poll_events, /* get_poll_events */
225 default_poll_event, /* poll_event */
226 pipe_end_get_fd_type, /* get_fd_type */
227 pipe_end_read, /* read */
228 pipe_end_write, /* write */
229 pipe_end_flush, /* flush */
230 pipe_client_get_file_info, /* get_file_info */
231 pipe_end_get_volume_info, /* get_volume_info */
232 pipe_client_ioctl, /* ioctl */
233 no_fd_queue_async, /* queue_async */
234 pipe_end_reselect_async /* reselect_async */
237 static void named_pipe_device_dump( struct object *obj, int verbose );
238 static struct object_type *named_pipe_device_get_type( struct object *obj );
239 static struct fd *named_pipe_device_get_fd( struct object *obj );
240 static struct object *named_pipe_device_lookup_name( struct object *obj,
241 struct unicode_str *name, unsigned int attr );
242 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
243 unsigned int sharing, unsigned int options );
244 static void named_pipe_device_destroy( struct object *obj );
245 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
246 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
248 static const struct object_ops named_pipe_device_ops =
250 sizeof(struct named_pipe_device), /* size */
251 named_pipe_device_dump, /* dump */
252 named_pipe_device_get_type, /* get_type */
253 no_add_queue, /* add_queue */
254 NULL, /* remove_queue */
255 NULL, /* signaled */
256 no_satisfied, /* satisfied */
257 no_signal, /* signal */
258 named_pipe_device_get_fd, /* get_fd */
259 no_map_access, /* map_access */
260 default_get_sd, /* get_sd */
261 default_set_sd, /* set_sd */
262 named_pipe_device_lookup_name, /* lookup_name */
263 directory_link_name, /* link_name */
264 default_unlink_name, /* unlink_name */
265 named_pipe_device_open_file, /* open_file */
266 fd_close_handle, /* close_handle */
267 named_pipe_device_destroy /* destroy */
270 static const struct fd_ops named_pipe_device_fd_ops =
272 default_fd_get_poll_events, /* get_poll_events */
273 default_poll_event, /* poll_event */
274 named_pipe_device_get_fd_type, /* get_fd_type */
275 no_fd_read, /* read */
276 no_fd_write, /* write */
277 no_fd_flush, /* flush */
278 no_fd_get_file_info, /* get_file_info */
279 no_fd_get_volume_info, /* get_volume_info */
280 named_pipe_device_ioctl, /* ioctl */
281 default_fd_queue_async, /* queue_async */
282 default_fd_reselect_async /* reselect_async */
285 static void named_pipe_dump( struct object *obj, int verbose )
287 fputs( "Named pipe\n", stderr );
290 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
292 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
293 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
294 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
295 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
296 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
299 static void pipe_server_dump( struct object *obj, int verbose )
301 struct pipe_server *server = (struct pipe_server *) obj;
302 assert( obj->ops == &pipe_server_ops );
303 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
306 static void pipe_client_dump( struct object *obj, int verbose )
308 struct pipe_client *client = (struct pipe_client *) obj;
309 assert( obj->ops == &pipe_client_ops );
310 fprintf( stderr, "Named pipe client server=%p\n", client->server );
313 static void named_pipe_destroy( struct object *obj)
315 struct named_pipe *pipe = (struct named_pipe *) obj;
317 assert( list_empty( &pipe->servers ) );
318 assert( !pipe->instances );
319 free_async_queue( &pipe->waiters );
322 static struct fd *pipe_end_get_fd( struct object *obj )
324 struct pipe_end *pipe_end = (struct pipe_end *) obj;
325 return (struct fd *) grab_object( pipe_end->fd );
328 static void set_server_state( struct pipe_server *server, enum pipe_state state )
330 server->state = state;
332 switch(state)
334 case ps_connected_server:
335 case ps_wait_disconnect:
336 break;
337 case ps_wait_open:
338 case ps_idle_server:
339 set_no_fd_status( server->pipe_end.fd, STATUS_PIPE_LISTENING );
340 break;
341 case ps_wait_connect:
342 set_no_fd_status( server->pipe_end.fd, STATUS_PIPE_DISCONNECTED );
343 break;
348 static void wake_message( struct pipe_message *message )
350 struct async *async = message->async;
352 message->async = NULL;
353 message->iosb->status = STATUS_SUCCESS;
354 message->iosb->result = message->iosb->in_size;
355 if (async)
357 async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
358 release_object( async );
362 static void free_message( struct pipe_message *message )
364 list_remove( &message->entry );
365 if (message->iosb) release_object( message->iosb );
366 free( message );
369 static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
371 struct pipe_end *connection = pipe_end->connection;
372 struct pipe_message *message, *next;
373 struct async *async;
375 pipe_end->connection = NULL;
377 fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
378 async_wake_up( &pipe_end->read_q, status );
379 LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
381 async = message->async;
382 if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
383 if (!async) continue;
384 async_terminate( async, status );
385 release_object( async );
387 if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
389 if (connection)
391 connection->connection = NULL;
392 pipe_end_disconnect( connection, status );
396 static void pipe_end_destroy( struct pipe_end *pipe_end )
398 struct pipe_message *message;
400 while (!list_empty( &pipe_end->message_queue ))
402 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
403 assert( !message->async );
404 free_message( message );
407 free_async_queue( &pipe_end->read_q );
408 free_async_queue( &pipe_end->write_q );
409 if (pipe_end->fd) release_object( pipe_end->fd );
412 static void pipe_server_destroy( struct object *obj)
414 struct pipe_server *server = (struct pipe_server *)obj;
416 assert( obj->ops == &pipe_server_ops );
418 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_BROKEN );
420 pipe_end_destroy( &server->pipe_end );
421 if (server->client)
423 server->client->server = NULL;
424 server->client = NULL;
427 assert( server->pipe->instances );
428 server->pipe->instances--;
430 list_remove( &server->entry );
431 release_object( server->pipe );
434 static void pipe_client_destroy( struct object *obj)
436 struct pipe_client *client = (struct pipe_client *)obj;
437 struct pipe_server *server = client->server;
439 assert( obj->ops == &pipe_client_ops );
441 pipe_end_disconnect( &client->pipe_end, STATUS_PIPE_BROKEN );
443 if (server)
445 switch(server->state)
447 case ps_connected_server:
448 /* Don't destroy the server's fd here as we can't
449 do a successful flush without it. */
450 set_server_state( server, ps_wait_disconnect );
451 break;
452 case ps_idle_server:
453 case ps_wait_open:
454 case ps_wait_disconnect:
455 case ps_wait_connect:
456 assert( 0 );
458 assert( server->client );
459 server->client = NULL;
460 client->server = NULL;
463 pipe_end_destroy( &client->pipe_end );
466 static void named_pipe_device_dump( struct object *obj, int verbose )
468 fputs( "Named pipe device\n", stderr );
471 static struct object_type *named_pipe_device_get_type( struct object *obj )
473 static const WCHAR name[] = {'D','e','v','i','c','e'};
474 static const struct unicode_str str = { name, sizeof(name) };
475 return get_object_type( &str );
478 static struct fd *named_pipe_device_get_fd( struct object *obj )
480 struct named_pipe_device *device = (struct named_pipe_device *)obj;
481 return (struct fd *)grab_object( device->fd );
484 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
485 unsigned int attr )
487 struct named_pipe_device *device = (struct named_pipe_device*)obj;
488 struct object *found;
490 assert( obj->ops == &named_pipe_device_ops );
491 assert( device->pipes );
493 if (!name) return NULL; /* open the device itself */
495 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
496 name->len = 0;
498 return found;
501 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
502 unsigned int sharing, unsigned int options )
504 return grab_object( obj );
507 static void named_pipe_device_destroy( struct object *obj )
509 struct named_pipe_device *device = (struct named_pipe_device*)obj;
510 assert( obj->ops == &named_pipe_device_ops );
511 if (device->fd) release_object( device->fd );
512 free( device->pipes );
515 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
517 return FD_TYPE_DEVICE;
520 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name )
522 struct named_pipe_device *dev;
524 if ((dev = create_named_object( root, &named_pipe_device_ops, name, 0, NULL )) &&
525 get_error() != STATUS_OBJECT_NAME_EXISTS)
527 dev->pipes = NULL;
528 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
529 !(dev->pipes = create_namespace( 7 )))
531 release_object( dev );
532 dev = NULL;
535 return &dev->obj;
538 static int pipe_end_flush( struct fd *fd, struct async *async )
540 struct pipe_end *pipe_end = get_fd_user( fd );
542 if (pipe_end->connection && !list_empty( &pipe_end->connection->message_queue ))
544 fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT );
545 set_error( STATUS_PENDING );
547 return 1;
550 static void pipe_end_get_file_info( struct fd *fd, struct named_pipe *pipe, unsigned int info_class )
552 switch (info_class)
554 case FileNameInformation:
556 FILE_NAME_INFORMATION *name_info;
557 data_size_t name_len, reply_size;
558 const WCHAR *name;
560 if (get_reply_max_size() < sizeof(*name_info))
562 set_error( STATUS_INFO_LENGTH_MISMATCH );
563 return;
566 name = get_object_name( &pipe->obj, &name_len );
567 reply_size = offsetof( FILE_NAME_INFORMATION, FileName[name_len/sizeof(WCHAR) + 1] );
568 if (reply_size > get_reply_max_size())
570 reply_size = get_reply_max_size();
571 set_error( STATUS_BUFFER_OVERFLOW );
574 if (!(name_info = set_reply_data_size( reply_size ))) return;
575 name_info->FileNameLength = name_len + sizeof(WCHAR);
576 name_info->FileName[0] = '\\';
577 reply_size -= offsetof( FILE_NAME_INFORMATION, FileName[1] );
578 if (reply_size) memcpy( &name_info->FileName[1], name, reply_size );
579 break;
581 default:
582 no_fd_get_file_info( fd, info_class );
586 static void pipe_server_get_file_info( struct fd *fd, unsigned int info_class )
588 struct pipe_server *server = get_fd_user( fd );
589 pipe_end_get_file_info( fd, server->pipe, info_class );
592 static void pipe_client_get_file_info( struct fd *fd, unsigned int info_class )
594 struct pipe_client *client = get_fd_user( fd );
595 if (client->server) pipe_end_get_file_info( fd, client->server->pipe, info_class );
596 else set_error( STATUS_PIPE_DISCONNECTED );
599 static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class )
601 switch (info_class)
603 case FileFsDeviceInformation:
605 static const FILE_FS_DEVICE_INFORMATION device_info =
607 FILE_DEVICE_NAMED_PIPE,
608 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
610 if (get_reply_max_size() >= sizeof(device_info))
611 set_reply_data( &device_info, sizeof(device_info) );
612 else
613 set_error( STATUS_BUFFER_TOO_SMALL );
614 break;
616 default:
617 set_error( STATUS_NOT_IMPLEMENTED );
621 static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
623 struct pipe_message *message;
625 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
627 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
628 iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
629 iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
630 ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
632 else
634 data_size_t avail = 0;
635 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
637 avail += message->iosb->in_size - message->read_pos;
638 if (avail >= iosb->out_size) break;
640 iosb->out_size = min( iosb->out_size, avail );
641 iosb->status = STATUS_SUCCESS;
644 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
645 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
647 iosb->out_data = message->iosb->in_data;
648 message->iosb->in_data = NULL;
649 wake_message( message );
650 free_message( message );
652 else
654 data_size_t write_pos = 0, writing;
655 char *buf = NULL;
657 if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
659 iosb->out_size = 0;
660 iosb->status = STATUS_NO_MEMORY;
661 return;
666 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
667 writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
668 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
669 write_pos += writing;
670 message->read_pos += writing;
671 if (message->read_pos == message->iosb->in_size)
673 wake_message(message);
674 free_message(message);
676 } while (write_pos < iosb->out_size);
678 iosb->result = iosb->out_size;
681 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
682 * We're not interested in such reselect calls, so we ignore them. */
683 static int ignore_reselect;
685 static void reselect_write_queue( struct pipe_end *pipe_end );
687 static void reselect_read_queue( struct pipe_end *pipe_end )
689 struct async *async;
690 struct iosb *iosb;
691 int read_done = 0;
693 ignore_reselect = 1;
694 while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
696 iosb = async_get_iosb( async );
697 message_queue_read( pipe_end, iosb );
698 async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
699 release_object( async );
700 release_object( iosb );
701 read_done = 1;
703 ignore_reselect = 0;
705 if (pipe_end->connection)
707 if (list_empty( &pipe_end->message_queue ))
708 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
709 else if (read_done)
710 reselect_write_queue( pipe_end->connection );
714 static void reselect_write_queue( struct pipe_end *pipe_end )
716 struct pipe_message *message, *next;
717 struct pipe_end *reader = pipe_end->connection;
718 data_size_t avail = 0;
720 if (!reader) return;
722 ignore_reselect = 1;
724 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
726 if (message->async && message->iosb->status != STATUS_PENDING)
728 release_object( message->async );
729 message->async = NULL;
730 free_message( message );
732 else
734 avail += message->iosb->in_size - message->read_pos;
735 if (message->iosb->status == STATUS_PENDING && (avail <= reader->buffer_size || !message->iosb->in_size))
736 wake_message( message );
740 ignore_reselect = 0;
741 reselect_read_queue( reader );
744 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
746 struct pipe_end *pipe_end = get_fd_user( fd );
748 if (!pipe_end->connection && list_empty( &pipe_end->message_queue ))
750 set_error( STATUS_PIPE_BROKEN );
751 return 0;
754 queue_async( &pipe_end->read_q, async );
755 reselect_read_queue( pipe_end );
756 set_error( STATUS_PENDING );
757 return 1;
760 static int pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
762 struct pipe_end *write_end = get_fd_user( fd );
763 struct pipe_end *read_end = write_end->connection;
764 struct pipe_message *message;
766 if (!read_end)
768 set_error( STATUS_PIPE_DISCONNECTED );
769 return 0;
772 if (!(write_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && !get_req_data_size()) return 1;
774 if (!(message = mem_alloc( sizeof(*message) ))) return 0;
775 message->async = (struct async *)grab_object( async );
776 message->iosb = async_get_iosb( async );
777 message->read_pos = 0;
778 list_add_tail( &read_end->message_queue, &message->entry );
780 queue_async( &write_end->write_q, async );
781 reselect_write_queue( write_end );
782 set_error( STATUS_PENDING );
783 return 1;
786 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
788 struct pipe_end *pipe_end = get_fd_user( fd );
790 if (ignore_reselect) return;
792 if (&pipe_end->write_q == queue)
793 reselect_write_queue( pipe_end );
794 else if (&pipe_end->read_q == queue)
795 reselect_read_queue( pipe_end );
798 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
800 return FD_TYPE_PIPE;
803 static int pipe_end_peek( struct pipe_end *pipe_end )
805 unsigned reply_size = get_reply_max_size();
806 FILE_PIPE_PEEK_BUFFER *buffer;
807 struct pipe_message *message;
808 data_size_t avail = 0;
809 data_size_t message_length = 0;
811 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
813 set_error( STATUS_INFO_LENGTH_MISMATCH );
814 return 0;
816 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
818 if (!pipe_end->connection && list_empty( &pipe_end->message_queue ))
820 set_error( STATUS_PIPE_BROKEN );
821 return 0;
824 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
825 avail += message->iosb->in_size - message->read_pos;
826 reply_size = min( reply_size, avail );
828 if (avail && (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE))
830 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
831 message_length = message->iosb->in_size - message->read_pos;
832 reply_size = min( reply_size, message_length );
835 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return 0;
836 buffer->NamedPipeState = 0; /* FIXME */
837 buffer->ReadDataAvailable = avail;
838 buffer->NumberOfMessages = 0; /* FIXME */
839 buffer->MessageLength = message_length;
841 if (reply_size)
843 data_size_t write_pos = 0, writing;
844 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
846 writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
847 memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
848 writing );
849 write_pos += writing;
850 if (write_pos == reply_size) break;
853 return 1;
856 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
858 struct pipe_server *server = get_fd_user( fd );
860 switch(code)
862 case FSCTL_PIPE_LISTEN:
863 switch(server->state)
865 case ps_idle_server:
866 case ps_wait_connect:
867 fd_queue_async( server->pipe_end.fd, async, ASYNC_TYPE_WAIT );
868 set_server_state( server, ps_wait_open );
869 async_wake_up( &server->pipe->waiters, STATUS_SUCCESS );
870 set_error( STATUS_PENDING );
871 return 1;
872 case ps_connected_server:
873 set_error( STATUS_PIPE_CONNECTED );
874 break;
875 case ps_wait_disconnect:
876 set_error( STATUS_NO_DATA_DETECTED );
877 break;
878 case ps_wait_open:
879 set_error( STATUS_INVALID_HANDLE );
880 break;
882 return 0;
884 case FSCTL_PIPE_DISCONNECT:
885 switch(server->state)
887 case ps_connected_server:
888 assert( server->client );
890 /* dump the client and server fds - client loses all waiting data */
891 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
892 server->client->server = NULL;
893 server->client = NULL;
894 set_server_state( server, ps_wait_connect );
895 break;
896 case ps_wait_disconnect:
897 assert( !server->client );
898 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
899 set_server_state( server, ps_wait_connect );
900 break;
901 case ps_idle_server:
902 case ps_wait_open:
903 set_error( STATUS_PIPE_LISTENING );
904 return 0;
905 case ps_wait_connect:
906 set_error( STATUS_PIPE_DISCONNECTED );
907 return 0;
909 return 1;
911 case FSCTL_PIPE_PEEK:
912 return pipe_end_peek( &server->pipe_end );
914 default:
915 return default_fd_ioctl( fd, code, async );
919 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
921 struct pipe_client *client = get_fd_user( fd );
923 switch(code)
925 case FSCTL_PIPE_LISTEN:
926 set_error( STATUS_ILLEGAL_FUNCTION );
927 return 0;
929 case FSCTL_PIPE_PEEK:
930 return pipe_end_peek( &client->pipe_end );
932 default:
933 return default_fd_ioctl( fd, code, async );
937 static struct pipe_server *get_pipe_server_obj( struct process *process,
938 obj_handle_t handle, unsigned int access )
940 struct object *obj;
941 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
942 return (struct pipe_server *) obj;
945 static void init_pipe_end( struct pipe_end *pipe_end, unsigned int pipe_flags, data_size_t buffer_size )
947 pipe_end->fd = NULL;
948 pipe_end->flags = pipe_flags;
949 pipe_end->connection = NULL;
950 pipe_end->buffer_size = buffer_size;
951 init_async_queue( &pipe_end->read_q );
952 init_async_queue( &pipe_end->write_q );
953 list_init( &pipe_end->message_queue );
956 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
957 unsigned int pipe_flags )
959 struct pipe_server *server;
961 server = alloc_object( &pipe_server_ops );
962 if (!server)
963 return NULL;
965 server->pipe = pipe;
966 server->client = NULL;
967 server->options = options;
968 init_pipe_end( &server->pipe_end, pipe_flags, pipe->insize );
970 list_add_head( &pipe->servers, &server->entry );
971 grab_object( pipe );
972 if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
974 release_object( server );
975 return NULL;
977 set_fd_signaled( server->pipe_end.fd, 1 );
978 set_server_state( server, ps_idle_server );
979 return server;
982 static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags,
983 data_size_t buffer_size, unsigned int options )
985 struct pipe_client *client;
987 client = alloc_object( &pipe_client_ops );
988 if (!client)
989 return NULL;
991 client->server = NULL;
992 client->flags = flags;
993 init_pipe_end( &client->pipe_end, pipe_flags, buffer_size );
995 client->pipe_end.fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->pipe_end.obj, options );
996 if (!client->pipe_end.fd)
998 release_object( client );
999 return NULL;
1001 allow_fd_caching( client->pipe_end.fd );
1002 set_fd_signaled( client->pipe_end.fd, 1 );
1004 return client;
1007 static struct pipe_server *find_available_server( struct named_pipe *pipe )
1009 struct pipe_server *server;
1011 /* look for pipe servers that are listening */
1012 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1014 if (server->state == ps_wait_open)
1015 return (struct pipe_server *)grab_object( server );
1018 /* fall back to pipe servers that are idle */
1019 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1021 if (server->state == ps_idle_server)
1022 return (struct pipe_server *)grab_object( server );
1025 return NULL;
1028 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1030 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1032 if (parent->ops != &named_pipe_device_ops)
1034 set_error( STATUS_OBJECT_NAME_INVALID );
1035 return 0;
1037 namespace_add( dev->pipes, name );
1038 name->parent = grab_object( parent );
1039 return 1;
1042 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1043 unsigned int sharing, unsigned int options )
1045 struct named_pipe *pipe = (struct named_pipe *)obj;
1046 struct pipe_server *server;
1047 struct pipe_client *client;
1048 unsigned int pipe_sharing;
1050 if (!(server = find_available_server( pipe )))
1052 set_error( STATUS_PIPE_NOT_AVAILABLE );
1053 return NULL;
1056 pipe_sharing = server->pipe->sharing;
1057 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1058 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1060 set_error( STATUS_ACCESS_DENIED );
1061 release_object( server );
1062 return NULL;
1065 if ((client = create_pipe_client( options, pipe->flags, pipe->outsize, options )))
1067 set_no_fd_status( server->pipe_end.fd, STATUS_BAD_DEVICE_TYPE );
1068 allow_fd_caching( server->pipe_end.fd );
1069 if (server->state == ps_wait_open)
1070 fd_async_wake_up( server->pipe_end.fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
1071 set_server_state( server, ps_connected_server );
1072 server->client = client;
1073 client->server = server;
1074 server->pipe_end.connection = &client->pipe_end;
1075 client->pipe_end.connection = &server->pipe_end;
1077 release_object( server );
1078 return &client->pipe_end.obj;
1081 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1083 struct named_pipe_device *device = get_fd_user( fd );
1085 switch(code)
1087 case FSCTL_PIPE_WAIT:
1089 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1090 data_size_t size = get_req_data_size();
1091 struct named_pipe *pipe;
1092 struct pipe_server *server;
1093 struct unicode_str name;
1094 timeout_t when;
1096 if (size < sizeof(*buffer) ||
1097 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1099 set_error( STATUS_INVALID_PARAMETER );
1100 return 0;
1102 name.str = buffer->Name;
1103 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1104 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;
1106 if (!(server = find_available_server( pipe )))
1108 queue_async( &pipe->waiters, async );
1109 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1110 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1111 release_object( pipe );
1112 set_error( STATUS_PENDING );
1113 return 1;
1116 release_object( server );
1117 release_object( pipe );
1118 return 0;
1121 default:
1122 return default_fd_ioctl( fd, code, async );
1127 DECL_HANDLER(create_named_pipe)
1129 struct named_pipe *pipe;
1130 struct pipe_server *server;
1131 struct unicode_str name;
1132 struct object *root;
1133 const struct security_descriptor *sd;
1134 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1136 if (!objattr) return;
1138 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1139 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1141 if (root) release_object( root );
1142 set_error( STATUS_INVALID_PARAMETER );
1143 return;
1146 if (!name.len) /* pipes need a root directory even without a name */
1148 if (!objattr->rootdir)
1150 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1151 return;
1153 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
1156 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1158 if (root) release_object( root );
1159 if (!pipe) return;
1161 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1163 /* initialize it if it didn't already exist */
1164 pipe->instances = 0;
1165 init_async_queue( &pipe->waiters );
1166 list_init( &pipe->servers );
1167 pipe->insize = req->insize;
1168 pipe->outsize = req->outsize;
1169 pipe->maxinstances = req->maxinstances;
1170 pipe->timeout = req->timeout;
1171 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
1172 pipe->sharing = req->sharing;
1174 else
1176 if (pipe->maxinstances <= pipe->instances)
1178 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1179 release_object( pipe );
1180 return;
1182 if (pipe->sharing != req->sharing)
1184 set_error( STATUS_ACCESS_DENIED );
1185 release_object( pipe );
1186 return;
1188 clear_error(); /* clear the name collision */
1191 server = create_pipe_server( pipe, req->options, req->flags );
1192 if (server)
1194 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1195 server->pipe->instances++;
1196 if (sd) default_set_sd( &server->pipe_end.obj, sd, OWNER_SECURITY_INFORMATION |
1197 GROUP_SECURITY_INFORMATION |
1198 DACL_SECURITY_INFORMATION |
1199 SACL_SECURITY_INFORMATION );
1200 release_object( server );
1203 release_object( pipe );
1206 DECL_HANDLER(get_named_pipe_info)
1208 struct pipe_server *server;
1209 struct pipe_client *client = NULL;
1211 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1212 if (!server)
1214 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1215 return;
1217 clear_error();
1218 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1219 0, &pipe_client_ops );
1220 if (!client) return;
1221 server = client->server;
1224 reply->flags = client ? client->pipe_end.flags : server->pipe_end.flags;
1225 if (server)
1227 reply->sharing = server->pipe->sharing;
1228 reply->maxinstances = server->pipe->maxinstances;
1229 reply->instances = server->pipe->instances;
1230 reply->insize = server->pipe->insize;
1231 reply->outsize = server->pipe->outsize;
1234 if (client)
1235 release_object(client);
1236 else
1238 reply->flags |= NAMED_PIPE_SERVER_END;
1239 release_object(server);
1243 DECL_HANDLER(set_named_pipe_info)
1245 struct pipe_server *server;
1246 struct pipe_client *client = NULL;
1248 server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
1249 if (!server)
1251 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1252 return;
1254 clear_error();
1255 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1256 0, &pipe_client_ops );
1257 if (!client) return;
1258 if (!(server = client->server))
1260 release_object( client );
1261 return;
1265 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1266 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1268 set_error( STATUS_INVALID_PARAMETER );
1270 else if (client)
1272 client->pipe_end.flags = server->pipe->flags | req->flags;
1274 else
1276 server->pipe_end.flags = server->pipe->flags | req->flags;
1279 if (client)
1280 release_object(client);
1281 else
1282 release_object(server);