kernel32: Simplify Linux implementation of GlobalMemoryStatusEx.
[wine.git] / server / named_pipe.c
blob11e74573171255c4eb07d4c94fc30597d8dccf85
1 /*
2 * Server-side pipe management
4 * Copyright (C) 1998 Alexandre Julliard
5 * Copyright (C) 2001 Mike McCormack
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * TODO:
22 * message mode
25 #include "config.h"
26 #include "wine/port.h"
28 #include <assert.h>
29 #include <fcntl.h>
30 #include <string.h>
31 #include <stdarg.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/time.h>
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
38 #endif
39 #include <time.h>
40 #include <unistd.h>
41 #ifdef HAVE_POLL_H
42 #include <poll.h>
43 #endif
45 #include "ntstatus.h"
46 #define WIN32_NO_STATUS
47 #include "windef.h"
48 #include "winternl.h"
49 #include "winioctl.h"
51 #include "file.h"
52 #include "handle.h"
53 #include "thread.h"
54 #include "request.h"
55 #include "security.h"
57 enum pipe_state
59 ps_idle_server,
60 ps_wait_open,
61 ps_connected_server,
62 ps_wait_disconnect,
63 ps_wait_connect
66 struct named_pipe;
68 struct pipe_message
70 struct list entry; /* entry in message queue */
71 data_size_t read_pos; /* already read bytes */
72 struct iosb *iosb; /* message iosb */
73 struct async *async; /* async of pending write */
76 struct pipe_end
78 struct object obj; /* object header */
79 struct fd *fd; /* pipe file descriptor */
80 unsigned int flags; /* pipe flags */
81 struct pipe_end *connection; /* the other end of the pipe */
82 data_size_t buffer_size;/* size of buffered data that doesn't block caller */
83 struct list message_queue;
84 struct async_queue *read_q; /* read queue */
85 struct async_queue *write_q; /* write queue */
88 struct pipe_server
90 struct pipe_end pipe_end; /* common header for pipe_client and pipe_server */
91 struct fd *ioctl_fd; /* file descriptor for ioctls when not connected */
92 struct list entry; /* entry in named pipe servers list */
93 enum pipe_state state; /* server state */
94 struct pipe_client *client; /* client that this server is connected to */
95 struct named_pipe *pipe;
96 struct timeout_user *flush_poll;
97 unsigned int options; /* pipe options */
100 struct pipe_client
102 struct pipe_end pipe_end; /* common header for pipe_client and pipe_server */
103 struct pipe_server *server; /* server that this client is connected to */
104 unsigned int flags; /* file flags */
107 struct named_pipe
109 struct object obj; /* object header */
110 unsigned int flags;
111 unsigned int sharing;
112 unsigned int maxinstances;
113 unsigned int outsize;
114 unsigned int insize;
115 unsigned int instances;
116 timeout_t timeout;
117 struct list servers; /* list of servers using this pipe */
118 struct async_queue *waiters; /* list of clients waiting to connect */
121 struct named_pipe_device
123 struct object obj; /* object header */
124 struct fd *fd; /* pseudo-fd for ioctls */
125 struct namespace *pipes; /* named pipe namespace */
128 static void named_pipe_dump( struct object *obj, int verbose );
129 static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
130 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent );
131 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
132 unsigned int sharing, unsigned int options );
133 static void named_pipe_destroy( struct object *obj );
135 static const struct object_ops named_pipe_ops =
137 sizeof(struct named_pipe), /* size */
138 named_pipe_dump, /* dump */
139 no_get_type, /* get_type */
140 no_add_queue, /* add_queue */
141 NULL, /* remove_queue */
142 NULL, /* signaled */
143 NULL, /* satisfied */
144 no_signal, /* signal */
145 no_get_fd, /* get_fd */
146 named_pipe_map_access, /* map_access */
147 default_get_sd, /* get_sd */
148 default_set_sd, /* set_sd */
149 no_lookup_name, /* lookup_name */
150 named_pipe_link_name, /* link_name */
151 default_unlink_name, /* unlink_name */
152 named_pipe_open_file, /* open_file */
153 no_close_handle, /* close_handle */
154 named_pipe_destroy /* destroy */
157 /* common server and client pipe end functions */
158 static obj_handle_t pipe_end_read( struct fd *fd, struct async *async, int blocking, file_pos_t pos );
159 static obj_handle_t pipe_end_write( struct fd *fd, struct async *async_data, int blocking, file_pos_t pos );
160 static void pipe_end_queue_async( struct fd *fd, struct async *async, int type, int count );
161 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue );
163 /* server end functions */
164 static void pipe_server_dump( struct object *obj, int verbose );
165 static struct fd *pipe_server_get_fd( struct object *obj );
166 static void pipe_server_destroy( struct object *obj);
167 static obj_handle_t pipe_server_flush( struct fd *fd, struct async *async, int blocking );
168 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd );
169 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async,
170 int blocking );
172 static const struct object_ops pipe_server_ops =
174 sizeof(struct pipe_server), /* size */
175 pipe_server_dump, /* dump */
176 no_get_type, /* get_type */
177 add_queue, /* add_queue */
178 remove_queue, /* remove_queue */
179 default_fd_signaled, /* signaled */
180 no_satisfied, /* satisfied */
181 no_signal, /* signal */
182 pipe_server_get_fd, /* get_fd */
183 default_fd_map_access, /* map_access */
184 default_get_sd, /* get_sd */
185 default_set_sd, /* set_sd */
186 no_lookup_name, /* lookup_name */
187 no_link_name, /* link_name */
188 NULL, /* unlink_name */
189 no_open_file, /* open_file */
190 fd_close_handle, /* close_handle */
191 pipe_server_destroy /* destroy */
194 static const struct fd_ops pipe_server_fd_ops =
196 default_fd_get_poll_events, /* get_poll_events */
197 default_poll_event, /* poll_event */
198 pipe_server_get_fd_type, /* get_fd_type */
199 pipe_end_read, /* read */
200 pipe_end_write, /* write */
201 pipe_server_flush, /* flush */
202 pipe_server_ioctl, /* ioctl */
203 pipe_end_queue_async, /* queue_async */
204 pipe_end_reselect_async /* reselect_async */
207 /* client end functions */
208 static void pipe_client_dump( struct object *obj, int verbose );
209 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry );
210 static struct fd *pipe_client_get_fd( struct object *obj );
211 static void pipe_client_destroy( struct object *obj );
212 static obj_handle_t pipe_client_flush( struct fd *fd, struct async *async, int blocking );
213 static obj_handle_t pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async,
214 int blocking );
215 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd );
217 static const struct object_ops pipe_client_ops =
219 sizeof(struct pipe_client), /* size */
220 pipe_client_dump, /* dump */
221 no_get_type, /* get_type */
222 add_queue, /* add_queue */
223 remove_queue, /* remove_queue */
224 pipe_client_signaled, /* signaled */
225 no_satisfied, /* satisfied */
226 no_signal, /* signal */
227 pipe_client_get_fd, /* get_fd */
228 default_fd_map_access, /* map_access */
229 default_get_sd, /* get_sd */
230 default_set_sd, /* set_sd */
231 no_lookup_name, /* lookup_name */
232 no_link_name, /* link_name */
233 NULL, /* unlink_name */
234 no_open_file, /* open_file */
235 fd_close_handle, /* close_handle */
236 pipe_client_destroy /* destroy */
239 static const struct fd_ops pipe_client_fd_ops =
241 default_fd_get_poll_events, /* get_poll_events */
242 default_poll_event, /* poll_event */
243 pipe_client_get_fd_type, /* get_fd_type */
244 pipe_end_read, /* read */
245 pipe_end_write, /* write */
246 pipe_client_flush, /* flush */
247 pipe_client_ioctl, /* ioctl */
248 pipe_end_queue_async, /* queue_async */
249 pipe_end_reselect_async /* reselect_async */
252 static void named_pipe_device_dump( struct object *obj, int verbose );
253 static struct object_type *named_pipe_device_get_type( struct object *obj );
254 static struct fd *named_pipe_device_get_fd( struct object *obj );
255 static struct object *named_pipe_device_lookup_name( struct object *obj,
256 struct unicode_str *name, unsigned int attr );
257 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
258 unsigned int sharing, unsigned int options );
259 static void named_pipe_device_destroy( struct object *obj );
260 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
261 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code,
262 struct async *async, int blocking );
264 static const struct object_ops named_pipe_device_ops =
266 sizeof(struct named_pipe_device), /* size */
267 named_pipe_device_dump, /* dump */
268 named_pipe_device_get_type, /* get_type */
269 no_add_queue, /* add_queue */
270 NULL, /* remove_queue */
271 NULL, /* signaled */
272 no_satisfied, /* satisfied */
273 no_signal, /* signal */
274 named_pipe_device_get_fd, /* get_fd */
275 no_map_access, /* map_access */
276 default_get_sd, /* get_sd */
277 default_set_sd, /* set_sd */
278 named_pipe_device_lookup_name, /* lookup_name */
279 directory_link_name, /* link_name */
280 default_unlink_name, /* unlink_name */
281 named_pipe_device_open_file, /* open_file */
282 fd_close_handle, /* close_handle */
283 named_pipe_device_destroy /* destroy */
286 static const struct fd_ops named_pipe_device_fd_ops =
288 default_fd_get_poll_events, /* get_poll_events */
289 default_poll_event, /* poll_event */
290 named_pipe_device_get_fd_type, /* get_fd_type */
291 no_fd_read, /* read */
292 no_fd_write, /* write */
293 no_fd_flush, /* flush */
294 named_pipe_device_ioctl, /* ioctl */
295 default_fd_queue_async, /* queue_async */
296 default_fd_reselect_async /* reselect_async */
299 /* Returns if we handle I/O via server calls. Currently message-mode pipes are handled this way. */
300 static int use_server_io( struct pipe_end *pipe_end )
302 return pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
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, server->state );
326 static void pipe_client_dump( struct object *obj, int verbose )
328 struct pipe_client *client = (struct pipe_client *) obj;
329 assert( obj->ops == &pipe_client_ops );
330 fprintf( stderr, "Named pipe client server=%p\n", client->server );
333 static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry )
335 struct pipe_client *client = (struct pipe_client *) obj;
337 return client->pipe_end.fd && is_fd_signaled(client->pipe_end.fd);
340 static void named_pipe_destroy( struct object *obj)
342 struct named_pipe *pipe = (struct named_pipe *) obj;
344 assert( list_empty( &pipe->servers ) );
345 assert( !pipe->instances );
346 free_async_queue( pipe->waiters );
349 static struct fd *pipe_client_get_fd( struct object *obj )
351 struct pipe_client *client = (struct pipe_client *) obj;
352 if (client->pipe_end.fd)
353 return (struct fd *) grab_object( client->pipe_end.fd );
354 set_error( STATUS_PIPE_DISCONNECTED );
355 return NULL;
358 static void set_server_state( struct pipe_server *server, enum pipe_state state )
360 server->state = state;
362 switch(state)
364 case ps_connected_server:
365 case ps_wait_disconnect:
366 assert( server->pipe_end.fd );
367 break;
368 case ps_wait_open:
369 case ps_idle_server:
370 assert( !server->pipe_end.fd );
371 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
372 break;
373 case ps_wait_connect:
374 assert( !server->pipe_end.fd );
375 set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
376 break;
380 static struct fd *pipe_server_get_fd( struct object *obj )
382 struct pipe_server *server = (struct pipe_server *) obj;
384 return (struct fd *)grab_object( server->pipe_end.fd ? server->pipe_end.fd : server->ioctl_fd );
388 static void notify_empty( struct pipe_server *server )
390 if (!server->flush_poll)
391 return;
392 assert( server->state == ps_connected_server );
393 remove_timeout_user( server->flush_poll );
394 server->flush_poll = NULL;
395 fd_async_wake_up( server->pipe_end.fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
398 static void wake_message( struct pipe_message *message )
400 struct async *async = message->async;
402 message->async = NULL;
403 message->iosb->status = STATUS_SUCCESS;
404 message->iosb->result = message->iosb->in_size;
405 if (async)
407 async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
408 release_object( async );
412 static void free_message( struct pipe_message *message )
414 list_remove( &message->entry );
415 if (message->iosb) release_object( message->iosb );
416 free( message );
419 static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
421 struct pipe_end *connection = pipe_end->connection;
423 pipe_end->connection = NULL;
425 if (use_server_io( pipe_end ))
427 struct pipe_message *message, *next;
428 struct async *async;
429 if (pipe_end->fd) fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
430 async_wake_up( pipe_end->read_q, status );
431 LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
433 async = message->async;
434 if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
435 if (!async) continue;
436 async_terminate( async, status );
437 release_object( async );
439 if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );
441 if (connection)
443 connection->connection = NULL;
444 pipe_end_disconnect( connection, status );
448 static void do_disconnect( struct pipe_server *server )
450 /* we may only have a server fd, if the client disconnected */
451 if (server->client)
453 assert( server->client->server == server );
454 assert( server->client->pipe_end.fd );
455 if (!use_server_io( &server->pipe_end ))
457 release_object( server->client->pipe_end.fd );
458 server->client->pipe_end.fd = NULL;
461 assert( server->pipe_end.fd );
462 if (!use_server_io( &server->pipe_end ))
463 shutdown( get_unix_fd( server->pipe_end.fd ), SHUT_RDWR );
464 release_object( server->pipe_end.fd );
465 server->pipe_end.fd = NULL;
468 static void pipe_end_destroy( struct pipe_end *pipe_end )
470 struct pipe_message *message;
472 while (!list_empty( &pipe_end->message_queue ))
474 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
475 assert( !message->async );
476 free_message( message );
479 free_async_queue( pipe_end->read_q );
480 free_async_queue( pipe_end->write_q );
483 static void pipe_server_destroy( struct object *obj)
485 struct pipe_server *server = (struct pipe_server *)obj;
487 assert( obj->ops == &pipe_server_ops );
489 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_BROKEN );
491 if (server->pipe_end.fd)
493 notify_empty( server );
494 do_disconnect( server );
497 pipe_end_destroy( &server->pipe_end );
498 if (server->client)
500 server->client->server = NULL;
501 server->client = NULL;
504 assert( server->pipe->instances );
505 server->pipe->instances--;
507 if (server->ioctl_fd) release_object( server->ioctl_fd );
508 list_remove( &server->entry );
509 release_object( server->pipe );
512 static void pipe_client_destroy( struct object *obj)
514 struct pipe_client *client = (struct pipe_client *)obj;
515 struct pipe_server *server = client->server;
517 assert( obj->ops == &pipe_client_ops );
519 pipe_end_disconnect( &client->pipe_end, STATUS_PIPE_BROKEN );
521 if (server)
523 notify_empty( server );
525 switch(server->state)
527 case ps_connected_server:
528 /* Don't destroy the server's fd here as we can't
529 do a successful flush without it. */
530 set_server_state( server, ps_wait_disconnect );
531 break;
532 case ps_idle_server:
533 case ps_wait_open:
534 case ps_wait_disconnect:
535 case ps_wait_connect:
536 assert( 0 );
538 assert( server->client );
539 server->client = NULL;
540 client->server = NULL;
543 pipe_end_destroy( &client->pipe_end );
544 if (client->pipe_end.fd) release_object( client->pipe_end.fd );
547 static void named_pipe_device_dump( struct object *obj, int verbose )
549 fputs( "Named pipe device\n", stderr );
552 static struct object_type *named_pipe_device_get_type( struct object *obj )
554 static const WCHAR name[] = {'D','e','v','i','c','e'};
555 static const struct unicode_str str = { name, sizeof(name) };
556 return get_object_type( &str );
559 static struct fd *named_pipe_device_get_fd( struct object *obj )
561 struct named_pipe_device *device = (struct named_pipe_device *)obj;
562 return (struct fd *)grab_object( device->fd );
565 static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
566 unsigned int attr )
568 struct named_pipe_device *device = (struct named_pipe_device*)obj;
569 struct object *found;
571 assert( obj->ops == &named_pipe_device_ops );
572 assert( device->pipes );
574 if (!name) return NULL; /* open the device itself */
576 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
577 name->len = 0;
579 return found;
582 static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
583 unsigned int sharing, unsigned int options )
585 return grab_object( obj );
588 static void named_pipe_device_destroy( struct object *obj )
590 struct named_pipe_device *device = (struct named_pipe_device*)obj;
591 assert( obj->ops == &named_pipe_device_ops );
592 if (device->fd) release_object( device->fd );
593 free( device->pipes );
596 static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
598 return FD_TYPE_DEVICE;
601 struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name )
603 struct named_pipe_device *dev;
605 if ((dev = create_named_object( root, &named_pipe_device_ops, name, 0, NULL )) &&
606 get_error() != STATUS_OBJECT_NAME_EXISTS)
608 dev->pipes = NULL;
609 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
610 !(dev->pipes = create_namespace( 7 )))
612 release_object( dev );
613 dev = NULL;
616 return &dev->obj;
619 static int pipe_data_remaining( struct pipe_server *server )
621 struct pollfd pfd;
622 int fd;
624 assert( server->client );
626 if (use_server_io( &server->pipe_end ))
627 return !list_empty( &server->client->pipe_end.message_queue );
629 fd = get_unix_fd( server->client->pipe_end.fd );
630 if (fd < 0)
631 return 0;
632 pfd.fd = fd;
633 pfd.events = POLLIN;
634 pfd.revents = 0;
636 if (0 > poll( &pfd, 1, 0 ))
637 return 0;
639 return pfd.revents&POLLIN;
642 static void check_flushed( void *arg )
644 struct pipe_server *server = (struct pipe_server*) arg;
646 if (pipe_data_remaining( server ))
648 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
650 else
652 server->flush_poll = NULL;
653 fd_async_wake_up( server->pipe_end.fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
657 static obj_handle_t pipe_end_flush( struct pipe_end *pipe_end, struct async *async, int blocking )
659 obj_handle_t handle = 0;
661 if (!fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT )) return 0;
663 if (!blocking || (handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 )))
664 set_error( STATUS_PENDING );
665 return handle;
668 static obj_handle_t pipe_server_flush( struct fd *fd, struct async *async, int blocking )
670 struct pipe_server *server = get_fd_user( fd );
671 obj_handle_t handle;
673 if (!server || server->state != ps_connected_server) return 0;
675 if (!pipe_data_remaining( server )) return 0;
677 handle = pipe_end_flush( &server->pipe_end, async, blocking );
679 /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
680 if (handle && !use_server_io( &server->pipe_end ) && !server->flush_poll)
681 server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
682 return handle;
685 static obj_handle_t pipe_client_flush( struct fd *fd, struct async *async, int blocking )
687 /* FIXME: what do we have to do for this? */
688 return 0;
691 static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
693 struct pipe_message *message;
694 data_size_t avail = 0;
696 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
698 avail += message->iosb->in_size - message->read_pos;
699 if (avail >= iosb->out_size) break;
701 iosb->out_size = min( iosb->out_size, avail );
702 iosb->status = STATUS_SUCCESS;
704 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
705 if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
707 iosb->out_data = message->iosb->in_data;
708 message->iosb->in_data = NULL;
709 wake_message( message );
710 free_message( message );
712 else
714 data_size_t write_pos = 0, writing;
715 char *buf = NULL;
717 if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
719 iosb->out_size = 0;
720 iosb->status = STATUS_NO_MEMORY;
721 return;
726 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
727 writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
728 if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
729 write_pos += writing;
730 message->read_pos += writing;
731 if (message->read_pos == message->iosb->in_size)
733 wake_message(message);
734 free_message(message);
736 } while (write_pos < iosb->out_size);
738 iosb->result = iosb->out_size;
741 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
742 * We're not interested in such reselect calls, so we ignore them. */
743 static int ignore_reselect;
745 static void reselect_write_queue( struct pipe_end *pipe_end );
747 static void reselect_read_queue( struct pipe_end *pipe_end )
749 struct async *async;
750 struct iosb *iosb;
751 int read_done = 0;
753 ignore_reselect = 1;
754 while (!list_empty( &pipe_end->message_queue) && (async = find_pending_async( pipe_end->read_q )))
756 iosb = async_get_iosb( async );
757 message_queue_read( pipe_end, iosb );
758 async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
759 release_object( async );
760 release_object( iosb );
761 read_done = 1;
763 ignore_reselect = 0;
765 if (pipe_end->connection)
767 if (list_empty( &pipe_end->message_queue ))
768 fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
769 else if (read_done)
770 reselect_write_queue( pipe_end->connection );
774 static void reselect_write_queue( struct pipe_end *pipe_end )
776 struct pipe_message *message, *next;
777 struct pipe_end *reader = pipe_end->connection;
778 data_size_t avail = 0;
780 if (!reader) return;
782 ignore_reselect = 1;
784 LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
786 if (message->async && message->iosb->status != STATUS_PENDING)
788 release_object( message->async );
789 message->async = NULL;
790 free_message( message );
792 else
794 avail += message->iosb->in_size - message->read_pos;
795 if (message->iosb->status == STATUS_PENDING && (avail <= reader->buffer_size || !message->iosb->in_size))
796 wake_message( message );
800 ignore_reselect = 0;
801 reselect_read_queue( reader );
804 static obj_handle_t pipe_end_read( struct fd *fd, struct async *async, int blocking, file_pos_t pos )
806 struct pipe_end *pipe_end = get_fd_user( fd );
807 obj_handle_t handle = 0;
809 if (!use_server_io( pipe_end )) return no_fd_read( fd, async, blocking, pos );
811 if (!pipe_end->connection && list_empty( &pipe_end->message_queue ))
813 set_error( STATUS_PIPE_BROKEN );
814 return 0;
817 if (!pipe_end->read_q && !(pipe_end->read_q = create_async_queue( fd ))) return 0;
818 if (!(handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 ))) return 0;
820 queue_async( pipe_end->read_q, async );
821 reselect_read_queue( pipe_end );
822 set_error( STATUS_PENDING );
824 if (!blocking)
826 struct iosb *iosb;
827 iosb = async_get_iosb( async );
828 if (iosb->status == STATUS_PENDING)
830 close_handle( current->process, handle );
831 handle = 0;
833 release_object( iosb );
835 return handle;
838 static obj_handle_t pipe_end_write( struct fd *fd, struct async *async, int blocking, file_pos_t pos )
840 struct pipe_end *write_end = get_fd_user( fd );
841 struct pipe_end *read_end = write_end->connection;
842 struct pipe_message *message;
843 obj_handle_t handle = 0;
845 if (!use_server_io( write_end )) return no_fd_write( fd, async, blocking, pos );
847 if (!read_end)
849 set_error( STATUS_PIPE_DISCONNECTED );
850 return 0;
853 if (!write_end->write_q && !(write_end->write_q = create_async_queue( fd ))) return 0;
854 if (!(handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 ))) return 0;
856 if (!(message = mem_alloc( sizeof(*message) )))
858 close_handle( current->process, handle );
859 return 0;
861 message->async = (struct async *)grab_object( async );
862 message->iosb = async_get_iosb( async );
863 message->read_pos = 0;
864 list_add_tail( &read_end->message_queue, &message->entry );
866 queue_async( write_end->write_q, async );
867 reselect_write_queue( write_end );
868 set_error( STATUS_PENDING );
870 if (!blocking)
872 struct iosb *iosb;
873 iosb = async_get_iosb( async );
874 if (iosb->status == STATUS_PENDING)
876 close_handle( current->process, handle );
877 handle = 0;
879 release_object( iosb );
881 return handle;
884 static void pipe_end_queue_async( struct fd *fd, struct async *async, int type, int count )
886 struct pipe_end *pipe_end = get_fd_user( fd );
887 if (use_server_io( pipe_end )) no_fd_queue_async( fd, async, type, count );
888 else default_fd_queue_async( fd, async, type, count );
891 static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
893 struct pipe_end *pipe_end = get_fd_user( fd );
895 if (ignore_reselect) return;
897 if (!use_server_io( pipe_end ))
898 default_fd_reselect_async( fd, queue );
899 else if (pipe_end->write_q && pipe_end->write_q == queue)
900 reselect_write_queue( pipe_end );
901 else if (pipe_end->read_q && pipe_end->read_q == queue)
902 reselect_read_queue( pipe_end );
905 static inline int is_overlapped( unsigned int options )
907 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
910 static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
912 return FD_TYPE_PIPE;
915 static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
917 return FD_TYPE_PIPE;
920 static void 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;
927 if (!use_server_io( pipe_end ))
929 set_error( STATUS_NOT_SUPPORTED );
930 return;
933 if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
935 set_error( STATUS_INFO_LENGTH_MISMATCH );
936 return;
938 reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );
940 LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
941 avail += message->iosb->in_size - message->read_pos;
943 if (avail)
945 message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
946 reply_size = min( reply_size, message->iosb->in_size - message->read_pos );
948 else reply_size = 0;
950 if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return;
951 buffer->NamedPipeState = 0; /* FIXME */
952 buffer->ReadDataAvailable = avail;
953 buffer->NumberOfMessages = 0; /* FIXME */
954 buffer->MessageLength = 0; /* FIXME */
955 if (reply_size) memcpy( buffer->Data, (const char *)message->iosb->in_data + message->read_pos, reply_size );
958 static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async,
959 int blocking )
961 struct pipe_server *server = get_fd_user( fd );
962 obj_handle_t wait_handle = 0;
964 switch(code)
966 case FSCTL_PIPE_LISTEN:
967 switch(server->state)
969 case ps_idle_server:
970 case ps_wait_connect:
971 if (fd_queue_async( server->ioctl_fd, async, ASYNC_TYPE_WAIT ))
973 if (blocking) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
974 set_server_state( server, ps_wait_open );
975 if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
976 set_error( STATUS_PENDING );
977 return wait_handle;
979 break;
980 case ps_connected_server:
981 set_error( STATUS_PIPE_CONNECTED );
982 break;
983 case ps_wait_disconnect:
984 set_error( STATUS_NO_DATA_DETECTED );
985 break;
986 case ps_wait_open:
987 set_error( STATUS_INVALID_HANDLE );
988 break;
990 return 0;
992 case FSCTL_PIPE_DISCONNECT:
993 switch(server->state)
995 case ps_connected_server:
996 assert( server->client );
997 assert( server->client->pipe_end.fd );
999 notify_empty( server );
1001 /* dump the client and server fds - client loses all waiting data */
1002 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1003 do_disconnect( server );
1004 server->client->server = NULL;
1005 server->client = NULL;
1006 set_server_state( server, ps_wait_connect );
1007 break;
1008 case ps_wait_disconnect:
1009 assert( !server->client );
1010 pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1011 do_disconnect( server );
1012 set_server_state( server, ps_wait_connect );
1013 break;
1014 case ps_idle_server:
1015 case ps_wait_open:
1016 set_error( STATUS_PIPE_LISTENING );
1017 break;
1018 case ps_wait_connect:
1019 set_error( STATUS_PIPE_DISCONNECTED );
1020 break;
1022 return 0;
1024 case FSCTL_PIPE_PEEK:
1025 pipe_end_peek( &server->pipe_end );
1026 return 0;
1028 default:
1029 return default_fd_ioctl( fd, code, async, blocking );
1033 static obj_handle_t pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async,
1034 int blocking )
1036 struct pipe_client *client = get_fd_user( fd );
1038 switch(code)
1040 case FSCTL_PIPE_PEEK:
1041 pipe_end_peek( &client->pipe_end );
1042 return 0;
1044 default:
1045 return default_fd_ioctl( fd, code, async, blocking );
1049 static struct pipe_server *get_pipe_server_obj( struct process *process,
1050 obj_handle_t handle, unsigned int access )
1052 struct object *obj;
1053 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
1054 return (struct pipe_server *) obj;
1057 static void init_pipe_end( struct pipe_end *pipe_end, unsigned int pipe_flags, data_size_t buffer_size )
1059 pipe_end->fd = NULL;
1060 pipe_end->flags = pipe_flags;
1061 pipe_end->connection = NULL;
1062 pipe_end->buffer_size = buffer_size;
1063 pipe_end->read_q = NULL;
1064 pipe_end->write_q = NULL;
1065 list_init( &pipe_end->message_queue );
1068 static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
1069 unsigned int pipe_flags )
1071 struct pipe_server *server;
1073 server = alloc_object( &pipe_server_ops );
1074 if (!server)
1075 return NULL;
1077 server->pipe = pipe;
1078 server->client = NULL;
1079 server->flush_poll = NULL;
1080 server->options = options;
1081 init_pipe_end( &server->pipe_end, pipe_flags, pipe->insize );
1083 list_add_head( &pipe->servers, &server->entry );
1084 grab_object( pipe );
1085 if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1087 release_object( server );
1088 return NULL;
1090 set_fd_signaled( server->ioctl_fd, 1 );
1091 set_server_state( server, ps_idle_server );
1092 return server;
1095 static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags, data_size_t buffer_size )
1097 struct pipe_client *client;
1099 client = alloc_object( &pipe_client_ops );
1100 if (!client)
1101 return NULL;
1103 client->server = NULL;
1104 client->flags = flags;
1105 init_pipe_end( &client->pipe_end, pipe_flags, buffer_size );
1107 return client;
1110 static struct pipe_server *find_available_server( struct named_pipe *pipe )
1112 struct pipe_server *server;
1114 /* look for pipe servers that are listening */
1115 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1117 if (server->state == ps_wait_open)
1118 return (struct pipe_server *)grab_object( server );
1121 /* fall back to pipe servers that are idle */
1122 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
1124 if (server->state == ps_idle_server)
1125 return (struct pipe_server *)grab_object( server );
1128 return NULL;
1131 static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
1133 struct named_pipe_device *dev = (struct named_pipe_device *)parent;
1135 if (parent->ops != &named_pipe_device_ops)
1137 set_error( STATUS_OBJECT_NAME_INVALID );
1138 return 0;
1140 namespace_add( dev->pipes, name );
1141 name->parent = grab_object( parent );
1142 return 1;
1145 static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
1146 unsigned int sharing, unsigned int options )
1148 struct named_pipe *pipe = (struct named_pipe *)obj;
1149 struct pipe_server *server;
1150 struct pipe_client *client;
1151 unsigned int pipe_sharing;
1152 int fds[2];
1154 if (!(server = find_available_server( pipe )))
1156 set_error( STATUS_PIPE_NOT_AVAILABLE );
1157 return NULL;
1160 pipe_sharing = server->pipe->sharing;
1161 if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
1162 ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
1164 set_error( STATUS_ACCESS_DENIED );
1165 release_object( server );
1166 return NULL;
1169 if ((client = create_pipe_client( options, pipe->flags, pipe->outsize )))
1171 if (use_server_io( &server->pipe_end ))
1173 client->pipe_end.fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->pipe_end.obj, options );
1174 if (client->pipe_end.fd)
1176 set_fd_signaled( client->pipe_end.fd, 1 );
1177 server->pipe_end.fd = (struct fd *)grab_object( server->ioctl_fd );
1178 set_no_fd_status( server->ioctl_fd, STATUS_BAD_DEVICE_TYPE );
1180 else
1182 release_object( client );
1183 client = NULL;
1186 else if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
1188 assert( !server->pipe_end.fd );
1190 /* for performance reasons, only set nonblocking mode when using
1191 * overlapped I/O. Otherwise, we will be doing too much busy
1192 * looping */
1193 if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
1194 if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
1196 if (pipe->insize)
1198 setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
1199 setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
1201 if (pipe->outsize)
1203 setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
1204 setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
1207 client->pipe_end.fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->pipe_end.obj, options );
1208 server->pipe_end.fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->pipe_end.obj, server->options );
1209 if (client->pipe_end.fd && server->pipe_end.fd)
1211 fd_copy_completion( server->ioctl_fd, server->pipe_end.fd );
1213 else
1215 release_object( client );
1216 client = NULL;
1219 else
1221 file_set_error();
1222 release_object( client );
1223 client = NULL;
1225 if (client)
1227 allow_fd_caching( client->pipe_end.fd );
1228 allow_fd_caching( server->pipe_end.fd );
1229 if (server->state == ps_wait_open)
1230 fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
1231 set_server_state( server, ps_connected_server );
1232 server->client = client;
1233 client->server = server;
1234 server->pipe_end.connection = &client->pipe_end;
1235 client->pipe_end.connection = &server->pipe_end;
1238 release_object( server );
1239 return &client->pipe_end.obj;
1242 static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code,
1243 struct async *async, int blocking )
1245 struct named_pipe_device *device = get_fd_user( fd );
1247 switch(code)
1249 case FSCTL_PIPE_WAIT:
1251 const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
1252 data_size_t size = get_req_data_size();
1253 obj_handle_t wait_handle = 0;
1254 struct named_pipe *pipe;
1255 struct pipe_server *server;
1256 struct unicode_str name;
1257 timeout_t when;
1259 if (size < sizeof(*buffer) ||
1260 size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
1262 set_error( STATUS_INVALID_PARAMETER );
1263 return 0;
1265 name.str = buffer->Name;
1266 name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1267 if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;
1269 if (!(server = find_available_server( pipe )))
1271 if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;
1273 queue_async( pipe->waiters, async );
1274 when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
1275 async_set_timeout( async, when, STATUS_IO_TIMEOUT );
1276 if (blocking) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
1277 set_error( STATUS_PENDING );
1279 else release_object( server );
1281 done:
1282 release_object( pipe );
1283 return wait_handle;
1286 default:
1287 return default_fd_ioctl( fd, code, async, blocking );
1292 DECL_HANDLER(create_named_pipe)
1294 struct named_pipe *pipe;
1295 struct pipe_server *server;
1296 struct unicode_str name;
1297 struct object *root;
1298 const struct security_descriptor *sd;
1299 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1301 if (!objattr) return;
1303 if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
1304 (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
1306 if (root) release_object( root );
1307 set_error( STATUS_INVALID_PARAMETER );
1308 return;
1311 if (!name.len) /* pipes need a root directory even without a name */
1313 if (!objattr->rootdir)
1315 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
1316 return;
1318 if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
1321 pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1323 if (root) release_object( root );
1324 if (!pipe) return;
1326 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1328 /* initialize it if it didn't already exist */
1329 pipe->instances = 0;
1330 pipe->waiters = NULL;
1331 list_init( &pipe->servers );
1332 pipe->insize = req->insize;
1333 pipe->outsize = req->outsize;
1334 pipe->maxinstances = req->maxinstances;
1335 pipe->timeout = req->timeout;
1336 pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
1337 pipe->sharing = req->sharing;
1339 else
1341 if (pipe->maxinstances <= pipe->instances)
1343 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1344 release_object( pipe );
1345 return;
1347 if (pipe->sharing != req->sharing)
1349 set_error( STATUS_ACCESS_DENIED );
1350 release_object( pipe );
1351 return;
1353 clear_error(); /* clear the name collision */
1356 server = create_pipe_server( pipe, req->options, req->flags );
1357 if (server)
1359 reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1360 server->pipe->instances++;
1361 if (sd) default_set_sd( &server->pipe_end.obj, sd, OWNER_SECURITY_INFORMATION |
1362 GROUP_SECURITY_INFORMATION |
1363 DACL_SECURITY_INFORMATION |
1364 SACL_SECURITY_INFORMATION );
1365 release_object( server );
1368 release_object( pipe );
1371 DECL_HANDLER(get_named_pipe_info)
1373 struct pipe_server *server;
1374 struct pipe_client *client = NULL;
1376 server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1377 if (!server)
1379 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1380 return;
1382 clear_error();
1383 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1384 0, &pipe_client_ops );
1385 if (!client) return;
1386 server = client->server;
1389 reply->flags = client ? client->pipe_end.flags : server->pipe_end.flags;
1390 if (server)
1392 reply->sharing = server->pipe->sharing;
1393 reply->maxinstances = server->pipe->maxinstances;
1394 reply->instances = server->pipe->instances;
1395 reply->insize = server->pipe->insize;
1396 reply->outsize = server->pipe->outsize;
1399 if (client)
1400 release_object(client);
1401 else
1403 reply->flags |= NAMED_PIPE_SERVER_END;
1404 release_object(server);
1408 DECL_HANDLER(set_named_pipe_info)
1410 struct pipe_server *server;
1411 struct pipe_client *client = NULL;
1413 server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
1414 if (!server)
1416 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
1417 return;
1419 clear_error();
1420 client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1421 0, &pipe_client_ops );
1422 if (!client) return;
1423 if (!(server = client->server))
1425 release_object( client );
1426 return;
1430 if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1431 ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
1433 set_error( STATUS_INVALID_PARAMETER );
1435 else if (client)
1437 client->pipe_end.flags = server->pipe->flags | req->flags;
1439 else
1441 server->pipe_end.flags = server->pipe->flags | req->flags;
1444 if (client)
1445 release_object(client);
1446 else
1447 release_object(server);