cmd: Reset totals after trailer output.
[wine.git] / server / named_pipe.c
blobf3404a33c3b53eb4968da2be677958df36667447
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"
25 #include <assert.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.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 both pipe ends */
74 struct list entry; /* entry in named pipe listeners list */
75 unsigned int options; /* pipe options */
76 struct async_queue listen_q; /* listen queue */
79 struct named_pipe
81 struct object obj; /* object header */
82 int message_mode;
83 unsigned int sharing;
84 unsigned int maxinstances;
85 unsigned int outsize;
86 unsigned int insize;
87 unsigned int instances;
88 timeout_t timeout;
89 struct list listeners; /* list of servers listening on this pipe */
90 struct async_queue waiters; /* list of clients waiting to connect */
93 struct named_pipe_device
95 struct object obj; /* object header */
96 struct namespace *pipes; /* named pipe namespace */
99 struct named_pipe_device_file
101 struct object obj; /* object header */
102 struct fd *fd; /* pseudo-fd for ioctls */
103 struct named_pipe_device *device; /* named pipe device */
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 WCHAR *named_pipe_get_full_name( struct object *obj, data_size_t *ret_len );
109 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent );
110 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
111 unsigned int sharing, unsigned int options );
112 static void named_pipe_destroy( struct object *obj );
114 static const struct object_ops named_pipe_ops =
116 sizeof(struct named_pipe), /* size */
117 &no_type, /* type */
118 named_pipe_dump, /* dump */
119 no_add_queue, /* add_queue */
120 NULL, /* remove_queue */
121 NULL, /* signaled */
122 NULL, /* satisfied */
123 no_signal, /* signal */
124 no_get_fd, /* get_fd */
125 named_pipe_map_access, /* map_access */
126 default_get_sd, /* get_sd */
127 default_set_sd, /* set_sd */
128 named_pipe_get_full_name, /* get_full_name */
129 no_lookup_name, /* lookup_name */
130 named_pipe_link_name, /* link_name */
131 default_unlink_name, /* unlink_name */
132 named_pipe_open_file, /* open_file */
133 no_kernel_obj_list, /* get_kernel_obj_list */
134 no_close_handle, /* close_handle */
135 named_pipe_destroy /* destroy */
138 /* common server and client pipe end functions */
139 static void pipe_end_destroy( struct object *obj );
140 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd );
141 static struct fd *pipe_end_get_fd( struct object *obj );
142 static struct security_descriptor *pipe_end_get_sd( struct object *obj );
143 static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
144 unsigned int set_info );
145 static WCHAR *pipe_end_get_full_name( struct object *obj, data_size_t *len );
146 static void pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos );
147 static void pipe_end_write( struct fd *fd, struct async *async_data, file_pos_t pos );
148 static void pipe_end_flush( struct fd *fd, struct async *async );
149 static void pipe_end_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class );
150 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue );
151 static void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class );
153 /* server end functions */
154 static void pipe_server_dump( struct object *obj, int verbose );
155 static struct object *pipe_server_lookup_name( struct object *obj, struct unicode_str *name,
156 unsigned int attr, struct object *root );
157 static struct object *pipe_server_open_file( struct object *obj, unsigned int access,
158 unsigned int sharing, unsigned int options );
159 static void pipe_server_destroy( struct object *obj);
160 static void pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
162 static const struct object_ops pipe_server_ops =
164 sizeof(struct pipe_server), /* size */
165 &file_type, /* type */
166 pipe_server_dump, /* dump */
167 add_queue, /* add_queue */
168 remove_queue, /* remove_queue */
169 default_fd_signaled, /* signaled */
170 no_satisfied, /* satisfied */
171 no_signal, /* signal */
172 pipe_end_get_fd, /* get_fd */
173 default_map_access, /* map_access */
174 pipe_end_get_sd, /* get_sd */
175 pipe_end_set_sd, /* set_sd */
176 pipe_end_get_full_name, /* get_full_name */
177 pipe_server_lookup_name, /* lookup_name */
178 no_link_name, /* link_name */
179 NULL, /* unlink_name */
180 pipe_server_open_file, /* open_file */
181 no_kernel_obj_list, /* get_kernel_obj_list */
182 async_close_obj_handle, /* close_handle */
183 pipe_server_destroy /* destroy */
186 static const struct fd_ops pipe_server_fd_ops =
188 default_fd_get_poll_events, /* get_poll_events */
189 default_poll_event, /* poll_event */
190 pipe_end_get_fd_type, /* get_fd_type */
191 pipe_end_read, /* read */
192 pipe_end_write, /* write */
193 pipe_end_flush, /* flush */
194 pipe_end_get_file_info, /* get_file_info */
195 pipe_end_get_volume_info, /* get_volume_info */
196 pipe_server_ioctl, /* ioctl */
197 default_fd_cancel_async, /* cancel_async */
198 no_fd_queue_async, /* queue_async */
199 pipe_end_reselect_async /* reselect_async */
202 /* client end functions */
203 static void pipe_client_dump( struct object *obj, int verbose );
204 static void pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
206 static const struct object_ops pipe_client_ops =
208 sizeof(struct pipe_end), /* size */
209 &file_type, /* type */
210 pipe_client_dump, /* dump */
211 add_queue, /* add_queue */
212 remove_queue, /* remove_queue */
213 default_fd_signaled, /* signaled */
214 no_satisfied, /* satisfied */
215 no_signal, /* signal */
216 pipe_end_get_fd, /* get_fd */
217 default_map_access, /* map_access */
218 pipe_end_get_sd, /* get_sd */
219 pipe_end_set_sd, /* set_sd */
220 pipe_end_get_full_name, /* get_full_name */
221 no_lookup_name, /* lookup_name */
222 no_link_name, /* link_name */
223 NULL, /* unlink_name */
224 no_open_file, /* open_file */
225 no_kernel_obj_list, /* get_kernel_obj_list */
226 async_close_obj_handle, /* close_handle */
227 pipe_end_destroy /* destroy */
230 static const struct fd_ops pipe_client_fd_ops =
232 default_fd_get_poll_events, /* get_poll_events */
233 default_poll_event, /* poll_event */
234 pipe_end_get_fd_type, /* get_fd_type */
235 pipe_end_read, /* read */
236 pipe_end_write, /* write */
237 pipe_end_flush, /* flush */
238 pipe_end_get_file_info, /* get_file_info */
239 pipe_end_get_volume_info, /* get_volume_info */
240 pipe_client_ioctl, /* ioctl */
241 default_fd_cancel_async, /* cancel_async */
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 *named_pipe_device_lookup_name( struct object *obj,
248 struct unicode_str *name, unsigned int attr, struct object *root );
249 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
250 unsigned int sharing, unsigned int options );
251 static void named_pipe_device_destroy( struct object *obj );
253 static const struct object_ops named_pipe_device_ops =
255 sizeof(struct named_pipe_device), /* size */
256 &device_type, /* type */
257 named_pipe_device_dump, /* dump */
258 no_add_queue, /* add_queue */
259 NULL, /* remove_queue */
260 NULL, /* signaled */
261 no_satisfied, /* satisfied */
262 no_signal, /* signal */
263 no_get_fd, /* get_fd */
264 default_map_access, /* map_access */
265 default_get_sd, /* get_sd */
266 default_set_sd, /* set_sd */
267 default_get_full_name, /* get_full_name */
268 named_pipe_device_lookup_name, /* lookup_name */
269 directory_link_name, /* link_name */
270 default_unlink_name, /* unlink_name */
271 named_pipe_device_open_file, /* open_file */
272 no_kernel_obj_list, /* get_kernel_obj_list */
273 no_close_handle, /* close_handle */
274 named_pipe_device_destroy /* destroy */
277 static void named_pipe_device_file_dump( struct object *obj, int verbose );
278 static struct fd *named_pipe_device_file_get_fd( struct object *obj );
279 static WCHAR *named_pipe_device_file_get_full_name( struct object *obj, data_size_t *len );
280 static void named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
281 static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd );
282 static void named_pipe_device_file_destroy( struct object *obj );
284 static const struct object_ops named_pipe_device_file_ops =
286 sizeof(struct named_pipe_device_file), /* size */
287 &file_type, /* type */
288 named_pipe_device_file_dump, /* dump */
289 add_queue, /* add_queue */
290 remove_queue, /* remove_queue */
291 default_fd_signaled, /* signaled */
292 no_satisfied, /* satisfied */
293 no_signal, /* signal */
294 named_pipe_device_file_get_fd, /* get_fd */
295 default_map_access, /* map_access */
296 default_get_sd, /* get_sd */
297 default_set_sd, /* set_sd */
298 named_pipe_device_file_get_full_name, /* get_full_name */
299 no_lookup_name, /* lookup_name */
300 no_link_name, /* link_name */
301 NULL, /* unlink_name */
302 no_open_file, /* open_file */
303 no_kernel_obj_list, /* get_kernel_obj_list */
304 no_close_handle, /* close_handle */
305 named_pipe_device_file_destroy /* destroy */
308 static const struct fd_ops named_pipe_device_fd_ops =
310 default_fd_get_poll_events, /* get_poll_events */
311 default_poll_event, /* poll_event */
312 named_pipe_device_file_get_fd_type, /* get_fd_type */
313 no_fd_read, /* read */
314 no_fd_write, /* write */
315 no_fd_flush, /* flush */
316 default_fd_get_file_info, /* get_file_info */
317 no_fd_get_volume_info, /* get_volume_info */
318 named_pipe_device_ioctl, /* ioctl */
319 default_fd_cancel_async, /* cancel_async */
320 default_fd_queue_async, /* queue_async */
321 default_fd_reselect_async /* reselect_async */
324 static void named_pipe_dump( struct object *obj, int verbose )
326 fputs( "Named pipe\n", stderr );
329 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
331 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
332 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
333 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
334 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
335 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
338 static WCHAR *named_pipe_get_full_name( struct object *obj, data_size_t *ret_len )
340 WCHAR *ret;
342 if (!(ret = default_get_full_name( obj, ret_len )))
343 set_error( STATUS_OBJECT_PATH_INVALID );
344 return ret;
347 static void pipe_server_dump( struct object *obj, int verbose )
349 struct pipe_server *server = (struct pipe_server *) obj;
350 assert( obj->ops == &pipe_server_ops );
351 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe_end.pipe,
352 server->pipe_end.state );
355 static void pipe_client_dump( struct object *obj, int verbose )
357 struct pipe_end *client = (struct pipe_end *) obj;
358 assert( obj->ops == &pipe_client_ops );
359 fprintf( stderr, "Named pipe client server=%p\n", client->connection );
362 static void named_pipe_destroy( struct object *obj)
364 struct named_pipe *pipe = (struct named_pipe *) obj;
366 assert( list_empty( &pipe->listeners ) );
367 assert( !pipe->instances );
368 free_async_queue( &pipe->waiters );
371 static struct fd *pipe_end_get_fd( struct object *obj )
373 struct pipe_end *pipe_end = (struct pipe_end *) obj;
374 return (struct fd *) grab_object( pipe_end->fd );
377 static struct pipe_message *queue_message( struct pipe_end *pipe_end, struct iosb *iosb )
379 struct pipe_message *message;
381 if (!(message = mem_alloc( sizeof(*message) ))) return NULL;
382 message->iosb = (struct iosb *)grab_object( iosb );
383 message->async = NULL;
384 message->read_pos = 0;
385 list_add_tail( &pipe_end->message_queue, &message->entry );
386 return message;
389 static void wake_message( struct pipe_message *message, data_size_t result )
391 struct async *async = message->async;
393 message->async = NULL;
394 if (!async) return;
396 async_request_complete( async, STATUS_SUCCESS, result, 0, NULL );
397 release_object( async );
400 static void free_message( struct pipe_message *message )
402 list_remove( &message->entry );
403 if (message->iosb) release_object( message->iosb );
404 free( message );
407 static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
409 struct pipe_end *connection = pipe_end->connection;
410 struct pipe_message *message, *next;
411 struct async *async;
413 pipe_end->connection = NULL;
415 pipe_end->state = status == STATUS_PIPE_DISCONNECTED
416 ? FILE_PIPE_DISCONNECTED_STATE : FILE_PIPE_CLOSING_STATE;
417 fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
418 async_wake_up( &pipe_end->read_q, status );
419 LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
421 async = message->async;
422 if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
423 if (!async) continue;
424 async_terminate( async, status );
425 release_object( async );
427 if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
429 if (connection)
431 connection->connection = NULL;
432 pipe_end_disconnect( connection, status );
436 static void pipe_end_destroy( struct object *obj )
438 struct pipe_end *pipe_end = (struct pipe_end *)obj;
439 struct pipe_message *message;
441 pipe_end_disconnect( pipe_end, STATUS_PIPE_BROKEN );
443 while (!list_empty( &pipe_end->message_queue ))
445 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
446 assert( !message->async );
447 free_message( message );
450 free_async_queue( &pipe_end->read_q );
451 free_async_queue( &pipe_end->write_q );
452 if (pipe_end->fd) release_object( pipe_end->fd );
453 if (pipe_end->pipe) release_object( pipe_end->pipe );
456 static struct object *pipe_server_lookup_name( struct object *obj, struct unicode_str *name,
457 unsigned int attr, struct object *root )
459 if (name && name->len)
460 set_error( STATUS_OBJECT_NAME_INVALID );
462 return NULL;
465 static struct object *pipe_server_open_file( struct object *obj, unsigned int access,
466 unsigned int sharing, unsigned int options )
468 struct pipe_server *server = (struct pipe_server *)obj;
470 return server->pipe_end.pipe->obj.ops->open_file( &server->pipe_end.pipe->obj, access, sharing, options );
473 static void pipe_server_destroy( struct object *obj )
475 struct pipe_server *server = (struct pipe_server *)obj;
476 struct named_pipe *pipe = server->pipe_end.pipe;
478 assert( obj->ops == &pipe_server_ops );
480 assert( pipe->instances );
481 if (!--pipe->instances) unlink_named_object( &pipe->obj );
482 if (server->pipe_end.state == FILE_PIPE_LISTENING_STATE)
483 list_remove( &server->entry );
485 free_async_queue( &server->listen_q );
486 pipe_end_destroy( obj );
489 static void named_pipe_device_dump( struct object *obj, int verbose )
491 fputs( "Named pipe device\n", stderr );
494 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
495 unsigned int attr, struct object *root )
497 struct named_pipe_device *device = (struct named_pipe_device*)obj;
498 struct object *found;
500 assert( obj->ops == &named_pipe_device_ops );
501 assert( device->pipes );
503 if (!name) return NULL; /* open the device itself */
505 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
506 name->len = 0;
508 return found;
511 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
512 unsigned int sharing, unsigned int options )
514 struct named_pipe_device_file *file;
516 if (!(file = alloc_object( &named_pipe_device_file_ops ))) return NULL;
517 file->device = (struct named_pipe_device *)grab_object( obj );
518 if (!(file->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, obj, options )))
520 release_object( file );
521 return NULL;
523 allow_fd_caching( file->fd );
524 return &file->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 free( device->pipes );
534 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name,
535 unsigned int attr, const struct security_descriptor *sd )
537 struct named_pipe_device *dev;
539 if ((dev = create_named_object( root, &named_pipe_device_ops, name, attr, sd )) &&
540 get_error() != STATUS_OBJECT_NAME_EXISTS)
542 dev->pipes = NULL;
543 if (!(dev->pipes = create_namespace( 7 )))
545 release_object( dev );
546 dev = NULL;
549 return &dev->obj;
552 static void named_pipe_device_file_dump( struct object *obj, int verbose )
554 struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
556 fprintf( stderr, "File on named pipe device %p\n", file->device );
559 static struct fd *named_pipe_device_file_get_fd( struct object *obj )
561 struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
562 return (struct fd *)grab_object( file->fd );
565 static WCHAR *named_pipe_device_file_get_full_name( struct object *obj, data_size_t *len )
567 struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
568 return file->device->obj.ops->get_full_name( &file->device->obj, len );
571 static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd )
573 return FD_TYPE_DEVICE;
576 static void named_pipe_device_file_destroy( struct object *obj )
578 struct named_pipe_device_file *file = (struct named_pipe_device_file*)obj;
579 assert( obj->ops == &named_pipe_device_file_ops );
580 if (file->fd) release_object( file->fd );
581 release_object( file->device );
584 static void pipe_end_flush( struct fd *fd, struct async *async )
586 struct pipe_end *pipe_end = get_fd_user( fd );
588 if (!pipe_end->pipe)
590 set_error( STATUS_PIPE_DISCONNECTED );
591 return;
594 if (pipe_end->connection && !list_empty( &pipe_end->connection->message_queue ))
596 fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT );
597 set_error( STATUS_PENDING );
601 static data_size_t pipe_end_get_avail( struct pipe_end *pipe_end )
603 struct pipe_message *message;
604 data_size_t avail = 0;
606 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
607 avail += message->iosb->in_size - message->read_pos;
609 return avail;
612 static void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
614 struct pipe_end *pipe_end = get_fd_user( fd );
615 struct named_pipe *pipe = pipe_end->pipe;
617 switch (info_class)
619 case FileNameInformation:
621 FILE_NAME_INFORMATION *name_info;
622 data_size_t name_len, reply_size;
623 const WCHAR *name;
625 if (get_reply_max_size() < sizeof(*name_info))
627 set_error( STATUS_INFO_LENGTH_MISMATCH );
628 return;
631 /* FIXME: We should be able to return on unlinked pipe */
632 if (!pipe || !(name = get_object_name( &pipe->obj, &name_len )))
634 set_error( STATUS_PIPE_DISCONNECTED );
635 return;
638 reply_size = offsetof( FILE_NAME_INFORMATION, FileName[name_len/sizeof(WCHAR) + 1] );
639 if (reply_size > get_reply_max_size())
641 reply_size = get_reply_max_size();
642 set_error( STATUS_BUFFER_OVERFLOW );
645 if (!(name_info = set_reply_data_size( reply_size ))) return;
646 name_info->FileNameLength = name_len + sizeof(WCHAR);
647 name_info->FileName[0] = '\\';
648 reply_size -= offsetof( FILE_NAME_INFORMATION, FileName[1] );
649 if (reply_size) memcpy( &name_info->FileName[1], name, reply_size );
650 break;
652 case FilePipeInformation:
654 FILE_PIPE_INFORMATION *pipe_info;
656 if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
658 set_error( STATUS_ACCESS_DENIED );
659 return;
662 if (get_reply_max_size() < sizeof(*pipe_info))
664 set_error( STATUS_INFO_LENGTH_MISMATCH );
665 return;
668 if (!pipe)
670 set_error( STATUS_PIPE_DISCONNECTED );
671 return;
674 if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
675 pipe_info->ReadMode = (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
676 ? FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
677 pipe_info->CompletionMode = (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE)
678 ? FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
679 break;
681 case FilePipeLocalInformation:
683 FILE_PIPE_LOCAL_INFORMATION *pipe_info;
685 if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
687 set_error( STATUS_ACCESS_DENIED );
688 return;
691 if (get_reply_max_size() < sizeof(*pipe_info))
693 set_error( STATUS_INFO_LENGTH_MISMATCH );
694 return;
697 if (!pipe)
699 set_error( STATUS_PIPE_DISCONNECTED );
700 return;
703 if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
704 pipe_info->NamedPipeType = pipe->message_mode;
705 switch (pipe->sharing)
707 case FILE_SHARE_READ:
708 pipe_info->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
709 break;
710 case FILE_SHARE_WRITE:
711 pipe_info->NamedPipeConfiguration = FILE_PIPE_INBOUND;
712 break;
713 case FILE_SHARE_READ | FILE_SHARE_WRITE:
714 pipe_info->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
715 break;
717 pipe_info->MaximumInstances = pipe->maxinstances;
718 pipe_info->CurrentInstances = pipe->instances;
719 pipe_info->InboundQuota = pipe->insize;
721 pipe_info->ReadDataAvailable = pipe_end_get_avail( pipe_end );
723 pipe_info->OutboundQuota = pipe->outsize;
724 pipe_info->WriteQuotaAvailable = 0; /* FIXME */
725 pipe_info->NamedPipeState = pipe_end->state;
726 pipe_info->NamedPipeEnd = pipe_end->obj.ops == &pipe_server_ops
727 ? FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
728 break;
730 case FileStandardInformation:
732 FILE_STANDARD_INFORMATION *std_info;
734 if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
736 set_error( STATUS_ACCESS_DENIED );
737 return;
740 if (get_reply_max_size() < sizeof(*std_info))
742 set_error( STATUS_INFO_LENGTH_MISMATCH );
743 return;
746 if (!pipe)
748 set_error( STATUS_PIPE_DISCONNECTED );
749 return;
752 if (!(std_info = set_reply_data_size( sizeof(*std_info) ))) return;
753 std_info->AllocationSize.QuadPart = pipe->outsize + pipe->insize;
754 std_info->EndOfFile.QuadPart = pipe_end_get_avail( pipe_end );
755 std_info->NumberOfLinks = 1; /* FIXME */
756 std_info->DeletePending = 0; /* FIXME */
757 std_info->Directory = 0;
758 break;
760 default:
761 default_fd_get_file_info( fd, handle, info_class );
765 static struct security_descriptor *pipe_end_get_sd( struct object *obj )
767 struct pipe_end *pipe_end = (struct pipe_end *) obj;
768 if (pipe_end->pipe) return default_get_sd( &pipe_end->pipe->obj );
769 set_error( STATUS_PIPE_DISCONNECTED );
770 return NULL;
773 static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
774 unsigned int set_info )
776 struct pipe_end *pipe_end = (struct pipe_end *) obj;
777 if (pipe_end->pipe) return default_set_sd( &pipe_end->pipe->obj, sd, set_info );
778 set_error( STATUS_PIPE_DISCONNECTED );
779 return 0;
782 static WCHAR *pipe_end_get_full_name( struct object *obj, data_size_t *len )
784 struct pipe_end *pipe_end = (struct pipe_end *) obj;
785 return pipe_end->pipe->obj.ops->get_full_name( &pipe_end->pipe->obj, len );
788 static void pipe_end_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class )
790 switch (info_class)
792 case FileFsDeviceInformation:
794 static const FILE_FS_DEVICE_INFORMATION device_info =
796 FILE_DEVICE_NAMED_PIPE,
797 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
799 if (get_reply_max_size() >= sizeof(device_info))
800 set_reply_data( &device_info, sizeof(device_info) );
801 else
802 set_error( STATUS_BUFFER_TOO_SMALL );
803 break;
805 default:
806 set_error( STATUS_NOT_IMPLEMENTED );
810 static void message_queue_read( struct pipe_end *pipe_end, struct async *async )
812 struct iosb *iosb = async_get_iosb( async );
813 unsigned int status = STATUS_SUCCESS;
814 struct pipe_message *message;
815 data_size_t out_size;
817 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
819 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
820 out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
822 if (message->read_pos + out_size < message->iosb->in_size)
823 status = STATUS_BUFFER_OVERFLOW;
825 else
827 data_size_t avail = 0;
828 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
830 avail += message->iosb->in_size - message->read_pos;
831 if (avail >= iosb->out_size) break;
833 out_size = min( iosb->out_size, avail );
836 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
837 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
839 async_request_complete( async, status, out_size, out_size, message->iosb->in_data );
840 message->iosb->in_data = NULL;
841 wake_message( message, message->iosb->in_size );
842 free_message( message );
844 else
846 data_size_t write_pos = 0, writing;
847 char *buf = NULL;
849 if (out_size && !(buf = malloc( out_size )))
851 async_terminate( async, STATUS_NO_MEMORY );
852 release_object( iosb );
853 return;
858 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
859 writing = min( out_size - write_pos, message->iosb->in_size - message->read_pos );
860 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
861 write_pos += writing;
862 message->read_pos += writing;
863 if (message->read_pos == message->iosb->in_size)
865 wake_message(message, message->iosb->in_size);
866 free_message(message);
868 } while (write_pos < out_size);
870 async_request_complete( async, status, out_size, out_size, buf );
873 release_object( iosb );
876 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
877 * We're not interested in such reselect calls, so we ignore them. */
878 static int ignore_reselect;
880 static void reselect_write_queue( struct pipe_end *pipe_end );
882 static void reselect_read_queue( struct pipe_end *pipe_end, int reselect_write )
884 struct async *async;
886 ignore_reselect = 1;
887 while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
889 message_queue_read( pipe_end, async );
890 release_object( async );
891 reselect_write = 1;
893 ignore_reselect = 0;
895 if (pipe_end->connection)
897 if (list_empty( &pipe_end->message_queue ))
898 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
899 else if (reselect_write)
900 reselect_write_queue( pipe_end->connection );
904 static void reselect_write_queue( struct pipe_end *pipe_end )
906 struct pipe_message *message, *next;
907 struct pipe_end *reader = pipe_end->connection;
908 data_size_t avail = 0;
910 if (!reader) return;
912 ignore_reselect = 1;
914 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
916 if (message->async && message->iosb->status != STATUS_PENDING)
918 release_object( message->async );
919 message->async = NULL;
920 free_message( message );
922 else
924 avail += message->iosb->in_size - message->read_pos;
925 if (message->async && (avail <= reader->buffer_size || !message->iosb->in_size))
927 wake_message( message, message->iosb->in_size );
929 else if (message->async && (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE))
931 wake_message( message, message->read_pos );
932 free_message( message );
937 ignore_reselect = 0;
938 reselect_read_queue( reader, 0 );
941 static void pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
943 struct pipe_end *pipe_end = get_fd_user( fd );
945 switch (pipe_end->state)
947 case FILE_PIPE_CONNECTED_STATE:
948 if ((pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE) && list_empty( &pipe_end->message_queue ))
950 set_error( STATUS_PIPE_EMPTY );
951 return;
953 break;
954 case FILE_PIPE_DISCONNECTED_STATE:
955 set_error( STATUS_PIPE_DISCONNECTED );
956 return;
957 case FILE_PIPE_LISTENING_STATE:
958 set_error( STATUS_PIPE_LISTENING );
959 return;
960 case FILE_PIPE_CLOSING_STATE:
961 if (!list_empty( &pipe_end->message_queue )) break;
962 set_error( STATUS_PIPE_BROKEN );
963 return;
966 queue_async( &pipe_end->read_q, async );
967 reselect_read_queue( pipe_end, 0 );
968 set_error( STATUS_PENDING );
971 static void pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
973 struct pipe_end *pipe_end = get_fd_user( fd );
974 struct pipe_message *message;
975 struct iosb *iosb;
977 switch (pipe_end->state)
979 case FILE_PIPE_CONNECTED_STATE:
980 break;
981 case FILE_PIPE_DISCONNECTED_STATE:
982 set_error( STATUS_PIPE_DISCONNECTED );
983 return;
984 case FILE_PIPE_LISTENING_STATE:
985 set_error( STATUS_PIPE_LISTENING );
986 return;
987 case FILE_PIPE_CLOSING_STATE:
988 set_error( STATUS_PIPE_CLOSING );
989 return;
992 if (!pipe_end->pipe->message_mode && !get_req_data_size()) return;
994 iosb = async_get_iosb( async );
995 message = queue_message( pipe_end->connection, iosb );
996 release_object( iosb );
997 if (!message) return;
999 message->async = (struct async *)grab_object( async );
1000 queue_async( &pipe_end->write_q, async );
1001 reselect_read_queue( pipe_end->connection, 1 );
1002 set_error( STATUS_PENDING );
1005 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
1007 struct pipe_end *pipe_end = get_fd_user( fd );
1009 if (ignore_reselect) return;
1011 if (&pipe_end->write_q == queue)
1012 reselect_write_queue( pipe_end );
1013 else if (&pipe_end->read_q == queue)
1014 reselect_read_queue( pipe_end, 0 );
1017 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
1019 return FD_TYPE_PIPE;
1022 static void pipe_end_peek( struct pipe_end *pipe_end )
1024 unsigned reply_size = get_reply_max_size();
1025 FILE_PIPE_PEEK_BUFFER *buffer;
1026 struct pipe_message *message;
1027 data_size_t avail = 0;
1028 data_size_t message_length = 0;
1030 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
1032 set_error( STATUS_INFO_LENGTH_MISMATCH );
1033 return;
1035 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
1037 switch (pipe_end->state)
1039 case FILE_PIPE_CONNECTED_STATE:
1040 break;
1041 case FILE_PIPE_CLOSING_STATE:
1042 if (!list_empty( &pipe_end->message_queue )) break;
1043 set_error( STATUS_PIPE_BROKEN );
1044 return;
1045 default:
1046 set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
1047 return;
1050 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
1051 avail += message->iosb->in_size - message->read_pos;
1052 reply_size = min( reply_size, avail );
1054 if (avail && pipe_end->pipe->message_mode)
1056 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
1057 message_length = message->iosb->in_size - message->read_pos;
1058 reply_size = min( reply_size, message_length );
1061 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return;
1062 buffer->NamedPipeState = pipe_end->state;
1063 buffer->ReadDataAvailable = avail;
1064 buffer->NumberOfMessages = 0; /* FIXME */
1065 buffer->MessageLength = message_length;
1067 if (reply_size)
1069 data_size_t write_pos = 0, writing;
1070 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
1072 writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
1073 memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
1074 writing );
1075 write_pos += writing;
1076 if (write_pos == reply_size) break;
1079 if (message_length > reply_size) set_error( STATUS_BUFFER_OVERFLOW );
1082 static void pipe_end_transceive( struct pipe_end *pipe_end, struct async *async )
1084 struct pipe_message *message;
1085 struct iosb *iosb;
1087 if (!pipe_end->connection)
1089 set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
1090 return;
1093 if (!(pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ))
1095 set_error( STATUS_INVALID_READ_MODE );
1096 return;
1099 /* not allowed if we already have read data buffered */
1100 if (!list_empty( &pipe_end->message_queue ))
1102 set_error( STATUS_PIPE_BUSY );
1103 return;
1106 iosb = async_get_iosb( async );
1107 /* ignore output buffer copy transferred because of METHOD_NEITHER */
1108 iosb->in_size -= iosb->out_size;
1109 /* transaction never blocks on write, so just queue a message without async */
1110 message = queue_message( pipe_end->connection, iosb );
1111 release_object( iosb );
1112 if (!message) return;
1113 reselect_read_queue( pipe_end->connection, 0 );
1115 queue_async( &pipe_end->read_q, async );
1116 reselect_read_queue( pipe_end, 0 );
1117 set_error( STATUS_PENDING );
1120 static void pipe_end_get_connection_attribute( struct pipe_end *pipe_end )
1122 const char *attr = get_req_data();
1123 data_size_t value_size, attr_size = get_req_data_size();
1124 void *value;
1126 if (attr_size == sizeof("ClientProcessId") && !memcmp( attr, "ClientProcessId", attr_size ))
1128 value = &pipe_end->client_pid;
1129 value_size = sizeof(pipe_end->client_pid);
1131 else if (attr_size == sizeof("ServerProcessId") && !memcmp( attr, "ServerProcessId", attr_size ))
1133 value = &pipe_end->server_pid;
1134 value_size = sizeof(pipe_end->server_pid);
1136 else
1138 set_error( STATUS_ILLEGAL_FUNCTION );
1139 return;
1142 if (get_reply_max_size() < value_size)
1144 set_error( STATUS_INFO_LENGTH_MISMATCH );
1145 return;
1148 set_reply_data( value, value_size );
1151 static void pipe_end_ioctl( struct pipe_end *pipe_end, ioctl_code_t code, struct async *async )
1153 switch(code)
1155 case FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE:
1156 pipe_end_get_connection_attribute( pipe_end );
1157 break;
1159 case FSCTL_PIPE_PEEK:
1160 pipe_end_peek( pipe_end );
1161 break;
1163 case FSCTL_PIPE_TRANSCEIVE:
1164 pipe_end_transceive( pipe_end, async );
1165 break;
1167 default:
1168 default_fd_ioctl( pipe_end->fd, code, async );
1172 static void pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1174 struct pipe_server *server = get_fd_user( fd );
1176 switch(code)
1178 case FSCTL_PIPE_LISTEN:
1179 switch(server->pipe_end.state)
1181 case FILE_PIPE_LISTENING_STATE:
1182 break;
1183 case FILE_PIPE_DISCONNECTED_STATE:
1184 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1185 list_add_tail( &server->pipe_end.pipe->listeners, &server->entry );
1186 break;
1187 case FILE_PIPE_CONNECTED_STATE:
1188 set_error( STATUS_PIPE_CONNECTED );
1189 return;
1190 case FILE_PIPE_CLOSING_STATE:
1191 set_error( STATUS_PIPE_CLOSING );
1192 return;
1195 if (server->pipe_end.flags & NAMED_PIPE_NONBLOCKING_MODE)
1197 set_error( STATUS_PIPE_LISTENING );
1198 return;
1200 queue_async( &server->listen_q, async );
1201 async_wake_up( &server->pipe_end.pipe->waiters, STATUS_SUCCESS );
1202 set_error( STATUS_PENDING );
1203 return;
1205 case FSCTL_PIPE_DISCONNECT:
1206 switch(server->pipe_end.state)
1208 case FILE_PIPE_CONNECTED_STATE:
1209 /* dump the client connection - all data is lost */
1210 assert( server->pipe_end.connection );
1211 release_object( server->pipe_end.connection->pipe );
1212 server->pipe_end.connection->pipe = NULL;
1213 break;
1214 case FILE_PIPE_CLOSING_STATE:
1215 break;
1216 case FILE_PIPE_LISTENING_STATE:
1217 set_error( STATUS_PIPE_LISTENING );
1218 return;
1219 case FILE_PIPE_DISCONNECTED_STATE:
1220 set_error( STATUS_PIPE_DISCONNECTED );
1221 return;
1224 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1225 return;
1227 case FSCTL_PIPE_IMPERSONATE:
1228 if (current->process->token) /* FIXME: use the client token */
1230 struct token *token;
1231 if (!(token = token_duplicate( current->process->token, 0, SecurityImpersonation, NULL, NULL, 0, NULL, 0 )))
1232 return;
1233 if (current->token) release_object( current->token );
1234 current->token = token;
1236 return;
1238 default:
1239 pipe_end_ioctl( &server->pipe_end, code, async );
1243 static void pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1245 struct pipe_end *client = get_fd_user( fd );
1247 switch(code)
1249 case FSCTL_PIPE_LISTEN:
1250 set_error( STATUS_ILLEGAL_FUNCTION );
1251 return;
1253 default:
1254 pipe_end_ioctl( client, code, async );
1258 static void init_pipe_end( struct pipe_end *pipe_end, struct named_pipe *pipe,
1259 unsigned int pipe_flags, data_size_t buffer_size )
1261 pipe_end->pipe = (struct named_pipe *)grab_object( pipe );
1262 pipe_end->fd = NULL;
1263 pipe_end->flags = pipe_flags;
1264 pipe_end->connection = NULL;
1265 pipe_end->buffer_size = buffer_size;
1266 init_async_queue( &pipe_end->read_q );
1267 init_async_queue( &pipe_end->write_q );
1268 list_init( &pipe_end->message_queue );
1271 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
1272 unsigned int pipe_flags )
1274 struct pipe_server *server;
1276 server = alloc_object( &pipe_server_ops );
1277 if (!server)
1278 return NULL;
1280 server->options = options;
1281 init_pipe_end( &server->pipe_end, pipe, pipe_flags, pipe->insize );
1282 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1283 server->pipe_end.server_pid = get_process_id( current->process );
1284 init_async_queue( &server->listen_q );
1286 list_add_tail( &pipe->listeners, &server->entry );
1287 if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1289 release_object( server );
1290 return NULL;
1292 allow_fd_caching( server->pipe_end.fd );
1293 set_fd_signaled( server->pipe_end.fd, 1 );
1294 async_wake_up( &pipe->waiters, STATUS_SUCCESS );
1295 return server;
1298 static struct pipe_end *create_pipe_client( struct named_pipe *pipe, data_size_t buffer_size, unsigned int options )
1300 struct pipe_end *client;
1302 client = alloc_object( &pipe_client_ops );
1303 if (!client)
1304 return NULL;
1306 init_pipe_end( client, pipe, 0, buffer_size );
1307 client->state = FILE_PIPE_CONNECTED_STATE;
1308 client->client_pid = get_process_id( current->process );
1310 client->fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->obj, options );
1311 if (!client->fd)
1313 release_object( client );
1314 return NULL;
1316 allow_fd_caching( client->fd );
1317 set_fd_signaled( client->fd, 1 );
1319 return client;
1322 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1324 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1326 if (parent->ops != &named_pipe_device_ops)
1328 set_error( STATUS_OBJECT_NAME_INVALID );
1329 return 0;
1331 namespace_add( dev->pipes, name );
1332 name->parent = grab_object( parent );
1333 return 1;
1336 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1337 unsigned int sharing, unsigned int options )
1339 struct named_pipe *pipe = (struct named_pipe *)obj;
1340 struct pipe_server *server;
1341 struct pipe_end *client;
1342 unsigned int pipe_sharing;
1344 if (list_empty( &pipe->listeners ))
1346 set_error( STATUS_PIPE_NOT_AVAILABLE );
1347 return NULL;
1349 server = LIST_ENTRY( list_head( &pipe->listeners ), struct pipe_server, entry );
1351 pipe_sharing = pipe->sharing;
1352 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1353 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1355 set_error( STATUS_ACCESS_DENIED );
1356 return NULL;
1359 if ((client = create_pipe_client( pipe, pipe->outsize, options )))
1361 async_wake_up( &server->listen_q, STATUS_SUCCESS );
1362 server->pipe_end.state = FILE_PIPE_CONNECTED_STATE;
1363 server->pipe_end.connection = client;
1364 client->connection = &server->pipe_end;
1365 server->pipe_end.client_pid = client->client_pid;
1366 client->server_pid = server->pipe_end.server_pid;
1367 list_remove( &server->entry );
1369 return &client->obj;
1372 static void named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1374 struct named_pipe_device *device = get_fd_user( fd );
1376 switch(code)
1378 case FSCTL_PIPE_WAIT:
1380 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1381 data_size_t size = get_req_data_size();
1382 struct named_pipe *pipe;
1383 struct unicode_str name;
1384 timeout_t when;
1386 if (size < sizeof(*buffer) ||
1387 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1389 set_error( STATUS_INVALID_PARAMETER );
1390 return;
1392 name.str = buffer->Name;
1393 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1394 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return;
1396 if (list_empty( &pipe->listeners ))
1398 queue_async( &pipe->waiters, async );
1399 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1400 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1401 set_error( STATUS_PENDING );
1404 release_object( pipe );
1405 return;
1408 default:
1409 default_fd_ioctl( fd, code, async );
1414 DECL_HANDLER(create_named_pipe)
1416 struct named_pipe *pipe;
1417 struct pipe_server *server;
1418 struct unicode_str name;
1419 struct object *root;
1420 const struct security_descriptor *sd;
1421 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1423 if (!objattr) return;
1425 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1426 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1428 if (root) release_object( root );
1429 set_error( STATUS_INVALID_PARAMETER );
1430 return;
1433 if (!name.len) /* pipes need a root directory even without a name */
1435 if (!objattr->rootdir)
1437 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1438 return;
1440 if (!(root = get_handle_obj( current->process, objattr->rootdir, 0, NULL ))) return;
1443 switch (req->disposition)
1445 case FILE_OPEN:
1446 pipe = open_named_object( root, &named_pipe_ops, &name, objattr->attributes );
1447 break;
1448 case FILE_CREATE:
1449 case FILE_OPEN_IF:
1450 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1451 break;
1452 default:
1453 pipe = NULL;
1454 set_error( STATUS_INVALID_PARAMETER );
1455 break;
1458 if (root) release_object( root );
1459 if (!pipe) return;
1461 if (get_error() != STATUS_OBJECT_NAME_EXISTS && req->disposition != FILE_OPEN)
1463 /* initialize it if it didn't already exist */
1464 pipe->instances = 0;
1465 init_async_queue( &pipe->waiters );
1466 list_init( &pipe->listeners );
1467 pipe->insize = req->insize;
1468 pipe->outsize = req->outsize;
1469 pipe->maxinstances = req->maxinstances;
1470 pipe->timeout = req->timeout;
1471 pipe->message_mode = (req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) != 0;
1472 pipe->sharing = req->sharing;
1473 if (sd) default_set_sd( &pipe->obj, sd, OWNER_SECURITY_INFORMATION |
1474 GROUP_SECURITY_INFORMATION |
1475 DACL_SECURITY_INFORMATION |
1476 SACL_SECURITY_INFORMATION );
1477 reply->created = 1;
1479 else
1481 if (pipe->maxinstances <= pipe->instances)
1483 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1484 release_object( pipe );
1485 return;
1487 if (pipe->sharing != req->sharing || req->disposition == FILE_CREATE)
1489 set_error( STATUS_ACCESS_DENIED );
1490 release_object( pipe );
1491 return;
1493 clear_error(); /* clear the name collision */
1496 server = create_pipe_server( pipe, req->options, req->flags );
1497 if (server)
1499 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1500 pipe->instances++;
1501 release_object( server );
1504 release_object( pipe );
1507 DECL_HANDLER(set_named_pipe_info)
1509 struct pipe_end *pipe_end;
1511 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1512 FILE_WRITE_ATTRIBUTES, &pipe_server_ops );
1513 if (!pipe_end)
1515 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1516 return;
1518 clear_error();
1519 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1520 0, &pipe_client_ops );
1521 if (!pipe_end) return;
1524 if (!pipe_end->pipe)
1526 set_error( STATUS_PIPE_DISCONNECTED );
1528 else if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1529 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !pipe_end->pipe->message_mode))
1531 set_error( STATUS_INVALID_PARAMETER );
1533 else
1535 pipe_end->flags = req->flags;
1538 release_object( pipe_end );