winmm/tests: Test the mmioOpen() search path with various flags.
[wine.git] / server / named_pipe.c
blobff1fdbe3f074d1076703136df0b42e820bd59bb3
1 /*
2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2001 Mike McCormack
6 * Copyright 2016 Jacek Caban for CodeWeavers
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
24 #include "wine/port.h"
26 #include <assert.h>
27 #include <string.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <stdlib.h>
32 #include "ntstatus.h"
33 #define WIN32_NO_STATUS
34 #include "windef.h"
35 #include "winternl.h"
36 #include "winioctl.h"
38 #include "file.h"
39 #include "handle.h"
40 #include "thread.h"
41 #include "request.h"
42 #include "security.h"
43 #include "process.h"
45 struct named_pipe;
47 struct pipe_message
49 struct list entry; /* entry in message queue */
50 data_size_t read_pos; /* already read bytes */
51 struct iosb *iosb; /* message iosb */
52 struct async *async; /* async of pending write */
55 struct pipe_end
57 struct object obj; /* object header */
58 struct fd *fd; /* pipe file descriptor */
59 unsigned int flags; /* pipe flags */
60 unsigned int state; /* pipe state */
61 struct named_pipe *pipe;
62 struct pipe_end *connection; /* the other end of the pipe */
63 process_id_t client_pid; /* process that created the client */
64 process_id_t server_pid; /* process that created the server */
65 data_size_t buffer_size;/* size of buffered data that doesn't block caller */
66 struct list message_queue;
67 struct async_queue read_q; /* read queue */
68 struct async_queue write_q; /* write queue */
71 struct pipe_server
73 struct pipe_end pipe_end; /* common header for 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 named_pipe_dump, /* dump */
118 no_get_type, /* get_type */
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 int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos );
147 static int pipe_end_write( struct fd *fd, struct async *async_data, file_pos_t pos );
148 static int pipe_end_flush( struct fd *fd, struct async *async );
149 static void pipe_end_get_volume_info( struct fd *fd, 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 int 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 pipe_server_dump, /* dump */
166 file_get_type, /* get_type */
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_fd_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 fd_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 no_fd_queue_async, /* queue_async */
198 pipe_end_reselect_async /* reselect_async */
201 /* client end functions */
202 static void pipe_client_dump( struct object *obj, int verbose );
203 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
205 static const struct object_ops pipe_client_ops =
207 sizeof(struct pipe_end), /* size */
208 pipe_client_dump, /* dump */
209 file_get_type, /* get_type */
210 add_queue, /* add_queue */
211 remove_queue, /* remove_queue */
212 default_fd_signaled, /* signaled */
213 no_satisfied, /* satisfied */
214 no_signal, /* signal */
215 pipe_end_get_fd, /* get_fd */
216 default_fd_map_access, /* map_access */
217 pipe_end_get_sd, /* get_sd */
218 pipe_end_set_sd, /* set_sd */
219 pipe_end_get_full_name, /* get_full_name */
220 no_lookup_name, /* lookup_name */
221 no_link_name, /* link_name */
222 NULL, /* unlink_name */
223 no_open_file, /* open_file */
224 no_kernel_obj_list, /* get_kernel_obj_list */
225 fd_close_handle, /* close_handle */
226 pipe_end_destroy /* destroy */
229 static const struct fd_ops pipe_client_fd_ops =
231 default_fd_get_poll_events, /* get_poll_events */
232 default_poll_event, /* poll_event */
233 pipe_end_get_fd_type, /* get_fd_type */
234 pipe_end_read, /* read */
235 pipe_end_write, /* write */
236 pipe_end_flush, /* flush */
237 pipe_end_get_file_info, /* get_file_info */
238 pipe_end_get_volume_info, /* get_volume_info */
239 pipe_client_ioctl, /* ioctl */
240 no_fd_queue_async, /* queue_async */
241 pipe_end_reselect_async /* reselect_async */
244 static void named_pipe_device_dump( struct object *obj, int verbose );
245 static struct object_type *named_pipe_device_get_type( struct object *obj );
246 static struct object *named_pipe_device_lookup_name( struct object *obj,
247 struct unicode_str *name, unsigned int attr, struct object *root );
248 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
249 unsigned int sharing, unsigned int options );
250 static void named_pipe_device_destroy( struct object *obj );
252 static const struct object_ops named_pipe_device_ops =
254 sizeof(struct named_pipe_device), /* size */
255 named_pipe_device_dump, /* dump */
256 named_pipe_device_get_type, /* get_type */
257 no_add_queue, /* add_queue */
258 NULL, /* remove_queue */
259 NULL, /* signaled */
260 no_satisfied, /* satisfied */
261 no_signal, /* signal */
262 no_get_fd, /* get_fd */
263 no_map_access, /* map_access */
264 default_get_sd, /* get_sd */
265 default_set_sd, /* set_sd */
266 default_get_full_name, /* get_full_name */
267 named_pipe_device_lookup_name, /* lookup_name */
268 directory_link_name, /* link_name */
269 default_unlink_name, /* unlink_name */
270 named_pipe_device_open_file, /* open_file */
271 no_kernel_obj_list, /* get_kernel_obj_list */
272 no_close_handle, /* close_handle */
273 named_pipe_device_destroy /* destroy */
276 static void named_pipe_device_file_dump( struct object *obj, int verbose );
277 static struct fd *named_pipe_device_file_get_fd( struct object *obj );
278 static WCHAR *named_pipe_device_file_get_full_name( struct object *obj, data_size_t *len );
279 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
280 static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd );
281 static void named_pipe_device_file_destroy( struct object *obj );
283 static const struct object_ops named_pipe_device_file_ops =
285 sizeof(struct named_pipe_device_file), /* size */
286 named_pipe_device_file_dump, /* dump */
287 file_get_type, /* get_type */
288 add_queue, /* add_queue */
289 remove_queue, /* remove_queue */
290 default_fd_signaled, /* signaled */
291 no_satisfied, /* satisfied */
292 no_signal, /* signal */
293 named_pipe_device_file_get_fd, /* get_fd */
294 default_fd_map_access, /* map_access */
295 default_get_sd, /* get_sd */
296 default_set_sd, /* set_sd */
297 named_pipe_device_file_get_full_name, /* get_full_name */
298 no_lookup_name, /* lookup_name */
299 no_link_name, /* link_name */
300 NULL, /* unlink_name */
301 no_open_file, /* open_file */
302 no_kernel_obj_list, /* get_kernel_obj_list */
303 fd_close_handle, /* close_handle */
304 named_pipe_device_file_destroy /* destroy */
307 static const struct fd_ops named_pipe_device_fd_ops =
309 default_fd_get_poll_events, /* get_poll_events */
310 default_poll_event, /* poll_event */
311 named_pipe_device_file_get_fd_type, /* get_fd_type */
312 no_fd_read, /* read */
313 no_fd_write, /* write */
314 no_fd_flush, /* flush */
315 default_fd_get_file_info, /* get_file_info */
316 no_fd_get_volume_info, /* get_volume_info */
317 named_pipe_device_ioctl, /* ioctl */
318 default_fd_queue_async, /* queue_async */
319 default_fd_reselect_async /* reselect_async */
322 static void named_pipe_dump( struct object *obj, int verbose )
324 fputs( "Named pipe\n", stderr );
327 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
329 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
330 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
331 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
332 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
333 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
336 static WCHAR *named_pipe_get_full_name( struct object *obj, data_size_t *ret_len )
338 WCHAR *ret;
340 if (!(ret = default_get_full_name( obj, ret_len )))
341 set_error( STATUS_OBJECT_PATH_INVALID );
342 return ret;
345 static void pipe_server_dump( struct object *obj, int verbose )
347 struct pipe_server *server = (struct pipe_server *) obj;
348 assert( obj->ops == &pipe_server_ops );
349 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe_end.pipe,
350 server->pipe_end.state );
353 static void pipe_client_dump( struct object *obj, int verbose )
355 struct pipe_end *client = (struct pipe_end *) obj;
356 assert( obj->ops == &pipe_client_ops );
357 fprintf( stderr, "Named pipe client server=%p\n", client->connection );
360 static void named_pipe_destroy( struct object *obj)
362 struct named_pipe *pipe = (struct named_pipe *) obj;
364 assert( list_empty( &pipe->listeners ) );
365 assert( !pipe->instances );
366 free_async_queue( &pipe->waiters );
369 static struct fd *pipe_end_get_fd( struct object *obj )
371 struct pipe_end *pipe_end = (struct pipe_end *) obj;
372 return (struct fd *) grab_object( pipe_end->fd );
375 static struct pipe_message *queue_message( struct pipe_end *pipe_end, struct iosb *iosb )
377 struct pipe_message *message;
379 if (!(message = mem_alloc( sizeof(*message) ))) return NULL;
380 message->iosb = (struct iosb *)grab_object( iosb );
381 message->async = NULL;
382 message->read_pos = 0;
383 list_add_tail( &pipe_end->message_queue, &message->entry );
384 return message;
387 static void wake_message( struct pipe_message *message, data_size_t result )
389 struct async *async = message->async;
391 message->async = NULL;
392 if (!async) return;
394 message->iosb->status = STATUS_SUCCESS;
395 message->iosb->result = result;
396 async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
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_type *named_pipe_device_get_type( struct object *obj )
496 static const WCHAR name[] = {'D','e','v','i','c','e'};
497 static const struct unicode_str str = { name, sizeof(name) };
498 return get_object_type( &str );
501 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
502 unsigned int attr, struct object *root )
504 struct named_pipe_device *device = (struct named_pipe_device*)obj;
505 struct object *found;
507 assert( obj->ops == &named_pipe_device_ops );
508 assert( device->pipes );
510 if (!name) return NULL; /* open the device itself */
512 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
513 name->len = 0;
515 return found;
518 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
519 unsigned int sharing, unsigned int options )
521 struct named_pipe_device_file *file;
523 if (!(file = alloc_object( &named_pipe_device_file_ops ))) return NULL;
524 file->device = (struct named_pipe_device *)grab_object( obj );
525 if (!(file->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, obj, options )))
527 release_object( file );
528 return NULL;
530 allow_fd_caching( file->fd );
531 return &file->obj;
534 static void named_pipe_device_destroy( struct object *obj )
536 struct named_pipe_device *device = (struct named_pipe_device*)obj;
537 assert( obj->ops == &named_pipe_device_ops );
538 free( device->pipes );
541 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name,
542 unsigned int attr, const struct security_descriptor *sd )
544 struct named_pipe_device *dev;
546 if ((dev = create_named_object( root, &named_pipe_device_ops, name, attr, sd )) &&
547 get_error() != STATUS_OBJECT_NAME_EXISTS)
549 dev->pipes = NULL;
550 if (!(dev->pipes = create_namespace( 7 )))
552 release_object( dev );
553 dev = NULL;
556 return &dev->obj;
559 static void named_pipe_device_file_dump( struct object *obj, int verbose )
561 struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
563 fprintf( stderr, "File on named pipe device %p\n", file->device );
566 static struct fd *named_pipe_device_file_get_fd( struct object *obj )
568 struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
569 return (struct fd *)grab_object( file->fd );
572 static WCHAR *named_pipe_device_file_get_full_name( struct object *obj, data_size_t *len )
574 struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
575 return file->device->obj.ops->get_full_name( &file->device->obj, len );
578 static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd )
580 return FD_TYPE_DEVICE;
583 static void named_pipe_device_file_destroy( struct object *obj )
585 struct named_pipe_device_file *file = (struct named_pipe_device_file*)obj;
586 assert( obj->ops == &named_pipe_device_file_ops );
587 if (file->fd) release_object( file->fd );
588 release_object( file->device );
591 static int pipe_end_flush( struct fd *fd, struct async *async )
593 struct pipe_end *pipe_end = get_fd_user( fd );
595 if (!pipe_end->pipe)
597 set_error( STATUS_PIPE_DISCONNECTED );
598 return 0;
601 if (pipe_end->connection && !list_empty( &pipe_end->connection->message_queue ))
603 fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT );
604 set_error( STATUS_PENDING );
606 return 1;
609 static void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
611 struct pipe_end *pipe_end = get_fd_user( fd );
612 struct named_pipe *pipe = pipe_end->pipe;
614 switch (info_class)
616 case FileNameInformation:
618 FILE_NAME_INFORMATION *name_info;
619 data_size_t name_len, reply_size;
620 const WCHAR *name;
622 if (get_reply_max_size() < sizeof(*name_info))
624 set_error( STATUS_INFO_LENGTH_MISMATCH );
625 return;
628 /* FIXME: We should be able to return on unlinked pipe */
629 if (!pipe || !(name = get_object_name( &pipe->obj, &name_len )))
631 set_error( STATUS_PIPE_DISCONNECTED );
632 return;
635 reply_size = offsetof( FILE_NAME_INFORMATION, FileName[name_len/sizeof(WCHAR) + 1] );
636 if (reply_size > get_reply_max_size())
638 reply_size = get_reply_max_size();
639 set_error( STATUS_BUFFER_OVERFLOW );
642 if (!(name_info = set_reply_data_size( reply_size ))) return;
643 name_info->FileNameLength = name_len + sizeof(WCHAR);
644 name_info->FileName[0] = '\\';
645 reply_size -= offsetof( FILE_NAME_INFORMATION, FileName[1] );
646 if (reply_size) memcpy( &name_info->FileName[1], name, reply_size );
647 break;
649 case FilePipeInformation:
651 FILE_PIPE_INFORMATION *pipe_info;
653 if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
655 set_error( STATUS_ACCESS_DENIED );
656 return;
659 if (get_reply_max_size() < sizeof(*pipe_info))
661 set_error( STATUS_INFO_LENGTH_MISMATCH );
662 return;
665 if (!pipe)
667 set_error( STATUS_PIPE_DISCONNECTED );
668 return;
671 if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
672 pipe_info->ReadMode = (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
673 ? FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
674 pipe_info->CompletionMode = (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE)
675 ? FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
676 break;
678 case FilePipeLocalInformation:
680 FILE_PIPE_LOCAL_INFORMATION *pipe_info;
682 if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
684 set_error( STATUS_ACCESS_DENIED );
685 return;
688 if (get_reply_max_size() < sizeof(*pipe_info))
690 set_error( STATUS_INFO_LENGTH_MISMATCH );
691 return;
694 if (!pipe)
696 set_error( STATUS_PIPE_DISCONNECTED );
697 return;
700 if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
701 pipe_info->NamedPipeType = pipe->message_mode;
702 switch (pipe->sharing)
704 case FILE_SHARE_READ:
705 pipe_info->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
706 break;
707 case FILE_SHARE_WRITE:
708 pipe_info->NamedPipeConfiguration = FILE_PIPE_INBOUND;
709 break;
710 case FILE_SHARE_READ | FILE_SHARE_WRITE:
711 pipe_info->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
712 break;
714 pipe_info->MaximumInstances = pipe->maxinstances;
715 pipe_info->CurrentInstances = pipe->instances;
716 pipe_info->InboundQuota = pipe->insize;
717 pipe_info->ReadDataAvailable = 0; /* FIXME */
718 pipe_info->OutboundQuota = pipe->outsize;
719 pipe_info->WriteQuotaAvailable = 0; /* FIXME */
720 pipe_info->NamedPipeState = pipe_end->state;
721 pipe_info->NamedPipeEnd = pipe_end->obj.ops == &pipe_server_ops
722 ? FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
723 break;
725 default:
726 default_fd_get_file_info( fd, handle, info_class );
730 static struct security_descriptor *pipe_end_get_sd( struct object *obj )
732 struct pipe_end *pipe_end = (struct pipe_end *) obj;
733 if (pipe_end->pipe) return default_get_sd( &pipe_end->pipe->obj );
734 set_error( STATUS_PIPE_DISCONNECTED );
735 return NULL;
738 static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
739 unsigned int set_info )
741 struct pipe_end *pipe_end = (struct pipe_end *) obj;
742 if (pipe_end->pipe) return default_set_sd( &pipe_end->pipe->obj, sd, set_info );
743 set_error( STATUS_PIPE_DISCONNECTED );
744 return 0;
747 static WCHAR *pipe_end_get_full_name( struct object *obj, data_size_t *len )
749 struct pipe_end *pipe_end = (struct pipe_end *) obj;
750 return pipe_end->pipe->obj.ops->get_full_name( &pipe_end->pipe->obj, len );
753 static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class )
755 switch (info_class)
757 case FileFsDeviceInformation:
759 static const FILE_FS_DEVICE_INFORMATION device_info =
761 FILE_DEVICE_NAMED_PIPE,
762 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
764 if (get_reply_max_size() >= sizeof(device_info))
765 set_reply_data( &device_info, sizeof(device_info) );
766 else
767 set_error( STATUS_BUFFER_TOO_SMALL );
768 break;
770 default:
771 set_error( STATUS_NOT_IMPLEMENTED );
775 static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
777 struct pipe_message *message;
779 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
781 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
782 iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
783 iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
784 ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
786 else
788 data_size_t avail = 0;
789 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
791 avail += message->iosb->in_size - message->read_pos;
792 if (avail >= iosb->out_size) break;
794 iosb->out_size = min( iosb->out_size, avail );
795 iosb->status = STATUS_SUCCESS;
798 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
799 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
801 iosb->out_data = message->iosb->in_data;
802 message->iosb->in_data = NULL;
803 wake_message( message, message->iosb->in_size );
804 free_message( message );
806 else
808 data_size_t write_pos = 0, writing;
809 char *buf = NULL;
811 if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
813 iosb->out_size = 0;
814 iosb->status = STATUS_NO_MEMORY;
815 return;
820 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
821 writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
822 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
823 write_pos += writing;
824 message->read_pos += writing;
825 if (message->read_pos == message->iosb->in_size)
827 wake_message(message, message->iosb->in_size);
828 free_message(message);
830 } while (write_pos < iosb->out_size);
832 iosb->result = iosb->out_size;
835 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
836 * We're not interested in such reselect calls, so we ignore them. */
837 static int ignore_reselect;
839 static void reselect_write_queue( struct pipe_end *pipe_end );
841 static void reselect_read_queue( struct pipe_end *pipe_end, int reselect_write )
843 struct async *async;
844 struct iosb *iosb;
846 ignore_reselect = 1;
847 while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
849 iosb = async_get_iosb( async );
850 message_queue_read( pipe_end, iosb );
851 async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
852 release_object( async );
853 release_object( iosb );
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 int 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 0;
916 break;
917 case FILE_PIPE_DISCONNECTED_STATE:
918 set_error( STATUS_PIPE_DISCONNECTED );
919 return 0;
920 case FILE_PIPE_LISTENING_STATE:
921 set_error( STATUS_PIPE_LISTENING );
922 return 0;
923 case FILE_PIPE_CLOSING_STATE:
924 if (!list_empty( &pipe_end->message_queue )) break;
925 set_error( STATUS_PIPE_BROKEN );
926 return 0;
929 queue_async( &pipe_end->read_q, async );
930 reselect_read_queue( pipe_end, 0 );
931 set_error( STATUS_PENDING );
932 return 1;
935 static int pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
937 struct pipe_end *pipe_end = get_fd_user( fd );
938 struct pipe_message *message;
939 struct iosb *iosb;
941 switch (pipe_end->state)
943 case FILE_PIPE_CONNECTED_STATE:
944 break;
945 case FILE_PIPE_DISCONNECTED_STATE:
946 set_error( STATUS_PIPE_DISCONNECTED );
947 return 0;
948 case FILE_PIPE_LISTENING_STATE:
949 set_error( STATUS_PIPE_LISTENING );
950 return 0;
951 case FILE_PIPE_CLOSING_STATE:
952 set_error( STATUS_PIPE_CLOSING );
953 return 0;
956 if (!pipe_end->pipe->message_mode && !get_req_data_size()) return 1;
958 iosb = async_get_iosb( async );
959 message = queue_message( pipe_end->connection, iosb );
960 release_object( iosb );
961 if (!message) return 0;
963 message->async = (struct async *)grab_object( async );
964 queue_async( &pipe_end->write_q, async );
965 reselect_read_queue( pipe_end->connection, 1 );
966 set_error( STATUS_PENDING );
967 return 1;
970 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
972 struct pipe_end *pipe_end = get_fd_user( fd );
974 if (ignore_reselect) return;
976 if (&pipe_end->write_q == queue)
977 reselect_write_queue( pipe_end );
978 else if (&pipe_end->read_q == queue)
979 reselect_read_queue( pipe_end, 0 );
982 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
984 return FD_TYPE_PIPE;
987 static int pipe_end_peek( struct pipe_end *pipe_end )
989 unsigned reply_size = get_reply_max_size();
990 FILE_PIPE_PEEK_BUFFER *buffer;
991 struct pipe_message *message;
992 data_size_t avail = 0;
993 data_size_t message_length = 0;
995 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
997 set_error( STATUS_INFO_LENGTH_MISMATCH );
998 return 0;
1000 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
1002 switch (pipe_end->state)
1004 case FILE_PIPE_CONNECTED_STATE:
1005 break;
1006 case FILE_PIPE_CLOSING_STATE:
1007 if (!list_empty( &pipe_end->message_queue )) break;
1008 set_error( STATUS_PIPE_BROKEN );
1009 return 0;
1010 default:
1011 set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
1012 return 0;
1015 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
1016 avail += message->iosb->in_size - message->read_pos;
1017 reply_size = min( reply_size, avail );
1019 if (avail && pipe_end->pipe->message_mode)
1021 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
1022 message_length = message->iosb->in_size - message->read_pos;
1023 reply_size = min( reply_size, message_length );
1026 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return 0;
1027 buffer->NamedPipeState = pipe_end->state;
1028 buffer->ReadDataAvailable = avail;
1029 buffer->NumberOfMessages = 0; /* FIXME */
1030 buffer->MessageLength = message_length;
1032 if (reply_size)
1034 data_size_t write_pos = 0, writing;
1035 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
1037 writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
1038 memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
1039 writing );
1040 write_pos += writing;
1041 if (write_pos == reply_size) break;
1044 if (message_length > reply_size) set_error( STATUS_BUFFER_OVERFLOW );
1045 return 1;
1048 static int pipe_end_transceive( struct pipe_end *pipe_end, struct async *async )
1050 struct pipe_message *message;
1051 struct iosb *iosb;
1053 if (!pipe_end->connection)
1055 set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
1056 return 0;
1059 if (!(pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ))
1061 set_error( STATUS_INVALID_READ_MODE );
1062 return 0;
1065 /* not allowed if we already have read data buffered */
1066 if (!list_empty( &pipe_end->message_queue ))
1068 set_error( STATUS_PIPE_BUSY );
1069 return 0;
1072 iosb = async_get_iosb( async );
1073 /* ignore output buffer copy transferred because of METHOD_NEITHER */
1074 iosb->in_size -= iosb->out_size;
1075 /* transaction never blocks on write, so just queue a message without async */
1076 message = queue_message( pipe_end->connection, iosb );
1077 release_object( iosb );
1078 if (!message) return 0;
1079 reselect_read_queue( pipe_end->connection, 0 );
1081 queue_async( &pipe_end->read_q, async );
1082 reselect_read_queue( pipe_end, 0 );
1083 set_error( STATUS_PENDING );
1084 return 1;
1087 static int pipe_end_get_connection_attribute( struct pipe_end *pipe_end )
1089 const char *attr = get_req_data();
1090 data_size_t value_size, attr_size = get_req_data_size();
1091 void *value;
1093 if (attr_size == sizeof("ClientProcessId") && !memcmp( attr, "ClientProcessId", attr_size ))
1095 value = &pipe_end->client_pid;
1096 value_size = sizeof(pipe_end->client_pid);
1098 else if (attr_size == sizeof("ServerProcessId") && !memcmp( attr, "ServerProcessId", attr_size ))
1100 value = &pipe_end->server_pid;
1101 value_size = sizeof(pipe_end->server_pid);
1103 else
1105 set_error( STATUS_ILLEGAL_FUNCTION );
1106 return 0;
1109 if (get_reply_max_size() < value_size)
1111 set_error( STATUS_INFO_LENGTH_MISMATCH );
1112 return 0;
1115 set_reply_data( value, value_size );
1116 return 1;
1119 static int pipe_end_ioctl( struct pipe_end *pipe_end, ioctl_code_t code, struct async *async )
1121 switch(code)
1123 case FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE:
1124 return pipe_end_get_connection_attribute( pipe_end );
1126 case FSCTL_PIPE_PEEK:
1127 return pipe_end_peek( pipe_end );
1129 case FSCTL_PIPE_TRANSCEIVE:
1130 return pipe_end_transceive( pipe_end, async );
1132 default:
1133 return default_fd_ioctl( pipe_end->fd, code, async );
1137 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1139 struct pipe_server *server = get_fd_user( fd );
1141 switch(code)
1143 case FSCTL_PIPE_LISTEN:
1144 switch(server->pipe_end.state)
1146 case FILE_PIPE_LISTENING_STATE:
1147 break;
1148 case FILE_PIPE_DISCONNECTED_STATE:
1149 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1150 list_add_tail( &server->pipe_end.pipe->listeners, &server->entry );
1151 break;
1152 case FILE_PIPE_CONNECTED_STATE:
1153 set_error( STATUS_PIPE_CONNECTED );
1154 return 0;
1155 case FILE_PIPE_CLOSING_STATE:
1156 set_error( STATUS_PIPE_CLOSING );
1157 return 0;
1160 if (server->pipe_end.flags & NAMED_PIPE_NONBLOCKING_MODE)
1162 set_error( STATUS_PIPE_LISTENING );
1163 return 0;
1165 queue_async( &server->listen_q, async );
1166 async_wake_up( &server->pipe_end.pipe->waiters, STATUS_SUCCESS );
1167 set_error( STATUS_PENDING );
1168 return 1;
1170 case FSCTL_PIPE_DISCONNECT:
1171 switch(server->pipe_end.state)
1173 case FILE_PIPE_CONNECTED_STATE:
1174 /* dump the client connection - all data is lost */
1175 assert( server->pipe_end.connection );
1176 release_object( server->pipe_end.connection->pipe );
1177 server->pipe_end.connection->pipe = NULL;
1178 break;
1179 case FILE_PIPE_CLOSING_STATE:
1180 break;
1181 case FILE_PIPE_LISTENING_STATE:
1182 set_error( STATUS_PIPE_LISTENING );
1183 return 0;
1184 case FILE_PIPE_DISCONNECTED_STATE:
1185 set_error( STATUS_PIPE_DISCONNECTED );
1186 return 0;
1189 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1190 return 1;
1192 case FSCTL_PIPE_IMPERSONATE:
1193 if (current->process->token) /* FIXME: use the client token */
1195 struct token *token;
1196 if (!(token = token_duplicate( current->process->token, 0, SecurityImpersonation, NULL, NULL, 0, NULL, 0 )))
1197 return 0;
1198 if (current->token) release_object( current->token );
1199 current->token = token;
1201 return 1;
1203 default:
1204 return pipe_end_ioctl( &server->pipe_end, code, async );
1208 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1210 struct pipe_end *client = get_fd_user( fd );
1212 switch(code)
1214 case FSCTL_PIPE_LISTEN:
1215 set_error( STATUS_ILLEGAL_FUNCTION );
1216 return 0;
1218 default:
1219 return pipe_end_ioctl( client, code, async );
1223 static void init_pipe_end( struct pipe_end *pipe_end, struct named_pipe *pipe,
1224 unsigned int pipe_flags, data_size_t buffer_size )
1226 pipe_end->pipe = (struct named_pipe *)grab_object( pipe );
1227 pipe_end->fd = NULL;
1228 pipe_end->flags = pipe_flags;
1229 pipe_end->connection = NULL;
1230 pipe_end->buffer_size = buffer_size;
1231 init_async_queue( &pipe_end->read_q );
1232 init_async_queue( &pipe_end->write_q );
1233 list_init( &pipe_end->message_queue );
1236 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
1237 unsigned int pipe_flags )
1239 struct pipe_server *server;
1241 server = alloc_object( &pipe_server_ops );
1242 if (!server)
1243 return NULL;
1245 server->options = options;
1246 init_pipe_end( &server->pipe_end, pipe, pipe_flags, pipe->insize );
1247 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1248 server->pipe_end.server_pid = get_process_id( current->process );
1249 init_async_queue( &server->listen_q );
1251 list_add_tail( &pipe->listeners, &server->entry );
1252 if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1254 release_object( server );
1255 return NULL;
1257 allow_fd_caching( server->pipe_end.fd );
1258 set_fd_signaled( server->pipe_end.fd, 1 );
1259 async_wake_up( &pipe->waiters, STATUS_SUCCESS );
1260 return server;
1263 static struct pipe_end *create_pipe_client( struct named_pipe *pipe, data_size_t buffer_size, unsigned int options )
1265 struct pipe_end *client;
1267 client = alloc_object( &pipe_client_ops );
1268 if (!client)
1269 return NULL;
1271 init_pipe_end( client, pipe, 0, buffer_size );
1272 client->state = FILE_PIPE_CONNECTED_STATE;
1273 client->client_pid = get_process_id( current->process );
1275 client->fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->obj, options );
1276 if (!client->fd)
1278 release_object( client );
1279 return NULL;
1281 allow_fd_caching( client->fd );
1282 set_fd_signaled( client->fd, 1 );
1284 return client;
1287 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1289 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1291 if (parent->ops != &named_pipe_device_ops)
1293 set_error( STATUS_OBJECT_NAME_INVALID );
1294 return 0;
1296 namespace_add( dev->pipes, name );
1297 name->parent = grab_object( parent );
1298 return 1;
1301 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1302 unsigned int sharing, unsigned int options )
1304 struct named_pipe *pipe = (struct named_pipe *)obj;
1305 struct pipe_server *server;
1306 struct pipe_end *client;
1307 unsigned int pipe_sharing;
1309 if (list_empty( &pipe->listeners ))
1311 set_error( STATUS_PIPE_NOT_AVAILABLE );
1312 return NULL;
1314 server = LIST_ENTRY( list_head( &pipe->listeners ), struct pipe_server, entry );
1316 pipe_sharing = pipe->sharing;
1317 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1318 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1320 set_error( STATUS_ACCESS_DENIED );
1321 return NULL;
1324 if ((client = create_pipe_client( pipe, pipe->outsize, options )))
1326 async_wake_up( &server->listen_q, STATUS_SUCCESS );
1327 server->pipe_end.state = FILE_PIPE_CONNECTED_STATE;
1328 server->pipe_end.connection = client;
1329 client->connection = &server->pipe_end;
1330 server->pipe_end.client_pid = client->client_pid;
1331 client->server_pid = server->pipe_end.server_pid;
1332 list_remove( &server->entry );
1334 return &client->obj;
1337 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1339 struct named_pipe_device *device = get_fd_user( fd );
1341 switch(code)
1343 case FSCTL_PIPE_WAIT:
1345 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1346 data_size_t size = get_req_data_size();
1347 struct named_pipe *pipe;
1348 struct unicode_str name;
1349 timeout_t when;
1351 if (size < sizeof(*buffer) ||
1352 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1354 set_error( STATUS_INVALID_PARAMETER );
1355 return 0;
1357 name.str = buffer->Name;
1358 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1359 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;
1361 if (list_empty( &pipe->listeners ))
1363 queue_async( &pipe->waiters, async );
1364 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1365 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1366 set_error( STATUS_PENDING );
1369 release_object( pipe );
1370 return 1;
1373 default:
1374 return default_fd_ioctl( fd, code, async );
1379 DECL_HANDLER(create_named_pipe)
1381 struct named_pipe *pipe;
1382 struct pipe_server *server;
1383 struct unicode_str name;
1384 struct object *root;
1385 const struct security_descriptor *sd;
1386 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1388 if (!objattr) return;
1390 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1391 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1393 if (root) release_object( root );
1394 set_error( STATUS_INVALID_PARAMETER );
1395 return;
1398 if (!name.len) /* pipes need a root directory even without a name */
1400 if (!objattr->rootdir)
1402 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1403 return;
1405 if (!(root = get_handle_obj( current->process, objattr->rootdir, 0, NULL ))) return;
1408 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1410 if (root) release_object( root );
1411 if (!pipe) return;
1413 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1415 /* initialize it if it didn't already exist */
1416 pipe->instances = 0;
1417 init_async_queue( &pipe->waiters );
1418 list_init( &pipe->listeners );
1419 pipe->insize = req->insize;
1420 pipe->outsize = req->outsize;
1421 pipe->maxinstances = req->maxinstances;
1422 pipe->timeout = req->timeout;
1423 pipe->message_mode = (req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) != 0;
1424 pipe->sharing = req->sharing;
1425 if (sd) default_set_sd( &pipe->obj, sd, OWNER_SECURITY_INFORMATION |
1426 GROUP_SECURITY_INFORMATION |
1427 DACL_SECURITY_INFORMATION |
1428 SACL_SECURITY_INFORMATION );
1430 else
1432 if (pipe->maxinstances <= pipe->instances)
1434 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1435 release_object( pipe );
1436 return;
1438 if (pipe->sharing != req->sharing)
1440 set_error( STATUS_ACCESS_DENIED );
1441 release_object( pipe );
1442 return;
1444 clear_error(); /* clear the name collision */
1447 server = create_pipe_server( pipe, req->options, req->flags );
1448 if (server)
1450 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1451 pipe->instances++;
1452 release_object( server );
1455 release_object( pipe );
1458 DECL_HANDLER(set_named_pipe_info)
1460 struct pipe_end *pipe_end;
1462 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1463 FILE_WRITE_ATTRIBUTES, &pipe_server_ops );
1464 if (!pipe_end)
1466 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1467 return;
1469 clear_error();
1470 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1471 0, &pipe_client_ops );
1472 if (!pipe_end) return;
1475 if (!pipe_end->pipe)
1477 set_error( STATUS_PIPE_DISCONNECTED );
1479 else if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1480 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !pipe_end->pipe->message_mode))
1482 set_error( STATUS_INVALID_PARAMETER );
1484 else
1486 pipe_end->flags = req->flags;
1489 release_object( pipe_end );