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 #define WIN32_NO_STATUS
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 */
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 */
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 */
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 */
94 struct object obj
; /* object header */
97 unsigned int maxinstances
;
100 unsigned int instances
;
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 */
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 */
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
;
340 case ps_connected_server
:
341 case ps_wait_disconnect
:
345 set_no_fd_status( server
->pipe_end
.fd
, STATUS_PIPE_LISTENING
);
347 case ps_wait_connect
:
348 set_no_fd_status( server
->pipe_end
.fd
, STATUS_PIPE_DISCONNECTED
);
354 static struct pipe_message
*queue_message( struct pipe_end
*pipe_end
, struct iosb
*iosb
)
356 struct pipe_message
*message
;
358 if (!(message
= mem_alloc( sizeof(*message
) ))) return NULL
;
359 message
->iosb
= (struct iosb
*)grab_object( iosb
);
360 message
->async
= NULL
;
361 message
->read_pos
= 0;
362 list_add_tail( &pipe_end
->message_queue
, &message
->entry
);
366 static void wake_message( struct pipe_message
*message
)
368 struct async
*async
= message
->async
;
370 message
->async
= NULL
;
373 message
->iosb
->status
= STATUS_SUCCESS
;
374 message
->iosb
->result
= message
->iosb
->in_size
;
375 async_terminate( async
, message
->iosb
->result
? STATUS_ALERTED
: STATUS_SUCCESS
);
376 release_object( async
);
379 static void free_message( struct pipe_message
*message
)
381 list_remove( &message
->entry
);
382 if (message
->iosb
) release_object( message
->iosb
);
386 static void pipe_end_disconnect( struct pipe_end
*pipe_end
, unsigned int status
)
388 struct pipe_end
*connection
= pipe_end
->connection
;
389 struct pipe_message
*message
, *next
;
392 pipe_end
->connection
= NULL
;
394 fd_async_wake_up( pipe_end
->fd
, ASYNC_TYPE_WAIT
, status
);
395 async_wake_up( &pipe_end
->read_q
, status
);
396 LIST_FOR_EACH_ENTRY_SAFE( message
, next
, &pipe_end
->message_queue
, struct pipe_message
, entry
)
398 async
= message
->async
;
399 if (async
|| status
== STATUS_PIPE_DISCONNECTED
) free_message( message
);
400 if (!async
) continue;
401 async_terminate( async
, status
);
402 release_object( async
);
404 if (status
== STATUS_PIPE_DISCONNECTED
) set_fd_signaled( pipe_end
->fd
, 0 );
408 connection
->connection
= NULL
;
409 pipe_end_disconnect( connection
, status
);
413 static void pipe_end_destroy( struct pipe_end
*pipe_end
)
415 struct pipe_message
*message
;
417 while (!list_empty( &pipe_end
->message_queue
))
419 message
= LIST_ENTRY( list_head(&pipe_end
->message_queue
), struct pipe_message
, entry
);
420 assert( !message
->async
);
421 free_message( message
);
424 free_async_queue( &pipe_end
->read_q
);
425 free_async_queue( &pipe_end
->write_q
);
426 if (pipe_end
->fd
) release_object( pipe_end
->fd
);
429 static void pipe_server_destroy( struct object
*obj
)
431 struct pipe_server
*server
= (struct pipe_server
*)obj
;
433 assert( obj
->ops
== &pipe_server_ops
);
435 pipe_end_disconnect( &server
->pipe_end
, STATUS_PIPE_BROKEN
);
437 pipe_end_destroy( &server
->pipe_end
);
440 server
->client
->server
= NULL
;
441 server
->client
= NULL
;
444 assert( server
->pipe
->instances
);
445 server
->pipe
->instances
--;
447 list_remove( &server
->entry
);
448 release_object( server
->pipe
);
451 static void pipe_client_destroy( struct object
*obj
)
453 struct pipe_client
*client
= (struct pipe_client
*)obj
;
454 struct pipe_server
*server
= client
->server
;
456 assert( obj
->ops
== &pipe_client_ops
);
458 pipe_end_disconnect( &client
->pipe_end
, STATUS_PIPE_BROKEN
);
462 switch(server
->state
)
464 case ps_connected_server
:
465 /* Don't destroy the server's fd here as we can't
466 do a successful flush without it. */
467 set_server_state( server
, ps_wait_disconnect
);
471 case ps_wait_disconnect
:
472 case ps_wait_connect
:
475 assert( server
->client
);
476 server
->client
= NULL
;
477 client
->server
= NULL
;
480 pipe_end_destroy( &client
->pipe_end
);
483 static void named_pipe_device_dump( struct object
*obj
, int verbose
)
485 fputs( "Named pipe device\n", stderr
);
488 static struct object_type
*named_pipe_device_get_type( struct object
*obj
)
490 static const WCHAR name
[] = {'D','e','v','i','c','e'};
491 static const struct unicode_str str
= { name
, sizeof(name
) };
492 return get_object_type( &str
);
495 static struct fd
*named_pipe_device_get_fd( struct object
*obj
)
497 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
498 return (struct fd
*)grab_object( device
->fd
);
501 static struct object
*named_pipe_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
504 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
505 struct object
*found
;
507 assert( obj
->ops
== &named_pipe_device_ops
);
508 assert( device
->pipes
);
510 if (!name
) return NULL
; /* open the device itself */
512 if ((found
= find_object( device
->pipes
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
518 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
519 unsigned int sharing
, unsigned int options
)
521 return grab_object( obj
);
524 static void named_pipe_device_destroy( struct object
*obj
)
526 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
527 assert( obj
->ops
== &named_pipe_device_ops
);
528 if (device
->fd
) release_object( device
->fd
);
529 free( device
->pipes
);
532 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
)
534 return FD_TYPE_DEVICE
;
537 struct object
*create_named_pipe_device( struct object
*root
, const struct unicode_str
*name
)
539 struct named_pipe_device
*dev
;
541 if ((dev
= create_named_object( root
, &named_pipe_device_ops
, name
, 0, NULL
)) &&
542 get_error() != STATUS_OBJECT_NAME_EXISTS
)
545 if (!(dev
->fd
= alloc_pseudo_fd( &named_pipe_device_fd_ops
, &dev
->obj
, 0 )) ||
546 !(dev
->pipes
= create_namespace( 7 )))
548 release_object( dev
);
555 static int pipe_end_flush( struct fd
*fd
, struct async
*async
)
557 struct pipe_end
*pipe_end
= get_fd_user( fd
);
559 if (pipe_end
->connection
&& !list_empty( &pipe_end
->connection
->message_queue
))
561 fd_queue_async( pipe_end
->fd
, async
, ASYNC_TYPE_WAIT
);
562 set_error( STATUS_PENDING
);
567 static void pipe_end_get_file_info( struct fd
*fd
, struct named_pipe
*pipe
, unsigned int info_class
)
571 case FileNameInformation
:
573 FILE_NAME_INFORMATION
*name_info
;
574 data_size_t name_len
, reply_size
;
577 if (get_reply_max_size() < sizeof(*name_info
))
579 set_error( STATUS_INFO_LENGTH_MISMATCH
);
583 name
= get_object_name( &pipe
->obj
, &name_len
);
584 reply_size
= offsetof( FILE_NAME_INFORMATION
, FileName
[name_len
/sizeof(WCHAR
) + 1] );
585 if (reply_size
> get_reply_max_size())
587 reply_size
= get_reply_max_size();
588 set_error( STATUS_BUFFER_OVERFLOW
);
591 if (!(name_info
= set_reply_data_size( reply_size
))) return;
592 name_info
->FileNameLength
= name_len
+ sizeof(WCHAR
);
593 name_info
->FileName
[0] = '\\';
594 reply_size
-= offsetof( FILE_NAME_INFORMATION
, FileName
[1] );
595 if (reply_size
) memcpy( &name_info
->FileName
[1], name
, reply_size
);
599 no_fd_get_file_info( fd
, info_class
);
603 static struct security_descriptor
*pipe_server_get_sd( struct object
*obj
)
605 struct pipe_server
*server
= (struct pipe_server
*) obj
;
606 return default_get_sd( &server
->pipe
->obj
);
609 static struct security_descriptor
*pipe_client_get_sd( struct object
*obj
)
611 struct pipe_client
*client
= (struct pipe_client
*) obj
;
612 if (client
->server
) return default_get_sd( &client
->server
->pipe
->obj
);
613 set_error( STATUS_PIPE_DISCONNECTED
);
617 static int pipe_server_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
618 unsigned int set_info
)
620 struct pipe_server
*server
= (struct pipe_server
*) obj
;
621 return default_set_sd( &server
->pipe
->obj
, sd
, set_info
);
624 static int pipe_client_set_sd( struct object
*obj
, const struct security_descriptor
*sd
,
625 unsigned int set_info
)
627 struct pipe_client
*client
= (struct pipe_client
*) obj
;
628 if (client
->server
) return default_set_sd( &client
->server
->pipe
->obj
, sd
, set_info
);
629 set_error( STATUS_PIPE_DISCONNECTED
);
633 static void pipe_server_get_file_info( struct fd
*fd
, unsigned int info_class
)
635 struct pipe_server
*server
= get_fd_user( fd
);
636 pipe_end_get_file_info( fd
, server
->pipe
, info_class
);
639 static void pipe_client_get_file_info( struct fd
*fd
, unsigned int info_class
)
641 struct pipe_client
*client
= get_fd_user( fd
);
642 if (client
->server
) pipe_end_get_file_info( fd
, client
->server
->pipe
, info_class
);
643 else set_error( STATUS_PIPE_DISCONNECTED
);
646 static void pipe_end_get_volume_info( struct fd
*fd
, unsigned int info_class
)
650 case FileFsDeviceInformation
:
652 static const FILE_FS_DEVICE_INFORMATION device_info
=
654 FILE_DEVICE_NAMED_PIPE
,
655 FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
657 if (get_reply_max_size() >= sizeof(device_info
))
658 set_reply_data( &device_info
, sizeof(device_info
) );
660 set_error( STATUS_BUFFER_TOO_SMALL
);
664 set_error( STATUS_NOT_IMPLEMENTED
);
668 static void message_queue_read( struct pipe_end
*pipe_end
, struct iosb
*iosb
)
670 struct pipe_message
*message
;
672 if (pipe_end
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
)
674 message
= LIST_ENTRY( list_head(&pipe_end
->message_queue
), struct pipe_message
, entry
);
675 iosb
->out_size
= min( iosb
->out_size
, message
->iosb
->in_size
- message
->read_pos
);
676 iosb
->status
= message
->read_pos
+ iosb
->out_size
< message
->iosb
->in_size
677 ? STATUS_BUFFER_OVERFLOW
: STATUS_SUCCESS
;
681 data_size_t avail
= 0;
682 LIST_FOR_EACH_ENTRY( message
, &pipe_end
->message_queue
, struct pipe_message
, entry
)
684 avail
+= message
->iosb
->in_size
- message
->read_pos
;
685 if (avail
>= iosb
->out_size
) break;
687 iosb
->out_size
= min( iosb
->out_size
, avail
);
688 iosb
->status
= STATUS_SUCCESS
;
691 message
= LIST_ENTRY( list_head(&pipe_end
->message_queue
), struct pipe_message
, entry
);
692 if (!message
->read_pos
&& message
->iosb
->in_size
== iosb
->out_size
) /* fast path */
694 iosb
->out_data
= message
->iosb
->in_data
;
695 message
->iosb
->in_data
= NULL
;
696 wake_message( message
);
697 free_message( message
);
701 data_size_t write_pos
= 0, writing
;
704 if (iosb
->out_size
&& !(buf
= iosb
->out_data
= malloc( iosb
->out_size
)))
707 iosb
->status
= STATUS_NO_MEMORY
;
713 message
= LIST_ENTRY( list_head(&pipe_end
->message_queue
), struct pipe_message
, entry
);
714 writing
= min( iosb
->out_size
- write_pos
, message
->iosb
->in_size
- message
->read_pos
);
715 if (writing
) memcpy( buf
+ write_pos
, (const char *)message
->iosb
->in_data
+ message
->read_pos
, writing
);
716 write_pos
+= writing
;
717 message
->read_pos
+= writing
;
718 if (message
->read_pos
== message
->iosb
->in_size
)
720 wake_message(message
);
721 free_message(message
);
723 } while (write_pos
< iosb
->out_size
);
725 iosb
->result
= iosb
->out_size
;
728 /* We call async_terminate in our reselect implementation, which causes recursive reselect.
729 * We're not interested in such reselect calls, so we ignore them. */
730 static int ignore_reselect
;
732 static void reselect_write_queue( struct pipe_end
*pipe_end
);
734 static void reselect_read_queue( struct pipe_end
*pipe_end
)
741 while (!list_empty( &pipe_end
->message_queue
) && (async
= find_pending_async( &pipe_end
->read_q
)))
743 iosb
= async_get_iosb( async
);
744 message_queue_read( pipe_end
, iosb
);
745 async_terminate( async
, iosb
->result
? STATUS_ALERTED
: iosb
->status
);
746 release_object( async
);
747 release_object( iosb
);
752 if (pipe_end
->connection
)
754 if (list_empty( &pipe_end
->message_queue
))
755 fd_async_wake_up( pipe_end
->connection
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
757 reselect_write_queue( pipe_end
->connection
);
761 static void reselect_write_queue( struct pipe_end
*pipe_end
)
763 struct pipe_message
*message
, *next
;
764 struct pipe_end
*reader
= pipe_end
->connection
;
765 data_size_t avail
= 0;
771 LIST_FOR_EACH_ENTRY_SAFE( message
, next
, &reader
->message_queue
, struct pipe_message
, entry
)
773 if (message
->async
&& message
->iosb
->status
!= STATUS_PENDING
)
775 release_object( message
->async
);
776 message
->async
= NULL
;
777 free_message( message
);
781 avail
+= message
->iosb
->in_size
- message
->read_pos
;
782 if (message
->async
&& (avail
<= reader
->buffer_size
|| !message
->iosb
->in_size
))
783 wake_message( message
);
788 reselect_read_queue( reader
);
791 static int pipe_end_read( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
793 struct pipe_end
*pipe_end
= get_fd_user( fd
);
795 if (!pipe_end
->connection
&& list_empty( &pipe_end
->message_queue
))
797 set_error( STATUS_PIPE_BROKEN
);
801 queue_async( &pipe_end
->read_q
, async
);
802 reselect_read_queue( pipe_end
);
803 set_error( STATUS_PENDING
);
807 static int pipe_end_write( struct fd
*fd
, struct async
*async
, file_pos_t pos
)
809 struct pipe_end
*pipe_end
= get_fd_user( fd
);
810 struct pipe_message
*message
;
813 if (!pipe_end
->connection
)
815 set_error( STATUS_PIPE_DISCONNECTED
);
819 if (!(pipe_end
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
) && !get_req_data_size()) return 1;
821 iosb
= async_get_iosb( async
);
822 message
= queue_message( pipe_end
->connection
, iosb
);
823 release_object( iosb
);
824 if (!message
) return 0;
826 message
->async
= (struct async
*)grab_object( async
);
827 queue_async( &pipe_end
->write_q
, async
);
828 reselect_write_queue( pipe_end
);
829 set_error( STATUS_PENDING
);
833 static void pipe_end_reselect_async( struct fd
*fd
, struct async_queue
*queue
)
835 struct pipe_end
*pipe_end
= get_fd_user( fd
);
837 if (ignore_reselect
) return;
839 if (&pipe_end
->write_q
== queue
)
840 reselect_write_queue( pipe_end
);
841 else if (&pipe_end
->read_q
== queue
)
842 reselect_read_queue( pipe_end
);
845 static enum server_fd_type
pipe_end_get_fd_type( struct fd
*fd
)
850 static int pipe_end_peek( struct pipe_end
*pipe_end
)
852 unsigned reply_size
= get_reply_max_size();
853 FILE_PIPE_PEEK_BUFFER
*buffer
;
854 struct pipe_message
*message
;
855 data_size_t avail
= 0;
856 data_size_t message_length
= 0;
858 if (reply_size
< offsetof( FILE_PIPE_PEEK_BUFFER
, Data
))
860 set_error( STATUS_INFO_LENGTH_MISMATCH
);
863 reply_size
-= offsetof( FILE_PIPE_PEEK_BUFFER
, Data
);
865 if (!pipe_end
->connection
&& list_empty( &pipe_end
->message_queue
))
867 set_error( STATUS_PIPE_BROKEN
);
871 LIST_FOR_EACH_ENTRY( message
, &pipe_end
->message_queue
, struct pipe_message
, entry
)
872 avail
+= message
->iosb
->in_size
- message
->read_pos
;
873 reply_size
= min( reply_size
, avail
);
875 if (avail
&& (pipe_end
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
))
877 message
= LIST_ENTRY( list_head(&pipe_end
->message_queue
), struct pipe_message
, entry
);
878 message_length
= message
->iosb
->in_size
- message
->read_pos
;
879 reply_size
= min( reply_size
, message_length
);
882 if (!(buffer
= set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER
, Data
[reply_size
] )))) return 0;
883 buffer
->NamedPipeState
= 0; /* FIXME */
884 buffer
->ReadDataAvailable
= avail
;
885 buffer
->NumberOfMessages
= 0; /* FIXME */
886 buffer
->MessageLength
= message_length
;
890 data_size_t write_pos
= 0, writing
;
891 LIST_FOR_EACH_ENTRY( message
, &pipe_end
->message_queue
, struct pipe_message
, entry
)
893 writing
= min( reply_size
- write_pos
, message
->iosb
->in_size
- message
->read_pos
);
894 memcpy( buffer
->Data
+ write_pos
, (const char *)message
->iosb
->in_data
+ message
->read_pos
,
896 write_pos
+= writing
;
897 if (write_pos
== reply_size
) break;
903 static int pipe_end_transceive( struct pipe_end
*pipe_end
, struct async
*async
)
905 struct pipe_message
*message
;
908 if ((pipe_end
->flags
& (NAMED_PIPE_MESSAGE_STREAM_WRITE
| NAMED_PIPE_MESSAGE_STREAM_READ
))
909 != (NAMED_PIPE_MESSAGE_STREAM_WRITE
| NAMED_PIPE_MESSAGE_STREAM_READ
))
911 set_error( STATUS_INVALID_READ_MODE
);
915 if (!pipe_end
->connection
)
917 set_error( STATUS_PIPE_BROKEN
);
921 /* not allowed if we already have read data buffered */
922 if (!list_empty( &pipe_end
->message_queue
))
924 set_error( STATUS_PIPE_BUSY
);
928 iosb
= async_get_iosb( async
);
929 /* ignore output buffer copy transferred because of METHOD_NEITHER */
930 iosb
->in_size
-= iosb
->out_size
;
931 /* transaction never blocks on write, so just queue a message without async */
932 message
= queue_message( pipe_end
->connection
, iosb
);
933 release_object( iosb
);
934 if (!message
) return 0;
935 reselect_read_queue( pipe_end
->connection
);
937 queue_async( &pipe_end
->read_q
, async
);
938 reselect_read_queue( pipe_end
);
939 set_error( STATUS_PENDING
);
943 static int pipe_end_ioctl( struct pipe_end
*pipe_end
, ioctl_code_t code
, struct async
*async
)
947 case FSCTL_PIPE_PEEK
:
948 return pipe_end_peek( pipe_end
);
950 case FSCTL_PIPE_TRANSCEIVE
:
951 return pipe_end_transceive( pipe_end
, async
);
954 return default_fd_ioctl( pipe_end
->fd
, code
, async
);
958 static int pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
960 struct pipe_server
*server
= get_fd_user( fd
);
964 case FSCTL_PIPE_LISTEN
:
965 switch(server
->state
)
968 case ps_wait_connect
:
969 fd_queue_async( server
->pipe_end
.fd
, async
, ASYNC_TYPE_WAIT
);
970 set_server_state( server
, ps_wait_open
);
971 async_wake_up( &server
->pipe
->waiters
, STATUS_SUCCESS
);
972 set_error( STATUS_PENDING
);
974 case ps_connected_server
:
975 set_error( STATUS_PIPE_CONNECTED
);
977 case ps_wait_disconnect
:
978 set_error( STATUS_NO_DATA_DETECTED
);
981 set_error( STATUS_INVALID_HANDLE
);
986 case FSCTL_PIPE_DISCONNECT
:
987 switch(server
->state
)
989 case ps_connected_server
:
990 assert( server
->client
);
992 /* dump the client and server fds - client loses all waiting data */
993 pipe_end_disconnect( &server
->pipe_end
, STATUS_PIPE_DISCONNECTED
);
994 server
->client
->server
= NULL
;
995 server
->client
= NULL
;
996 set_server_state( server
, ps_wait_connect
);
998 case ps_wait_disconnect
:
999 assert( !server
->client
);
1000 pipe_end_disconnect( &server
->pipe_end
, STATUS_PIPE_DISCONNECTED
);
1001 set_server_state( server
, ps_wait_connect
);
1003 case ps_idle_server
:
1005 set_error( STATUS_PIPE_LISTENING
);
1007 case ps_wait_connect
:
1008 set_error( STATUS_PIPE_DISCONNECTED
);
1014 return pipe_end_ioctl( &server
->pipe_end
, code
, async
);
1018 static int pipe_client_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
1020 struct pipe_client
*client
= get_fd_user( fd
);
1024 case FSCTL_PIPE_LISTEN
:
1025 set_error( STATUS_ILLEGAL_FUNCTION
);
1029 return pipe_end_ioctl( &client
->pipe_end
, code
, async
);
1033 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
1034 obj_handle_t handle
, unsigned int access
)
1037 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
1038 return (struct pipe_server
*) obj
;
1041 static void init_pipe_end( struct pipe_end
*pipe_end
, unsigned int pipe_flags
, data_size_t buffer_size
)
1043 pipe_end
->fd
= NULL
;
1044 pipe_end
->flags
= pipe_flags
;
1045 pipe_end
->connection
= NULL
;
1046 pipe_end
->buffer_size
= buffer_size
;
1047 init_async_queue( &pipe_end
->read_q
);
1048 init_async_queue( &pipe_end
->write_q
);
1049 list_init( &pipe_end
->message_queue
);
1052 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
, unsigned int options
,
1053 unsigned int pipe_flags
)
1055 struct pipe_server
*server
;
1057 server
= alloc_object( &pipe_server_ops
);
1061 server
->pipe
= pipe
;
1062 server
->client
= NULL
;
1063 server
->options
= options
;
1064 init_pipe_end( &server
->pipe_end
, pipe_flags
, pipe
->insize
);
1066 list_add_head( &pipe
->servers
, &server
->entry
);
1067 grab_object( pipe
);
1068 if (!(server
->pipe_end
.fd
= alloc_pseudo_fd( &pipe_server_fd_ops
, &server
->pipe_end
.obj
, options
)))
1070 release_object( server
);
1073 set_fd_signaled( server
->pipe_end
.fd
, 1 );
1074 set_server_state( server
, ps_idle_server
);
1078 static struct pipe_client
*create_pipe_client( unsigned int flags
, unsigned int pipe_flags
,
1079 data_size_t buffer_size
, unsigned int options
)
1081 struct pipe_client
*client
;
1083 client
= alloc_object( &pipe_client_ops
);
1087 client
->server
= NULL
;
1088 client
->flags
= flags
;
1089 init_pipe_end( &client
->pipe_end
, pipe_flags
, buffer_size
);
1091 client
->pipe_end
.fd
= alloc_pseudo_fd( &pipe_client_fd_ops
, &client
->pipe_end
.obj
, options
);
1092 if (!client
->pipe_end
.fd
)
1094 release_object( client
);
1097 allow_fd_caching( client
->pipe_end
.fd
);
1098 set_fd_signaled( client
->pipe_end
.fd
, 1 );
1103 static struct pipe_server
*find_available_server( struct named_pipe
*pipe
)
1105 struct pipe_server
*server
;
1107 /* look for pipe servers that are listening */
1108 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
1110 if (server
->state
== ps_wait_open
)
1111 return (struct pipe_server
*)grab_object( server
);
1114 /* fall back to pipe servers that are idle */
1115 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
1117 if (server
->state
== ps_idle_server
)
1118 return (struct pipe_server
*)grab_object( server
);
1124 static int named_pipe_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
1126 struct named_pipe_device
*dev
= (struct named_pipe_device
*)parent
;
1128 if (parent
->ops
!= &named_pipe_device_ops
)
1130 set_error( STATUS_OBJECT_NAME_INVALID
);
1133 namespace_add( dev
->pipes
, name
);
1134 name
->parent
= grab_object( parent
);
1138 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
1139 unsigned int sharing
, unsigned int options
)
1141 struct named_pipe
*pipe
= (struct named_pipe
*)obj
;
1142 struct pipe_server
*server
;
1143 struct pipe_client
*client
;
1144 unsigned int pipe_sharing
;
1146 if (!(server
= find_available_server( pipe
)))
1148 set_error( STATUS_PIPE_NOT_AVAILABLE
);
1152 pipe_sharing
= server
->pipe
->sharing
;
1153 if (((access
& GENERIC_READ
) && !(pipe_sharing
& FILE_SHARE_READ
)) ||
1154 ((access
& GENERIC_WRITE
) && !(pipe_sharing
& FILE_SHARE_WRITE
)))
1156 set_error( STATUS_ACCESS_DENIED
);
1157 release_object( server
);
1161 if ((client
= create_pipe_client( options
, pipe
->flags
, pipe
->outsize
, options
)))
1163 set_no_fd_status( server
->pipe_end
.fd
, STATUS_BAD_DEVICE_TYPE
);
1164 allow_fd_caching( server
->pipe_end
.fd
);
1165 if (server
->state
== ps_wait_open
)
1166 fd_async_wake_up( server
->pipe_end
.fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
1167 set_server_state( server
, ps_connected_server
);
1168 server
->client
= client
;
1169 client
->server
= server
;
1170 server
->pipe_end
.connection
= &client
->pipe_end
;
1171 client
->pipe_end
.connection
= &server
->pipe_end
;
1173 release_object( server
);
1174 return &client
->pipe_end
.obj
;
1177 static int named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
)
1179 struct named_pipe_device
*device
= get_fd_user( fd
);
1183 case FSCTL_PIPE_WAIT
:
1185 const FILE_PIPE_WAIT_FOR_BUFFER
*buffer
= get_req_data();
1186 data_size_t size
= get_req_data_size();
1187 struct named_pipe
*pipe
;
1188 struct pipe_server
*server
;
1189 struct unicode_str name
;
1192 if (size
< sizeof(*buffer
) ||
1193 size
< FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[buffer
->NameLength
/sizeof(WCHAR
)]))
1195 set_error( STATUS_INVALID_PARAMETER
);
1198 name
.str
= buffer
->Name
;
1199 name
.len
= (buffer
->NameLength
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
1200 if (!(pipe
= open_named_object( &device
->obj
, &named_pipe_ops
, &name
, 0 ))) return 0;
1202 if (!(server
= find_available_server( pipe
)))
1204 queue_async( &pipe
->waiters
, async
);
1205 when
= buffer
->TimeoutSpecified
? buffer
->Timeout
.QuadPart
: pipe
->timeout
;
1206 async_set_timeout( async
, when
, STATUS_IO_TIMEOUT
);
1207 release_object( pipe
);
1208 set_error( STATUS_PENDING
);
1212 release_object( server
);
1213 release_object( pipe
);
1218 return default_fd_ioctl( fd
, code
, async
);
1223 DECL_HANDLER(create_named_pipe
)
1225 struct named_pipe
*pipe
;
1226 struct pipe_server
*server
;
1227 struct unicode_str name
;
1228 struct object
*root
;
1229 const struct security_descriptor
*sd
;
1230 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
1232 if (!objattr
) return;
1234 if (!req
->sharing
|| (req
->sharing
& ~(FILE_SHARE_READ
| FILE_SHARE_WRITE
)) ||
1235 (!(req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
) && (req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
)))
1237 if (root
) release_object( root
);
1238 set_error( STATUS_INVALID_PARAMETER
);
1242 if (!name
.len
) /* pipes need a root directory even without a name */
1244 if (!objattr
->rootdir
)
1246 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
1249 if (!(root
= get_directory_obj( current
->process
, objattr
->rootdir
))) return;
1252 pipe
= create_named_object( root
, &named_pipe_ops
, &name
, objattr
->attributes
| OBJ_OPENIF
, NULL
);
1254 if (root
) release_object( root
);
1257 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
1259 /* initialize it if it didn't already exist */
1260 pipe
->instances
= 0;
1261 init_async_queue( &pipe
->waiters
);
1262 list_init( &pipe
->servers
);
1263 pipe
->insize
= req
->insize
;
1264 pipe
->outsize
= req
->outsize
;
1265 pipe
->maxinstances
= req
->maxinstances
;
1266 pipe
->timeout
= req
->timeout
;
1267 pipe
->flags
= req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
;
1268 pipe
->sharing
= req
->sharing
;
1269 if (sd
) default_set_sd( &pipe
->obj
, sd
, OWNER_SECURITY_INFORMATION
|
1270 GROUP_SECURITY_INFORMATION
|
1271 DACL_SECURITY_INFORMATION
|
1272 SACL_SECURITY_INFORMATION
);
1276 if (pipe
->maxinstances
<= pipe
->instances
)
1278 set_error( STATUS_INSTANCE_NOT_AVAILABLE
);
1279 release_object( pipe
);
1282 if (pipe
->sharing
!= req
->sharing
)
1284 set_error( STATUS_ACCESS_DENIED
);
1285 release_object( pipe
);
1288 clear_error(); /* clear the name collision */
1291 server
= create_pipe_server( pipe
, req
->options
, req
->flags
);
1294 reply
->handle
= alloc_handle( current
->process
, server
, req
->access
, objattr
->attributes
);
1295 server
->pipe
->instances
++;
1296 release_object( server
);
1299 release_object( pipe
);
1302 DECL_HANDLER(get_named_pipe_info
)
1304 struct pipe_server
*server
;
1305 struct pipe_client
*client
= NULL
;
1307 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
);
1310 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
1314 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
1315 0, &pipe_client_ops
);
1316 if (!client
) return;
1317 server
= client
->server
;
1320 reply
->flags
= client
? client
->pipe_end
.flags
: server
->pipe_end
.flags
;
1323 reply
->sharing
= server
->pipe
->sharing
;
1324 reply
->maxinstances
= server
->pipe
->maxinstances
;
1325 reply
->instances
= server
->pipe
->instances
;
1326 reply
->insize
= server
->pipe
->insize
;
1327 reply
->outsize
= server
->pipe
->outsize
;
1331 release_object(client
);
1334 reply
->flags
|= NAMED_PIPE_SERVER_END
;
1335 release_object(server
);
1339 DECL_HANDLER(set_named_pipe_info
)
1341 struct pipe_server
*server
;
1342 struct pipe_client
*client
= NULL
;
1344 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
);
1347 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
1351 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
1352 0, &pipe_client_ops
);
1353 if (!client
) return;
1354 if (!(server
= client
->server
))
1356 release_object( client
);
1361 if ((req
->flags
& ~(NAMED_PIPE_MESSAGE_STREAM_READ
| NAMED_PIPE_NONBLOCKING_MODE
)) ||
1362 ((req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
) && !(server
->pipe
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
)))
1364 set_error( STATUS_INVALID_PARAMETER
);
1368 client
->pipe_end
.flags
= server
->pipe
->flags
| req
->flags
;
1372 server
->pipe_end
.flags
= server
->pipe
->flags
| req
->flags
;
1376 release_object(client
);
1378 release_object(server
);