d3d8: Toggle depth test state based on auto depth stencil.
[wine.git] / server / named_pipe.c
blob8c5027808d7a2b4c6dd59993f01f4653adb4a469
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"
44 enum pipe_state
46 ps_idle_server,
47 ps_wait_open,
48 ps_connected_server,
49 ps_wait_disconnect,
50 ps_wait_connect
53 struct named_pipe;
55 struct pipe_message
57 struct list entry; /* entry in message queue */
58 data_size_t read_pos; /* already read bytes */
59 struct iosb *iosb; /* message iosb */
60 struct async *async; /* async of pending write */
63 struct pipe_end
65 struct object obj; /* object header */
66 struct fd *fd; /* pipe file descriptor */
67 unsigned int flags; /* pipe flags */
68 struct pipe_end *connection; /* the other end of the pipe */
69 data_size_t buffer_size;/* size of buffered data that doesn't block caller */
70 struct list message_queue;
71 struct async_queue read_q; /* read queue */
72 struct async_queue write_q; /* write queue */
75 struct pipe_server
77 struct pipe_end pipe_end; /* common header for pipe_client and pipe_server */
78 struct list entry; /* entry in named pipe servers list */
79 enum pipe_state state; /* server state */
80 struct pipe_client *client; /* client that this server is connected to */
81 struct named_pipe *pipe;
82 unsigned int options; /* pipe options */
85 struct pipe_client
87 struct pipe_end pipe_end; /* common header for pipe_client and pipe_server */
88 struct pipe_server *server; /* server that this client is connected to */
89 unsigned int flags; /* file flags */
92 struct named_pipe
94 struct object obj; /* object header */
95 unsigned int flags;
96 unsigned int sharing;
97 unsigned int maxinstances;
98 unsigned int outsize;
99 unsigned int insize;
100 unsigned int instances;
101 timeout_t timeout;
102 struct list servers; /* list of servers using this pipe */
103 struct async_queue waiters; /* list of clients waiting to connect */
106 struct named_pipe_device
108 struct object obj; /* object header */
109 struct fd *fd; /* pseudo-fd for ioctls */
110 struct namespace *pipes; /* named pipe namespace */
113 static void named_pipe_dump( struct object *obj, int verbose );
114 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
115 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent );
116 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
117 unsigned int sharing, unsigned int options );
118 static void named_pipe_destroy( struct object *obj );
120 static const struct object_ops named_pipe_ops =
122 sizeof(struct named_pipe), /* size */
123 named_pipe_dump, /* dump */
124 no_get_type, /* get_type */
125 no_add_queue, /* add_queue */
126 NULL, /* remove_queue */
127 NULL, /* signaled */
128 NULL, /* satisfied */
129 no_signal, /* signal */
130 no_get_fd, /* get_fd */
131 named_pipe_map_access, /* map_access */
132 default_get_sd, /* get_sd */
133 default_set_sd, /* set_sd */
134 no_lookup_name, /* lookup_name */
135 named_pipe_link_name, /* link_name */
136 default_unlink_name, /* unlink_name */
137 named_pipe_open_file, /* open_file */
138 no_close_handle, /* close_handle */
139 named_pipe_destroy /* destroy */
142 /* common server and client pipe end functions */
143 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd );
144 static struct fd *pipe_end_get_fd( struct object *obj );
145 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos );
146 static int pipe_end_write( struct fd *fd, struct async *async_data, file_pos_t pos );
147 static int pipe_end_flush( struct fd *fd, struct async *async );
148 static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class );
149 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue );
151 /* server end functions */
152 static void pipe_server_dump( struct object *obj, int verbose );
153 static void pipe_server_destroy( struct object *obj);
154 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
156 static const struct object_ops pipe_server_ops =
158 sizeof(struct pipe_server), /* size */
159 pipe_server_dump, /* dump */
160 no_get_type, /* get_type */
161 add_queue, /* add_queue */
162 remove_queue, /* remove_queue */
163 default_fd_signaled, /* signaled */
164 no_satisfied, /* satisfied */
165 no_signal, /* signal */
166 pipe_end_get_fd, /* get_fd */
167 default_fd_map_access, /* map_access */
168 default_get_sd, /* get_sd */
169 default_set_sd, /* set_sd */
170 no_lookup_name, /* lookup_name */
171 no_link_name, /* link_name */
172 NULL, /* unlink_name */
173 no_open_file, /* open_file */
174 fd_close_handle, /* close_handle */
175 pipe_server_destroy /* destroy */
178 static const struct fd_ops pipe_server_fd_ops =
180 default_fd_get_poll_events, /* get_poll_events */
181 default_poll_event, /* poll_event */
182 pipe_end_get_fd_type, /* get_fd_type */
183 pipe_end_read, /* read */
184 pipe_end_write, /* write */
185 pipe_end_flush, /* flush */
186 pipe_end_get_volume_info, /* get_volume_info */
187 pipe_server_ioctl, /* ioctl */
188 no_fd_queue_async, /* queue_async */
189 pipe_end_reselect_async /* reselect_async */
192 /* client end functions */
193 static void pipe_client_dump( struct object *obj, int verbose );
194 static void pipe_client_destroy( struct object *obj );
195 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
197 static const struct object_ops pipe_client_ops =
199 sizeof(struct pipe_client), /* size */
200 pipe_client_dump, /* dump */
201 no_get_type, /* get_type */
202 add_queue, /* add_queue */
203 remove_queue, /* remove_queue */
204 default_fd_signaled, /* signaled */
205 no_satisfied, /* satisfied */
206 no_signal, /* signal */
207 pipe_end_get_fd, /* get_fd */
208 default_fd_map_access, /* map_access */
209 default_get_sd, /* get_sd */
210 default_set_sd, /* set_sd */
211 no_lookup_name, /* lookup_name */
212 no_link_name, /* link_name */
213 NULL, /* unlink_name */
214 no_open_file, /* open_file */
215 fd_close_handle, /* close_handle */
216 pipe_client_destroy /* destroy */
219 static const struct fd_ops pipe_client_fd_ops =
221 default_fd_get_poll_events, /* get_poll_events */
222 default_poll_event, /* poll_event */
223 pipe_end_get_fd_type, /* get_fd_type */
224 pipe_end_read, /* read */
225 pipe_end_write, /* write */
226 pipe_end_flush, /* flush */
227 pipe_end_get_volume_info, /* get_volume_info */
228 pipe_client_ioctl, /* ioctl */
229 no_fd_queue_async, /* queue_async */
230 pipe_end_reselect_async /* reselect_async */
233 static void named_pipe_device_dump( struct object *obj, int verbose );
234 static struct object_type *named_pipe_device_get_type( struct object *obj );
235 static struct fd *named_pipe_device_get_fd( struct object *obj );
236 static struct object *named_pipe_device_lookup_name( struct object *obj,
237 struct unicode_str *name, unsigned int attr );
238 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
239 unsigned int sharing, unsigned int options );
240 static void named_pipe_device_destroy( struct object *obj );
241 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
242 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
244 static const struct object_ops named_pipe_device_ops =
246 sizeof(struct named_pipe_device), /* size */
247 named_pipe_device_dump, /* dump */
248 named_pipe_device_get_type, /* get_type */
249 no_add_queue, /* add_queue */
250 NULL, /* remove_queue */
251 NULL, /* signaled */
252 no_satisfied, /* satisfied */
253 no_signal, /* signal */
254 named_pipe_device_get_fd, /* get_fd */
255 no_map_access, /* map_access */
256 default_get_sd, /* get_sd */
257 default_set_sd, /* set_sd */
258 named_pipe_device_lookup_name, /* lookup_name */
259 directory_link_name, /* link_name */
260 default_unlink_name, /* unlink_name */
261 named_pipe_device_open_file, /* open_file */
262 fd_close_handle, /* close_handle */
263 named_pipe_device_destroy /* destroy */
266 static const struct fd_ops named_pipe_device_fd_ops =
268 default_fd_get_poll_events, /* get_poll_events */
269 default_poll_event, /* poll_event */
270 named_pipe_device_get_fd_type, /* get_fd_type */
271 no_fd_read, /* read */
272 no_fd_write, /* write */
273 no_fd_flush, /* flush */
274 no_fd_get_volume_info, /* get_volume_info */
275 named_pipe_device_ioctl, /* ioctl */
276 default_fd_queue_async, /* queue_async */
277 default_fd_reselect_async /* reselect_async */
280 static void named_pipe_dump( struct object *obj, int verbose )
282 fputs( "Named pipe\n", stderr );
285 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
287 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
288 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
289 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
290 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
291 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
294 static void pipe_server_dump( struct object *obj, int verbose )
296 struct pipe_server *server = (struct pipe_server *) obj;
297 assert( obj->ops == &pipe_server_ops );
298 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
301 static void pipe_client_dump( struct object *obj, int verbose )
303 struct pipe_client *client = (struct pipe_client *) obj;
304 assert( obj->ops == &pipe_client_ops );
305 fprintf( stderr, "Named pipe client server=%p\n", client->server );
308 static void named_pipe_destroy( struct object *obj)
310 struct named_pipe *pipe = (struct named_pipe *) obj;
312 assert( list_empty( &pipe->servers ) );
313 assert( !pipe->instances );
314 free_async_queue( &pipe->waiters );
317 static struct fd *pipe_end_get_fd( struct object *obj )
319 struct pipe_end *pipe_end = (struct pipe_end *) obj;
320 return (struct fd *) grab_object( pipe_end->fd );
323 static void set_server_state( struct pipe_server *server, enum pipe_state state )
325 server->state = state;
327 switch(state)
329 case ps_connected_server:
330 case ps_wait_disconnect:
331 break;
332 case ps_wait_open:
333 case ps_idle_server:
334 set_no_fd_status( server->pipe_end.fd, STATUS_PIPE_LISTENING );
335 break;
336 case ps_wait_connect:
337 set_no_fd_status( server->pipe_end.fd, STATUS_PIPE_DISCONNECTED );
338 break;
343 static void wake_message( struct pipe_message *message )
345 struct async *async = message->async;
347 message->async = NULL;
348 message->iosb->status = STATUS_SUCCESS;
349 message->iosb->result = message->iosb->in_size;
350 if (async)
352 async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
353 release_object( async );
357 static void free_message( struct pipe_message *message )
359 list_remove( &message->entry );
360 if (message->iosb) release_object( message->iosb );
361 free( message );
364 static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
366 struct pipe_end *connection = pipe_end->connection;
367 struct pipe_message *message, *next;
368 struct async *async;
370 pipe_end->connection = NULL;
372 fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
373 async_wake_up( &pipe_end->read_q, status );
374 LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
376 async = message->async;
377 if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
378 if (!async) continue;
379 async_terminate( async, status );
380 release_object( async );
382 if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
384 if (connection)
386 connection->connection = NULL;
387 pipe_end_disconnect( connection, status );
391 static void pipe_end_destroy( struct pipe_end *pipe_end )
393 struct pipe_message *message;
395 while (!list_empty( &pipe_end->message_queue ))
397 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
398 assert( !message->async );
399 free_message( message );
402 free_async_queue( &pipe_end->read_q );
403 free_async_queue( &pipe_end->write_q );
404 if (pipe_end->fd) release_object( pipe_end->fd );
407 static void pipe_server_destroy( struct object *obj)
409 struct pipe_server *server = (struct pipe_server *)obj;
411 assert( obj->ops == &pipe_server_ops );
413 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_BROKEN );
415 pipe_end_destroy( &server->pipe_end );
416 if (server->client)
418 server->client->server = NULL;
419 server->client = NULL;
422 assert( server->pipe->instances );
423 server->pipe->instances--;
425 list_remove( &server->entry );
426 release_object( server->pipe );
429 static void pipe_client_destroy( struct object *obj)
431 struct pipe_client *client = (struct pipe_client *)obj;
432 struct pipe_server *server = client->server;
434 assert( obj->ops == &pipe_client_ops );
436 pipe_end_disconnect( &client->pipe_end, STATUS_PIPE_BROKEN );
438 if (server)
440 switch(server->state)
442 case ps_connected_server:
443 /* Don't destroy the server's fd here as we can't
444 do a successful flush without it. */
445 set_server_state( server, ps_wait_disconnect );
446 break;
447 case ps_idle_server:
448 case ps_wait_open:
449 case ps_wait_disconnect:
450 case ps_wait_connect:
451 assert( 0 );
453 assert( server->client );
454 server->client = NULL;
455 client->server = NULL;
458 pipe_end_destroy( &client->pipe_end );
461 static void named_pipe_device_dump( struct object *obj, int verbose )
463 fputs( "Named pipe device\n", stderr );
466 static struct object_type *named_pipe_device_get_type( struct object *obj )
468 static const WCHAR name[] = {'D','e','v','i','c','e'};
469 static const struct unicode_str str = { name, sizeof(name) };
470 return get_object_type( &str );
473 static struct fd *named_pipe_device_get_fd( struct object *obj )
475 struct named_pipe_device *device = (struct named_pipe_device *)obj;
476 return (struct fd *)grab_object( device->fd );
479 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
480 unsigned int attr )
482 struct named_pipe_device *device = (struct named_pipe_device*)obj;
483 struct object *found;
485 assert( obj->ops == &named_pipe_device_ops );
486 assert( device->pipes );
488 if (!name) return NULL; /* open the device itself */
490 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
491 name->len = 0;
493 return found;
496 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
497 unsigned int sharing, unsigned int options )
499 return grab_object( obj );
502 static void named_pipe_device_destroy( struct object *obj )
504 struct named_pipe_device *device = (struct named_pipe_device*)obj;
505 assert( obj->ops == &named_pipe_device_ops );
506 if (device->fd) release_object( device->fd );
507 free( device->pipes );
510 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
512 return FD_TYPE_DEVICE;
515 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name )
517 struct named_pipe_device *dev;
519 if ((dev = create_named_object( root, &named_pipe_device_ops, name, 0, NULL )) &&
520 get_error() != STATUS_OBJECT_NAME_EXISTS)
522 dev->pipes = NULL;
523 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
524 !(dev->pipes = create_namespace( 7 )))
526 release_object( dev );
527 dev = NULL;
530 return &dev->obj;
533 static int pipe_end_flush( struct fd *fd, struct async *async )
535 struct pipe_end *pipe_end = get_fd_user( fd );
537 if (pipe_end->connection && !list_empty( &pipe_end->connection->message_queue ))
539 fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT );
540 set_error( STATUS_PENDING );
542 return 1;
545 static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class )
547 switch (info_class)
549 case FileFsDeviceInformation:
551 static const FILE_FS_DEVICE_INFORMATION device_info =
553 FILE_DEVICE_NAMED_PIPE,
554 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
556 if (get_reply_max_size() >= sizeof(device_info))
557 set_reply_data( &device_info, sizeof(device_info) );
558 else
559 set_error( STATUS_BUFFER_TOO_SMALL );
560 break;
562 default:
563 set_error( STATUS_NOT_IMPLEMENTED );
567 static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
569 struct pipe_message *message;
571 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
573 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
574 iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
575 iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
576 ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
578 else
580 data_size_t avail = 0;
581 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
583 avail += message->iosb->in_size - message->read_pos;
584 if (avail >= iosb->out_size) break;
586 iosb->out_size = min( iosb->out_size, avail );
587 iosb->status = STATUS_SUCCESS;
590 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
591 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
593 iosb->out_data = message->iosb->in_data;
594 message->iosb->in_data = NULL;
595 wake_message( message );
596 free_message( message );
598 else
600 data_size_t write_pos = 0, writing;
601 char *buf = NULL;
603 if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
605 iosb->out_size = 0;
606 iosb->status = STATUS_NO_MEMORY;
607 return;
612 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
613 writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
614 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
615 write_pos += writing;
616 message->read_pos += writing;
617 if (message->read_pos == message->iosb->in_size)
619 wake_message(message);
620 free_message(message);
622 } while (write_pos < iosb->out_size);
624 iosb->result = iosb->out_size;
627 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
628 * We're not interested in such reselect calls, so we ignore them. */
629 static int ignore_reselect;
631 static void reselect_write_queue( struct pipe_end *pipe_end );
633 static void reselect_read_queue( struct pipe_end *pipe_end )
635 struct async *async;
636 struct iosb *iosb;
637 int read_done = 0;
639 ignore_reselect = 1;
640 while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
642 iosb = async_get_iosb( async );
643 message_queue_read( pipe_end, iosb );
644 async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
645 release_object( async );
646 release_object( iosb );
647 read_done = 1;
649 ignore_reselect = 0;
651 if (pipe_end->connection)
653 if (list_empty( &pipe_end->message_queue ))
654 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
655 else if (read_done)
656 reselect_write_queue( pipe_end->connection );
660 static void reselect_write_queue( struct pipe_end *pipe_end )
662 struct pipe_message *message, *next;
663 struct pipe_end *reader = pipe_end->connection;
664 data_size_t avail = 0;
666 if (!reader) return;
668 ignore_reselect = 1;
670 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
672 if (message->async && message->iosb->status != STATUS_PENDING)
674 release_object( message->async );
675 message->async = NULL;
676 free_message( message );
678 else
680 avail += message->iosb->in_size - message->read_pos;
681 if (message->iosb->status == STATUS_PENDING && (avail <= reader->buffer_size || !message->iosb->in_size))
682 wake_message( message );
686 ignore_reselect = 0;
687 reselect_read_queue( reader );
690 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
692 struct pipe_end *pipe_end = get_fd_user( fd );
694 if (!pipe_end->connection && list_empty( &pipe_end->message_queue ))
696 set_error( STATUS_PIPE_BROKEN );
697 return 0;
700 queue_async( &pipe_end->read_q, async );
701 reselect_read_queue( pipe_end );
702 set_error( STATUS_PENDING );
703 return 1;
706 static int pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
708 struct pipe_end *write_end = get_fd_user( fd );
709 struct pipe_end *read_end = write_end->connection;
710 struct pipe_message *message;
712 if (!read_end)
714 set_error( STATUS_PIPE_DISCONNECTED );
715 return 0;
718 if (!(write_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && !get_req_data_size()) return 1;
720 if (!(message = mem_alloc( sizeof(*message) ))) return 0;
721 message->async = (struct async *)grab_object( async );
722 message->iosb = async_get_iosb( async );
723 message->read_pos = 0;
724 list_add_tail( &read_end->message_queue, &message->entry );
726 queue_async( &write_end->write_q, async );
727 reselect_write_queue( write_end );
728 set_error( STATUS_PENDING );
729 return 1;
732 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
734 struct pipe_end *pipe_end = get_fd_user( fd );
736 if (ignore_reselect) return;
738 if (&pipe_end->write_q == queue)
739 reselect_write_queue( pipe_end );
740 else if (&pipe_end->read_q == queue)
741 reselect_read_queue( pipe_end );
744 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
746 return FD_TYPE_PIPE;
749 static int pipe_end_peek( struct pipe_end *pipe_end )
751 unsigned reply_size = get_reply_max_size();
752 FILE_PIPE_PEEK_BUFFER *buffer;
753 struct pipe_message *message;
754 data_size_t avail = 0;
755 data_size_t message_length = 0;
757 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
759 set_error( STATUS_INFO_LENGTH_MISMATCH );
760 return 0;
762 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
764 if (!pipe_end->connection && list_empty( &pipe_end->message_queue ))
766 set_error( STATUS_PIPE_BROKEN );
767 return 0;
770 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
771 avail += message->iosb->in_size - message->read_pos;
772 reply_size = min( reply_size, avail );
774 if (avail && (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE))
776 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
777 message_length = message->iosb->in_size - message->read_pos;
778 reply_size = min( reply_size, message_length );
781 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return 0;
782 buffer->NamedPipeState = 0; /* FIXME */
783 buffer->ReadDataAvailable = avail;
784 buffer->NumberOfMessages = 0; /* FIXME */
785 buffer->MessageLength = message_length;
787 if (reply_size)
789 data_size_t write_pos = 0, writing;
790 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
792 writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
793 memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
794 writing );
795 write_pos += writing;
796 if (write_pos == reply_size) break;
799 return 1;
802 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
804 struct pipe_server *server = get_fd_user( fd );
806 switch(code)
808 case FSCTL_PIPE_LISTEN:
809 switch(server->state)
811 case ps_idle_server:
812 case ps_wait_connect:
813 fd_queue_async( server->pipe_end.fd, async, ASYNC_TYPE_WAIT );
814 set_server_state( server, ps_wait_open );
815 async_wake_up( &server->pipe->waiters, STATUS_SUCCESS );
816 set_error( STATUS_PENDING );
817 return 1;
818 case ps_connected_server:
819 set_error( STATUS_PIPE_CONNECTED );
820 break;
821 case ps_wait_disconnect:
822 set_error( STATUS_NO_DATA_DETECTED );
823 break;
824 case ps_wait_open:
825 set_error( STATUS_INVALID_HANDLE );
826 break;
828 return 0;
830 case FSCTL_PIPE_DISCONNECT:
831 switch(server->state)
833 case ps_connected_server:
834 assert( server->client );
836 /* dump the client and server fds - client loses all waiting data */
837 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
838 server->client->server = NULL;
839 server->client = NULL;
840 set_server_state( server, ps_wait_connect );
841 break;
842 case ps_wait_disconnect:
843 assert( !server->client );
844 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
845 set_server_state( server, ps_wait_connect );
846 break;
847 case ps_idle_server:
848 case ps_wait_open:
849 set_error( STATUS_PIPE_LISTENING );
850 return 0;
851 case ps_wait_connect:
852 set_error( STATUS_PIPE_DISCONNECTED );
853 return 0;
855 return 1;
857 case FSCTL_PIPE_PEEK:
858 return pipe_end_peek( &server->pipe_end );
860 default:
861 return default_fd_ioctl( fd, code, async );
865 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
867 struct pipe_client *client = get_fd_user( fd );
869 switch(code)
871 case FSCTL_PIPE_LISTEN:
872 set_error( STATUS_ILLEGAL_FUNCTION );
873 return 0;
875 case FSCTL_PIPE_PEEK:
876 return pipe_end_peek( &client->pipe_end );
878 default:
879 return default_fd_ioctl( fd, code, async );
883 static struct pipe_server *get_pipe_server_obj( struct process *process,
884 obj_handle_t handle, unsigned int access )
886 struct object *obj;
887 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
888 return (struct pipe_server *) obj;
891 static void init_pipe_end( struct pipe_end *pipe_end, unsigned int pipe_flags, data_size_t buffer_size )
893 pipe_end->fd = NULL;
894 pipe_end->flags = pipe_flags;
895 pipe_end->connection = NULL;
896 pipe_end->buffer_size = buffer_size;
897 init_async_queue( &pipe_end->read_q );
898 init_async_queue( &pipe_end->write_q );
899 list_init( &pipe_end->message_queue );
902 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
903 unsigned int pipe_flags )
905 struct pipe_server *server;
907 server = alloc_object( &pipe_server_ops );
908 if (!server)
909 return NULL;
911 server->pipe = pipe;
912 server->client = NULL;
913 server->options = options;
914 init_pipe_end( &server->pipe_end, pipe_flags, pipe->insize );
916 list_add_head( &pipe->servers, &server->entry );
917 grab_object( pipe );
918 if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
920 release_object( server );
921 return NULL;
923 set_fd_signaled( server->pipe_end.fd, 1 );
924 set_server_state( server, ps_idle_server );
925 return server;
928 static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags,
929 data_size_t buffer_size, unsigned int options )
931 struct pipe_client *client;
933 client = alloc_object( &pipe_client_ops );
934 if (!client)
935 return NULL;
937 client->server = NULL;
938 client->flags = flags;
939 init_pipe_end( &client->pipe_end, pipe_flags, buffer_size );
941 client->pipe_end.fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->pipe_end.obj, options );
942 if (!client->pipe_end.fd)
944 release_object( client );
945 return NULL;
947 allow_fd_caching( client->pipe_end.fd );
948 set_fd_signaled( client->pipe_end.fd, 1 );
950 return client;
953 static struct pipe_server *find_available_server( struct named_pipe *pipe )
955 struct pipe_server *server;
957 /* look for pipe servers that are listening */
958 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
960 if (server->state == ps_wait_open)
961 return (struct pipe_server *)grab_object( server );
964 /* fall back to pipe servers that are idle */
965 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
967 if (server->state == ps_idle_server)
968 return (struct pipe_server *)grab_object( server );
971 return NULL;
974 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
976 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
978 if (parent->ops != &named_pipe_device_ops)
980 set_error( STATUS_OBJECT_NAME_INVALID );
981 return 0;
983 namespace_add( dev->pipes, name );
984 name->parent = grab_object( parent );
985 return 1;
988 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
989 unsigned int sharing, unsigned int options )
991 struct named_pipe *pipe = (struct named_pipe *)obj;
992 struct pipe_server *server;
993 struct pipe_client *client;
994 unsigned int pipe_sharing;
996 if (!(server = find_available_server( pipe )))
998 set_error( STATUS_PIPE_NOT_AVAILABLE );
999 return NULL;
1002 pipe_sharing = server->pipe->sharing;
1003 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1004 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1006 set_error( STATUS_ACCESS_DENIED );
1007 release_object( server );
1008 return NULL;
1011 if ((client = create_pipe_client( options, pipe->flags, pipe->outsize, options )))
1013 set_no_fd_status( server->pipe_end.fd, STATUS_BAD_DEVICE_TYPE );
1014 allow_fd_caching( server->pipe_end.fd );
1015 if (server->state == ps_wait_open)
1016 fd_async_wake_up( server->pipe_end.fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
1017 set_server_state( server, ps_connected_server );
1018 server->client = client;
1019 client->server = server;
1020 server->pipe_end.connection = &client->pipe_end;
1021 client->pipe_end.connection = &server->pipe_end;
1023 release_object( server );
1024 return &client->pipe_end.obj;
1027 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1029 struct named_pipe_device *device = get_fd_user( fd );
1031 switch(code)
1033 case FSCTL_PIPE_WAIT:
1035 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1036 data_size_t size = get_req_data_size();
1037 struct named_pipe *pipe;
1038 struct pipe_server *server;
1039 struct unicode_str name;
1040 timeout_t when;
1042 if (size < sizeof(*buffer) ||
1043 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1045 set_error( STATUS_INVALID_PARAMETER );
1046 return 0;
1048 name.str = buffer->Name;
1049 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1050 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;
1052 if (!(server = find_available_server( pipe )))
1054 queue_async( &pipe->waiters, async );
1055 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1056 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1057 release_object( pipe );
1058 set_error( STATUS_PENDING );
1059 return 1;
1062 release_object( server );
1063 release_object( pipe );
1064 return 0;
1067 default:
1068 return default_fd_ioctl( fd, code, async );
1073 DECL_HANDLER(create_named_pipe)
1075 struct named_pipe *pipe;
1076 struct pipe_server *server;
1077 struct unicode_str name;
1078 struct object *root;
1079 const struct security_descriptor *sd;
1080 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1082 if (!objattr) return;
1084 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1085 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1087 if (root) release_object( root );
1088 set_error( STATUS_INVALID_PARAMETER );
1089 return;
1092 if (!name.len) /* pipes need a root directory even without a name */
1094 if (!objattr->rootdir)
1096 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1097 return;
1099 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
1102 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1104 if (root) release_object( root );
1105 if (!pipe) return;
1107 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1109 /* initialize it if it didn't already exist */
1110 pipe->instances = 0;
1111 init_async_queue( &pipe->waiters );
1112 list_init( &pipe->servers );
1113 pipe->insize = req->insize;
1114 pipe->outsize = req->outsize;
1115 pipe->maxinstances = req->maxinstances;
1116 pipe->timeout = req->timeout;
1117 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
1118 pipe->sharing = req->sharing;
1120 else
1122 if (pipe->maxinstances <= pipe->instances)
1124 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1125 release_object( pipe );
1126 return;
1128 if (pipe->sharing != req->sharing)
1130 set_error( STATUS_ACCESS_DENIED );
1131 release_object( pipe );
1132 return;
1134 clear_error(); /* clear the name collision */
1137 server = create_pipe_server( pipe, req->options, req->flags );
1138 if (server)
1140 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1141 server->pipe->instances++;
1142 if (sd) default_set_sd( &server->pipe_end.obj, sd, OWNER_SECURITY_INFORMATION |
1143 GROUP_SECURITY_INFORMATION |
1144 DACL_SECURITY_INFORMATION |
1145 SACL_SECURITY_INFORMATION );
1146 release_object( server );
1149 release_object( pipe );
1152 DECL_HANDLER(get_named_pipe_info)
1154 struct pipe_server *server;
1155 struct pipe_client *client = NULL;
1157 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1158 if (!server)
1160 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1161 return;
1163 clear_error();
1164 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1165 0, &pipe_client_ops );
1166 if (!client) return;
1167 server = client->server;
1170 reply->flags = client ? client->pipe_end.flags : server->pipe_end.flags;
1171 if (server)
1173 reply->sharing = server->pipe->sharing;
1174 reply->maxinstances = server->pipe->maxinstances;
1175 reply->instances = server->pipe->instances;
1176 reply->insize = server->pipe->insize;
1177 reply->outsize = server->pipe->outsize;
1180 if (client)
1181 release_object(client);
1182 else
1184 reply->flags |= NAMED_PIPE_SERVER_END;
1185 release_object(server);
1189 DECL_HANDLER(set_named_pipe_info)
1191 struct pipe_server *server;
1192 struct pipe_client *client = NULL;
1194 server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
1195 if (!server)
1197 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1198 return;
1200 clear_error();
1201 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1202 0, &pipe_client_ops );
1203 if (!client) return;
1204 if (!(server = client->server))
1206 release_object( client );
1207 return;
1211 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1212 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1214 set_error( STATUS_INVALID_PARAMETER );
1216 else if (client)
1218 client->pipe_end.flags = server->pipe->flags | req->flags;
1220 else
1222 server->pipe_end.flags = server->pipe->flags | req->flags;
1225 if (client)
1226 release_object(client);
1227 else
1228 release_object(server);