ntdll: Add RtlDosPathNameToRelativeNtPathName_U.
[wine.git] / server / named_pipe.c
blob3e6cf09d4f2e04692d68856ea0e353a2d15ca530
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 no_close_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 no_close_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 void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
603 struct pipe_end *pipe_end = get_fd_user( fd );
604 struct named_pipe *pipe = pipe_end->pipe;
606 switch (info_class)
608 case FileNameInformation:
610 FILE_NAME_INFORMATION *name_info;
611 data_size_t name_len, reply_size;
612 const WCHAR *name;
614 if (get_reply_max_size() < sizeof(*name_info))
616 set_error( STATUS_INFO_LENGTH_MISMATCH );
617 return;
620 /* FIXME: We should be able to return on unlinked pipe */
621 if (!pipe || !(name = get_object_name( &pipe->obj, &name_len )))
623 set_error( STATUS_PIPE_DISCONNECTED );
624 return;
627 reply_size = offsetof( FILE_NAME_INFORMATION, FileName[name_len/sizeof(WCHAR) + 1] );
628 if (reply_size > get_reply_max_size())
630 reply_size = get_reply_max_size();
631 set_error( STATUS_BUFFER_OVERFLOW );
634 if (!(name_info = set_reply_data_size( reply_size ))) return;
635 name_info->FileNameLength = name_len + sizeof(WCHAR);
636 name_info->FileName[0] = '\\';
637 reply_size -= offsetof( FILE_NAME_INFORMATION, FileName[1] );
638 if (reply_size) memcpy( &name_info->FileName[1], name, reply_size );
639 break;
641 case FilePipeInformation:
643 FILE_PIPE_INFORMATION *pipe_info;
645 if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
647 set_error( STATUS_ACCESS_DENIED );
648 return;
651 if (get_reply_max_size() < sizeof(*pipe_info))
653 set_error( STATUS_INFO_LENGTH_MISMATCH );
654 return;
657 if (!pipe)
659 set_error( STATUS_PIPE_DISCONNECTED );
660 return;
663 if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
664 pipe_info->ReadMode = (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
665 ? FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
666 pipe_info->CompletionMode = (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE)
667 ? FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
668 break;
670 case FilePipeLocalInformation:
672 FILE_PIPE_LOCAL_INFORMATION *pipe_info;
673 struct pipe_message *message;
674 data_size_t avail = 0;
676 if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
678 set_error( STATUS_ACCESS_DENIED );
679 return;
682 if (get_reply_max_size() < sizeof(*pipe_info))
684 set_error( STATUS_INFO_LENGTH_MISMATCH );
685 return;
688 if (!pipe)
690 set_error( STATUS_PIPE_DISCONNECTED );
691 return;
694 if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
695 pipe_info->NamedPipeType = pipe->message_mode;
696 switch (pipe->sharing)
698 case FILE_SHARE_READ:
699 pipe_info->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
700 break;
701 case FILE_SHARE_WRITE:
702 pipe_info->NamedPipeConfiguration = FILE_PIPE_INBOUND;
703 break;
704 case FILE_SHARE_READ | FILE_SHARE_WRITE:
705 pipe_info->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
706 break;
708 pipe_info->MaximumInstances = pipe->maxinstances;
709 pipe_info->CurrentInstances = pipe->instances;
710 pipe_info->InboundQuota = pipe->insize;
712 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
713 avail += message->iosb->in_size - message->read_pos;
714 pipe_info->ReadDataAvailable = avail;
716 pipe_info->OutboundQuota = pipe->outsize;
717 pipe_info->WriteQuotaAvailable = 0; /* FIXME */
718 pipe_info->NamedPipeState = pipe_end->state;
719 pipe_info->NamedPipeEnd = pipe_end->obj.ops == &pipe_server_ops
720 ? FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
721 break;
723 default:
724 default_fd_get_file_info( fd, handle, info_class );
728 static struct security_descriptor *pipe_end_get_sd( struct object *obj )
730 struct pipe_end *pipe_end = (struct pipe_end *) obj;
731 if (pipe_end->pipe) return default_get_sd( &pipe_end->pipe->obj );
732 set_error( STATUS_PIPE_DISCONNECTED );
733 return NULL;
736 static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
737 unsigned int set_info )
739 struct pipe_end *pipe_end = (struct pipe_end *) obj;
740 if (pipe_end->pipe) return default_set_sd( &pipe_end->pipe->obj, sd, set_info );
741 set_error( STATUS_PIPE_DISCONNECTED );
742 return 0;
745 static WCHAR *pipe_end_get_full_name( struct object *obj, data_size_t *len )
747 struct pipe_end *pipe_end = (struct pipe_end *) obj;
748 return pipe_end->pipe->obj.ops->get_full_name( &pipe_end->pipe->obj, len );
751 static void pipe_end_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class )
753 switch (info_class)
755 case FileFsDeviceInformation:
757 static const FILE_FS_DEVICE_INFORMATION device_info =
759 FILE_DEVICE_NAMED_PIPE,
760 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
762 if (get_reply_max_size() >= sizeof(device_info))
763 set_reply_data( &device_info, sizeof(device_info) );
764 else
765 set_error( STATUS_BUFFER_TOO_SMALL );
766 break;
768 default:
769 set_error( STATUS_NOT_IMPLEMENTED );
773 static void message_queue_read( struct pipe_end *pipe_end, struct async *async )
775 struct iosb *iosb = async_get_iosb( async );
776 unsigned int status = STATUS_SUCCESS;
777 struct pipe_message *message;
778 data_size_t out_size;
780 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
782 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
783 out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
785 if (message->read_pos + out_size < message->iosb->in_size)
786 status = STATUS_BUFFER_OVERFLOW;
788 else
790 data_size_t avail = 0;
791 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
793 avail += message->iosb->in_size - message->read_pos;
794 if (avail >= iosb->out_size) break;
796 out_size = min( iosb->out_size, avail );
799 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
800 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
802 async_request_complete( async, status, out_size, out_size, message->iosb->in_data );
803 message->iosb->in_data = NULL;
804 wake_message( message, message->iosb->in_size );
805 free_message( message );
807 else
809 data_size_t write_pos = 0, writing;
810 char *buf = NULL;
812 if (out_size && !(buf = malloc( out_size )))
814 async_terminate( async, STATUS_NO_MEMORY );
815 release_object( iosb );
816 return;
821 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
822 writing = min( out_size - write_pos, message->iosb->in_size - message->read_pos );
823 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
824 write_pos += writing;
825 message->read_pos += writing;
826 if (message->read_pos == message->iosb->in_size)
828 wake_message(message, message->iosb->in_size);
829 free_message(message);
831 } while (write_pos < out_size);
833 async_request_complete( async, status, out_size, out_size, buf );
836 release_object( iosb );
839 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
840 * We're not interested in such reselect calls, so we ignore them. */
841 static int ignore_reselect;
843 static void reselect_write_queue( struct pipe_end *pipe_end );
845 static void reselect_read_queue( struct pipe_end *pipe_end, int reselect_write )
847 struct async *async;
849 ignore_reselect = 1;
850 while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
852 message_queue_read( pipe_end, async );
853 release_object( async );
854 reselect_write = 1;
856 ignore_reselect = 0;
858 if (pipe_end->connection)
860 if (list_empty( &pipe_end->message_queue ))
861 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
862 else if (reselect_write)
863 reselect_write_queue( pipe_end->connection );
867 static void reselect_write_queue( struct pipe_end *pipe_end )
869 struct pipe_message *message, *next;
870 struct pipe_end *reader = pipe_end->connection;
871 data_size_t avail = 0;
873 if (!reader) return;
875 ignore_reselect = 1;
877 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
879 if (message->async && message->iosb->status != STATUS_PENDING)
881 release_object( message->async );
882 message->async = NULL;
883 free_message( message );
885 else
887 avail += message->iosb->in_size - message->read_pos;
888 if (message->async && (avail <= reader->buffer_size || !message->iosb->in_size))
890 wake_message( message, message->iosb->in_size );
892 else if (message->async && (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE))
894 wake_message( message, message->read_pos );
895 free_message( message );
900 ignore_reselect = 0;
901 reselect_read_queue( reader, 0 );
904 static void pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
906 struct pipe_end *pipe_end = get_fd_user( fd );
908 switch (pipe_end->state)
910 case FILE_PIPE_CONNECTED_STATE:
911 if ((pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE) && list_empty( &pipe_end->message_queue ))
913 set_error( STATUS_PIPE_EMPTY );
914 return;
916 break;
917 case FILE_PIPE_DISCONNECTED_STATE:
918 set_error( STATUS_PIPE_DISCONNECTED );
919 return;
920 case FILE_PIPE_LISTENING_STATE:
921 set_error( STATUS_PIPE_LISTENING );
922 return;
923 case FILE_PIPE_CLOSING_STATE:
924 if (!list_empty( &pipe_end->message_queue )) break;
925 set_error( STATUS_PIPE_BROKEN );
926 return;
929 queue_async( &pipe_end->read_q, async );
930 reselect_read_queue( pipe_end, 0 );
931 set_error( STATUS_PENDING );
934 static void pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
936 struct pipe_end *pipe_end = get_fd_user( fd );
937 struct pipe_message *message;
938 struct iosb *iosb;
940 switch (pipe_end->state)
942 case FILE_PIPE_CONNECTED_STATE:
943 break;
944 case FILE_PIPE_DISCONNECTED_STATE:
945 set_error( STATUS_PIPE_DISCONNECTED );
946 return;
947 case FILE_PIPE_LISTENING_STATE:
948 set_error( STATUS_PIPE_LISTENING );
949 return;
950 case FILE_PIPE_CLOSING_STATE:
951 set_error( STATUS_PIPE_CLOSING );
952 return;
955 if (!pipe_end->pipe->message_mode && !get_req_data_size()) return;
957 iosb = async_get_iosb( async );
958 message = queue_message( pipe_end->connection, iosb );
959 release_object( iosb );
960 if (!message) return;
962 message->async = (struct async *)grab_object( async );
963 queue_async( &pipe_end->write_q, async );
964 reselect_read_queue( pipe_end->connection, 1 );
965 set_error( STATUS_PENDING );
968 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
970 struct pipe_end *pipe_end = get_fd_user( fd );
972 if (ignore_reselect) return;
974 if (&pipe_end->write_q == queue)
975 reselect_write_queue( pipe_end );
976 else if (&pipe_end->read_q == queue)
977 reselect_read_queue( pipe_end, 0 );
980 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
982 return FD_TYPE_PIPE;
985 static void pipe_end_peek( struct pipe_end *pipe_end )
987 unsigned reply_size = get_reply_max_size();
988 FILE_PIPE_PEEK_BUFFER *buffer;
989 struct pipe_message *message;
990 data_size_t avail = 0;
991 data_size_t message_length = 0;
993 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
995 set_error( STATUS_INFO_LENGTH_MISMATCH );
996 return;
998 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
1000 switch (pipe_end->state)
1002 case FILE_PIPE_CONNECTED_STATE:
1003 break;
1004 case FILE_PIPE_CLOSING_STATE:
1005 if (!list_empty( &pipe_end->message_queue )) break;
1006 set_error( STATUS_PIPE_BROKEN );
1007 return;
1008 default:
1009 set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
1010 return;
1013 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
1014 avail += message->iosb->in_size - message->read_pos;
1015 reply_size = min( reply_size, avail );
1017 if (avail && pipe_end->pipe->message_mode)
1019 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
1020 message_length = message->iosb->in_size - message->read_pos;
1021 reply_size = min( reply_size, message_length );
1024 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return;
1025 buffer->NamedPipeState = pipe_end->state;
1026 buffer->ReadDataAvailable = avail;
1027 buffer->NumberOfMessages = 0; /* FIXME */
1028 buffer->MessageLength = message_length;
1030 if (reply_size)
1032 data_size_t write_pos = 0, writing;
1033 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
1035 writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
1036 memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
1037 writing );
1038 write_pos += writing;
1039 if (write_pos == reply_size) break;
1042 if (message_length > reply_size) set_error( STATUS_BUFFER_OVERFLOW );
1045 static void pipe_end_transceive( struct pipe_end *pipe_end, struct async *async )
1047 struct pipe_message *message;
1048 struct iosb *iosb;
1050 if (!pipe_end->connection)
1052 set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
1053 return;
1056 if (!(pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ))
1058 set_error( STATUS_INVALID_READ_MODE );
1059 return;
1062 /* not allowed if we already have read data buffered */
1063 if (!list_empty( &pipe_end->message_queue ))
1065 set_error( STATUS_PIPE_BUSY );
1066 return;
1069 iosb = async_get_iosb( async );
1070 /* ignore output buffer copy transferred because of METHOD_NEITHER */
1071 iosb->in_size -= iosb->out_size;
1072 /* transaction never blocks on write, so just queue a message without async */
1073 message = queue_message( pipe_end->connection, iosb );
1074 release_object( iosb );
1075 if (!message) return;
1076 reselect_read_queue( pipe_end->connection, 0 );
1078 queue_async( &pipe_end->read_q, async );
1079 reselect_read_queue( pipe_end, 0 );
1080 set_error( STATUS_PENDING );
1083 static void pipe_end_get_connection_attribute( struct pipe_end *pipe_end )
1085 const char *attr = get_req_data();
1086 data_size_t value_size, attr_size = get_req_data_size();
1087 void *value;
1089 if (attr_size == sizeof("ClientProcessId") && !memcmp( attr, "ClientProcessId", attr_size ))
1091 value = &pipe_end->client_pid;
1092 value_size = sizeof(pipe_end->client_pid);
1094 else if (attr_size == sizeof("ServerProcessId") && !memcmp( attr, "ServerProcessId", attr_size ))
1096 value = &pipe_end->server_pid;
1097 value_size = sizeof(pipe_end->server_pid);
1099 else
1101 set_error( STATUS_ILLEGAL_FUNCTION );
1102 return;
1105 if (get_reply_max_size() < value_size)
1107 set_error( STATUS_INFO_LENGTH_MISMATCH );
1108 return;
1111 set_reply_data( value, value_size );
1114 static void pipe_end_ioctl( struct pipe_end *pipe_end, ioctl_code_t code, struct async *async )
1116 switch(code)
1118 case FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE:
1119 pipe_end_get_connection_attribute( pipe_end );
1120 break;
1122 case FSCTL_PIPE_PEEK:
1123 pipe_end_peek( pipe_end );
1124 break;
1126 case FSCTL_PIPE_TRANSCEIVE:
1127 pipe_end_transceive( pipe_end, async );
1128 break;
1130 default:
1131 default_fd_ioctl( pipe_end->fd, code, async );
1135 static void pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1137 struct pipe_server *server = get_fd_user( fd );
1139 switch(code)
1141 case FSCTL_PIPE_LISTEN:
1142 switch(server->pipe_end.state)
1144 case FILE_PIPE_LISTENING_STATE:
1145 break;
1146 case FILE_PIPE_DISCONNECTED_STATE:
1147 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1148 list_add_tail( &server->pipe_end.pipe->listeners, &server->entry );
1149 break;
1150 case FILE_PIPE_CONNECTED_STATE:
1151 set_error( STATUS_PIPE_CONNECTED );
1152 return;
1153 case FILE_PIPE_CLOSING_STATE:
1154 set_error( STATUS_PIPE_CLOSING );
1155 return;
1158 if (server->pipe_end.flags & NAMED_PIPE_NONBLOCKING_MODE)
1160 set_error( STATUS_PIPE_LISTENING );
1161 return;
1163 queue_async( &server->listen_q, async );
1164 async_wake_up( &server->pipe_end.pipe->waiters, STATUS_SUCCESS );
1165 set_error( STATUS_PENDING );
1166 return;
1168 case FSCTL_PIPE_DISCONNECT:
1169 switch(server->pipe_end.state)
1171 case FILE_PIPE_CONNECTED_STATE:
1172 /* dump the client connection - all data is lost */
1173 assert( server->pipe_end.connection );
1174 release_object( server->pipe_end.connection->pipe );
1175 server->pipe_end.connection->pipe = NULL;
1176 break;
1177 case FILE_PIPE_CLOSING_STATE:
1178 break;
1179 case FILE_PIPE_LISTENING_STATE:
1180 set_error( STATUS_PIPE_LISTENING );
1181 return;
1182 case FILE_PIPE_DISCONNECTED_STATE:
1183 set_error( STATUS_PIPE_DISCONNECTED );
1184 return;
1187 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1188 return;
1190 case FSCTL_PIPE_IMPERSONATE:
1191 if (current->process->token) /* FIXME: use the client token */
1193 struct token *token;
1194 if (!(token = token_duplicate( current->process->token, 0, SecurityImpersonation, NULL, NULL, 0, NULL, 0 )))
1195 return;
1196 if (current->token) release_object( current->token );
1197 current->token = token;
1199 return;
1201 default:
1202 pipe_end_ioctl( &server->pipe_end, code, async );
1206 static void pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1208 struct pipe_end *client = get_fd_user( fd );
1210 switch(code)
1212 case FSCTL_PIPE_LISTEN:
1213 set_error( STATUS_ILLEGAL_FUNCTION );
1214 return;
1216 default:
1217 pipe_end_ioctl( client, code, async );
1221 static void init_pipe_end( struct pipe_end *pipe_end, struct named_pipe *pipe,
1222 unsigned int pipe_flags, data_size_t buffer_size )
1224 pipe_end->pipe = (struct named_pipe *)grab_object( pipe );
1225 pipe_end->fd = NULL;
1226 pipe_end->flags = pipe_flags;
1227 pipe_end->connection = NULL;
1228 pipe_end->buffer_size = buffer_size;
1229 init_async_queue( &pipe_end->read_q );
1230 init_async_queue( &pipe_end->write_q );
1231 list_init( &pipe_end->message_queue );
1234 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
1235 unsigned int pipe_flags )
1237 struct pipe_server *server;
1239 server = alloc_object( &pipe_server_ops );
1240 if (!server)
1241 return NULL;
1243 server->options = options;
1244 init_pipe_end( &server->pipe_end, pipe, pipe_flags, pipe->insize );
1245 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1246 server->pipe_end.server_pid = get_process_id( current->process );
1247 init_async_queue( &server->listen_q );
1249 list_add_tail( &pipe->listeners, &server->entry );
1250 if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1252 release_object( server );
1253 return NULL;
1255 allow_fd_caching( server->pipe_end.fd );
1256 set_fd_signaled( server->pipe_end.fd, 1 );
1257 async_wake_up( &pipe->waiters, STATUS_SUCCESS );
1258 return server;
1261 static struct pipe_end *create_pipe_client( struct named_pipe *pipe, data_size_t buffer_size, unsigned int options )
1263 struct pipe_end *client;
1265 client = alloc_object( &pipe_client_ops );
1266 if (!client)
1267 return NULL;
1269 init_pipe_end( client, pipe, 0, buffer_size );
1270 client->state = FILE_PIPE_CONNECTED_STATE;
1271 client->client_pid = get_process_id( current->process );
1273 client->fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->obj, options );
1274 if (!client->fd)
1276 release_object( client );
1277 return NULL;
1279 allow_fd_caching( client->fd );
1280 set_fd_signaled( client->fd, 1 );
1282 return client;
1285 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1287 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1289 if (parent->ops != &named_pipe_device_ops)
1291 set_error( STATUS_OBJECT_NAME_INVALID );
1292 return 0;
1294 namespace_add( dev->pipes, name );
1295 name->parent = grab_object( parent );
1296 return 1;
1299 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1300 unsigned int sharing, unsigned int options )
1302 struct named_pipe *pipe = (struct named_pipe *)obj;
1303 struct pipe_server *server;
1304 struct pipe_end *client;
1305 unsigned int pipe_sharing;
1307 if (list_empty( &pipe->listeners ))
1309 set_error( STATUS_PIPE_NOT_AVAILABLE );
1310 return NULL;
1312 server = LIST_ENTRY( list_head( &pipe->listeners ), struct pipe_server, entry );
1314 pipe_sharing = pipe->sharing;
1315 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1316 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1318 set_error( STATUS_ACCESS_DENIED );
1319 return NULL;
1322 if ((client = create_pipe_client( pipe, pipe->outsize, options )))
1324 async_wake_up( &server->listen_q, STATUS_SUCCESS );
1325 server->pipe_end.state = FILE_PIPE_CONNECTED_STATE;
1326 server->pipe_end.connection = client;
1327 client->connection = &server->pipe_end;
1328 server->pipe_end.client_pid = client->client_pid;
1329 client->server_pid = server->pipe_end.server_pid;
1330 list_remove( &server->entry );
1332 return &client->obj;
1335 static void named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1337 struct named_pipe_device *device = get_fd_user( fd );
1339 switch(code)
1341 case FSCTL_PIPE_WAIT:
1343 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1344 data_size_t size = get_req_data_size();
1345 struct named_pipe *pipe;
1346 struct unicode_str name;
1347 timeout_t when;
1349 if (size < sizeof(*buffer) ||
1350 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1352 set_error( STATUS_INVALID_PARAMETER );
1353 return;
1355 name.str = buffer->Name;
1356 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1357 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return;
1359 if (list_empty( &pipe->listeners ))
1361 queue_async( &pipe->waiters, async );
1362 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1363 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1364 set_error( STATUS_PENDING );
1367 release_object( pipe );
1368 return;
1371 default:
1372 default_fd_ioctl( fd, code, async );
1377 DECL_HANDLER(create_named_pipe)
1379 struct named_pipe *pipe;
1380 struct pipe_server *server;
1381 struct unicode_str name;
1382 struct object *root;
1383 const struct security_descriptor *sd;
1384 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1386 if (!objattr) return;
1388 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1389 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1391 if (root) release_object( root );
1392 set_error( STATUS_INVALID_PARAMETER );
1393 return;
1396 if (!name.len) /* pipes need a root directory even without a name */
1398 if (!objattr->rootdir)
1400 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1401 return;
1403 if (!(root = get_handle_obj( current->process, objattr->rootdir, 0, NULL ))) return;
1406 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1408 if (root) release_object( root );
1409 if (!pipe) return;
1411 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1413 /* initialize it if it didn't already exist */
1414 pipe->instances = 0;
1415 init_async_queue( &pipe->waiters );
1416 list_init( &pipe->listeners );
1417 pipe->insize = req->insize;
1418 pipe->outsize = req->outsize;
1419 pipe->maxinstances = req->maxinstances;
1420 pipe->timeout = req->timeout;
1421 pipe->message_mode = (req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) != 0;
1422 pipe->sharing = req->sharing;
1423 if (sd) default_set_sd( &pipe->obj, sd, OWNER_SECURITY_INFORMATION |
1424 GROUP_SECURITY_INFORMATION |
1425 DACL_SECURITY_INFORMATION |
1426 SACL_SECURITY_INFORMATION );
1428 else
1430 if (pipe->maxinstances <= pipe->instances)
1432 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1433 release_object( pipe );
1434 return;
1436 if (pipe->sharing != req->sharing)
1438 set_error( STATUS_ACCESS_DENIED );
1439 release_object( pipe );
1440 return;
1442 clear_error(); /* clear the name collision */
1445 server = create_pipe_server( pipe, req->options, req->flags );
1446 if (server)
1448 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1449 pipe->instances++;
1450 release_object( server );
1453 release_object( pipe );
1456 DECL_HANDLER(set_named_pipe_info)
1458 struct pipe_end *pipe_end;
1460 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1461 FILE_WRITE_ATTRIBUTES, &pipe_server_ops );
1462 if (!pipe_end)
1464 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1465 return;
1467 clear_error();
1468 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1469 0, &pipe_client_ops );
1470 if (!pipe_end) return;
1473 if (!pipe_end->pipe)
1475 set_error( STATUS_PIPE_DISCONNECTED );
1477 else if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1478 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !pipe_end->pipe->message_mode))
1480 set_error( STATUS_INVALID_PARAMETER );
1482 else
1484 pipe_end->flags = req->flags;
1487 release_object( pipe_end );