wined3d: Implement updating swap interval through wined3d_swapchain_present().
[wine.git] / server / named_pipe.c
blobc77b07fc91ba80ecac9e8347a112a00b188402de
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 struct security_descriptor *pipe_server_get_sd( struct object *obj );
154 static int pipe_server_set_sd( struct object *obj, const struct security_descriptor *sd,
155 unsigned int set_info );
156 static void pipe_server_destroy( struct object *obj);
157 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
158 static void pipe_server_get_file_info( struct fd *fd, unsigned int info_class );
160 static const struct object_ops pipe_server_ops =
162 sizeof(struct pipe_server), /* size */
163 pipe_server_dump, /* dump */
164 no_get_type, /* get_type */
165 add_queue, /* add_queue */
166 remove_queue, /* remove_queue */
167 default_fd_signaled, /* signaled */
168 no_satisfied, /* satisfied */
169 no_signal, /* signal */
170 pipe_end_get_fd, /* get_fd */
171 default_fd_map_access, /* map_access */
172 pipe_server_get_sd, /* get_sd */
173 pipe_server_set_sd, /* set_sd */
174 no_lookup_name, /* lookup_name */
175 no_link_name, /* link_name */
176 NULL, /* unlink_name */
177 no_open_file, /* open_file */
178 fd_close_handle, /* close_handle */
179 pipe_server_destroy /* destroy */
182 static const struct fd_ops pipe_server_fd_ops =
184 default_fd_get_poll_events, /* get_poll_events */
185 default_poll_event, /* poll_event */
186 pipe_end_get_fd_type, /* get_fd_type */
187 pipe_end_read, /* read */
188 pipe_end_write, /* write */
189 pipe_end_flush, /* flush */
190 pipe_server_get_file_info, /* get_file_info */
191 pipe_end_get_volume_info, /* get_volume_info */
192 pipe_server_ioctl, /* ioctl */
193 no_fd_queue_async, /* queue_async */
194 pipe_end_reselect_async /* reselect_async */
197 /* client end functions */
198 static void pipe_client_dump( struct object *obj, int verbose );
199 static struct security_descriptor *pipe_client_get_sd( struct object *obj );
200 static int pipe_client_set_sd( struct object *obj, const struct security_descriptor *sd,
201 unsigned int set_info );
202 static void pipe_client_destroy( struct object *obj );
203 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
204 static void pipe_client_get_file_info( struct fd *fd, unsigned int info_class );
206 static const struct object_ops pipe_client_ops =
208 sizeof(struct pipe_client), /* size */
209 pipe_client_dump, /* dump */
210 no_get_type, /* get_type */
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_fd_map_access, /* map_access */
218 pipe_client_get_sd, /* get_sd */
219 pipe_client_set_sd, /* set_sd */
220 no_lookup_name, /* lookup_name */
221 no_link_name, /* link_name */
222 NULL, /* unlink_name */
223 no_open_file, /* open_file */
224 fd_close_handle, /* close_handle */
225 pipe_client_destroy /* destroy */
228 static const struct fd_ops pipe_client_fd_ops =
230 default_fd_get_poll_events, /* get_poll_events */
231 default_poll_event, /* poll_event */
232 pipe_end_get_fd_type, /* get_fd_type */
233 pipe_end_read, /* read */
234 pipe_end_write, /* write */
235 pipe_end_flush, /* flush */
236 pipe_client_get_file_info, /* get_file_info */
237 pipe_end_get_volume_info, /* get_volume_info */
238 pipe_client_ioctl, /* ioctl */
239 no_fd_queue_async, /* queue_async */
240 pipe_end_reselect_async /* reselect_async */
243 static void named_pipe_device_dump( struct object *obj, int verbose );
244 static struct object_type *named_pipe_device_get_type( struct object *obj );
245 static struct fd *named_pipe_device_get_fd( struct object *obj );
246 static struct object *named_pipe_device_lookup_name( struct object *obj,
247 struct unicode_str *name, unsigned int attr );
248 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
249 unsigned int sharing, unsigned int options );
250 static void named_pipe_device_destroy( struct object *obj );
251 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
252 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
254 static const struct object_ops named_pipe_device_ops =
256 sizeof(struct named_pipe_device), /* size */
257 named_pipe_device_dump, /* dump */
258 named_pipe_device_get_type, /* get_type */
259 no_add_queue, /* add_queue */
260 NULL, /* remove_queue */
261 NULL, /* signaled */
262 no_satisfied, /* satisfied */
263 no_signal, /* signal */
264 named_pipe_device_get_fd, /* get_fd */
265 no_map_access, /* map_access */
266 default_get_sd, /* get_sd */
267 default_set_sd, /* set_sd */
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 fd_close_handle, /* close_handle */
273 named_pipe_device_destroy /* destroy */
276 static const struct fd_ops named_pipe_device_fd_ops =
278 default_fd_get_poll_events, /* get_poll_events */
279 default_poll_event, /* poll_event */
280 named_pipe_device_get_fd_type, /* get_fd_type */
281 no_fd_read, /* read */
282 no_fd_write, /* write */
283 no_fd_flush, /* flush */
284 no_fd_get_file_info, /* get_file_info */
285 no_fd_get_volume_info, /* get_volume_info */
286 named_pipe_device_ioctl, /* ioctl */
287 default_fd_queue_async, /* queue_async */
288 default_fd_reselect_async /* reselect_async */
291 static void named_pipe_dump( struct object *obj, int verbose )
293 fputs( "Named pipe\n", stderr );
296 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
298 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
299 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
300 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
301 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
302 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
305 static void pipe_server_dump( struct object *obj, int verbose )
307 struct pipe_server *server = (struct pipe_server *) obj;
308 assert( obj->ops == &pipe_server_ops );
309 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
312 static void pipe_client_dump( struct object *obj, int verbose )
314 struct pipe_client *client = (struct pipe_client *) obj;
315 assert( obj->ops == &pipe_client_ops );
316 fprintf( stderr, "Named pipe client server=%p\n", client->server );
319 static void named_pipe_destroy( struct object *obj)
321 struct named_pipe *pipe = (struct named_pipe *) obj;
323 assert( list_empty( &pipe->servers ) );
324 assert( !pipe->instances );
325 free_async_queue( &pipe->waiters );
328 static struct fd *pipe_end_get_fd( struct object *obj )
330 struct pipe_end *pipe_end = (struct pipe_end *) obj;
331 return (struct fd *) grab_object( pipe_end->fd );
334 static void set_server_state( struct pipe_server *server, enum pipe_state state )
336 server->state = state;
338 switch(state)
340 case ps_connected_server:
341 case ps_wait_disconnect:
342 break;
343 case ps_wait_open:
344 case ps_idle_server:
345 set_no_fd_status( server->pipe_end.fd, STATUS_PIPE_LISTENING );
346 break;
347 case ps_wait_connect:
348 set_no_fd_status( server->pipe_end.fd, STATUS_PIPE_DISCONNECTED );
349 break;
354 static void wake_message( struct pipe_message *message )
356 struct async *async = message->async;
358 message->async = NULL;
359 message->iosb->status = STATUS_SUCCESS;
360 message->iosb->result = message->iosb->in_size;
361 if (async)
363 async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
364 release_object( async );
368 static void free_message( struct pipe_message *message )
370 list_remove( &message->entry );
371 if (message->iosb) release_object( message->iosb );
372 free( message );
375 static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
377 struct pipe_end *connection = pipe_end->connection;
378 struct pipe_message *message, *next;
379 struct async *async;
381 pipe_end->connection = NULL;
383 fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
384 async_wake_up( &pipe_end->read_q, status );
385 LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
387 async = message->async;
388 if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
389 if (!async) continue;
390 async_terminate( async, status );
391 release_object( async );
393 if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
395 if (connection)
397 connection->connection = NULL;
398 pipe_end_disconnect( connection, status );
402 static void pipe_end_destroy( struct pipe_end *pipe_end )
404 struct pipe_message *message;
406 while (!list_empty( &pipe_end->message_queue ))
408 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
409 assert( !message->async );
410 free_message( message );
413 free_async_queue( &pipe_end->read_q );
414 free_async_queue( &pipe_end->write_q );
415 if (pipe_end->fd) release_object( pipe_end->fd );
418 static void pipe_server_destroy( struct object *obj)
420 struct pipe_server *server = (struct pipe_server *)obj;
422 assert( obj->ops == &pipe_server_ops );
424 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_BROKEN );
426 pipe_end_destroy( &server->pipe_end );
427 if (server->client)
429 server->client->server = NULL;
430 server->client = NULL;
433 assert( server->pipe->instances );
434 server->pipe->instances--;
436 list_remove( &server->entry );
437 release_object( server->pipe );
440 static void pipe_client_destroy( struct object *obj)
442 struct pipe_client *client = (struct pipe_client *)obj;
443 struct pipe_server *server = client->server;
445 assert( obj->ops == &pipe_client_ops );
447 pipe_end_disconnect( &client->pipe_end, STATUS_PIPE_BROKEN );
449 if (server)
451 switch(server->state)
453 case ps_connected_server:
454 /* Don't destroy the server's fd here as we can't
455 do a successful flush without it. */
456 set_server_state( server, ps_wait_disconnect );
457 break;
458 case ps_idle_server:
459 case ps_wait_open:
460 case ps_wait_disconnect:
461 case ps_wait_connect:
462 assert( 0 );
464 assert( server->client );
465 server->client = NULL;
466 client->server = NULL;
469 pipe_end_destroy( &client->pipe_end );
472 static void named_pipe_device_dump( struct object *obj, int verbose )
474 fputs( "Named pipe device\n", stderr );
477 static struct object_type *named_pipe_device_get_type( struct object *obj )
479 static const WCHAR name[] = {'D','e','v','i','c','e'};
480 static const struct unicode_str str = { name, sizeof(name) };
481 return get_object_type( &str );
484 static struct fd *named_pipe_device_get_fd( struct object *obj )
486 struct named_pipe_device *device = (struct named_pipe_device *)obj;
487 return (struct fd *)grab_object( device->fd );
490 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
491 unsigned int attr )
493 struct named_pipe_device *device = (struct named_pipe_device*)obj;
494 struct object *found;
496 assert( obj->ops == &named_pipe_device_ops );
497 assert( device->pipes );
499 if (!name) return NULL; /* open the device itself */
501 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
502 name->len = 0;
504 return found;
507 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
508 unsigned int sharing, unsigned int options )
510 return grab_object( obj );
513 static void named_pipe_device_destroy( struct object *obj )
515 struct named_pipe_device *device = (struct named_pipe_device*)obj;
516 assert( obj->ops == &named_pipe_device_ops );
517 if (device->fd) release_object( device->fd );
518 free( device->pipes );
521 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
523 return FD_TYPE_DEVICE;
526 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name )
528 struct named_pipe_device *dev;
530 if ((dev = create_named_object( root, &named_pipe_device_ops, name, 0, NULL )) &&
531 get_error() != STATUS_OBJECT_NAME_EXISTS)
533 dev->pipes = NULL;
534 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
535 !(dev->pipes = create_namespace( 7 )))
537 release_object( dev );
538 dev = NULL;
541 return &dev->obj;
544 static int pipe_end_flush( struct fd *fd, struct async *async )
546 struct pipe_end *pipe_end = get_fd_user( fd );
548 if (pipe_end->connection && !list_empty( &pipe_end->connection->message_queue ))
550 fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT );
551 set_error( STATUS_PENDING );
553 return 1;
556 static void pipe_end_get_file_info( struct fd *fd, struct named_pipe *pipe, unsigned int info_class )
558 switch (info_class)
560 case FileNameInformation:
562 FILE_NAME_INFORMATION *name_info;
563 data_size_t name_len, reply_size;
564 const WCHAR *name;
566 if (get_reply_max_size() < sizeof(*name_info))
568 set_error( STATUS_INFO_LENGTH_MISMATCH );
569 return;
572 name = get_object_name( &pipe->obj, &name_len );
573 reply_size = offsetof( FILE_NAME_INFORMATION, FileName[name_len/sizeof(WCHAR) + 1] );
574 if (reply_size > get_reply_max_size())
576 reply_size = get_reply_max_size();
577 set_error( STATUS_BUFFER_OVERFLOW );
580 if (!(name_info = set_reply_data_size( reply_size ))) return;
581 name_info->FileNameLength = name_len + sizeof(WCHAR);
582 name_info->FileName[0] = '\\';
583 reply_size -= offsetof( FILE_NAME_INFORMATION, FileName[1] );
584 if (reply_size) memcpy( &name_info->FileName[1], name, reply_size );
585 break;
587 default:
588 no_fd_get_file_info( fd, info_class );
592 static struct security_descriptor *pipe_server_get_sd( struct object *obj )
594 struct pipe_server *server = (struct pipe_server *) obj;
595 return default_get_sd( &server->pipe->obj );
598 static struct security_descriptor *pipe_client_get_sd( struct object *obj )
600 struct pipe_client *client = (struct pipe_client *) obj;
601 if (client->server) return default_get_sd( &client->server->pipe->obj );
602 set_error( STATUS_PIPE_DISCONNECTED );
603 return NULL;
606 static int pipe_server_set_sd( struct object *obj, const struct security_descriptor *sd,
607 unsigned int set_info )
609 struct pipe_server *server = (struct pipe_server *) obj;
610 return default_set_sd( &server->pipe->obj, sd, set_info );
613 static int pipe_client_set_sd( struct object *obj, const struct security_descriptor *sd,
614 unsigned int set_info )
616 struct pipe_client *client = (struct pipe_client *) obj;
617 if (client->server) return default_set_sd( &client->server->pipe->obj, sd, set_info );
618 set_error( STATUS_PIPE_DISCONNECTED );
619 return 0;
622 static void pipe_server_get_file_info( struct fd *fd, unsigned int info_class )
624 struct pipe_server *server = get_fd_user( fd );
625 pipe_end_get_file_info( fd, server->pipe, info_class );
628 static void pipe_client_get_file_info( struct fd *fd, unsigned int info_class )
630 struct pipe_client *client = get_fd_user( fd );
631 if (client->server) pipe_end_get_file_info( fd, client->server->pipe, info_class );
632 else set_error( STATUS_PIPE_DISCONNECTED );
635 static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class )
637 switch (info_class)
639 case FileFsDeviceInformation:
641 static const FILE_FS_DEVICE_INFORMATION device_info =
643 FILE_DEVICE_NAMED_PIPE,
644 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
646 if (get_reply_max_size() >= sizeof(device_info))
647 set_reply_data( &device_info, sizeof(device_info) );
648 else
649 set_error( STATUS_BUFFER_TOO_SMALL );
650 break;
652 default:
653 set_error( STATUS_NOT_IMPLEMENTED );
657 static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
659 struct pipe_message *message;
661 if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
663 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
664 iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
665 iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
666 ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
668 else
670 data_size_t avail = 0;
671 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
673 avail += message->iosb->in_size - message->read_pos;
674 if (avail >= iosb->out_size) break;
676 iosb->out_size = min( iosb->out_size, avail );
677 iosb->status = STATUS_SUCCESS;
680 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
681 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
683 iosb->out_data = message->iosb->in_data;
684 message->iosb->in_data = NULL;
685 wake_message( message );
686 free_message( message );
688 else
690 data_size_t write_pos = 0, writing;
691 char *buf = NULL;
693 if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
695 iosb->out_size = 0;
696 iosb->status = STATUS_NO_MEMORY;
697 return;
702 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
703 writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
704 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
705 write_pos += writing;
706 message->read_pos += writing;
707 if (message->read_pos == message->iosb->in_size)
709 wake_message(message);
710 free_message(message);
712 } while (write_pos < iosb->out_size);
714 iosb->result = iosb->out_size;
717 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
718 * We're not interested in such reselect calls, so we ignore them. */
719 static int ignore_reselect;
721 static void reselect_write_queue( struct pipe_end *pipe_end );
723 static void reselect_read_queue( struct pipe_end *pipe_end )
725 struct async *async;
726 struct iosb *iosb;
727 int read_done = 0;
729 ignore_reselect = 1;
730 while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
732 iosb = async_get_iosb( async );
733 message_queue_read( pipe_end, iosb );
734 async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
735 release_object( async );
736 release_object( iosb );
737 read_done = 1;
739 ignore_reselect = 0;
741 if (pipe_end->connection)
743 if (list_empty( &pipe_end->message_queue ))
744 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
745 else if (read_done)
746 reselect_write_queue( pipe_end->connection );
750 static void reselect_write_queue( struct pipe_end *pipe_end )
752 struct pipe_message *message, *next;
753 struct pipe_end *reader = pipe_end->connection;
754 data_size_t avail = 0;
756 if (!reader) return;
758 ignore_reselect = 1;
760 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
762 if (message->async && message->iosb->status != STATUS_PENDING)
764 release_object( message->async );
765 message->async = NULL;
766 free_message( message );
768 else
770 avail += message->iosb->in_size - message->read_pos;
771 if (message->iosb->status == STATUS_PENDING && (avail <= reader->buffer_size || !message->iosb->in_size))
772 wake_message( message );
776 ignore_reselect = 0;
777 reselect_read_queue( reader );
780 static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
782 struct pipe_end *pipe_end = get_fd_user( fd );
784 if (!pipe_end->connection && list_empty( &pipe_end->message_queue ))
786 set_error( STATUS_PIPE_BROKEN );
787 return 0;
790 queue_async( &pipe_end->read_q, async );
791 reselect_read_queue( pipe_end );
792 set_error( STATUS_PENDING );
793 return 1;
796 static int pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
798 struct pipe_end *write_end = get_fd_user( fd );
799 struct pipe_end *read_end = write_end->connection;
800 struct pipe_message *message;
802 if (!read_end)
804 set_error( STATUS_PIPE_DISCONNECTED );
805 return 0;
808 if (!(write_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && !get_req_data_size()) return 1;
810 if (!(message = mem_alloc( sizeof(*message) ))) return 0;
811 message->async = (struct async *)grab_object( async );
812 message->iosb = async_get_iosb( async );
813 message->read_pos = 0;
814 list_add_tail( &read_end->message_queue, &message->entry );
816 queue_async( &write_end->write_q, async );
817 reselect_write_queue( write_end );
818 set_error( STATUS_PENDING );
819 return 1;
822 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
824 struct pipe_end *pipe_end = get_fd_user( fd );
826 if (ignore_reselect) return;
828 if (&pipe_end->write_q == queue)
829 reselect_write_queue( pipe_end );
830 else if (&pipe_end->read_q == queue)
831 reselect_read_queue( pipe_end );
834 static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
836 return FD_TYPE_PIPE;
839 static int pipe_end_peek( struct pipe_end *pipe_end )
841 unsigned reply_size = get_reply_max_size();
842 FILE_PIPE_PEEK_BUFFER *buffer;
843 struct pipe_message *message;
844 data_size_t avail = 0;
845 data_size_t message_length = 0;
847 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
849 set_error( STATUS_INFO_LENGTH_MISMATCH );
850 return 0;
852 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
854 if (!pipe_end->connection && list_empty( &pipe_end->message_queue ))
856 set_error( STATUS_PIPE_BROKEN );
857 return 0;
860 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
861 avail += message->iosb->in_size - message->read_pos;
862 reply_size = min( reply_size, avail );
864 if (avail && (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE))
866 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
867 message_length = message->iosb->in_size - message->read_pos;
868 reply_size = min( reply_size, message_length );
871 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return 0;
872 buffer->NamedPipeState = 0; /* FIXME */
873 buffer->ReadDataAvailable = avail;
874 buffer->NumberOfMessages = 0; /* FIXME */
875 buffer->MessageLength = message_length;
877 if (reply_size)
879 data_size_t write_pos = 0, writing;
880 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
882 writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
883 memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
884 writing );
885 write_pos += writing;
886 if (write_pos == reply_size) break;
889 return 1;
892 static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
894 struct pipe_server *server = get_fd_user( fd );
896 switch(code)
898 case FSCTL_PIPE_LISTEN:
899 switch(server->state)
901 case ps_idle_server:
902 case ps_wait_connect:
903 fd_queue_async( server->pipe_end.fd, async, ASYNC_TYPE_WAIT );
904 set_server_state( server, ps_wait_open );
905 async_wake_up( &server->pipe->waiters, STATUS_SUCCESS );
906 set_error( STATUS_PENDING );
907 return 1;
908 case ps_connected_server:
909 set_error( STATUS_PIPE_CONNECTED );
910 break;
911 case ps_wait_disconnect:
912 set_error( STATUS_NO_DATA_DETECTED );
913 break;
914 case ps_wait_open:
915 set_error( STATUS_INVALID_HANDLE );
916 break;
918 return 0;
920 case FSCTL_PIPE_DISCONNECT:
921 switch(server->state)
923 case ps_connected_server:
924 assert( server->client );
926 /* dump the client and server fds - client loses all waiting data */
927 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
928 server->client->server = NULL;
929 server->client = NULL;
930 set_server_state( server, ps_wait_connect );
931 break;
932 case ps_wait_disconnect:
933 assert( !server->client );
934 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
935 set_server_state( server, ps_wait_connect );
936 break;
937 case ps_idle_server:
938 case ps_wait_open:
939 set_error( STATUS_PIPE_LISTENING );
940 return 0;
941 case ps_wait_connect:
942 set_error( STATUS_PIPE_DISCONNECTED );
943 return 0;
945 return 1;
947 case FSCTL_PIPE_PEEK:
948 return pipe_end_peek( &server->pipe_end );
950 default:
951 return default_fd_ioctl( fd, code, async );
955 static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
957 struct pipe_client *client = get_fd_user( fd );
959 switch(code)
961 case FSCTL_PIPE_LISTEN:
962 set_error( STATUS_ILLEGAL_FUNCTION );
963 return 0;
965 case FSCTL_PIPE_PEEK:
966 return pipe_end_peek( &client->pipe_end );
968 default:
969 return default_fd_ioctl( fd, code, async );
973 static struct pipe_server *get_pipe_server_obj( struct process *process,
974 obj_handle_t handle, unsigned int access )
976 struct object *obj;
977 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
978 return (struct pipe_server *) obj;
981 static void init_pipe_end( struct pipe_end *pipe_end, unsigned int pipe_flags, data_size_t buffer_size )
983 pipe_end->fd = NULL;
984 pipe_end->flags = pipe_flags;
985 pipe_end->connection = NULL;
986 pipe_end->buffer_size = buffer_size;
987 init_async_queue( &pipe_end->read_q );
988 init_async_queue( &pipe_end->write_q );
989 list_init( &pipe_end->message_queue );
992 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
993 unsigned int pipe_flags )
995 struct pipe_server *server;
997 server = alloc_object( &pipe_server_ops );
998 if (!server)
999 return NULL;
1001 server->pipe = pipe;
1002 server->client = NULL;
1003 server->options = options;
1004 init_pipe_end( &server->pipe_end, pipe_flags, pipe->insize );
1006 list_add_head( &pipe->servers, &server->entry );
1007 grab_object( pipe );
1008 if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1010 release_object( server );
1011 return NULL;
1013 set_fd_signaled( server->pipe_end.fd, 1 );
1014 set_server_state( server, ps_idle_server );
1015 return server;
1018 static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags,
1019 data_size_t buffer_size, unsigned int options )
1021 struct pipe_client *client;
1023 client = alloc_object( &pipe_client_ops );
1024 if (!client)
1025 return NULL;
1027 client->server = NULL;
1028 client->flags = flags;
1029 init_pipe_end( &client->pipe_end, pipe_flags, buffer_size );
1031 client->pipe_end.fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->pipe_end.obj, options );
1032 if (!client->pipe_end.fd)
1034 release_object( client );
1035 return NULL;
1037 allow_fd_caching( client->pipe_end.fd );
1038 set_fd_signaled( client->pipe_end.fd, 1 );
1040 return client;
1043 static struct pipe_server *find_available_server( struct named_pipe *pipe )
1045 struct pipe_server *server;
1047 /* look for pipe servers that are listening */
1048 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1050 if (server->state == ps_wait_open)
1051 return (struct pipe_server *)grab_object( server );
1054 /* fall back to pipe servers that are idle */
1055 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1057 if (server->state == ps_idle_server)
1058 return (struct pipe_server *)grab_object( server );
1061 return NULL;
1064 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1066 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1068 if (parent->ops != &named_pipe_device_ops)
1070 set_error( STATUS_OBJECT_NAME_INVALID );
1071 return 0;
1073 namespace_add( dev->pipes, name );
1074 name->parent = grab_object( parent );
1075 return 1;
1078 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1079 unsigned int sharing, unsigned int options )
1081 struct named_pipe *pipe = (struct named_pipe *)obj;
1082 struct pipe_server *server;
1083 struct pipe_client *client;
1084 unsigned int pipe_sharing;
1086 if (!(server = find_available_server( pipe )))
1088 set_error( STATUS_PIPE_NOT_AVAILABLE );
1089 return NULL;
1092 pipe_sharing = server->pipe->sharing;
1093 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1094 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1096 set_error( STATUS_ACCESS_DENIED );
1097 release_object( server );
1098 return NULL;
1101 if ((client = create_pipe_client( options, pipe->flags, pipe->outsize, options )))
1103 set_no_fd_status( server->pipe_end.fd, STATUS_BAD_DEVICE_TYPE );
1104 allow_fd_caching( server->pipe_end.fd );
1105 if (server->state == ps_wait_open)
1106 fd_async_wake_up( server->pipe_end.fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
1107 set_server_state( server, ps_connected_server );
1108 server->client = client;
1109 client->server = server;
1110 server->pipe_end.connection = &client->pipe_end;
1111 client->pipe_end.connection = &server->pipe_end;
1113 release_object( server );
1114 return &client->pipe_end.obj;
1117 static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1119 struct named_pipe_device *device = get_fd_user( fd );
1121 switch(code)
1123 case FSCTL_PIPE_WAIT:
1125 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1126 data_size_t size = get_req_data_size();
1127 struct named_pipe *pipe;
1128 struct pipe_server *server;
1129 struct unicode_str name;
1130 timeout_t when;
1132 if (size < sizeof(*buffer) ||
1133 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1135 set_error( STATUS_INVALID_PARAMETER );
1136 return 0;
1138 name.str = buffer->Name;
1139 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1140 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;
1142 if (!(server = find_available_server( pipe )))
1144 queue_async( &pipe->waiters, async );
1145 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1146 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1147 release_object( pipe );
1148 set_error( STATUS_PENDING );
1149 return 1;
1152 release_object( server );
1153 release_object( pipe );
1154 return 0;
1157 default:
1158 return default_fd_ioctl( fd, code, async );
1163 DECL_HANDLER(create_named_pipe)
1165 struct named_pipe *pipe;
1166 struct pipe_server *server;
1167 struct unicode_str name;
1168 struct object *root;
1169 const struct security_descriptor *sd;
1170 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1172 if (!objattr) return;
1174 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1175 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1177 if (root) release_object( root );
1178 set_error( STATUS_INVALID_PARAMETER );
1179 return;
1182 if (!name.len) /* pipes need a root directory even without a name */
1184 if (!objattr->rootdir)
1186 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1187 return;
1189 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
1192 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1194 if (root) release_object( root );
1195 if (!pipe) return;
1197 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1199 /* initialize it if it didn't already exist */
1200 pipe->instances = 0;
1201 init_async_queue( &pipe->waiters );
1202 list_init( &pipe->servers );
1203 pipe->insize = req->insize;
1204 pipe->outsize = req->outsize;
1205 pipe->maxinstances = req->maxinstances;
1206 pipe->timeout = req->timeout;
1207 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
1208 pipe->sharing = req->sharing;
1209 if (sd) default_set_sd( &pipe->obj, sd, OWNER_SECURITY_INFORMATION |
1210 GROUP_SECURITY_INFORMATION |
1211 DACL_SECURITY_INFORMATION |
1212 SACL_SECURITY_INFORMATION );
1214 else
1216 if (pipe->maxinstances <= pipe->instances)
1218 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1219 release_object( pipe );
1220 return;
1222 if (pipe->sharing != req->sharing)
1224 set_error( STATUS_ACCESS_DENIED );
1225 release_object( pipe );
1226 return;
1228 clear_error(); /* clear the name collision */
1231 server = create_pipe_server( pipe, req->options, req->flags );
1232 if (server)
1234 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1235 server->pipe->instances++;
1236 release_object( server );
1239 release_object( pipe );
1242 DECL_HANDLER(get_named_pipe_info)
1244 struct pipe_server *server;
1245 struct pipe_client *client = NULL;
1247 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1248 if (!server)
1250 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1251 return;
1253 clear_error();
1254 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1255 0, &pipe_client_ops );
1256 if (!client) return;
1257 server = client->server;
1260 reply->flags = client ? client->pipe_end.flags : server->pipe_end.flags;
1261 if (server)
1263 reply->sharing = server->pipe->sharing;
1264 reply->maxinstances = server->pipe->maxinstances;
1265 reply->instances = server->pipe->instances;
1266 reply->insize = server->pipe->insize;
1267 reply->outsize = server->pipe->outsize;
1270 if (client)
1271 release_object(client);
1272 else
1274 reply->flags |= NAMED_PIPE_SERVER_END;
1275 release_object(server);
1279 DECL_HANDLER(set_named_pipe_info)
1281 struct pipe_server *server;
1282 struct pipe_client *client = NULL;
1284 server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
1285 if (!server)
1287 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1288 return;
1290 clear_error();
1291 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1292 0, &pipe_client_ops );
1293 if (!client) return;
1294 if (!(server = client->server))
1296 release_object( client );
1297 return;
1301 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1302 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1304 set_error( STATUS_INVALID_PARAMETER );
1306 else if (client)
1308 client->pipe_end.flags = server->pipe->flags | req->flags;
1310 else
1312 server->pipe_end.flags = server->pipe->flags | req->flags;
1315 if (client)
1316 release_object(client);
1317 else
1318 release_object(server);