msidb: Permit specifying tables to import by filename.
[wine.git] / server / named_pipe.c
bloba6ec8ae06a0ff6349d6a0ed36f44a0e71692a521
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 int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent );
109 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
110 unsigned int sharing, unsigned int options );
111 static void named_pipe_destroy( struct object *obj );
113 static const struct object_ops named_pipe_ops =
115 sizeof(struct named_pipe), /* size */
116 named_pipe_dump, /* dump */
117 no_get_type, /* get_type */
118 no_add_queue, /* add_queue */
119 NULL, /* remove_queue */
120 NULL, /* signaled */
121 NULL, /* satisfied */
122 no_signal, /* signal */
123 no_get_fd, /* get_fd */
124 named_pipe_map_access, /* map_access */
125 default_get_sd, /* get_sd */
126 default_set_sd, /* set_sd */
127 no_lookup_name, /* lookup_name */
128 named_pipe_link_name, /* link_name */
129 default_unlink_name, /* unlink_name */
130 named_pipe_open_file, /* open_file */
131 no_close_handle, /* close_handle */
132 named_pipe_destroy /* destroy */
135 /* common server and client pipe end functions */
136 static void pipe_end_destroy( struct object *obj );
137 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd );
138 static struct fd *pipe_end_get_fd( struct object *obj );
139 static struct security_descriptor *pipe_end_get_sd( struct object *obj );
140 static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
141 unsigned int set_info );
142 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos );
143 static int pipe_end_write( struct fd *fd, struct async *async_data, file_pos_t pos );
144 static int pipe_end_flush( struct fd *fd, struct async *async );
145 static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class );
146 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue );
147 static void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class );
149 /* server end functions */
150 static void pipe_server_dump( struct object *obj, int verbose );
151 static void pipe_server_destroy( struct object *obj);
152 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
154 static const struct object_ops pipe_server_ops =
156 sizeof(struct pipe_server), /* size */
157 pipe_server_dump, /* dump */
158 file_get_type, /* get_type */
159 add_queue, /* add_queue */
160 remove_queue, /* remove_queue */
161 default_fd_signaled, /* signaled */
162 no_satisfied, /* satisfied */
163 no_signal, /* signal */
164 pipe_end_get_fd, /* get_fd */
165 default_fd_map_access, /* map_access */
166 pipe_end_get_sd, /* get_sd */
167 pipe_end_set_sd, /* set_sd */
168 no_lookup_name, /* lookup_name */
169 no_link_name, /* link_name */
170 NULL, /* unlink_name */
171 no_open_file, /* open_file */
172 fd_close_handle, /* close_handle */
173 pipe_server_destroy /* destroy */
176 static const struct fd_ops pipe_server_fd_ops =
178 default_fd_get_poll_events, /* get_poll_events */
179 default_poll_event, /* poll_event */
180 pipe_end_get_fd_type, /* get_fd_type */
181 pipe_end_read, /* read */
182 pipe_end_write, /* write */
183 pipe_end_flush, /* flush */
184 pipe_end_get_file_info, /* get_file_info */
185 pipe_end_get_volume_info, /* get_volume_info */
186 pipe_server_ioctl, /* ioctl */
187 no_fd_queue_async, /* queue_async */
188 pipe_end_reselect_async /* reselect_async */
191 /* client end functions */
192 static void pipe_client_dump( struct object *obj, int verbose );
193 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
195 static const struct object_ops pipe_client_ops =
197 sizeof(struct pipe_end), /* size */
198 pipe_client_dump, /* dump */
199 file_get_type, /* get_type */
200 add_queue, /* add_queue */
201 remove_queue, /* remove_queue */
202 default_fd_signaled, /* signaled */
203 no_satisfied, /* satisfied */
204 no_signal, /* signal */
205 pipe_end_get_fd, /* get_fd */
206 default_fd_map_access, /* map_access */
207 pipe_end_get_sd, /* get_sd */
208 pipe_end_set_sd, /* set_sd */
209 no_lookup_name, /* lookup_name */
210 no_link_name, /* link_name */
211 NULL, /* unlink_name */
212 no_open_file, /* open_file */
213 fd_close_handle, /* close_handle */
214 pipe_end_destroy /* destroy */
217 static const struct fd_ops pipe_client_fd_ops =
219 default_fd_get_poll_events, /* get_poll_events */
220 default_poll_event, /* poll_event */
221 pipe_end_get_fd_type, /* get_fd_type */
222 pipe_end_read, /* read */
223 pipe_end_write, /* write */
224 pipe_end_flush, /* flush */
225 pipe_end_get_file_info, /* get_file_info */
226 pipe_end_get_volume_info, /* get_volume_info */
227 pipe_client_ioctl, /* ioctl */
228 no_fd_queue_async, /* queue_async */
229 pipe_end_reselect_async /* reselect_async */
232 static void named_pipe_device_dump( struct object *obj, int verbose );
233 static struct object_type *named_pipe_device_get_type( struct object *obj );
234 static struct object *named_pipe_device_lookup_name( struct object *obj,
235 struct unicode_str *name, unsigned int attr );
236 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
237 unsigned int sharing, unsigned int options );
238 static void named_pipe_device_destroy( struct object *obj );
240 static const struct object_ops named_pipe_device_ops =
242 sizeof(struct named_pipe_device), /* size */
243 named_pipe_device_dump, /* dump */
244 named_pipe_device_get_type, /* get_type */
245 no_add_queue, /* add_queue */
246 NULL, /* remove_queue */
247 NULL, /* signaled */
248 no_satisfied, /* satisfied */
249 no_signal, /* signal */
250 no_get_fd, /* get_fd */
251 no_map_access, /* map_access */
252 default_get_sd, /* get_sd */
253 default_set_sd, /* set_sd */
254 named_pipe_device_lookup_name, /* lookup_name */
255 directory_link_name, /* link_name */
256 default_unlink_name, /* unlink_name */
257 named_pipe_device_open_file, /* open_file */
258 no_close_handle, /* close_handle */
259 named_pipe_device_destroy /* destroy */
262 static void named_pipe_device_file_dump( struct object *obj, int verbose );
263 static struct fd *named_pipe_device_file_get_fd( struct object *obj );
264 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
265 static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd );
266 static void named_pipe_device_file_destroy( struct object *obj );
268 static const struct object_ops named_pipe_device_file_ops =
270 sizeof(struct named_pipe_device_file), /* size */
271 named_pipe_device_file_dump, /* dump */
272 no_get_type, /* get_type */
273 add_queue, /* add_queue */
274 remove_queue, /* remove_queue */
275 default_fd_signaled, /* signaled */
276 no_satisfied, /* satisfied */
277 no_signal, /* signal */
278 named_pipe_device_file_get_fd, /* get_fd */
279 default_fd_map_access, /* map_access */
280 default_get_sd, /* get_sd */
281 default_set_sd, /* set_sd */
282 no_lookup_name, /* lookup_name */
283 no_link_name, /* link_name */
284 NULL, /* unlink_name */
285 no_open_file, /* open_file */
286 fd_close_handle, /* close_handle */
287 named_pipe_device_file_destroy /* destroy */
290 static const struct fd_ops named_pipe_device_fd_ops =
292 default_fd_get_poll_events, /* get_poll_events */
293 default_poll_event, /* poll_event */
294 named_pipe_device_file_get_fd_type, /* get_fd_type */
295 no_fd_read, /* read */
296 no_fd_write, /* write */
297 no_fd_flush, /* flush */
298 default_fd_get_file_info, /* get_file_info */
299 no_fd_get_volume_info, /* get_volume_info */
300 named_pipe_device_ioctl, /* ioctl */
301 default_fd_queue_async, /* queue_async */
302 default_fd_reselect_async /* reselect_async */
305 static void named_pipe_dump( struct object *obj, int verbose )
307 fputs( "Named pipe\n", stderr );
310 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
312 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
313 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
314 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
315 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
316 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
319 static void pipe_server_dump( struct object *obj, int verbose )
321 struct pipe_server *server = (struct pipe_server *) obj;
322 assert( obj->ops == &pipe_server_ops );
323 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe_end.pipe,
324 server->pipe_end.state );
327 static void pipe_client_dump( struct object *obj, int verbose )
329 struct pipe_end *client = (struct pipe_end *) obj;
330 assert( obj->ops == &pipe_client_ops );
331 fprintf( stderr, "Named pipe client server=%p\n", client->connection );
334 static void named_pipe_destroy( struct object *obj)
336 struct named_pipe *pipe = (struct named_pipe *) obj;
338 assert( list_empty( &pipe->listeners ) );
339 assert( !pipe->instances );
340 free_async_queue( &pipe->waiters );
343 static struct fd *pipe_end_get_fd( struct object *obj )
345 struct pipe_end *pipe_end = (struct pipe_end *) obj;
346 return (struct fd *) grab_object( pipe_end->fd );
349 static struct pipe_message *queue_message( struct pipe_end *pipe_end, struct iosb *iosb )
351 struct pipe_message *message;
353 if (!(message = mem_alloc( sizeof(*message) ))) return NULL;
354 message->iosb = (struct iosb *)grab_object( iosb );
355 message->async = NULL;
356 message->read_pos = 0;
357 list_add_tail( &pipe_end->message_queue, &message->entry );
358 return message;
361 static void wake_message( struct pipe_message *message )
363 struct async *async = message->async;
365 message->async = NULL;
366 if (!async) return;
368 message->iosb->status = STATUS_SUCCESS;
369 message->iosb->result = message->iosb->in_size;
370 async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
371 release_object( async );
374 static void free_message( struct pipe_message *message )
376 list_remove( &message->entry );
377 if (message->iosb) release_object( message->iosb );
378 free( message );
381 static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
383 struct pipe_end *connection = pipe_end->connection;
384 struct pipe_message *message, *next;
385 struct async *async;
387 pipe_end->connection = NULL;
389 pipe_end->state = status == STATUS_PIPE_DISCONNECTED
390 ? FILE_PIPE_DISCONNECTED_STATE : FILE_PIPE_CLOSING_STATE;
391 fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
392 async_wake_up( &pipe_end->read_q, status );
393 LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
395 async = message->async;
396 if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
397 if (!async) continue;
398 async_terminate( async, status );
399 release_object( async );
401 if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
403 if (connection)
405 connection->connection = NULL;
406 pipe_end_disconnect( connection, status );
410 static void pipe_end_destroy( struct object *obj )
412 struct pipe_end *pipe_end = (struct pipe_end *)obj;
413 struct pipe_message *message;
415 pipe_end_disconnect( pipe_end, STATUS_PIPE_BROKEN );
417 while (!list_empty( &pipe_end->message_queue ))
419 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
420 assert( !message->async );
421 free_message( message );
424 free_async_queue( &pipe_end->read_q );
425 free_async_queue( &pipe_end->write_q );
426 if (pipe_end->fd) release_object( pipe_end->fd );
427 if (pipe_end->pipe) release_object( pipe_end->pipe );
430 static void pipe_server_destroy( struct object *obj )
432 struct pipe_server *server = (struct pipe_server *)obj;
433 struct named_pipe *pipe = server->pipe_end.pipe;
435 assert( obj->ops == &pipe_server_ops );
437 assert( pipe->instances );
438 if (!--pipe->instances) unlink_named_object( &pipe->obj );
439 if (server->pipe_end.state == FILE_PIPE_LISTENING_STATE)
440 list_remove( &server->entry );
442 free_async_queue( &server->listen_q );
443 pipe_end_destroy( obj );
446 static void named_pipe_device_dump( struct object *obj, int verbose )
448 fputs( "Named pipe device\n", stderr );
451 static struct object_type *named_pipe_device_get_type( struct object *obj )
453 static const WCHAR name[] = {'D','e','v','i','c','e'};
454 static const struct unicode_str str = { name, sizeof(name) };
455 return get_object_type( &str );
458 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
459 unsigned int attr )
461 struct named_pipe_device *device = (struct named_pipe_device*)obj;
462 struct object *found;
464 assert( obj->ops == &named_pipe_device_ops );
465 assert( device->pipes );
467 if (!name) return NULL; /* open the device itself */
469 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
470 name->len = 0;
472 return found;
475 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
476 unsigned int sharing, unsigned int options )
478 struct named_pipe_device_file *file;
480 if (!(file = alloc_object( &named_pipe_device_file_ops ))) return NULL;
481 file->device = (struct named_pipe_device *)grab_object( obj );
482 if (!(file->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, obj, options )))
484 release_object( file );
485 return NULL;
487 allow_fd_caching( file->fd );
488 return &file->obj;
491 static void named_pipe_device_destroy( struct object *obj )
493 struct named_pipe_device *device = (struct named_pipe_device*)obj;
494 assert( obj->ops == &named_pipe_device_ops );
495 free( device->pipes );
498 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name )
500 struct named_pipe_device *dev;
502 if ((dev = create_named_object( root, &named_pipe_device_ops, name, 0, NULL )) &&
503 get_error() != STATUS_OBJECT_NAME_EXISTS)
505 dev->pipes = NULL;
506 if (!(dev->pipes = create_namespace( 7 )))
508 release_object( dev );
509 dev = NULL;
512 return &dev->obj;
515 static void named_pipe_device_file_dump( struct object *obj, int verbose )
517 struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
519 fprintf( stderr, "File on named pipe device %p\n", file->device );
522 static struct fd *named_pipe_device_file_get_fd( struct object *obj )
524 struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
525 return (struct fd *)grab_object( file->fd );
528 static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd )
530 return FD_TYPE_DEVICE;
533 static void named_pipe_device_file_destroy( struct object *obj )
535 struct named_pipe_device_file *file = (struct named_pipe_device_file*)obj;
536 assert( obj->ops == &named_pipe_device_file_ops );
537 if (file->fd) release_object( file->fd );
538 release_object( file->device );
541 static int pipe_end_flush( struct fd *fd, struct async *async )
543 struct pipe_end *pipe_end = get_fd_user( fd );
545 if (!pipe_end->pipe)
547 set_error( STATUS_PIPE_DISCONNECTED );
548 return 0;
551 if (pipe_end->connection && !list_empty( &pipe_end->connection->message_queue ))
553 fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT );
554 set_error( STATUS_PENDING );
556 return 1;
559 static void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
561 struct pipe_end *pipe_end = get_fd_user( fd );
562 struct named_pipe *pipe = pipe_end->pipe;
564 switch (info_class)
566 case FileNameInformation:
568 FILE_NAME_INFORMATION *name_info;
569 data_size_t name_len, reply_size;
570 const WCHAR *name;
572 if (get_reply_max_size() < sizeof(*name_info))
574 set_error( STATUS_INFO_LENGTH_MISMATCH );
575 return;
578 /* FIXME: We should be able to return on unlinked pipe */
579 if (!pipe || !(name = get_object_name( &pipe->obj, &name_len )))
581 set_error( STATUS_PIPE_DISCONNECTED );
582 return;
585 reply_size = offsetof( FILE_NAME_INFORMATION, FileName[name_len/sizeof(WCHAR) + 1] );
586 if (reply_size > get_reply_max_size())
588 reply_size = get_reply_max_size();
589 set_error( STATUS_BUFFER_OVERFLOW );
592 if (!(name_info = set_reply_data_size( reply_size ))) return;
593 name_info->FileNameLength = name_len + sizeof(WCHAR);
594 name_info->FileName[0] = '\\';
595 reply_size -= offsetof( FILE_NAME_INFORMATION, FileName[1] );
596 if (reply_size) memcpy( &name_info->FileName[1], name, reply_size );
597 break;
599 case FilePipeInformation:
601 FILE_PIPE_INFORMATION *pipe_info;
603 if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
605 set_error( STATUS_ACCESS_DENIED );
606 return;
609 if (get_reply_max_size() < sizeof(*pipe_info))
611 set_error( STATUS_INFO_LENGTH_MISMATCH );
612 return;
615 if (!pipe)
617 set_error( STATUS_PIPE_DISCONNECTED );
618 return;
621 if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
622 pipe_info->ReadMode = (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
623 ? FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
624 pipe_info->CompletionMode = (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE)
625 ? FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
626 break;
628 case FilePipeLocalInformation:
630 FILE_PIPE_LOCAL_INFORMATION *pipe_info;
632 if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
634 set_error( STATUS_ACCESS_DENIED );
635 return;
638 if (get_reply_max_size() < sizeof(*pipe_info))
640 set_error( STATUS_INFO_LENGTH_MISMATCH );
641 return;
644 if (!pipe)
646 set_error( STATUS_PIPE_DISCONNECTED );
647 return;
650 if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
651 pipe_info->NamedPipeType = pipe->message_mode;
652 switch (pipe->sharing)
654 case FILE_SHARE_READ:
655 pipe_info->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
656 break;
657 case FILE_SHARE_WRITE:
658 pipe_info->NamedPipeConfiguration = FILE_PIPE_INBOUND;
659 break;
660 case FILE_SHARE_READ | FILE_SHARE_WRITE:
661 pipe_info->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
662 break;
664 pipe_info->MaximumInstances = pipe->maxinstances;
665 pipe_info->CurrentInstances = pipe->instances;
666 pipe_info->InboundQuota = pipe->insize;
667 pipe_info->ReadDataAvailable = 0; /* FIXME */
668 pipe_info->OutboundQuota = pipe->outsize;
669 pipe_info->WriteQuotaAvailable = 0; /* FIXME */
670 pipe_info->NamedPipeState = pipe_end->state;
671 pipe_info->NamedPipeEnd = pipe_end->obj.ops == &pipe_server_ops
672 ? FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
673 break;
675 default:
676 default_fd_get_file_info( fd, handle, info_class );
680 static struct security_descriptor *pipe_end_get_sd( struct object *obj )
682 struct pipe_end *pipe_end = (struct pipe_end *) obj;
683 if (pipe_end->pipe) return default_get_sd( &pipe_end->pipe->obj );
684 set_error( STATUS_PIPE_DISCONNECTED );
685 return NULL;
688 static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
689 unsigned int set_info )
691 struct pipe_end *pipe_end = (struct pipe_end *) obj;
692 if (pipe_end->pipe) return default_set_sd( &pipe_end->pipe->obj, sd, set_info );
693 set_error( STATUS_PIPE_DISCONNECTED );
694 return 0;
697 static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class )
699 switch (info_class)
701 case FileFsDeviceInformation:
703 static const FILE_FS_DEVICE_INFORMATION device_info =
705 FILE_DEVICE_NAMED_PIPE,
706 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
708 if (get_reply_max_size() >= sizeof(device_info))
709 set_reply_data( &device_info, sizeof(device_info) );
710 else
711 set_error( STATUS_BUFFER_TOO_SMALL );
712 break;
714 default:
715 set_error( STATUS_NOT_IMPLEMENTED );
719 static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
721 struct pipe_message *message;
723 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
725 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
726 iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
727 iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
728 ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
730 else
732 data_size_t avail = 0;
733 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
735 avail += message->iosb->in_size - message->read_pos;
736 if (avail >= iosb->out_size) break;
738 iosb->out_size = min( iosb->out_size, avail );
739 iosb->status = STATUS_SUCCESS;
742 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
743 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
745 iosb->out_data = message->iosb->in_data;
746 message->iosb->in_data = NULL;
747 wake_message( message );
748 free_message( message );
750 else
752 data_size_t write_pos = 0, writing;
753 char *buf = NULL;
755 if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
757 iosb->out_size = 0;
758 iosb->status = STATUS_NO_MEMORY;
759 return;
764 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
765 writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
766 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
767 write_pos += writing;
768 message->read_pos += writing;
769 if (message->read_pos == message->iosb->in_size)
771 wake_message(message);
772 free_message(message);
774 } while (write_pos < iosb->out_size);
776 iosb->result = iosb->out_size;
779 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
780 * We're not interested in such reselect calls, so we ignore them. */
781 static int ignore_reselect;
783 static void reselect_write_queue( struct pipe_end *pipe_end );
785 static void reselect_read_queue( struct pipe_end *pipe_end )
787 struct async *async;
788 struct iosb *iosb;
789 int read_done = 0;
791 ignore_reselect = 1;
792 while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
794 iosb = async_get_iosb( async );
795 message_queue_read( pipe_end, iosb );
796 async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
797 release_object( async );
798 release_object( iosb );
799 read_done = 1;
801 ignore_reselect = 0;
803 if (pipe_end->connection)
805 if (list_empty( &pipe_end->message_queue ))
806 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
807 else if (read_done)
808 reselect_write_queue( pipe_end->connection );
812 static void reselect_write_queue( struct pipe_end *pipe_end )
814 struct pipe_message *message, *next;
815 struct pipe_end *reader = pipe_end->connection;
816 data_size_t avail = 0;
818 if (!reader) return;
820 ignore_reselect = 1;
822 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
824 if (message->async && message->iosb->status != STATUS_PENDING)
826 release_object( message->async );
827 message->async = NULL;
828 free_message( message );
830 else
832 avail += message->iosb->in_size - message->read_pos;
833 if (message->async && (avail <= reader->buffer_size || !message->iosb->in_size))
834 wake_message( message );
838 ignore_reselect = 0;
839 reselect_read_queue( reader );
842 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
844 struct pipe_end *pipe_end = get_fd_user( fd );
846 switch (pipe_end->state)
848 case FILE_PIPE_CONNECTED_STATE:
849 break;
850 case FILE_PIPE_DISCONNECTED_STATE:
851 set_error( STATUS_PIPE_DISCONNECTED );
852 return 0;
853 case FILE_PIPE_LISTENING_STATE:
854 set_error( STATUS_PIPE_LISTENING );
855 return 0;
856 case FILE_PIPE_CLOSING_STATE:
857 if (!list_empty( &pipe_end->message_queue )) break;
858 set_error( STATUS_PIPE_BROKEN );
859 return 0;
862 queue_async( &pipe_end->read_q, async );
863 reselect_read_queue( pipe_end );
864 set_error( STATUS_PENDING );
865 return 1;
868 static int pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
870 struct pipe_end *pipe_end = get_fd_user( fd );
871 struct pipe_message *message;
872 struct iosb *iosb;
874 switch (pipe_end->state)
876 case FILE_PIPE_CONNECTED_STATE:
877 break;
878 case FILE_PIPE_DISCONNECTED_STATE:
879 set_error( STATUS_PIPE_DISCONNECTED );
880 return 0;
881 case FILE_PIPE_LISTENING_STATE:
882 set_error( STATUS_PIPE_LISTENING );
883 return 0;
884 case FILE_PIPE_CLOSING_STATE:
885 set_error( STATUS_PIPE_CLOSING );
886 return 0;
889 if (!pipe_end->pipe->message_mode && !get_req_data_size()) return 1;
891 iosb = async_get_iosb( async );
892 message = queue_message( pipe_end->connection, iosb );
893 release_object( iosb );
894 if (!message) return 0;
896 message->async = (struct async *)grab_object( async );
897 queue_async( &pipe_end->write_q, async );
898 reselect_write_queue( pipe_end );
899 set_error( STATUS_PENDING );
900 return 1;
903 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
905 struct pipe_end *pipe_end = get_fd_user( fd );
907 if (ignore_reselect) return;
909 if (&pipe_end->write_q == queue)
910 reselect_write_queue( pipe_end );
911 else if (&pipe_end->read_q == queue)
912 reselect_read_queue( pipe_end );
915 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
917 return FD_TYPE_PIPE;
920 static int pipe_end_peek( struct pipe_end *pipe_end )
922 unsigned reply_size = get_reply_max_size();
923 FILE_PIPE_PEEK_BUFFER *buffer;
924 struct pipe_message *message;
925 data_size_t avail = 0;
926 data_size_t message_length = 0;
928 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
930 set_error( STATUS_INFO_LENGTH_MISMATCH );
931 return 0;
933 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
935 switch (pipe_end->state)
937 case FILE_PIPE_CONNECTED_STATE:
938 break;
939 case FILE_PIPE_CLOSING_STATE:
940 if (!list_empty( &pipe_end->message_queue )) break;
941 set_error( STATUS_PIPE_BROKEN );
942 return 0;
943 default:
944 set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
945 return 0;
948 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
949 avail += message->iosb->in_size - message->read_pos;
950 reply_size = min( reply_size, avail );
952 if (avail && pipe_end->pipe->message_mode)
954 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
955 message_length = message->iosb->in_size - message->read_pos;
956 reply_size = min( reply_size, message_length );
959 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return 0;
960 buffer->NamedPipeState = pipe_end->state;
961 buffer->ReadDataAvailable = avail;
962 buffer->NumberOfMessages = 0; /* FIXME */
963 buffer->MessageLength = message_length;
965 if (reply_size)
967 data_size_t write_pos = 0, writing;
968 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
970 writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
971 memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
972 writing );
973 write_pos += writing;
974 if (write_pos == reply_size) break;
977 if (message_length > reply_size) set_error( STATUS_BUFFER_OVERFLOW );
978 return 1;
981 static int pipe_end_transceive( struct pipe_end *pipe_end, struct async *async )
983 struct pipe_message *message;
984 struct iosb *iosb;
986 if (!pipe_end->connection)
988 set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
989 return 0;
992 if (!(pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ))
994 set_error( STATUS_INVALID_READ_MODE );
995 return 0;
998 /* not allowed if we already have read data buffered */
999 if (!list_empty( &pipe_end->message_queue ))
1001 set_error( STATUS_PIPE_BUSY );
1002 return 0;
1005 iosb = async_get_iosb( async );
1006 /* ignore output buffer copy transferred because of METHOD_NEITHER */
1007 iosb->in_size -= iosb->out_size;
1008 /* transaction never blocks on write, so just queue a message without async */
1009 message = queue_message( pipe_end->connection, iosb );
1010 release_object( iosb );
1011 if (!message) return 0;
1012 reselect_read_queue( pipe_end->connection );
1014 queue_async( &pipe_end->read_q, async );
1015 reselect_read_queue( pipe_end );
1016 set_error( STATUS_PENDING );
1017 return 1;
1020 static int pipe_end_get_connection_attribute( struct pipe_end *pipe_end )
1022 const char *attr = get_req_data();
1023 data_size_t value_size, attr_size = get_req_data_size();
1024 void *value;
1026 if (attr_size == sizeof("ClientProcessId") && !memcmp( attr, "ClientProcessId", attr_size ))
1028 value = &pipe_end->client_pid;
1029 value_size = sizeof(pipe_end->client_pid);
1031 else if (attr_size == sizeof("ServerProcessId") && !memcmp( attr, "ServerProcessId", attr_size ))
1033 value = &pipe_end->server_pid;
1034 value_size = sizeof(pipe_end->server_pid);
1036 else
1038 set_error( STATUS_ILLEGAL_FUNCTION );
1039 return 0;
1042 if (get_reply_max_size() < value_size)
1044 set_error( STATUS_INFO_LENGTH_MISMATCH );
1045 return 0;
1048 set_reply_data( value, value_size );
1049 return 1;
1052 static int pipe_end_ioctl( struct pipe_end *pipe_end, ioctl_code_t code, struct async *async )
1054 switch(code)
1056 case FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE:
1057 return pipe_end_get_connection_attribute( pipe_end );
1059 case FSCTL_PIPE_PEEK:
1060 return pipe_end_peek( pipe_end );
1062 case FSCTL_PIPE_TRANSCEIVE:
1063 return pipe_end_transceive( pipe_end, async );
1065 default:
1066 return default_fd_ioctl( pipe_end->fd, code, async );
1070 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1072 struct pipe_server *server = get_fd_user( fd );
1074 switch(code)
1076 case FSCTL_PIPE_LISTEN:
1077 switch(server->pipe_end.state)
1079 case FILE_PIPE_LISTENING_STATE:
1080 break;
1081 case FILE_PIPE_DISCONNECTED_STATE:
1082 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1083 list_add_tail( &server->pipe_end.pipe->listeners, &server->entry );
1084 break;
1085 case FILE_PIPE_CONNECTED_STATE:
1086 set_error( STATUS_PIPE_CONNECTED );
1087 return 0;
1088 case FILE_PIPE_CLOSING_STATE:
1089 set_error( STATUS_PIPE_CLOSING );
1090 return 0;
1093 queue_async( &server->listen_q, async );
1094 async_wake_up( &server->pipe_end.pipe->waiters, STATUS_SUCCESS );
1095 set_error( STATUS_PENDING );
1096 return 1;
1098 case FSCTL_PIPE_DISCONNECT:
1099 switch(server->pipe_end.state)
1101 case FILE_PIPE_CONNECTED_STATE:
1102 /* dump the client connection - all data is lost */
1103 assert( server->pipe_end.connection );
1104 release_object( server->pipe_end.connection->pipe );
1105 server->pipe_end.connection->pipe = NULL;
1106 break;
1107 case FILE_PIPE_CLOSING_STATE:
1108 break;
1109 case FILE_PIPE_LISTENING_STATE:
1110 set_error( STATUS_PIPE_LISTENING );
1111 return 0;
1112 case FILE_PIPE_DISCONNECTED_STATE:
1113 set_error( STATUS_PIPE_DISCONNECTED );
1114 return 0;
1117 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1118 return 1;
1120 default:
1121 return pipe_end_ioctl( &server->pipe_end, code, async );
1125 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1127 struct pipe_end *client = get_fd_user( fd );
1129 switch(code)
1131 case FSCTL_PIPE_LISTEN:
1132 set_error( STATUS_ILLEGAL_FUNCTION );
1133 return 0;
1135 default:
1136 return pipe_end_ioctl( client, code, async );
1140 static void init_pipe_end( struct pipe_end *pipe_end, struct named_pipe *pipe,
1141 unsigned int pipe_flags, data_size_t buffer_size )
1143 pipe_end->pipe = (struct named_pipe *)grab_object( pipe );
1144 pipe_end->fd = NULL;
1145 pipe_end->flags = pipe_flags;
1146 pipe_end->connection = NULL;
1147 pipe_end->buffer_size = buffer_size;
1148 init_async_queue( &pipe_end->read_q );
1149 init_async_queue( &pipe_end->write_q );
1150 list_init( &pipe_end->message_queue );
1153 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
1154 unsigned int pipe_flags )
1156 struct pipe_server *server;
1158 server = alloc_object( &pipe_server_ops );
1159 if (!server)
1160 return NULL;
1162 server->options = options;
1163 init_pipe_end( &server->pipe_end, pipe, pipe_flags, pipe->insize );
1164 server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1165 server->pipe_end.server_pid = get_process_id( current->process );
1166 init_async_queue( &server->listen_q );
1168 list_add_tail( &pipe->listeners, &server->entry );
1169 if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1171 release_object( server );
1172 return NULL;
1174 allow_fd_caching( server->pipe_end.fd );
1175 set_fd_signaled( server->pipe_end.fd, 1 );
1176 async_wake_up( &pipe->waiters, STATUS_SUCCESS );
1177 return server;
1180 static struct pipe_end *create_pipe_client( struct named_pipe *pipe, data_size_t buffer_size, unsigned int options )
1182 struct pipe_end *client;
1184 client = alloc_object( &pipe_client_ops );
1185 if (!client)
1186 return NULL;
1188 init_pipe_end( client, pipe, 0, buffer_size );
1189 client->state = FILE_PIPE_CONNECTED_STATE;
1190 client->client_pid = get_process_id( current->process );
1192 client->fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->obj, options );
1193 if (!client->fd)
1195 release_object( client );
1196 return NULL;
1198 allow_fd_caching( client->fd );
1199 set_fd_signaled( client->fd, 1 );
1201 return client;
1204 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1206 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1208 if (parent->ops != &named_pipe_device_ops)
1210 set_error( STATUS_OBJECT_NAME_INVALID );
1211 return 0;
1213 namespace_add( dev->pipes, name );
1214 name->parent = grab_object( parent );
1215 return 1;
1218 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1219 unsigned int sharing, unsigned int options )
1221 struct named_pipe *pipe = (struct named_pipe *)obj;
1222 struct pipe_server *server;
1223 struct pipe_end *client;
1224 unsigned int pipe_sharing;
1226 if (list_empty( &pipe->listeners ))
1228 set_error( STATUS_PIPE_NOT_AVAILABLE );
1229 return NULL;
1231 server = LIST_ENTRY( list_head( &pipe->listeners ), struct pipe_server, entry );
1233 pipe_sharing = pipe->sharing;
1234 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1235 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1237 set_error( STATUS_ACCESS_DENIED );
1238 return NULL;
1241 if ((client = create_pipe_client( pipe, pipe->outsize, options )))
1243 async_wake_up( &server->listen_q, STATUS_SUCCESS );
1244 server->pipe_end.state = FILE_PIPE_CONNECTED_STATE;
1245 server->pipe_end.connection = client;
1246 client->connection = &server->pipe_end;
1247 server->pipe_end.client_pid = client->client_pid;
1248 client->server_pid = server->pipe_end.server_pid;
1249 list_remove( &server->entry );
1251 return &client->obj;
1254 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1256 struct named_pipe_device *device = get_fd_user( fd );
1258 switch(code)
1260 case FSCTL_PIPE_WAIT:
1262 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1263 data_size_t size = get_req_data_size();
1264 struct named_pipe *pipe;
1265 struct unicode_str name;
1266 timeout_t when;
1268 if (size < sizeof(*buffer) ||
1269 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1271 set_error( STATUS_INVALID_PARAMETER );
1272 return 0;
1274 name.str = buffer->Name;
1275 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1276 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;
1278 if (list_empty( &pipe->listeners ))
1280 queue_async( &pipe->waiters, async );
1281 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1282 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1283 set_error( STATUS_PENDING );
1286 release_object( pipe );
1287 return 1;
1290 default:
1291 return default_fd_ioctl( fd, code, async );
1296 DECL_HANDLER(create_named_pipe)
1298 struct named_pipe *pipe;
1299 struct pipe_server *server;
1300 struct unicode_str name;
1301 struct object *root;
1302 const struct security_descriptor *sd;
1303 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1305 if (!objattr) return;
1307 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1308 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1310 if (root) release_object( root );
1311 set_error( STATUS_INVALID_PARAMETER );
1312 return;
1315 if (!name.len) /* pipes need a root directory even without a name */
1317 if (!objattr->rootdir)
1319 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1320 return;
1322 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
1325 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1327 if (root) release_object( root );
1328 if (!pipe) return;
1330 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1332 /* initialize it if it didn't already exist */
1333 pipe->instances = 0;
1334 init_async_queue( &pipe->waiters );
1335 list_init( &pipe->listeners );
1336 pipe->insize = req->insize;
1337 pipe->outsize = req->outsize;
1338 pipe->maxinstances = req->maxinstances;
1339 pipe->timeout = req->timeout;
1340 pipe->message_mode = (req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) != 0;
1341 pipe->sharing = req->sharing;
1342 if (sd) default_set_sd( &pipe->obj, sd, OWNER_SECURITY_INFORMATION |
1343 GROUP_SECURITY_INFORMATION |
1344 DACL_SECURITY_INFORMATION |
1345 SACL_SECURITY_INFORMATION );
1347 else
1349 if (pipe->maxinstances <= pipe->instances)
1351 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1352 release_object( pipe );
1353 return;
1355 if (pipe->sharing != req->sharing)
1357 set_error( STATUS_ACCESS_DENIED );
1358 release_object( pipe );
1359 return;
1361 clear_error(); /* clear the name collision */
1364 server = create_pipe_server( pipe, req->options, req->flags );
1365 if (server)
1367 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1368 pipe->instances++;
1369 release_object( server );
1372 release_object( pipe );
1375 DECL_HANDLER(set_named_pipe_info)
1377 struct pipe_end *pipe_end;
1379 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1380 FILE_WRITE_ATTRIBUTES, &pipe_server_ops );
1381 if (!pipe_end)
1383 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1384 return;
1386 clear_error();
1387 pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
1388 0, &pipe_client_ops );
1389 if (!pipe_end) return;
1392 if (!pipe_end->pipe)
1394 set_error( STATUS_PIPE_DISCONNECTED );
1396 else if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1397 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !pipe_end->pipe->message_mode))
1399 set_error( STATUS_INVALID_PARAMETER );
1401 else
1403 pipe_end->flags = req->flags;
1406 release_object( pipe_end );