wined3d: Don't return the BO address offset if glMapBuffer() fails.
[wine.git] / server / named_pipe.c
blob1bbf7f17a3a12f9377acb19e2ca0b89140c0de5a
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;
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 void 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 );
767 static void message_queue_read( struct pipe_end *pipe_end, struct async *async )
769 struct iosb *iosb = async_get_iosb( async );
770 unsigned int status = STATUS_SUCCESS;
771 struct pipe_message *message;
772 data_size_t out_size;
774 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
776 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
777 out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
779 if (message->read_pos + out_size < message->iosb->in_size)
780 status = STATUS_BUFFER_OVERFLOW;
782 else
784 data_size_t avail = 0;
785 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
787 avail += message->iosb->in_size - message->read_pos;
788 if (avail >= iosb->out_size) break;
790 out_size = min( iosb->out_size, avail );
793 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
794 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
796 async_request_complete( async, status, out_size, out_size, message->iosb->in_data );
797 message->iosb->in_data = NULL;
798 wake_message( message, message->iosb->in_size );
799 free_message( message );
801 else
803 data_size_t write_pos = 0, writing;
804 char *buf = NULL;
806 if (out_size && !(buf = malloc( out_size )))
808 async_terminate( async, STATUS_NO_MEMORY );
809 release_object( iosb );
810 return;
815 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
816 writing = min( out_size - write_pos, message->iosb->in_size - message->read_pos );
817 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
818 write_pos += writing;
819 message->read_pos += writing;
820 if (message->read_pos == message->iosb->in_size)
822 wake_message(message, message->iosb->in_size);
823 free_message(message);
825 } while (write_pos < out_size);
827 async_request_complete( async, status, out_size, out_size, buf );
830 release_object( iosb );
833 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
834 * We're not interested in such reselect calls, so we ignore them. */
835 static int ignore_reselect;
837 static void reselect_write_queue( struct pipe_end *pipe_end );
839 static void reselect_read_queue( struct pipe_end *pipe_end, int reselect_write )
841 struct async *async;
843 ignore_reselect = 1;
844 while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
846 message_queue_read( pipe_end, async );
847 release_object( async );
848 reselect_write = 1;
850 ignore_reselect = 0;
852 if (pipe_end->connection)
854 if (list_empty( &pipe_end->message_queue ))
855 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
856 else if (reselect_write)
857 reselect_write_queue( pipe_end->connection );
861 static void reselect_write_queue( struct pipe_end *pipe_end )
863 struct pipe_message *message, *next;
864 struct pipe_end *reader = pipe_end->connection;
865 data_size_t avail = 0;
867 if (!reader) return;
869 ignore_reselect = 1;
871 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
873 if (message->async && message->iosb->status != STATUS_PENDING)
875 release_object( message->async );
876 message->async = NULL;
877 free_message( message );
879 else
881 avail += message->iosb->in_size - message->read_pos;
882 if (message->async && (avail <= reader->buffer_size || !message->iosb->in_size))
884 wake_message( message, message->iosb->in_size );
886 else if (message->async && (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE))
888 wake_message( message, message->read_pos );
889 free_message( message );
894 ignore_reselect = 0;
895 reselect_read_queue( reader, 0 );
898 static void pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
900 struct pipe_end *pipe_end = get_fd_user( fd );
902 switch (pipe_end->state)
904 case FILE_PIPE_CONNECTED_STATE:
905 if ((pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE) && list_empty( &pipe_end->message_queue ))
907 set_error( STATUS_PIPE_EMPTY );
908 return;
910 break;
911 case FILE_PIPE_DISCONNECTED_STATE:
912 set_error( STATUS_PIPE_DISCONNECTED );
913 return;
914 case FILE_PIPE_LISTENING_STATE:
915 set_error( STATUS_PIPE_LISTENING );
916 return;
917 case FILE_PIPE_CLOSING_STATE:
918 if (!list_empty( &pipe_end->message_queue )) break;
919 set_error( STATUS_PIPE_BROKEN );
920 return;
923 queue_async( &pipe_end->read_q, async );
924 reselect_read_queue( pipe_end, 0 );
925 set_error( STATUS_PENDING );
928 static void 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;
941 case FILE_PIPE_LISTENING_STATE:
942 set_error( STATUS_PIPE_LISTENING );
943 return;
944 case FILE_PIPE_CLOSING_STATE:
945 set_error( STATUS_PIPE_CLOSING );
946 return;
949 if (!pipe_end->pipe->message_mode && !get_req_data_size()) return;
951 iosb = async_get_iosb( async );
952 message = queue_message( pipe_end->connection, iosb );
953 release_object( iosb );
954 if (!message) return;
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 );
962 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
964 struct pipe_end *pipe_end = get_fd_user( fd );
966 if (ignore_reselect) return;
968 if (&pipe_end->write_q == queue)
969 reselect_write_queue( pipe_end );
970 else if (&pipe_end->read_q == queue)
971 reselect_read_queue( pipe_end, 0 );
974 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
976 return FD_TYPE_PIPE;
979 static void pipe_end_peek( struct pipe_end *pipe_end )
981 unsigned reply_size = get_reply_max_size();
982 FILE_PIPE_PEEK_BUFFER *buffer;
983 struct pipe_message *message;
984 data_size_t avail = 0;
985 data_size_t message_length = 0;
987 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
989 set_error( STATUS_INFO_LENGTH_MISMATCH );
990 return;
992 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
994 switch (pipe_end->state)
996 case FILE_PIPE_CONNECTED_STATE:
997 break;
998 case FILE_PIPE_CLOSING_STATE:
999 if (!list_empty( &pipe_end->message_queue )) break;
1000 set_error( STATUS_PIPE_BROKEN );
1001 return;
1002 default:
1003 set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
1004 return;
1007 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
1008 avail += message->iosb->in_size - message->read_pos;
1009 reply_size = min( reply_size, avail );
1011 if (avail && pipe_end->pipe->message_mode)
1013 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
1014 message_length = message->iosb->in_size - message->read_pos;
1015 reply_size = min( reply_size, message_length );
1018 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return;
1019 buffer->NamedPipeState = pipe_end->state;
1020 buffer->ReadDataAvailable = avail;
1021 buffer->NumberOfMessages = 0; /* FIXME */
1022 buffer->MessageLength = message_length;
1024 if (reply_size)
1026 data_size_t write_pos = 0, writing;
1027 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
1029 writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
1030 memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
1031 writing );
1032 write_pos += writing;
1033 if (write_pos == reply_size) break;
1036 if (message_length > reply_size) set_error( STATUS_BUFFER_OVERFLOW );
1039 static void pipe_end_transceive( struct pipe_end *pipe_end, struct async *async )
1041 struct pipe_message *message;
1042 struct iosb *iosb;
1044 if (!pipe_end->connection)
1046 set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
1047 return;
1050 if (!(pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ))
1052 set_error( STATUS_INVALID_READ_MODE );
1053 return;
1056 /* not allowed if we already have read data buffered */
1057 if (!list_empty( &pipe_end->message_queue ))
1059 set_error( STATUS_PIPE_BUSY );
1060 return;
1063 iosb = async_get_iosb( async );
1064 /* ignore output buffer copy transferred because of METHOD_NEITHER */
1065 iosb->in_size -= iosb->out_size;
1066 /* transaction never blocks on write, so just queue a message without async */
1067 message = queue_message( pipe_end->connection, iosb );
1068 release_object( iosb );
1069 if (!message) return;
1070 reselect_read_queue( pipe_end->connection, 0 );
1072 queue_async( &pipe_end->read_q, async );
1073 reselect_read_queue( pipe_end, 0 );
1074 set_error( STATUS_PENDING );
1077 static void pipe_end_get_connection_attribute( struct pipe_end *pipe_end )
1079 const char *attr = get_req_data();
1080 data_size_t value_size, attr_size = get_req_data_size();
1081 void *value;
1083 if (attr_size == sizeof("ClientProcessId") && !memcmp( attr, "ClientProcessId", attr_size ))
1085 value = &pipe_end->client_pid;
1086 value_size = sizeof(pipe_end->client_pid);
1088 else if (attr_size == sizeof("ServerProcessId") && !memcmp( attr, "ServerProcessId", attr_size ))
1090 value = &pipe_end->server_pid;
1091 value_size = sizeof(pipe_end->server_pid);
1093 else
1095 set_error( STATUS_ILLEGAL_FUNCTION );
1096 return;
1099 if (get_reply_max_size() < value_size)
1101 set_error( STATUS_INFO_LENGTH_MISMATCH );
1102 return;
1105 set_reply_data( value, value_size );
1108 static void pipe_end_ioctl( struct pipe_end *pipe_end, ioctl_code_t code, struct async *async )
1110 switch(code)
1112 case FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE:
1113 pipe_end_get_connection_attribute( pipe_end );
1114 break;
1116 case FSCTL_PIPE_PEEK:
1117 pipe_end_peek( pipe_end );
1118 break;
1120 case FSCTL_PIPE_TRANSCEIVE:
1121 pipe_end_transceive( pipe_end, async );
1122 break;
1124 default:
1125 default_fd_ioctl( pipe_end->fd, code, async );
1129 static void pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1131 struct pipe_server *server = get_fd_user( fd );
1133 switch(code)
1135 case FSCTL_PIPE_LISTEN:
1136 switch(server->pipe_end.state)
1138 case FILE_PIPE_LISTENING_STATE:
1139 break;
1140 case FILE_PIPE_DISCONNECTED_STATE:
1141 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1142 list_add_tail( &server->pipe_end.pipe->listeners, &server->entry );
1143 break;
1144 case FILE_PIPE_CONNECTED_STATE:
1145 set_error( STATUS_PIPE_CONNECTED );
1146 return;
1147 case FILE_PIPE_CLOSING_STATE:
1148 set_error( STATUS_PIPE_CLOSING );
1149 return;
1152 if (server->pipe_end.flags & NAMED_PIPE_NONBLOCKING_MODE)
1154 set_error( STATUS_PIPE_LISTENING );
1155 return;
1157 queue_async( &server->listen_q, async );
1158 async_wake_up( &server->pipe_end.pipe->waiters, STATUS_SUCCESS );
1159 set_error( STATUS_PENDING );
1160 return;
1162 case FSCTL_PIPE_DISCONNECT:
1163 switch(server->pipe_end.state)
1165 case FILE_PIPE_CONNECTED_STATE:
1166 /* dump the client connection - all data is lost */
1167 assert( server->pipe_end.connection );
1168 release_object( server->pipe_end.connection->pipe );
1169 server->pipe_end.connection->pipe = NULL;
1170 break;
1171 case FILE_PIPE_CLOSING_STATE:
1172 break;
1173 case FILE_PIPE_LISTENING_STATE:
1174 set_error( STATUS_PIPE_LISTENING );
1175 return;
1176 case FILE_PIPE_DISCONNECTED_STATE:
1177 set_error( STATUS_PIPE_DISCONNECTED );
1178 return;
1181 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1182 return;
1184 case FSCTL_PIPE_IMPERSONATE:
1185 if (current->process->token) /* FIXME: use the client token */
1187 struct token *token;
1188 if (!(token = token_duplicate( current->process->token, 0, SecurityImpersonation, NULL, NULL, 0, NULL, 0 )))
1189 return;
1190 if (current->token) release_object( current->token );
1191 current->token = token;
1193 return;
1195 default:
1196 pipe_end_ioctl( &server->pipe_end, code, async );
1200 static void pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1202 struct pipe_end *client = get_fd_user( fd );
1204 switch(code)
1206 case FSCTL_PIPE_LISTEN:
1207 set_error( STATUS_ILLEGAL_FUNCTION );
1208 return;
1210 default:
1211 pipe_end_ioctl( client, code, async );
1215 static void init_pipe_end( struct pipe_end *pipe_end, struct named_pipe *pipe,
1216 unsigned int pipe_flags, data_size_t buffer_size )
1218 pipe_end->pipe = (struct named_pipe *)grab_object( pipe );
1219 pipe_end->fd = NULL;
1220 pipe_end->flags = pipe_flags;
1221 pipe_end->connection = NULL;
1222 pipe_end->buffer_size = buffer_size;
1223 init_async_queue( &pipe_end->read_q );
1224 init_async_queue( &pipe_end->write_q );
1225 list_init( &pipe_end->message_queue );
1228 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
1229 unsigned int pipe_flags )
1231 struct pipe_server *server;
1233 server = alloc_object( &pipe_server_ops );
1234 if (!server)
1235 return NULL;
1237 server->options = options;
1238 init_pipe_end( &server->pipe_end, pipe, pipe_flags, pipe->insize );
1239 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1240 server->pipe_end.server_pid = get_process_id( current->process );
1241 init_async_queue( &server->listen_q );
1243 list_add_tail( &pipe->listeners, &server->entry );
1244 if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1246 release_object( server );
1247 return NULL;
1249 allow_fd_caching( server->pipe_end.fd );
1250 set_fd_signaled( server->pipe_end.fd, 1 );
1251 async_wake_up( &pipe->waiters, STATUS_SUCCESS );
1252 return server;
1255 static struct pipe_end *create_pipe_client( struct named_pipe *pipe, data_size_t buffer_size, unsigned int options )
1257 struct pipe_end *client;
1259 client = alloc_object( &pipe_client_ops );
1260 if (!client)
1261 return NULL;
1263 init_pipe_end( client, pipe, 0, buffer_size );
1264 client->state = FILE_PIPE_CONNECTED_STATE;
1265 client->client_pid = get_process_id( current->process );
1267 client->fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->obj, options );
1268 if (!client->fd)
1270 release_object( client );
1271 return NULL;
1273 allow_fd_caching( client->fd );
1274 set_fd_signaled( client->fd, 1 );
1276 return client;
1279 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1281 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1283 if (parent->ops != &named_pipe_device_ops)
1285 set_error( STATUS_OBJECT_NAME_INVALID );
1286 return 0;
1288 namespace_add( dev->pipes, name );
1289 name->parent = grab_object( parent );
1290 return 1;
1293 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1294 unsigned int sharing, unsigned int options )
1296 struct named_pipe *pipe = (struct named_pipe *)obj;
1297 struct pipe_server *server;
1298 struct pipe_end *client;
1299 unsigned int pipe_sharing;
1301 if (list_empty( &pipe->listeners ))
1303 set_error( STATUS_PIPE_NOT_AVAILABLE );
1304 return NULL;
1306 server = LIST_ENTRY( list_head( &pipe->listeners ), struct pipe_server, entry );
1308 pipe_sharing = pipe->sharing;
1309 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1310 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1312 set_error( STATUS_ACCESS_DENIED );
1313 return NULL;
1316 if ((client = create_pipe_client( pipe, pipe->outsize, options )))
1318 async_wake_up( &server->listen_q, STATUS_SUCCESS );
1319 server->pipe_end.state = FILE_PIPE_CONNECTED_STATE;
1320 server->pipe_end.connection = client;
1321 client->connection = &server->pipe_end;
1322 server->pipe_end.client_pid = client->client_pid;
1323 client->server_pid = server->pipe_end.server_pid;
1324 list_remove( &server->entry );
1326 return &client->obj;
1329 static void named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1331 struct named_pipe_device *device = get_fd_user( fd );
1333 switch(code)
1335 case FSCTL_PIPE_WAIT:
1337 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1338 data_size_t size = get_req_data_size();
1339 struct named_pipe *pipe;
1340 struct unicode_str name;
1341 timeout_t when;
1343 if (size < sizeof(*buffer) ||
1344 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1346 set_error( STATUS_INVALID_PARAMETER );
1347 return;
1349 name.str = buffer->Name;
1350 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1351 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return;
1353 if (list_empty( &pipe->listeners ))
1355 queue_async( &pipe->waiters, async );
1356 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1357 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1358 set_error( STATUS_PENDING );
1361 release_object( pipe );
1362 return;
1365 default:
1366 default_fd_ioctl( fd, code, async );
1371 DECL_HANDLER(create_named_pipe)
1373 struct named_pipe *pipe;
1374 struct pipe_server *server;
1375 struct unicode_str name;
1376 struct object *root;
1377 const struct security_descriptor *sd;
1378 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1380 if (!objattr) return;
1382 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1383 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1385 if (root) release_object( root );
1386 set_error( STATUS_INVALID_PARAMETER );
1387 return;
1390 if (!name.len) /* pipes need a root directory even without a name */
1392 if (!objattr->rootdir)
1394 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1395 return;
1397 if (!(root = get_handle_obj( current->process, objattr->rootdir, 0, NULL ))) return;
1400 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1402 if (root) release_object( root );
1403 if (!pipe) return;
1405 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1407 /* initialize it if it didn't already exist */
1408 pipe->instances = 0;
1409 init_async_queue( &pipe->waiters );
1410 list_init( &pipe->listeners );
1411 pipe->insize = req->insize;
1412 pipe->outsize = req->outsize;
1413 pipe->maxinstances = req->maxinstances;
1414 pipe->timeout = req->timeout;
1415 pipe->message_mode = (req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) != 0;
1416 pipe->sharing = req->sharing;
1417 if (sd) default_set_sd( &pipe->obj, sd, OWNER_SECURITY_INFORMATION |
1418 GROUP_SECURITY_INFORMATION |
1419 DACL_SECURITY_INFORMATION |
1420 SACL_SECURITY_INFORMATION );
1422 else
1424 if (pipe->maxinstances <= pipe->instances)
1426 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1427 release_object( pipe );
1428 return;
1430 if (pipe->sharing != req->sharing)
1432 set_error( STATUS_ACCESS_DENIED );
1433 release_object( pipe );
1434 return;
1436 clear_error(); /* clear the name collision */
1439 server = create_pipe_server( pipe, req->options, req->flags );
1440 if (server)
1442 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1443 pipe->instances++;
1444 release_object( server );
1447 release_object( pipe );
1450 DECL_HANDLER(set_named_pipe_info)
1452 struct pipe_end *pipe_end;
1454 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1455 FILE_WRITE_ATTRIBUTES, &pipe_server_ops );
1456 if (!pipe_end)
1458 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1459 return;
1461 clear_error();
1462 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1463 0, &pipe_client_ops );
1464 if (!pipe_end) return;
1467 if (!pipe_end->pipe)
1469 set_error( STATUS_PIPE_DISCONNECTED );
1471 else if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1472 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !pipe_end->pipe->message_mode))
1474 set_error( STATUS_INVALID_PARAMETER );
1476 else
1478 pipe_end->flags = req->flags;
1481 release_object( pipe_end );