msvcrt: Import cbrtf implementation from musl.
[wine.git] / server / named_pipe.c
blobdf8c7e3170c7526c1f3796b6ec2909a2d686cfe0
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 &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 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 int 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 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 &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 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 &file_type, /* type */
209 pipe_client_dump, /* dump */
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_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 no_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 *named_pipe_device_lookup_name( struct object *obj,
246 struct unicode_str *name, unsigned int attr, struct object *root );
247 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
248 unsigned int sharing, unsigned int options );
249 static void named_pipe_device_destroy( struct object *obj );
251 static const struct object_ops named_pipe_device_ops =
253 sizeof(struct named_pipe_device), /* size */
254 &device_type, /* type */
255 named_pipe_device_dump, /* dump */
256 no_add_queue, /* add_queue */
257 NULL, /* remove_queue */
258 NULL, /* signaled */
259 no_satisfied, /* satisfied */
260 no_signal, /* signal */
261 no_get_fd, /* get_fd */
262 default_map_access, /* map_access */
263 default_get_sd, /* get_sd */
264 default_set_sd, /* set_sd */
265 default_get_full_name, /* get_full_name */
266 named_pipe_device_lookup_name, /* lookup_name */
267 directory_link_name, /* link_name */
268 default_unlink_name, /* unlink_name */
269 named_pipe_device_open_file, /* open_file */
270 no_kernel_obj_list, /* get_kernel_obj_list */
271 no_close_handle, /* close_handle */
272 named_pipe_device_destroy /* destroy */
275 static void named_pipe_device_file_dump( struct object *obj, int verbose );
276 static struct fd *named_pipe_device_file_get_fd( struct object *obj );
277 static WCHAR *named_pipe_device_file_get_full_name( struct object *obj, data_size_t *len );
278 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
279 static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd );
280 static void named_pipe_device_file_destroy( struct object *obj );
282 static const struct object_ops named_pipe_device_file_ops =
284 sizeof(struct named_pipe_device_file), /* size */
285 &file_type, /* type */
286 named_pipe_device_file_dump, /* dump */
287 add_queue, /* add_queue */
288 remove_queue, /* remove_queue */
289 default_fd_signaled, /* signaled */
290 no_satisfied, /* satisfied */
291 no_signal, /* signal */
292 named_pipe_device_file_get_fd, /* get_fd */
293 default_map_access, /* map_access */
294 default_get_sd, /* get_sd */
295 default_set_sd, /* set_sd */
296 named_pipe_device_file_get_full_name, /* get_full_name */
297 no_lookup_name, /* lookup_name */
298 no_link_name, /* link_name */
299 NULL, /* unlink_name */
300 no_open_file, /* open_file */
301 no_kernel_obj_list, /* get_kernel_obj_list */
302 no_close_handle, /* close_handle */
303 named_pipe_device_file_destroy /* destroy */
306 static const struct fd_ops named_pipe_device_fd_ops =
308 default_fd_get_poll_events, /* get_poll_events */
309 default_poll_event, /* poll_event */
310 named_pipe_device_file_get_fd_type, /* get_fd_type */
311 no_fd_read, /* read */
312 no_fd_write, /* write */
313 no_fd_flush, /* flush */
314 default_fd_get_file_info, /* get_file_info */
315 no_fd_get_volume_info, /* get_volume_info */
316 named_pipe_device_ioctl, /* ioctl */
317 default_fd_queue_async, /* queue_async */
318 default_fd_reselect_async /* reselect_async */
321 static void named_pipe_dump( struct object *obj, int verbose )
323 fputs( "Named pipe\n", stderr );
326 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
328 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
329 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
330 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
331 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
332 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
335 static WCHAR *named_pipe_get_full_name( struct object *obj, data_size_t *ret_len )
337 WCHAR *ret;
339 if (!(ret = default_get_full_name( obj, ret_len )))
340 set_error( STATUS_OBJECT_PATH_INVALID );
341 return ret;
344 static void pipe_server_dump( struct object *obj, int verbose )
346 struct pipe_server *server = (struct pipe_server *) obj;
347 assert( obj->ops == &pipe_server_ops );
348 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe_end.pipe,
349 server->pipe_end.state );
352 static void pipe_client_dump( struct object *obj, int verbose )
354 struct pipe_end *client = (struct pipe_end *) obj;
355 assert( obj->ops == &pipe_client_ops );
356 fprintf( stderr, "Named pipe client server=%p\n", client->connection );
359 static void named_pipe_destroy( struct object *obj)
361 struct named_pipe *pipe = (struct named_pipe *) obj;
363 assert( list_empty( &pipe->listeners ) );
364 assert( !pipe->instances );
365 free_async_queue( &pipe->waiters );
368 static struct fd *pipe_end_get_fd( struct object *obj )
370 struct pipe_end *pipe_end = (struct pipe_end *) obj;
371 return (struct fd *) grab_object( pipe_end->fd );
374 static struct pipe_message *queue_message( struct pipe_end *pipe_end, struct iosb *iosb )
376 struct pipe_message *message;
378 if (!(message = mem_alloc( sizeof(*message) ))) return NULL;
379 message->iosb = (struct iosb *)grab_object( iosb );
380 message->async = NULL;
381 message->read_pos = 0;
382 list_add_tail( &pipe_end->message_queue, &message->entry );
383 return message;
386 static void wake_message( struct pipe_message *message, data_size_t result )
388 struct async *async = message->async;
390 message->async = NULL;
391 if (!async) return;
393 message->iosb->status = STATUS_SUCCESS;
394 message->iosb->result = result;
395 async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
396 release_object( async );
399 static void free_message( struct pipe_message *message )
401 list_remove( &message->entry );
402 if (message->iosb) release_object( message->iosb );
403 free( message );
406 static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
408 struct pipe_end *connection = pipe_end->connection;
409 struct pipe_message *message, *next;
410 struct async *async;
412 pipe_end->connection = NULL;
414 pipe_end->state = status == STATUS_PIPE_DISCONNECTED
415 ? FILE_PIPE_DISCONNECTED_STATE : FILE_PIPE_CLOSING_STATE;
416 fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
417 async_wake_up( &pipe_end->read_q, status );
418 LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
420 async = message->async;
421 if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
422 if (!async) continue;
423 async_terminate( async, status );
424 release_object( async );
426 if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
428 if (connection)
430 connection->connection = NULL;
431 pipe_end_disconnect( connection, status );
435 static void pipe_end_destroy( struct object *obj )
437 struct pipe_end *pipe_end = (struct pipe_end *)obj;
438 struct pipe_message *message;
440 pipe_end_disconnect( pipe_end, STATUS_PIPE_BROKEN );
442 while (!list_empty( &pipe_end->message_queue ))
444 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
445 assert( !message->async );
446 free_message( message );
449 free_async_queue( &pipe_end->read_q );
450 free_async_queue( &pipe_end->write_q );
451 if (pipe_end->fd) release_object( pipe_end->fd );
452 if (pipe_end->pipe) release_object( pipe_end->pipe );
455 static struct object *pipe_server_lookup_name( struct object *obj, struct unicode_str *name,
456 unsigned int attr, struct object *root )
458 if (name && name->len)
459 set_error( STATUS_OBJECT_NAME_INVALID );
461 return NULL;
464 static struct object *pipe_server_open_file( struct object *obj, unsigned int access,
465 unsigned int sharing, unsigned int options )
467 struct pipe_server *server = (struct pipe_server *)obj;
469 return server->pipe_end.pipe->obj.ops->open_file( &server->pipe_end.pipe->obj, access, sharing, options );
472 static void pipe_server_destroy( struct object *obj )
474 struct pipe_server *server = (struct pipe_server *)obj;
475 struct named_pipe *pipe = server->pipe_end.pipe;
477 assert( obj->ops == &pipe_server_ops );
479 assert( pipe->instances );
480 if (!--pipe->instances) unlink_named_object( &pipe->obj );
481 if (server->pipe_end.state == FILE_PIPE_LISTENING_STATE)
482 list_remove( &server->entry );
484 free_async_queue( &server->listen_q );
485 pipe_end_destroy( obj );
488 static void named_pipe_device_dump( struct object *obj, int verbose )
490 fputs( "Named pipe device\n", stderr );
493 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
494 unsigned int attr, struct object *root )
496 struct named_pipe_device *device = (struct named_pipe_device*)obj;
497 struct object *found;
499 assert( obj->ops == &named_pipe_device_ops );
500 assert( device->pipes );
502 if (!name) return NULL; /* open the device itself */
504 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
505 name->len = 0;
507 return found;
510 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
511 unsigned int sharing, unsigned int options )
513 struct named_pipe_device_file *file;
515 if (!(file = alloc_object( &named_pipe_device_file_ops ))) return NULL;
516 file->device = (struct named_pipe_device *)grab_object( obj );
517 if (!(file->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, obj, options )))
519 release_object( file );
520 return NULL;
522 allow_fd_caching( file->fd );
523 return &file->obj;
526 static void named_pipe_device_destroy( struct object *obj )
528 struct named_pipe_device *device = (struct named_pipe_device*)obj;
529 assert( obj->ops == &named_pipe_device_ops );
530 free( device->pipes );
533 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name,
534 unsigned int attr, const struct security_descriptor *sd )
536 struct named_pipe_device *dev;
538 if ((dev = create_named_object( root, &named_pipe_device_ops, name, attr, sd )) &&
539 get_error() != STATUS_OBJECT_NAME_EXISTS)
541 dev->pipes = NULL;
542 if (!(dev->pipes = create_namespace( 7 )))
544 release_object( dev );
545 dev = NULL;
548 return &dev->obj;
551 static void named_pipe_device_file_dump( struct object *obj, int verbose )
553 struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
555 fprintf( stderr, "File on named pipe device %p\n", file->device );
558 static struct fd *named_pipe_device_file_get_fd( struct object *obj )
560 struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
561 return (struct fd *)grab_object( file->fd );
564 static WCHAR *named_pipe_device_file_get_full_name( struct object *obj, data_size_t *len )
566 struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
567 return file->device->obj.ops->get_full_name( &file->device->obj, len );
570 static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd )
572 return FD_TYPE_DEVICE;
575 static void named_pipe_device_file_destroy( struct object *obj )
577 struct named_pipe_device_file *file = (struct named_pipe_device_file*)obj;
578 assert( obj->ops == &named_pipe_device_file_ops );
579 if (file->fd) release_object( file->fd );
580 release_object( file->device );
583 static int pipe_end_flush( struct fd *fd, struct async *async )
585 struct pipe_end *pipe_end = get_fd_user( fd );
587 if (!pipe_end->pipe)
589 set_error( STATUS_PIPE_DISCONNECTED );
590 return 0;
593 if (pipe_end->connection && !list_empty( &pipe_end->connection->message_queue ))
595 fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT );
596 set_error( STATUS_PENDING );
598 return 1;
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;
674 if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
676 set_error( STATUS_ACCESS_DENIED );
677 return;
680 if (get_reply_max_size() < sizeof(*pipe_info))
682 set_error( STATUS_INFO_LENGTH_MISMATCH );
683 return;
686 if (!pipe)
688 set_error( STATUS_PIPE_DISCONNECTED );
689 return;
692 if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
693 pipe_info->NamedPipeType = pipe->message_mode;
694 switch (pipe->sharing)
696 case FILE_SHARE_READ:
697 pipe_info->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
698 break;
699 case FILE_SHARE_WRITE:
700 pipe_info->NamedPipeConfiguration = FILE_PIPE_INBOUND;
701 break;
702 case FILE_SHARE_READ | FILE_SHARE_WRITE:
703 pipe_info->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
704 break;
706 pipe_info->MaximumInstances = pipe->maxinstances;
707 pipe_info->CurrentInstances = pipe->instances;
708 pipe_info->InboundQuota = pipe->insize;
709 pipe_info->ReadDataAvailable = 0; /* FIXME */
710 pipe_info->OutboundQuota = pipe->outsize;
711 pipe_info->WriteQuotaAvailable = 0; /* FIXME */
712 pipe_info->NamedPipeState = pipe_end->state;
713 pipe_info->NamedPipeEnd = pipe_end->obj.ops == &pipe_server_ops
714 ? FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
715 break;
717 default:
718 default_fd_get_file_info( fd, handle, info_class );
722 static struct security_descriptor *pipe_end_get_sd( struct object *obj )
724 struct pipe_end *pipe_end = (struct pipe_end *) obj;
725 if (pipe_end->pipe) return default_get_sd( &pipe_end->pipe->obj );
726 set_error( STATUS_PIPE_DISCONNECTED );
727 return NULL;
730 static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
731 unsigned int set_info )
733 struct pipe_end *pipe_end = (struct pipe_end *) obj;
734 if (pipe_end->pipe) return default_set_sd( &pipe_end->pipe->obj, sd, set_info );
735 set_error( STATUS_PIPE_DISCONNECTED );
736 return 0;
739 static WCHAR *pipe_end_get_full_name( struct object *obj, data_size_t *len )
741 struct pipe_end *pipe_end = (struct pipe_end *) obj;
742 return pipe_end->pipe->obj.ops->get_full_name( &pipe_end->pipe->obj, len );
745 static int pipe_end_get_volume_info( struct fd *fd, struct async *async, unsigned int info_class )
747 switch (info_class)
749 case FileFsDeviceInformation:
751 static const FILE_FS_DEVICE_INFORMATION device_info =
753 FILE_DEVICE_NAMED_PIPE,
754 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
756 if (get_reply_max_size() >= sizeof(device_info))
757 set_reply_data( &device_info, sizeof(device_info) );
758 else
759 set_error( STATUS_BUFFER_TOO_SMALL );
760 break;
762 default:
763 set_error( STATUS_NOT_IMPLEMENTED );
765 return 0;
768 static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
770 struct pipe_message *message;
772 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
774 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
775 iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
776 iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
777 ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
779 else
781 data_size_t avail = 0;
782 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
784 avail += message->iosb->in_size - message->read_pos;
785 if (avail >= iosb->out_size) break;
787 iosb->out_size = min( iosb->out_size, avail );
788 iosb->status = STATUS_SUCCESS;
791 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
792 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
794 iosb->out_data = message->iosb->in_data;
795 message->iosb->in_data = NULL;
796 wake_message( message, message->iosb->in_size );
797 free_message( message );
799 else
801 data_size_t write_pos = 0, writing;
802 char *buf = NULL;
804 if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
806 iosb->out_size = 0;
807 iosb->status = STATUS_NO_MEMORY;
808 return;
813 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
814 writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
815 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
816 write_pos += writing;
817 message->read_pos += writing;
818 if (message->read_pos == message->iosb->in_size)
820 wake_message(message, message->iosb->in_size);
821 free_message(message);
823 } while (write_pos < iosb->out_size);
825 iosb->result = iosb->out_size;
828 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
829 * We're not interested in such reselect calls, so we ignore them. */
830 static int ignore_reselect;
832 static void reselect_write_queue( struct pipe_end *pipe_end );
834 static void reselect_read_queue( struct pipe_end *pipe_end, int reselect_write )
836 struct async *async;
837 struct iosb *iosb;
839 ignore_reselect = 1;
840 while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
842 iosb = async_get_iosb( async );
843 message_queue_read( pipe_end, iosb );
844 async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
845 release_object( async );
846 release_object( iosb );
847 reselect_write = 1;
849 ignore_reselect = 0;
851 if (pipe_end->connection)
853 if (list_empty( &pipe_end->message_queue ))
854 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
855 else if (reselect_write)
856 reselect_write_queue( pipe_end->connection );
860 static void reselect_write_queue( struct pipe_end *pipe_end )
862 struct pipe_message *message, *next;
863 struct pipe_end *reader = pipe_end->connection;
864 data_size_t avail = 0;
866 if (!reader) return;
868 ignore_reselect = 1;
870 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
872 if (message->async && message->iosb->status != STATUS_PENDING)
874 release_object( message->async );
875 message->async = NULL;
876 free_message( message );
878 else
880 avail += message->iosb->in_size - message->read_pos;
881 if (message->async && (avail <= reader->buffer_size || !message->iosb->in_size))
883 wake_message( message, message->iosb->in_size );
885 else if (message->async && (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE))
887 wake_message( message, message->read_pos );
888 free_message( message );
893 ignore_reselect = 0;
894 reselect_read_queue( reader, 0 );
897 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
899 struct pipe_end *pipe_end = get_fd_user( fd );
901 switch (pipe_end->state)
903 case FILE_PIPE_CONNECTED_STATE:
904 if ((pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE) && list_empty( &pipe_end->message_queue ))
906 set_error( STATUS_PIPE_EMPTY );
907 return 0;
909 break;
910 case FILE_PIPE_DISCONNECTED_STATE:
911 set_error( STATUS_PIPE_DISCONNECTED );
912 return 0;
913 case FILE_PIPE_LISTENING_STATE:
914 set_error( STATUS_PIPE_LISTENING );
915 return 0;
916 case FILE_PIPE_CLOSING_STATE:
917 if (!list_empty( &pipe_end->message_queue )) break;
918 set_error( STATUS_PIPE_BROKEN );
919 return 0;
922 queue_async( &pipe_end->read_q, async );
923 reselect_read_queue( pipe_end, 0 );
924 set_error( STATUS_PENDING );
925 return 1;
928 static int pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
930 struct pipe_end *pipe_end = get_fd_user( fd );
931 struct pipe_message *message;
932 struct iosb *iosb;
934 switch (pipe_end->state)
936 case FILE_PIPE_CONNECTED_STATE:
937 break;
938 case FILE_PIPE_DISCONNECTED_STATE:
939 set_error( STATUS_PIPE_DISCONNECTED );
940 return 0;
941 case FILE_PIPE_LISTENING_STATE:
942 set_error( STATUS_PIPE_LISTENING );
943 return 0;
944 case FILE_PIPE_CLOSING_STATE:
945 set_error( STATUS_PIPE_CLOSING );
946 return 0;
949 if (!pipe_end->pipe->message_mode && !get_req_data_size()) return 1;
951 iosb = async_get_iosb( async );
952 message = queue_message( pipe_end->connection, iosb );
953 release_object( iosb );
954 if (!message) return 0;
956 message->async = (struct async *)grab_object( async );
957 queue_async( &pipe_end->write_q, async );
958 reselect_read_queue( pipe_end->connection, 1 );
959 set_error( STATUS_PENDING );
960 return 1;
963 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
965 struct pipe_end *pipe_end = get_fd_user( fd );
967 if (ignore_reselect) return;
969 if (&pipe_end->write_q == queue)
970 reselect_write_queue( pipe_end );
971 else if (&pipe_end->read_q == queue)
972 reselect_read_queue( pipe_end, 0 );
975 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
977 return FD_TYPE_PIPE;
980 static int pipe_end_peek( struct pipe_end *pipe_end )
982 unsigned reply_size = get_reply_max_size();
983 FILE_PIPE_PEEK_BUFFER *buffer;
984 struct pipe_message *message;
985 data_size_t avail = 0;
986 data_size_t message_length = 0;
988 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
990 set_error( STATUS_INFO_LENGTH_MISMATCH );
991 return 0;
993 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
995 switch (pipe_end->state)
997 case FILE_PIPE_CONNECTED_STATE:
998 break;
999 case FILE_PIPE_CLOSING_STATE:
1000 if (!list_empty( &pipe_end->message_queue )) break;
1001 set_error( STATUS_PIPE_BROKEN );
1002 return 0;
1003 default:
1004 set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
1005 return 0;
1008 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
1009 avail += message->iosb->in_size - message->read_pos;
1010 reply_size = min( reply_size, avail );
1012 if (avail && pipe_end->pipe->message_mode)
1014 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
1015 message_length = message->iosb->in_size - message->read_pos;
1016 reply_size = min( reply_size, message_length );
1019 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return 0;
1020 buffer->NamedPipeState = pipe_end->state;
1021 buffer->ReadDataAvailable = avail;
1022 buffer->NumberOfMessages = 0; /* FIXME */
1023 buffer->MessageLength = message_length;
1025 if (reply_size)
1027 data_size_t write_pos = 0, writing;
1028 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
1030 writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
1031 memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
1032 writing );
1033 write_pos += writing;
1034 if (write_pos == reply_size) break;
1037 if (message_length > reply_size) set_error( STATUS_BUFFER_OVERFLOW );
1038 return 1;
1041 static int pipe_end_transceive( struct pipe_end *pipe_end, struct async *async )
1043 struct pipe_message *message;
1044 struct iosb *iosb;
1046 if (!pipe_end->connection)
1048 set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
1049 return 0;
1052 if (!(pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ))
1054 set_error( STATUS_INVALID_READ_MODE );
1055 return 0;
1058 /* not allowed if we already have read data buffered */
1059 if (!list_empty( &pipe_end->message_queue ))
1061 set_error( STATUS_PIPE_BUSY );
1062 return 0;
1065 iosb = async_get_iosb( async );
1066 /* ignore output buffer copy transferred because of METHOD_NEITHER */
1067 iosb->in_size -= iosb->out_size;
1068 /* transaction never blocks on write, so just queue a message without async */
1069 message = queue_message( pipe_end->connection, iosb );
1070 release_object( iosb );
1071 if (!message) return 0;
1072 reselect_read_queue( pipe_end->connection, 0 );
1074 queue_async( &pipe_end->read_q, async );
1075 reselect_read_queue( pipe_end, 0 );
1076 set_error( STATUS_PENDING );
1077 return 1;
1080 static int pipe_end_get_connection_attribute( struct pipe_end *pipe_end )
1082 const char *attr = get_req_data();
1083 data_size_t value_size, attr_size = get_req_data_size();
1084 void *value;
1086 if (attr_size == sizeof("ClientProcessId") && !memcmp( attr, "ClientProcessId", attr_size ))
1088 value = &pipe_end->client_pid;
1089 value_size = sizeof(pipe_end->client_pid);
1091 else if (attr_size == sizeof("ServerProcessId") && !memcmp( attr, "ServerProcessId", attr_size ))
1093 value = &pipe_end->server_pid;
1094 value_size = sizeof(pipe_end->server_pid);
1096 else
1098 set_error( STATUS_ILLEGAL_FUNCTION );
1099 return 0;
1102 if (get_reply_max_size() < value_size)
1104 set_error( STATUS_INFO_LENGTH_MISMATCH );
1105 return 0;
1108 set_reply_data( value, value_size );
1109 return 1;
1112 static int pipe_end_ioctl( struct pipe_end *pipe_end, ioctl_code_t code, struct async *async )
1114 switch(code)
1116 case FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE:
1117 return pipe_end_get_connection_attribute( pipe_end );
1119 case FSCTL_PIPE_PEEK:
1120 return pipe_end_peek( pipe_end );
1122 case FSCTL_PIPE_TRANSCEIVE:
1123 return pipe_end_transceive( pipe_end, async );
1125 default:
1126 return default_fd_ioctl( pipe_end->fd, code, async );
1130 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1132 struct pipe_server *server = get_fd_user( fd );
1134 switch(code)
1136 case FSCTL_PIPE_LISTEN:
1137 switch(server->pipe_end.state)
1139 case FILE_PIPE_LISTENING_STATE:
1140 break;
1141 case FILE_PIPE_DISCONNECTED_STATE:
1142 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1143 list_add_tail( &server->pipe_end.pipe->listeners, &server->entry );
1144 break;
1145 case FILE_PIPE_CONNECTED_STATE:
1146 set_error( STATUS_PIPE_CONNECTED );
1147 return 0;
1148 case FILE_PIPE_CLOSING_STATE:
1149 set_error( STATUS_PIPE_CLOSING );
1150 return 0;
1153 if (server->pipe_end.flags & NAMED_PIPE_NONBLOCKING_MODE)
1155 set_error( STATUS_PIPE_LISTENING );
1156 return 0;
1158 queue_async( &server->listen_q, async );
1159 async_wake_up( &server->pipe_end.pipe->waiters, STATUS_SUCCESS );
1160 set_error( STATUS_PENDING );
1161 return 1;
1163 case FSCTL_PIPE_DISCONNECT:
1164 switch(server->pipe_end.state)
1166 case FILE_PIPE_CONNECTED_STATE:
1167 /* dump the client connection - all data is lost */
1168 assert( server->pipe_end.connection );
1169 release_object( server->pipe_end.connection->pipe );
1170 server->pipe_end.connection->pipe = NULL;
1171 break;
1172 case FILE_PIPE_CLOSING_STATE:
1173 break;
1174 case FILE_PIPE_LISTENING_STATE:
1175 set_error( STATUS_PIPE_LISTENING );
1176 return 0;
1177 case FILE_PIPE_DISCONNECTED_STATE:
1178 set_error( STATUS_PIPE_DISCONNECTED );
1179 return 0;
1182 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1183 return 1;
1185 case FSCTL_PIPE_IMPERSONATE:
1186 if (current->process->token) /* FIXME: use the client token */
1188 struct token *token;
1189 if (!(token = token_duplicate( current->process->token, 0, SecurityImpersonation, NULL, NULL, 0, NULL, 0 )))
1190 return 0;
1191 if (current->token) release_object( current->token );
1192 current->token = token;
1194 return 1;
1196 default:
1197 return pipe_end_ioctl( &server->pipe_end, code, async );
1201 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1203 struct pipe_end *client = get_fd_user( fd );
1205 switch(code)
1207 case FSCTL_PIPE_LISTEN:
1208 set_error( STATUS_ILLEGAL_FUNCTION );
1209 return 0;
1211 default:
1212 return pipe_end_ioctl( client, code, async );
1216 static void init_pipe_end( struct pipe_end *pipe_end, struct named_pipe *pipe,
1217 unsigned int pipe_flags, data_size_t buffer_size )
1219 pipe_end->pipe = (struct named_pipe *)grab_object( pipe );
1220 pipe_end->fd = NULL;
1221 pipe_end->flags = pipe_flags;
1222 pipe_end->connection = NULL;
1223 pipe_end->buffer_size = buffer_size;
1224 init_async_queue( &pipe_end->read_q );
1225 init_async_queue( &pipe_end->write_q );
1226 list_init( &pipe_end->message_queue );
1229 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
1230 unsigned int pipe_flags )
1232 struct pipe_server *server;
1234 server = alloc_object( &pipe_server_ops );
1235 if (!server)
1236 return NULL;
1238 server->options = options;
1239 init_pipe_end( &server->pipe_end, pipe, pipe_flags, pipe->insize );
1240 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1241 server->pipe_end.server_pid = get_process_id( current->process );
1242 init_async_queue( &server->listen_q );
1244 list_add_tail( &pipe->listeners, &server->entry );
1245 if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1247 release_object( server );
1248 return NULL;
1250 allow_fd_caching( server->pipe_end.fd );
1251 set_fd_signaled( server->pipe_end.fd, 1 );
1252 async_wake_up( &pipe->waiters, STATUS_SUCCESS );
1253 return server;
1256 static struct pipe_end *create_pipe_client( struct named_pipe *pipe, data_size_t buffer_size, unsigned int options )
1258 struct pipe_end *client;
1260 client = alloc_object( &pipe_client_ops );
1261 if (!client)
1262 return NULL;
1264 init_pipe_end( client, pipe, 0, buffer_size );
1265 client->state = FILE_PIPE_CONNECTED_STATE;
1266 client->client_pid = get_process_id( current->process );
1268 client->fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->obj, options );
1269 if (!client->fd)
1271 release_object( client );
1272 return NULL;
1274 allow_fd_caching( client->fd );
1275 set_fd_signaled( client->fd, 1 );
1277 return client;
1280 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1282 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1284 if (parent->ops != &named_pipe_device_ops)
1286 set_error( STATUS_OBJECT_NAME_INVALID );
1287 return 0;
1289 namespace_add( dev->pipes, name );
1290 name->parent = grab_object( parent );
1291 return 1;
1294 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1295 unsigned int sharing, unsigned int options )
1297 struct named_pipe *pipe = (struct named_pipe *)obj;
1298 struct pipe_server *server;
1299 struct pipe_end *client;
1300 unsigned int pipe_sharing;
1302 if (list_empty( &pipe->listeners ))
1304 set_error( STATUS_PIPE_NOT_AVAILABLE );
1305 return NULL;
1307 server = LIST_ENTRY( list_head( &pipe->listeners ), struct pipe_server, entry );
1309 pipe_sharing = pipe->sharing;
1310 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1311 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1313 set_error( STATUS_ACCESS_DENIED );
1314 return NULL;
1317 if ((client = create_pipe_client( pipe, pipe->outsize, options )))
1319 async_wake_up( &server->listen_q, STATUS_SUCCESS );
1320 server->pipe_end.state = FILE_PIPE_CONNECTED_STATE;
1321 server->pipe_end.connection = client;
1322 client->connection = &server->pipe_end;
1323 server->pipe_end.client_pid = client->client_pid;
1324 client->server_pid = server->pipe_end.server_pid;
1325 list_remove( &server->entry );
1327 return &client->obj;
1330 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1332 struct named_pipe_device *device = get_fd_user( fd );
1334 switch(code)
1336 case FSCTL_PIPE_WAIT:
1338 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1339 data_size_t size = get_req_data_size();
1340 struct named_pipe *pipe;
1341 struct unicode_str name;
1342 timeout_t when;
1344 if (size < sizeof(*buffer) ||
1345 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1347 set_error( STATUS_INVALID_PARAMETER );
1348 return 0;
1350 name.str = buffer->Name;
1351 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1352 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;
1354 if (list_empty( &pipe->listeners ))
1356 queue_async( &pipe->waiters, async );
1357 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1358 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1359 set_error( STATUS_PENDING );
1362 release_object( pipe );
1363 return 1;
1366 default:
1367 return default_fd_ioctl( fd, code, async );
1372 DECL_HANDLER(create_named_pipe)
1374 struct named_pipe *pipe;
1375 struct pipe_server *server;
1376 struct unicode_str name;
1377 struct object *root;
1378 const struct security_descriptor *sd;
1379 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1381 if (!objattr) return;
1383 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1384 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1386 if (root) release_object( root );
1387 set_error( STATUS_INVALID_PARAMETER );
1388 return;
1391 if (!name.len) /* pipes need a root directory even without a name */
1393 if (!objattr->rootdir)
1395 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1396 return;
1398 if (!(root = get_handle_obj( current->process, objattr->rootdir, 0, NULL ))) return;
1401 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1403 if (root) release_object( root );
1404 if (!pipe) return;
1406 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1408 /* initialize it if it didn't already exist */
1409 pipe->instances = 0;
1410 init_async_queue( &pipe->waiters );
1411 list_init( &pipe->listeners );
1412 pipe->insize = req->insize;
1413 pipe->outsize = req->outsize;
1414 pipe->maxinstances = req->maxinstances;
1415 pipe->timeout = req->timeout;
1416 pipe->message_mode = (req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) != 0;
1417 pipe->sharing = req->sharing;
1418 if (sd) default_set_sd( &pipe->obj, sd, OWNER_SECURITY_INFORMATION |
1419 GROUP_SECURITY_INFORMATION |
1420 DACL_SECURITY_INFORMATION |
1421 SACL_SECURITY_INFORMATION );
1423 else
1425 if (pipe->maxinstances <= pipe->instances)
1427 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1428 release_object( pipe );
1429 return;
1431 if (pipe->sharing != req->sharing)
1433 set_error( STATUS_ACCESS_DENIED );
1434 release_object( pipe );
1435 return;
1437 clear_error(); /* clear the name collision */
1440 server = create_pipe_server( pipe, req->options, req->flags );
1441 if (server)
1443 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1444 pipe->instances++;
1445 release_object( server );
1448 release_object( pipe );
1451 DECL_HANDLER(set_named_pipe_info)
1453 struct pipe_end *pipe_end;
1455 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1456 FILE_WRITE_ATTRIBUTES, &pipe_server_ops );
1457 if (!pipe_end)
1459 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1460 return;
1462 clear_error();
1463 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1464 0, &pipe_client_ops );
1465 if (!pipe_end) return;
1468 if (!pipe_end->pipe)
1470 set_error( STATUS_PIPE_DISCONNECTED );
1472 else if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1473 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !pipe_end->pipe->message_mode))
1475 set_error( STATUS_INVALID_PARAMETER );
1477 else
1479 pipe_end->flags = req->flags;
1482 release_object( pipe_end );