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
24 #include "wine/port.h"
33 #include <sys/types.h>
34 #ifdef HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
44 #define WIN32_NO_STATUS
68 struct list entry
; /* entry in message queue */
69 data_size_t read_pos
; /* already read bytes */
70 struct iosb
*iosb
; /* message iosb */
71 struct async
*async
; /* async of pending write */
76 struct object obj
; /* object header */
77 struct fd
*fd
; /* pipe file descriptor */
78 unsigned int flags
; /* pipe flags */
79 struct pipe_end
*connection
; /* the other end of the pipe */
80 data_size_t buffer_size
;/* size of buffered data that doesn't block caller */
81 struct list message_queue
;
82 struct async_queue read_q
; /* read queue */
83 struct async_queue write_q
; /* write queue */
88 struct pipe_end pipe_end
; /* common header for pipe_client and pipe_server */
89 struct fd
*ioctl_fd
; /* file descriptor for ioctls when not connected */
90 struct list entry
; /* entry in named pipe servers list */
91 enum pipe_state state
; /* server state */
92 struct pipe_client
*client
; /* client that this server is connected to */
93 struct named_pipe
*pipe
;
94 struct timeout_user
*flush_poll
;
95 unsigned int options
; /* pipe options */
100 struct pipe_end pipe_end
; /* common header for pipe_client and pipe_server */
101 struct pipe_server
*server
; /* server that this client is connected to */
102 unsigned int flags
; /* file flags */
107 struct object obj
; /* object header */
109 unsigned int sharing
;
110 unsigned int maxinstances
;
111 unsigned int outsize
;
113 unsigned int instances
;
115 struct list servers
; /* list of servers using this pipe */
116 struct async_queue waiters
; /* list of clients waiting to connect */
119 struct named_pipe_device
121 struct object obj
; /* object header */
122 struct fd
*fd
; /* pseudo-fd for ioctls */
123 struct namespace *pipes
; /* named pipe namespace */
126 static void named_pipe_dump( struct object
*obj
, int verbose
);
127 static unsigned int named_pipe_map_access( struct object
*obj
, unsigned int access
);
128 static int named_pipe_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
);
129 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
130 unsigned int sharing
, unsigned int options
);
131 static void named_pipe_destroy( struct object
*obj
);
133 static const struct object_ops named_pipe_ops
=
135 sizeof(struct named_pipe
), /* size */
136 named_pipe_dump
, /* dump */
137 no_get_type
, /* get_type */
138 no_add_queue
, /* add_queue */
139 NULL
, /* remove_queue */
141 NULL
, /* satisfied */
142 no_signal
, /* signal */
143 no_get_fd
, /* get_fd */
144 named_pipe_map_access
, /* map_access */
145 default_get_sd
, /* get_sd */
146 default_set_sd
, /* set_sd */
147 no_lookup_name
, /* lookup_name */
148 named_pipe_link_name
, /* link_name */
149 default_unlink_name
, /* unlink_name */
150 named_pipe_open_file
, /* open_file */
151 no_close_handle
, /* close_handle */
152 named_pipe_destroy
/* destroy */
155 /* common server and client pipe end functions */
156 static enum server_fd_type
pipe_end_get_fd_type( struct fd
*fd
);
157 static int pipe_end_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
);
158 static int pipe_end_write( struct fd
*fd
, struct async
*async_data
, file_pos_t pos
);
159 static void pipe_end_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
);
160 static void pipe_end_reselect_async( struct fd
*fd
, struct async_queue
*queue
);
162 /* server end functions */
163 static void pipe_server_dump( struct object
*obj
, int verbose
);
164 static struct fd
*pipe_server_get_fd( struct object
*obj
);
165 static void pipe_server_destroy( struct object
*obj
);
166 static int pipe_server_flush( struct fd
*fd
, struct async
*async
);
167 static int pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
169 static const struct object_ops pipe_server_ops
=
171 sizeof(struct pipe_server
), /* size */
172 pipe_server_dump
, /* dump */
173 no_get_type
, /* get_type */
174 add_queue
, /* add_queue */
175 remove_queue
, /* remove_queue */
176 default_fd_signaled
, /* signaled */
177 no_satisfied
, /* satisfied */
178 no_signal
, /* signal */
179 pipe_server_get_fd
, /* get_fd */
180 default_fd_map_access
, /* map_access */
181 default_get_sd
, /* get_sd */
182 default_set_sd
, /* set_sd */
183 no_lookup_name
, /* lookup_name */
184 no_link_name
, /* link_name */
185 NULL
, /* unlink_name */
186 no_open_file
, /* open_file */
187 fd_close_handle
, /* close_handle */
188 pipe_server_destroy
/* destroy */
191 static const struct fd_ops pipe_server_fd_ops
=
193 default_fd_get_poll_events
, /* get_poll_events */
194 default_poll_event
, /* poll_event */
195 pipe_end_get_fd_type
, /* get_fd_type */
196 pipe_end_read
, /* read */
197 pipe_end_write
, /* write */
198 pipe_server_flush
, /* flush */
199 pipe_server_ioctl
, /* ioctl */
200 pipe_end_queue_async
, /* queue_async */
201 pipe_end_reselect_async
/* reselect_async */
204 /* client end functions */
205 static void pipe_client_dump( struct object
*obj
, int verbose
);
206 static int pipe_client_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
207 static struct fd
*pipe_client_get_fd( struct object
*obj
);
208 static void pipe_client_destroy( struct object
*obj
);
209 static int pipe_client_flush( struct fd
*fd
, struct async
*async
);
210 static int pipe_client_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
212 static const struct object_ops pipe_client_ops
=
214 sizeof(struct pipe_client
), /* size */
215 pipe_client_dump
, /* dump */
216 no_get_type
, /* get_type */
217 add_queue
, /* add_queue */
218 remove_queue
, /* remove_queue */
219 pipe_client_signaled
, /* signaled */
220 no_satisfied
, /* satisfied */
221 no_signal
, /* signal */
222 pipe_client_get_fd
, /* get_fd */
223 default_fd_map_access
, /* map_access */
224 default_get_sd
, /* get_sd */
225 default_set_sd
, /* set_sd */
226 no_lookup_name
, /* lookup_name */
227 no_link_name
, /* link_name */
228 NULL
, /* unlink_name */
229 no_open_file
, /* open_file */
230 fd_close_handle
, /* close_handle */
231 pipe_client_destroy
/* destroy */
234 static const struct fd_ops pipe_client_fd_ops
=
236 default_fd_get_poll_events
, /* get_poll_events */
237 default_poll_event
, /* poll_event */
238 pipe_end_get_fd_type
, /* get_fd_type */
239 pipe_end_read
, /* read */
240 pipe_end_write
, /* write */
241 pipe_client_flush
, /* flush */
242 pipe_client_ioctl
, /* ioctl */
243 pipe_end_queue_async
, /* queue_async */
244 pipe_end_reselect_async
/* reselect_async */
247 static void named_pipe_device_dump( struct object
*obj
, int verbose
);
248 static struct object_type
*named_pipe_device_get_type( struct object
*obj
);
249 static struct fd
*named_pipe_device_get_fd( struct object
*obj
);
250 static struct object
*named_pipe_device_lookup_name( struct object
*obj
,
251 struct unicode_str
*name
, unsigned int attr
);
252 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
253 unsigned int sharing
, unsigned int options
);
254 static void named_pipe_device_destroy( struct object
*obj
);
255 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
);
256 static int named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
);
258 static const struct object_ops named_pipe_device_ops
=
260 sizeof(struct named_pipe_device
), /* size */
261 named_pipe_device_dump
, /* dump */
262 named_pipe_device_get_type
, /* get_type */
263 no_add_queue
, /* add_queue */
264 NULL
, /* remove_queue */
266 no_satisfied
, /* satisfied */
267 no_signal
, /* signal */
268 named_pipe_device_get_fd
, /* get_fd */
269 no_map_access
, /* map_access */
270 default_get_sd
, /* get_sd */
271 default_set_sd
, /* set_sd */
272 named_pipe_device_lookup_name
, /* lookup_name */
273 directory_link_name
, /* link_name */
274 default_unlink_name
, /* unlink_name */
275 named_pipe_device_open_file
, /* open_file */
276 fd_close_handle
, /* close_handle */
277 named_pipe_device_destroy
/* destroy */
280 static const struct fd_ops named_pipe_device_fd_ops
=
282 default_fd_get_poll_events
, /* get_poll_events */
283 default_poll_event
, /* poll_event */
284 named_pipe_device_get_fd_type
, /* get_fd_type */
285 no_fd_read
, /* read */
286 no_fd_write
, /* write */
287 no_fd_flush
, /* flush */
288 named_pipe_device_ioctl
, /* ioctl */
289 default_fd_queue_async
, /* queue_async */
290 default_fd_reselect_async
/* reselect_async */
293 /* Returns if we handle I/O via server calls. Currently message-mode pipes are handled this way. */
294 static int use_server_io( struct pipe_end
*pipe_end
)
296 return pipe_end
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
;
299 static void named_pipe_dump( struct object
*obj
, int verbose
)
301 fputs( "Named pipe\n", stderr
);
304 static unsigned int named_pipe_map_access( struct object
*obj
, unsigned int access
)
306 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
;
307 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| FILE_CREATE_PIPE_INSTANCE
;
308 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
309 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_ALL
;
310 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
313 static void pipe_server_dump( struct object
*obj
, int verbose
)
315 struct pipe_server
*server
= (struct pipe_server
*) obj
;
316 assert( obj
->ops
== &pipe_server_ops
);
317 fprintf( stderr
, "Named pipe server pipe=%p state=%d\n", server
->pipe
, server
->state
);
320 static void pipe_client_dump( struct object
*obj
, int verbose
)
322 struct pipe_client
*client
= (struct pipe_client
*) obj
;
323 assert( obj
->ops
== &pipe_client_ops
);
324 fprintf( stderr
, "Named pipe client server=%p\n", client
->server
);
327 static int pipe_client_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
329 struct pipe_client
*client
= (struct pipe_client
*) obj
;
331 return client
->pipe_end
.fd
&& is_fd_signaled(client
->pipe_end
.fd
);
334 static void named_pipe_destroy( struct object
*obj
)
336 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
338 assert( list_empty( &pipe
->servers
) );
339 assert( !pipe
->instances
);
340 free_async_queue( &pipe
->waiters
);
343 static struct fd
*pipe_client_get_fd( struct object
*obj
)
345 struct pipe_client
*client
= (struct pipe_client
*) obj
;
346 if (client
->pipe_end
.fd
)
347 return (struct fd
*) grab_object( client
->pipe_end
.fd
);
348 set_error( STATUS_PIPE_DISCONNECTED
);
352 static void set_server_state( struct pipe_server
*server
, enum pipe_state state
)
354 server
->state
= state
;
358 case ps_connected_server
:
359 case ps_wait_disconnect
:
360 assert( server
->pipe_end
.fd
);
364 assert( !server
->pipe_end
.fd
);
365 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_LISTENING
);
367 case ps_wait_connect
:
368 assert( !server
->pipe_end
.fd
);
369 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_DISCONNECTED
);
374 static struct fd
*pipe_server_get_fd( struct object
*obj
)
376 struct pipe_server
*server
= (struct pipe_server
*) obj
;
378 return (struct fd
*)grab_object( server
->pipe_end
.fd
? server
->pipe_end
.fd
: server
->ioctl_fd
);
382 static void notify_empty( struct pipe_server
*server
)
384 if (!server
->flush_poll
)
386 assert( server
->state
== ps_connected_server
);
387 remove_timeout_user( server
->flush_poll
);
388 server
->flush_poll
= NULL
;
389 fd_async_wake_up( server
->pipe_end
.fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
392 static void wake_message( struct pipe_message
*message
)
394 struct async
*async
= message
->async
;
396 message
->async
= NULL
;
397 message
->iosb
->status
= STATUS_SUCCESS
;
398 message
->iosb
->result
= message
->iosb
->in_size
;
401 async_terminate( async
, message
->iosb
->result
? STATUS_ALERTED
: STATUS_SUCCESS
);
402 release_object( async
);
406 static void free_message( struct pipe_message
*message
)
408 list_remove( &message
->entry
);
409 if (message
->iosb
) release_object( message
->iosb
);
413 static void pipe_end_disconnect( struct pipe_end
*pipe_end
, unsigned int status
)
415 struct pipe_end
*connection
= pipe_end
->connection
;
417 pipe_end
->connection
= NULL
;
419 if (use_server_io( pipe_end
))
421 struct pipe_message
*message
, *next
;
423 if (pipe_end
->fd
) fd_async_wake_up( pipe_end
->fd
, ASYNC_TYPE_WAIT
, status
);
424 async_wake_up( &pipe_end
->read_q
, status
);
425 LIST_FOR_EACH_ENTRY_SAFE( message
, next
, &pipe_end
->message_queue
, struct pipe_message
, entry
)
427 async
= message
->async
;
428 if (async
|| status
== STATUS_PIPE_DISCONNECTED
) free_message( message
);
429 if (!async
) continue;
430 async_terminate( async
, status
);
431 release_object( async
);
433 if (status
== STATUS_PIPE_DISCONNECTED
) set_fd_signaled( pipe_end
->fd
, 0 );
437 connection
->connection
= NULL
;
438 pipe_end_disconnect( connection
, status
);
442 static void do_disconnect( struct pipe_server
*server
)
444 /* we may only have a server fd, if the client disconnected */
447 assert( server
->client
->server
== server
);
448 assert( server
->client
->pipe_end
.fd
);
449 if (!use_server_io( &server
->pipe_end
))
451 release_object( server
->client
->pipe_end
.fd
);
452 server
->client
->pipe_end
.fd
= NULL
;
455 assert( server
->pipe_end
.fd
);
456 if (!use_server_io( &server
->pipe_end
))
457 shutdown( get_unix_fd( server
->pipe_end
.fd
), SHUT_RDWR
);
458 release_object( server
->pipe_end
.fd
);
459 server
->pipe_end
.fd
= NULL
;
462 static void pipe_end_destroy( struct pipe_end
*pipe_end
)
464 struct pipe_message
*message
;
466 while (!list_empty( &pipe_end
->message_queue
))
468 message
= LIST_ENTRY( list_head(&pipe_end
->message_queue
), struct pipe_message
, entry
);
469 assert( !message
->async
);
470 free_message( message
);
473 free_async_queue( &pipe_end
->read_q
);
474 free_async_queue( &pipe_end
->write_q
);
477 static void pipe_server_destroy( struct object
*obj
)
479 struct pipe_server
*server
= (struct pipe_server
*)obj
;
481 assert( obj
->ops
== &pipe_server_ops
);
483 pipe_end_disconnect( &server
->pipe_end
, STATUS_PIPE_BROKEN
);
485 if (server
->pipe_end
.fd
)
487 notify_empty( server
);
488 do_disconnect( server
);
491 pipe_end_destroy( &server
->pipe_end
);
494 server
->client
->server
= NULL
;
495 server
->client
= NULL
;
498 assert( server
->pipe
->instances
);
499 server
->pipe
->instances
--;
501 if (server
->ioctl_fd
) release_object( server
->ioctl_fd
);
502 list_remove( &server
->entry
);
503 release_object( server
->pipe
);
506 static void pipe_client_destroy( struct object
*obj
)
508 struct pipe_client
*client
= (struct pipe_client
*)obj
;
509 struct pipe_server
*server
= client
->server
;
511 assert( obj
->ops
== &pipe_client_ops
);
513 pipe_end_disconnect( &client
->pipe_end
, STATUS_PIPE_BROKEN
);
517 notify_empty( server
);
519 switch(server
->state
)
521 case ps_connected_server
:
522 /* Don't destroy the server's fd here as we can't
523 do a successful flush without it. */
524 set_server_state( server
, ps_wait_disconnect
);
528 case ps_wait_disconnect
:
529 case ps_wait_connect
:
532 assert( server
->client
);
533 server
->client
= NULL
;
534 client
->server
= NULL
;
537 pipe_end_destroy( &client
->pipe_end
);
538 if (client
->pipe_end
.fd
) release_object( client
->pipe_end
.fd
);
541 static void named_pipe_device_dump( struct object
*obj
, int verbose
)
543 fputs( "Named pipe device\n", stderr
);
546 static struct object_type
*named_pipe_device_get_type( struct object
*obj
)
548 static const WCHAR name
[] = {'D','e','v','i','c','e'};
549 static const struct unicode_str str
= { name
, sizeof(name
) };
550 return get_object_type( &str
);
553 static struct fd
*named_pipe_device_get_fd( struct object
*obj
)
555 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
556 return (struct fd
*)grab_object( device
->fd
);
559 static struct object
*named_pipe_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
562 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
563 struct object
*found
;
565 assert( obj
->ops
== &named_pipe_device_ops
);
566 assert( device
->pipes
);
568 if (!name
) return NULL
; /* open the device itself */
570 if ((found
= find_object( device
->pipes
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
576 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
577 unsigned int sharing
, unsigned int options
)
579 return grab_object( obj
);
582 static void named_pipe_device_destroy( struct object
*obj
)
584 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
585 assert( obj
->ops
== &named_pipe_device_ops
);
586 if (device
->fd
) release_object( device
->fd
);
587 free( device
->pipes
);
590 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
)
592 return FD_TYPE_DEVICE
;
595 struct object
*create_named_pipe_device( struct object
*root
, const struct unicode_str
*name
)
597 struct named_pipe_device
*dev
;
599 if ((dev
= create_named_object( root
, &named_pipe_device_ops
, name
, 0, NULL
)) &&
600 get_error() != STATUS_OBJECT_NAME_EXISTS
)
603 if (!(dev
->fd
= alloc_pseudo_fd( &named_pipe_device_fd_ops
, &dev
->obj
, 0 )) ||
604 !(dev
->pipes
= create_namespace( 7 )))
606 release_object( dev
);
613 static int pipe_data_remaining( struct pipe_server
*server
)
618 assert( server
->client
);
620 if (use_server_io( &server
->pipe_end
))
621 return !list_empty( &server
->client
->pipe_end
.message_queue
);
623 fd
= get_unix_fd( server
->client
->pipe_end
.fd
);
630 if (0 > poll( &pfd
, 1, 0 ))
633 return pfd
.revents
&POLLIN
;
636 static void check_flushed( void *arg
)
638 struct pipe_server
*server
= (struct pipe_server
*) arg
;
640 if (pipe_data_remaining( server
))
642 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
646 server
->flush_poll
= NULL
;
647 fd_async_wake_up( server
->pipe_end
.fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
651 static int pipe_end_flush( struct pipe_end
*pipe_end
, struct async
*async
)
653 if (use_server_io( pipe_end
) && (!pipe_end
->connection
|| list_empty( &pipe_end
->connection
->message_queue
)))
656 fd_queue_async( pipe_end
->fd
, async
, ASYNC_TYPE_WAIT
);
657 set_error( STATUS_PENDING
);
661 static int pipe_server_flush( struct fd
*fd
, struct async
*async
)
663 struct pipe_server
*server
= get_fd_user( fd
);
666 if (!server
|| server
->state
!= ps_connected_server
) return 1;
668 if (!pipe_data_remaining( server
)) return 1;
670 handle
= pipe_end_flush( &server
->pipe_end
, async
);
672 /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
673 if (handle
&& !use_server_io( &server
->pipe_end
) && !server
->flush_poll
)
674 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
678 static int pipe_client_flush( struct fd
*fd
, struct async
*async
)
680 struct pipe_end
*pipe_end
= get_fd_user( fd
);
681 /* FIXME: Support byte mode. */
682 return use_server_io( pipe_end
) ? pipe_end_flush( pipe_end
, async
) : 1;
685 static void message_queue_read( struct pipe_end
*pipe_end
, struct iosb
*iosb
)
687 struct pipe_message
*message
;
689 if (pipe_end
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
)
691 message
= LIST_ENTRY( list_head(&pipe_end
->message_queue
), struct pipe_message
, entry
);
692 iosb
->out_size
= min( iosb
->out_size
, message
->iosb
->in_size
- message
->read_pos
);
693 iosb
->status
= message
->read_pos
+ iosb
->out_size
< message
->iosb
->in_size
694 ? STATUS_BUFFER_OVERFLOW
: STATUS_SUCCESS
;
698 data_size_t avail
= 0;
699 LIST_FOR_EACH_ENTRY( message
, &pipe_end
->message_queue
, struct pipe_message
, entry
)
701 avail
+= message
->iosb
->in_size
- message
->read_pos
;
702 if (avail
>= iosb
->out_size
) break;
704 iosb
->out_size
= min( iosb
->out_size
, avail
);
705 iosb
->status
= STATUS_SUCCESS
;
708 message
= LIST_ENTRY( list_head(&pipe_end
->message_queue
), struct pipe_message
, entry
);
709 if (!message
->read_pos
&& message
->iosb
->in_size
== iosb
->out_size
) /* fast path */
711 iosb
->out_data
= message
->iosb
->in_data
;
712 message
->iosb
->in_data
= NULL
;
713 wake_message( message
);
714 free_message( message
);
718 data_size_t write_pos
= 0, writing
;
721 if (iosb
->out_size
&& !(buf
= iosb
->out_data
= malloc( iosb
->out_size
)))
724 iosb
->status
= STATUS_NO_MEMORY
;
730 message
= LIST_ENTRY( list_head(&pipe_end
->message_queue
), struct pipe_message
, entry
);
731 writing
= min( iosb
->out_size
- write_pos
, message
->iosb
->in_size
- message
->read_pos
);
732 if (writing
) memcpy( buf
+ write_pos
, (const char *)message
->iosb
->in_data
+ message
->read_pos
, writing
);
733 write_pos
+= writing
;
734 message
->read_pos
+= writing
;
735 if (message
->read_pos
== message
->iosb
->in_size
)
737 wake_message(message
);
738 free_message(message
);
740 } while (write_pos
< iosb
->out_size
);
742 iosb
->result
= iosb
->out_size
;
745 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
746 * We're not interested in such reselect calls, so we ignore them. */
747 static int ignore_reselect
;
749 static void reselect_write_queue( struct pipe_end
*pipe_end
);
751 static void reselect_read_queue( struct pipe_end
*pipe_end
)
758 while (!list_empty( &pipe_end
->message_queue
) && (async
= find_pending_async( &pipe_end
->read_q
)))
760 iosb
= async_get_iosb( async
);
761 message_queue_read( pipe_end
, iosb
);
762 async_terminate( async
, iosb
->result
? STATUS_ALERTED
: iosb
->status
);
763 release_object( async
);
764 release_object( iosb
);
769 if (pipe_end
->connection
)
771 if (list_empty( &pipe_end
->message_queue
))
772 fd_async_wake_up( pipe_end
->connection
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
774 reselect_write_queue( pipe_end
->connection
);
778 static void reselect_write_queue( struct pipe_end
*pipe_end
)
780 struct pipe_message
*message
, *next
;
781 struct pipe_end
*reader
= pipe_end
->connection
;
782 data_size_t avail
= 0;
788 LIST_FOR_EACH_ENTRY_SAFE( message
, next
, &reader
->message_queue
, struct pipe_message
, entry
)
790 if (message
->async
&& message
->iosb
->status
!= STATUS_PENDING
)
792 release_object( message
->async
);
793 message
->async
= NULL
;
794 free_message( message
);
798 avail
+= message
->iosb
->in_size
- message
->read_pos
;
799 if (message
->iosb
->status
== STATUS_PENDING
&& (avail
<= reader
->buffer_size
|| !message
->iosb
->in_size
))
800 wake_message( message
);
805 reselect_read_queue( reader
);
808 static int pipe_end_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
810 struct pipe_end
*pipe_end
= get_fd_user( fd
);
812 if (!use_server_io( pipe_end
)) return no_fd_read( fd
, async
, pos
);
814 if (!pipe_end
->connection
&& list_empty( &pipe_end
->message_queue
))
816 set_error( STATUS_PIPE_BROKEN
);
820 queue_async( &pipe_end
->read_q
, async
);
821 reselect_read_queue( pipe_end
);
822 set_error( STATUS_PENDING
);
826 static int pipe_end_write( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
828 struct pipe_end
*write_end
= get_fd_user( fd
);
829 struct pipe_end
*read_end
= write_end
->connection
;
830 struct pipe_message
*message
;
832 if (!use_server_io( write_end
)) return no_fd_write( fd
, async
, pos
);
836 set_error( STATUS_PIPE_DISCONNECTED
);
840 if (!(message
= mem_alloc( sizeof(*message
) ))) return 0;
841 message
->async
= (struct async
*)grab_object( async
);
842 message
->iosb
= async_get_iosb( async
);
843 message
->read_pos
= 0;
844 list_add_tail( &read_end
->message_queue
, &message
->entry
);
846 queue_async( &write_end
->write_q
, async
);
847 reselect_write_queue( write_end
);
848 set_error( STATUS_PENDING
);
852 static void pipe_end_queue_async( struct fd
*fd
, struct async
*async
, int type
, int count
)
854 struct pipe_end
*pipe_end
= get_fd_user( fd
);
855 if (use_server_io( pipe_end
)) no_fd_queue_async( fd
, async
, type
, count
);
856 else default_fd_queue_async( fd
, async
, type
, count
);
859 static void pipe_end_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
861 struct pipe_end
*pipe_end
= get_fd_user( fd
);
863 if (ignore_reselect
) return;
865 if (!use_server_io( pipe_end
))
866 default_fd_reselect_async( fd
, queue
);
867 else if (&pipe_end
->write_q
== queue
)
868 reselect_write_queue( pipe_end
);
869 else if (&pipe_end
->read_q
== queue
)
870 reselect_read_queue( pipe_end
);
873 static inline int is_overlapped( unsigned int options
)
875 return !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
878 static enum server_fd_type
pipe_end_get_fd_type( struct fd
*fd
)
883 static int pipe_end_peek( struct pipe_end
*pipe_end
)
885 unsigned reply_size
= get_reply_max_size();
886 FILE_PIPE_PEEK_BUFFER
*buffer
;
887 struct pipe_message
*message
;
888 data_size_t avail
= 0;
889 data_size_t message_length
= 0;
891 if (!use_server_io( pipe_end
))
893 set_error( STATUS_NOT_SUPPORTED
);
897 if (reply_size
< offsetof( FILE_PIPE_PEEK_BUFFER
, Data
))
899 set_error( STATUS_INFO_LENGTH_MISMATCH
);
902 reply_size
-= offsetof( FILE_PIPE_PEEK_BUFFER
, Data
);
904 LIST_FOR_EACH_ENTRY( message
, &pipe_end
->message_queue
, struct pipe_message
, entry
)
905 avail
+= message
->iosb
->in_size
- message
->read_pos
;
909 message
= LIST_ENTRY( list_head(&pipe_end
->message_queue
), struct pipe_message
, entry
);
910 message_length
= message
->iosb
->in_size
- message
->read_pos
;
911 reply_size
= min( reply_size
, message_length
);
915 if (!(buffer
= set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER
, Data
[reply_size
] )))) return 0;
916 buffer
->NamedPipeState
= 0; /* FIXME */
917 buffer
->ReadDataAvailable
= avail
;
918 buffer
->NumberOfMessages
= 0; /* FIXME */
919 buffer
->MessageLength
= message_length
;
920 if (reply_size
) memcpy( buffer
->Data
, (const char *)message
->iosb
->in_data
+ message
->read_pos
, reply_size
);
924 static int pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
926 struct pipe_server
*server
= get_fd_user( fd
);
930 case FSCTL_PIPE_LISTEN
:
931 switch(server
->state
)
934 case ps_wait_connect
:
935 fd_queue_async( server
->ioctl_fd
, async
, ASYNC_TYPE_WAIT
);
936 set_server_state( server
, ps_wait_open
);
937 async_wake_up( &server
->pipe
->waiters
, STATUS_SUCCESS
);
938 set_error( STATUS_PENDING
);
940 case ps_connected_server
:
941 set_error( STATUS_PIPE_CONNECTED
);
943 case ps_wait_disconnect
:
944 set_error( STATUS_NO_DATA_DETECTED
);
947 set_error( STATUS_INVALID_HANDLE
);
952 case FSCTL_PIPE_DISCONNECT
:
953 switch(server
->state
)
955 case ps_connected_server
:
956 assert( server
->client
);
957 assert( server
->client
->pipe_end
.fd
);
959 notify_empty( server
);
961 /* dump the client and server fds - client loses all waiting data */
962 pipe_end_disconnect( &server
->pipe_end
, STATUS_PIPE_DISCONNECTED
);
963 do_disconnect( server
);
964 server
->client
->server
= NULL
;
965 server
->client
= NULL
;
966 set_server_state( server
, ps_wait_connect
);
968 case ps_wait_disconnect
:
969 assert( !server
->client
);
970 pipe_end_disconnect( &server
->pipe_end
, STATUS_PIPE_DISCONNECTED
);
971 do_disconnect( server
);
972 set_server_state( server
, ps_wait_connect
);
976 set_error( STATUS_PIPE_LISTENING
);
978 case ps_wait_connect
:
979 set_error( STATUS_PIPE_DISCONNECTED
);
984 case FSCTL_PIPE_PEEK
:
985 return pipe_end_peek( &server
->pipe_end
);
988 return default_fd_ioctl( fd
, code
, async
);
992 static int pipe_client_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
994 struct pipe_client
*client
= get_fd_user( fd
);
998 case FSCTL_PIPE_PEEK
:
999 return pipe_end_peek( &client
->pipe_end
);
1002 return default_fd_ioctl( fd
, code
, async
);
1006 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
1007 obj_handle_t handle
, unsigned int access
)
1010 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
1011 return (struct pipe_server
*) obj
;
1014 static void init_pipe_end( struct pipe_end
*pipe_end
, unsigned int pipe_flags
, data_size_t buffer_size
)
1016 pipe_end
->fd
= NULL
;
1017 pipe_end
->flags
= pipe_flags
;
1018 pipe_end
->connection
= NULL
;
1019 pipe_end
->buffer_size
= buffer_size
;
1020 init_async_queue( &pipe_end
->read_q
);
1021 init_async_queue( &pipe_end
->write_q
);
1022 list_init( &pipe_end
->message_queue
);
1025 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
, unsigned int options
,
1026 unsigned int pipe_flags
)
1028 struct pipe_server
*server
;
1030 server
= alloc_object( &pipe_server_ops
);
1034 server
->pipe
= pipe
;
1035 server
->client
= NULL
;
1036 server
->flush_poll
= NULL
;
1037 server
->options
= options
;
1038 init_pipe_end( &server
->pipe_end
, pipe_flags
, pipe
->insize
);
1040 list_add_head( &pipe
->servers
, &server
->entry
);
1041 grab_object( pipe
);
1042 if (!(server
->ioctl_fd
= alloc_pseudo_fd( &pipe_server_fd_ops
, &server
->pipe_end
.obj
, options
)))
1044 release_object( server
);
1047 set_fd_signaled( server
->ioctl_fd
, 1 );
1048 set_server_state( server
, ps_idle_server
);
1052 static struct pipe_client
*create_pipe_client( unsigned int flags
, unsigned int pipe_flags
, data_size_t buffer_size
)
1054 struct pipe_client
*client
;
1056 client
= alloc_object( &pipe_client_ops
);
1060 client
->server
= NULL
;
1061 client
->flags
= flags
;
1062 init_pipe_end( &client
->pipe_end
, pipe_flags
, buffer_size
);
1067 static struct pipe_server
*find_available_server( struct named_pipe
*pipe
)
1069 struct pipe_server
*server
;
1071 /* look for pipe servers that are listening */
1072 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
1074 if (server
->state
== ps_wait_open
)
1075 return (struct pipe_server
*)grab_object( server
);
1078 /* fall back to pipe servers that are idle */
1079 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
1081 if (server
->state
== ps_idle_server
)
1082 return (struct pipe_server
*)grab_object( server
);
1088 static int named_pipe_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
1090 struct named_pipe_device
*dev
= (struct named_pipe_device
*)parent
;
1092 if (parent
->ops
!= &named_pipe_device_ops
)
1094 set_error( STATUS_OBJECT_NAME_INVALID
);
1097 namespace_add( dev
->pipes
, name
);
1098 name
->parent
= grab_object( parent
);
1102 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
1103 unsigned int sharing
, unsigned int options
)
1105 struct named_pipe
*pipe
= (struct named_pipe
*)obj
;
1106 struct pipe_server
*server
;
1107 struct pipe_client
*client
;
1108 unsigned int pipe_sharing
;
1111 if (!(server
= find_available_server( pipe
)))
1113 set_error( STATUS_PIPE_NOT_AVAILABLE
);
1117 pipe_sharing
= server
->pipe
->sharing
;
1118 if (((access
& GENERIC_READ
) && !(pipe_sharing
& FILE_SHARE_READ
)) ||
1119 ((access
& GENERIC_WRITE
) && !(pipe_sharing
& FILE_SHARE_WRITE
)))
1121 set_error( STATUS_ACCESS_DENIED
);
1122 release_object( server
);
1126 if ((client
= create_pipe_client( options
, pipe
->flags
, pipe
->outsize
)))
1128 if (use_server_io( &server
->pipe_end
))
1130 client
->pipe_end
.fd
= alloc_pseudo_fd( &pipe_client_fd_ops
, &client
->pipe_end
.obj
, options
);
1131 if (client
->pipe_end
.fd
)
1133 set_fd_signaled( client
->pipe_end
.fd
, 1 );
1134 server
->pipe_end
.fd
= (struct fd
*)grab_object( server
->ioctl_fd
);
1135 set_no_fd_status( server
->ioctl_fd
, STATUS_BAD_DEVICE_TYPE
);
1139 release_object( client
);
1143 else if (!socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
))
1145 assert( !server
->pipe_end
.fd
);
1147 /* for performance reasons, only set nonblocking mode when using
1148 * overlapped I/O. Otherwise, we will be doing too much busy
1150 if (is_overlapped( options
)) fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
1151 if (is_overlapped( server
->options
)) fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
1155 setsockopt( fds
[0], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
1156 setsockopt( fds
[1], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
1160 setsockopt( fds
[0], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
1161 setsockopt( fds
[1], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
1164 client
->pipe_end
.fd
= create_anonymous_fd( &pipe_client_fd_ops
, fds
[1], &client
->pipe_end
.obj
, options
);
1165 server
->pipe_end
.fd
= create_anonymous_fd( &pipe_server_fd_ops
, fds
[0], &server
->pipe_end
.obj
, server
->options
);
1166 if (client
->pipe_end
.fd
&& server
->pipe_end
.fd
)
1168 fd_copy_completion( server
->ioctl_fd
, server
->pipe_end
.fd
);
1172 release_object( client
);
1179 release_object( client
);
1184 allow_fd_caching( client
->pipe_end
.fd
);
1185 allow_fd_caching( server
->pipe_end
.fd
);
1186 if (server
->state
== ps_wait_open
)
1187 fd_async_wake_up( server
->ioctl_fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
1188 set_server_state( server
, ps_connected_server
);
1189 server
->client
= client
;
1190 client
->server
= server
;
1191 server
->pipe_end
.connection
= &client
->pipe_end
;
1192 client
->pipe_end
.connection
= &server
->pipe_end
;
1195 release_object( server
);
1196 return &client
->pipe_end
.obj
;
1199 static int named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
1201 struct named_pipe_device
*device
= get_fd_user( fd
);
1205 case FSCTL_PIPE_WAIT
:
1207 const FILE_PIPE_WAIT_FOR_BUFFER
*buffer
= get_req_data();
1208 data_size_t size
= get_req_data_size();
1209 struct named_pipe
*pipe
;
1210 struct pipe_server
*server
;
1211 struct unicode_str name
;
1214 if (size
< sizeof(*buffer
) ||
1215 size
< FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[buffer
->NameLength
/sizeof(WCHAR
)]))
1217 set_error( STATUS_INVALID_PARAMETER
);
1220 name
.str
= buffer
->Name
;
1221 name
.len
= (buffer
->NameLength
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
1222 if (!(pipe
= open_named_object( &device
->obj
, &named_pipe_ops
, &name
, 0 ))) return 0;
1224 if (!(server
= find_available_server( pipe
)))
1226 queue_async( &pipe
->waiters
, async
);
1227 when
= buffer
->TimeoutSpecified
? buffer
->Timeout
.QuadPart
: pipe
->timeout
;
1228 async_set_timeout( async
, when
, STATUS_IO_TIMEOUT
);
1229 release_object( pipe
);
1230 set_error( STATUS_PENDING
);
1234 release_object( server
);
1235 release_object( pipe
);
1240 return default_fd_ioctl( fd
, code
, async
);
1245 DECL_HANDLER(create_named_pipe
)
1247 struct named_pipe
*pipe
;
1248 struct pipe_server
*server
;
1249 struct unicode_str name
;
1250 struct object
*root
;
1251 const struct security_descriptor
*sd
;
1252 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
1254 if (!objattr
) return;
1256 if (!req
->sharing
|| (req
->sharing
& ~(FILE_SHARE_READ
| FILE_SHARE_WRITE
)) ||
1257 (!(req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
) && (req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
)))
1259 if (root
) release_object( root
);
1260 set_error( STATUS_INVALID_PARAMETER
);
1264 if (!name
.len
) /* pipes need a root directory even without a name */
1266 if (!objattr
->rootdir
)
1268 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
1271 if (!(root
= get_directory_obj( current
->process
, objattr
->rootdir
))) return;
1274 pipe
= create_named_object( root
, &named_pipe_ops
, &name
, objattr
->attributes
| OBJ_OPENIF
, NULL
);
1276 if (root
) release_object( root
);
1279 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
1281 /* initialize it if it didn't already exist */
1282 pipe
->instances
= 0;
1283 init_async_queue( &pipe
->waiters
);
1284 list_init( &pipe
->servers
);
1285 pipe
->insize
= req
->insize
;
1286 pipe
->outsize
= req
->outsize
;
1287 pipe
->maxinstances
= req
->maxinstances
;
1288 pipe
->timeout
= req
->timeout
;
1289 pipe
->flags
= req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
;
1290 pipe
->sharing
= req
->sharing
;
1294 if (pipe
->maxinstances
<= pipe
->instances
)
1296 set_error( STATUS_INSTANCE_NOT_AVAILABLE
);
1297 release_object( pipe
);
1300 if (pipe
->sharing
!= req
->sharing
)
1302 set_error( STATUS_ACCESS_DENIED
);
1303 release_object( pipe
);
1306 clear_error(); /* clear the name collision */
1309 server
= create_pipe_server( pipe
, req
->options
, req
->flags
);
1312 reply
->handle
= alloc_handle( current
->process
, server
, req
->access
, objattr
->attributes
);
1313 server
->pipe
->instances
++;
1314 if (sd
) default_set_sd( &server
->pipe_end
.obj
, sd
, OWNER_SECURITY_INFORMATION
|
1315 GROUP_SECURITY_INFORMATION
|
1316 DACL_SECURITY_INFORMATION
|
1317 SACL_SECURITY_INFORMATION
);
1318 release_object( server
);
1321 release_object( pipe
);
1324 DECL_HANDLER(get_named_pipe_info
)
1326 struct pipe_server
*server
;
1327 struct pipe_client
*client
= NULL
;
1329 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
);
1332 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
1336 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
1337 0, &pipe_client_ops
);
1338 if (!client
) return;
1339 server
= client
->server
;
1342 reply
->flags
= client
? client
->pipe_end
.flags
: server
->pipe_end
.flags
;
1345 reply
->sharing
= server
->pipe
->sharing
;
1346 reply
->maxinstances
= server
->pipe
->maxinstances
;
1347 reply
->instances
= server
->pipe
->instances
;
1348 reply
->insize
= server
->pipe
->insize
;
1349 reply
->outsize
= server
->pipe
->outsize
;
1353 release_object(client
);
1356 reply
->flags
|= NAMED_PIPE_SERVER_END
;
1357 release_object(server
);
1361 DECL_HANDLER(set_named_pipe_info
)
1363 struct pipe_server
*server
;
1364 struct pipe_client
*client
= NULL
;
1366 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
);
1369 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
1373 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
1374 0, &pipe_client_ops
);
1375 if (!client
) return;
1376 if (!(server
= client
->server
))
1378 release_object( client
);
1383 if ((req
->flags
& ~(NAMED_PIPE_MESSAGE_STREAM_READ
| NAMED_PIPE_NONBLOCKING_MODE
)) ||
1384 ((req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
) && !(server
->pipe
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
)))
1386 set_error( STATUS_INVALID_PARAMETER
);
1390 client
->pipe_end
.flags
= server
->pipe
->flags
| req
->flags
;
1394 server
->pipe_end
.flags
= server
->pipe
->flags
| req
->flags
;
1398 release_object(client
);
1400 release_object(server
);