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
26 #include "wine/port.h"
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_SOCKET_H
37 #include <sys/socket.h>
46 #define WIN32_NO_STATUS
70 struct object obj
; /* object header */
71 struct fd
*fd
; /* pipe file descriptor */
72 struct fd
*ioctl_fd
; /* file descriptor for ioctls when not connected */
73 struct list entry
; /* entry in named pipe servers list */
74 enum pipe_state state
; /* server state */
75 struct pipe_client
*client
; /* client that this server is connected to */
76 struct named_pipe
*pipe
;
77 struct timeout_user
*flush_poll
;
78 unsigned int options
; /* pipe options */
79 unsigned int pipe_flags
;
84 struct object obj
; /* object header */
85 struct fd
*fd
; /* pipe file descriptor */
86 struct pipe_server
*server
; /* server that this client is connected to */
87 unsigned int flags
; /* file flags */
88 unsigned int pipe_flags
;
93 struct object obj
; /* object header */
96 unsigned int maxinstances
;
99 unsigned int instances
;
101 struct list servers
; /* list of servers using this pipe */
102 struct async_queue
*waiters
; /* list of clients waiting to connect */
105 struct named_pipe_device
107 struct object obj
; /* object header */
108 struct fd
*fd
; /* pseudo-fd for ioctls */
109 struct namespace *pipes
; /* named pipe namespace */
112 static void named_pipe_dump( struct object
*obj
, int verbose
);
113 static unsigned int named_pipe_map_access( struct object
*obj
, unsigned int access
);
114 static int named_pipe_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
);
115 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
116 unsigned int sharing
, unsigned int options
);
117 static void named_pipe_destroy( struct object
*obj
);
119 static const struct object_ops named_pipe_ops
=
121 sizeof(struct named_pipe
), /* size */
122 named_pipe_dump
, /* dump */
123 no_get_type
, /* get_type */
124 no_add_queue
, /* add_queue */
125 NULL
, /* remove_queue */
127 NULL
, /* satisfied */
128 no_signal
, /* signal */
129 no_get_fd
, /* get_fd */
130 named_pipe_map_access
, /* map_access */
131 default_get_sd
, /* get_sd */
132 default_set_sd
, /* set_sd */
133 no_lookup_name
, /* lookup_name */
134 named_pipe_link_name
, /* link_name */
135 default_unlink_name
, /* unlink_name */
136 named_pipe_open_file
, /* open_file */
137 no_close_handle
, /* close_handle */
138 named_pipe_destroy
/* destroy */
141 /* server end functions */
142 static void pipe_server_dump( struct object
*obj
, int verbose
);
143 static struct fd
*pipe_server_get_fd( struct object
*obj
);
144 static void pipe_server_destroy( struct object
*obj
);
145 static obj_handle_t
pipe_server_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
);
146 static enum server_fd_type
pipe_server_get_fd_type( struct fd
*fd
);
147 static obj_handle_t
pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async
,
150 static const struct object_ops pipe_server_ops
=
152 sizeof(struct pipe_server
), /* size */
153 pipe_server_dump
, /* dump */
154 no_get_type
, /* get_type */
155 add_queue
, /* add_queue */
156 remove_queue
, /* remove_queue */
157 default_fd_signaled
, /* signaled */
158 no_satisfied
, /* satisfied */
159 no_signal
, /* signal */
160 pipe_server_get_fd
, /* get_fd */
161 default_fd_map_access
, /* map_access */
162 default_get_sd
, /* get_sd */
163 default_set_sd
, /* set_sd */
164 no_lookup_name
, /* lookup_name */
165 no_link_name
, /* link_name */
166 NULL
, /* unlink_name */
167 no_open_file
, /* open_file */
168 fd_close_handle
, /* close_handle */
169 pipe_server_destroy
/* destroy */
172 static const struct fd_ops pipe_server_fd_ops
=
174 default_fd_get_poll_events
, /* get_poll_events */
175 default_poll_event
, /* poll_event */
176 pipe_server_get_fd_type
, /* get_fd_type */
177 no_fd_read
, /* read */
178 no_fd_write
, /* write */
179 pipe_server_flush
, /* flush */
180 pipe_server_ioctl
, /* ioctl */
181 default_fd_queue_async
, /* queue_async */
182 default_fd_reselect_async
, /* reselect_async */
183 default_fd_cancel_async
, /* cancel_async */
186 /* client end functions */
187 static void pipe_client_dump( struct object
*obj
, int verbose
);
188 static int pipe_client_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
189 static struct fd
*pipe_client_get_fd( struct object
*obj
);
190 static void pipe_client_destroy( struct object
*obj
);
191 static obj_handle_t
pipe_client_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
);
192 static enum server_fd_type
pipe_client_get_fd_type( struct fd
*fd
);
194 static const struct object_ops pipe_client_ops
=
196 sizeof(struct pipe_client
), /* size */
197 pipe_client_dump
, /* dump */
198 no_get_type
, /* get_type */
199 add_queue
, /* add_queue */
200 remove_queue
, /* remove_queue */
201 pipe_client_signaled
, /* signaled */
202 no_satisfied
, /* satisfied */
203 no_signal
, /* signal */
204 pipe_client_get_fd
, /* get_fd */
205 default_fd_map_access
, /* map_access */
206 default_get_sd
, /* get_sd */
207 default_set_sd
, /* set_sd */
208 no_lookup_name
, /* lookup_name */
209 no_link_name
, /* link_name */
210 NULL
, /* unlink_name */
211 no_open_file
, /* open_file */
212 fd_close_handle
, /* close_handle */
213 pipe_client_destroy
/* destroy */
216 static const struct fd_ops pipe_client_fd_ops
=
218 default_fd_get_poll_events
, /* get_poll_events */
219 default_poll_event
, /* poll_event */
220 pipe_client_get_fd_type
, /* get_fd_type */
221 no_fd_read
, /* read */
222 no_fd_write
, /* write */
223 pipe_client_flush
, /* flush */
224 default_fd_ioctl
, /* ioctl */
225 default_fd_queue_async
, /* queue_async */
226 default_fd_reselect_async
, /* reselect_async */
227 default_fd_cancel_async
/* cancel_async */
230 static void named_pipe_device_dump( struct object
*obj
, int verbose
);
231 static struct object_type
*named_pipe_device_get_type( struct object
*obj
);
232 static struct fd
*named_pipe_device_get_fd( struct object
*obj
);
233 static struct object
*named_pipe_device_lookup_name( struct object
*obj
,
234 struct unicode_str
*name
, unsigned int attr
);
235 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
236 unsigned int sharing
, unsigned int options
);
237 static void named_pipe_device_destroy( struct object
*obj
);
238 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
);
239 static obj_handle_t
named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
,
240 const async_data_t
*async_data
, int blocking
);
242 static const struct object_ops named_pipe_device_ops
=
244 sizeof(struct named_pipe_device
), /* size */
245 named_pipe_device_dump
, /* dump */
246 named_pipe_device_get_type
, /* get_type */
247 no_add_queue
, /* add_queue */
248 NULL
, /* remove_queue */
250 no_satisfied
, /* satisfied */
251 no_signal
, /* signal */
252 named_pipe_device_get_fd
, /* get_fd */
253 no_map_access
, /* map_access */
254 default_get_sd
, /* get_sd */
255 default_set_sd
, /* set_sd */
256 named_pipe_device_lookup_name
, /* lookup_name */
257 directory_link_name
, /* link_name */
258 default_unlink_name
, /* unlink_name */
259 named_pipe_device_open_file
, /* open_file */
260 fd_close_handle
, /* close_handle */
261 named_pipe_device_destroy
/* destroy */
264 static const struct fd_ops named_pipe_device_fd_ops
=
266 default_fd_get_poll_events
, /* get_poll_events */
267 default_poll_event
, /* poll_event */
268 named_pipe_device_get_fd_type
, /* get_fd_type */
269 no_fd_read
, /* read */
270 no_fd_write
, /* write */
271 no_fd_flush
, /* flush */
272 named_pipe_device_ioctl
, /* ioctl */
273 default_fd_queue_async
, /* queue_async */
274 default_fd_reselect_async
, /* reselect_async */
275 default_fd_cancel_async
/* cancel_async */
278 static void named_pipe_dump( struct object
*obj
, int verbose
)
280 fputs( "Named pipe\n", stderr
);
283 static unsigned int named_pipe_map_access( struct object
*obj
, unsigned int access
)
285 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
;
286 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| FILE_CREATE_PIPE_INSTANCE
;
287 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
288 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_ALL
;
289 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
292 static void pipe_server_dump( struct object
*obj
, int verbose
)
294 struct pipe_server
*server
= (struct pipe_server
*) obj
;
295 assert( obj
->ops
== &pipe_server_ops
);
296 fprintf( stderr
, "Named pipe server pipe=%p state=%d\n", server
->pipe
, server
->state
);
299 static void pipe_client_dump( struct object
*obj
, int verbose
)
301 struct pipe_client
*client
= (struct pipe_client
*) obj
;
302 assert( obj
->ops
== &pipe_client_ops
);
303 fprintf( stderr
, "Named pipe client server=%p\n", client
->server
);
306 static int pipe_client_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
308 struct pipe_client
*client
= (struct pipe_client
*) obj
;
310 return client
->fd
&& is_fd_signaled(client
->fd
);
313 static void named_pipe_destroy( struct object
*obj
)
315 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
317 assert( list_empty( &pipe
->servers
) );
318 assert( !pipe
->instances
);
319 free_async_queue( pipe
->waiters
);
322 static struct fd
*pipe_client_get_fd( struct object
*obj
)
324 struct pipe_client
*client
= (struct pipe_client
*) obj
;
326 return (struct fd
*) grab_object( client
->fd
);
327 set_error( STATUS_PIPE_DISCONNECTED
);
331 static void set_server_state( struct pipe_server
*server
, enum pipe_state state
)
333 server
->state
= state
;
337 case ps_connected_server
:
338 case ps_wait_disconnect
:
339 assert( server
->fd
);
343 assert( !server
->fd
);
344 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_LISTENING
);
346 case ps_wait_connect
:
347 assert( !server
->fd
);
348 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_DISCONNECTED
);
353 static struct fd
*pipe_server_get_fd( struct object
*obj
)
355 struct pipe_server
*server
= (struct pipe_server
*) obj
;
357 return (struct fd
*)grab_object( server
->fd
? server
->fd
: server
->ioctl_fd
);
361 static void notify_empty( struct pipe_server
*server
)
363 if (!server
->flush_poll
)
365 assert( server
->state
== ps_connected_server
);
366 remove_timeout_user( server
->flush_poll
);
367 server
->flush_poll
= NULL
;
368 fd_async_wake_up( server
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
371 static void do_disconnect( struct pipe_server
*server
)
373 /* we may only have a server fd, if the client disconnected */
376 assert( server
->client
->server
== server
);
377 assert( server
->client
->fd
);
378 release_object( server
->client
->fd
);
379 server
->client
->fd
= NULL
;
381 assert( server
->fd
);
382 shutdown( get_unix_fd( server
->fd
), SHUT_RDWR
);
383 release_object( server
->fd
);
387 static void pipe_server_destroy( struct object
*obj
)
389 struct pipe_server
*server
= (struct pipe_server
*)obj
;
391 assert( obj
->ops
== &pipe_server_ops
);
395 notify_empty( server
);
396 do_disconnect( server
);
401 server
->client
->server
= NULL
;
402 server
->client
= NULL
;
405 assert( server
->pipe
->instances
);
406 server
->pipe
->instances
--;
408 if (server
->ioctl_fd
) release_object( server
->ioctl_fd
);
409 list_remove( &server
->entry
);
410 release_object( server
->pipe
);
413 static void pipe_client_destroy( struct object
*obj
)
415 struct pipe_client
*client
= (struct pipe_client
*)obj
;
416 struct pipe_server
*server
= client
->server
;
418 assert( obj
->ops
== &pipe_client_ops
);
422 notify_empty( server
);
424 switch(server
->state
)
426 case ps_connected_server
:
427 /* Don't destroy the server's fd here as we can't
428 do a successful flush without it. */
429 set_server_state( server
, ps_wait_disconnect
);
433 case ps_wait_disconnect
:
434 case ps_wait_connect
:
437 assert( server
->client
);
438 server
->client
= NULL
;
439 client
->server
= NULL
;
441 if (client
->fd
) release_object( client
->fd
);
444 static void named_pipe_device_dump( struct object
*obj
, int verbose
)
446 fputs( "Named pipe device\n", stderr
);
449 static struct object_type
*named_pipe_device_get_type( struct object
*obj
)
451 static const WCHAR name
[] = {'D','e','v','i','c','e'};
452 static const struct unicode_str str
= { name
, sizeof(name
) };
453 return get_object_type( &str
);
456 static struct fd
*named_pipe_device_get_fd( struct object
*obj
)
458 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
459 return (struct fd
*)grab_object( device
->fd
);
462 static struct object
*named_pipe_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
465 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
466 struct object
*found
;
468 assert( obj
->ops
== &named_pipe_device_ops
);
469 assert( device
->pipes
);
471 if (!name
) return NULL
; /* open the device itself */
473 if ((found
= find_object( device
->pipes
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
479 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
480 unsigned int sharing
, unsigned int options
)
482 return grab_object( obj
);
485 static void named_pipe_device_destroy( struct object
*obj
)
487 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
488 assert( obj
->ops
== &named_pipe_device_ops
);
489 if (device
->fd
) release_object( device
->fd
);
490 free( device
->pipes
);
493 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
)
495 return FD_TYPE_DEVICE
;
498 struct object
*create_named_pipe_device( struct object
*root
, const struct unicode_str
*name
)
500 struct named_pipe_device
*dev
;
502 if ((dev
= create_named_object( root
, &named_pipe_device_ops
, name
, 0, NULL
)) &&
503 get_error() != STATUS_OBJECT_NAME_EXISTS
)
506 if (!(dev
->fd
= alloc_pseudo_fd( &named_pipe_device_fd_ops
, &dev
->obj
, 0 )) ||
507 !(dev
->pipes
= create_namespace( 7 )))
509 release_object( dev
);
516 static int pipe_data_remaining( struct pipe_server
*server
)
521 assert( server
->client
);
523 fd
= get_unix_fd( server
->client
->fd
);
530 if (0 > poll( &pfd
, 1, 0 ))
533 return pfd
.revents
&POLLIN
;
536 static void check_flushed( void *arg
)
538 struct pipe_server
*server
= (struct pipe_server
*) arg
;
540 if (pipe_data_remaining( server
))
542 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
546 server
->flush_poll
= NULL
;
547 fd_async_wake_up( server
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
551 static obj_handle_t
pipe_server_flush( struct fd
*fd
, const async_data_t
*async_data
, int blocking
)
553 struct pipe_server
*server
= get_fd_user( fd
);
554 obj_handle_t handle
= 0;
557 if (!server
|| server
->state
!= ps_connected_server
) return 0;
559 if (!pipe_data_remaining( server
)) return 0;
561 if ((async
= fd_queue_async( server
->fd
, async_data
, ASYNC_TYPE_WAIT
)))
563 /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
564 if (!server
->flush_poll
)
565 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
566 if (blocking
) handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
567 release_object( async
);
568 set_error( STATUS_PENDING
);
573 static obj_handle_t
pipe_client_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
)
575 /* FIXME: what do we have to do for this? */
579 static inline int is_overlapped( unsigned int options
)
581 return !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
584 static enum server_fd_type
pipe_server_get_fd_type( struct fd
*fd
)
589 static enum server_fd_type
pipe_client_get_fd_type( struct fd
*fd
)
594 static obj_handle_t
pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
597 struct pipe_server
*server
= get_fd_user( fd
);
599 obj_handle_t wait_handle
= 0;
603 case FSCTL_PIPE_LISTEN
:
604 switch(server
->state
)
607 case ps_wait_connect
:
608 if ((async
= fd_queue_async( server
->ioctl_fd
, async_data
, ASYNC_TYPE_WAIT
)))
610 if (blocking
) wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
611 set_server_state( server
, ps_wait_open
);
612 if (server
->pipe
->waiters
) async_wake_up( server
->pipe
->waiters
, STATUS_SUCCESS
);
613 release_object( async
);
614 set_error( STATUS_PENDING
);
618 case ps_connected_server
:
619 set_error( STATUS_PIPE_CONNECTED
);
621 case ps_wait_disconnect
:
622 set_error( STATUS_NO_DATA_DETECTED
);
625 set_error( STATUS_INVALID_HANDLE
);
630 case FSCTL_PIPE_DISCONNECT
:
631 switch(server
->state
)
633 case ps_connected_server
:
634 assert( server
->client
);
635 assert( server
->client
->fd
);
637 notify_empty( server
);
639 /* dump the client and server fds - client loses all waiting data */
640 do_disconnect( server
);
641 server
->client
->server
= NULL
;
642 server
->client
= NULL
;
643 set_server_state( server
, ps_wait_connect
);
645 case ps_wait_disconnect
:
646 assert( !server
->client
);
647 do_disconnect( server
);
648 set_server_state( server
, ps_wait_connect
);
652 set_error( STATUS_PIPE_LISTENING
);
654 case ps_wait_connect
:
655 set_error( STATUS_PIPE_DISCONNECTED
);
661 return default_fd_ioctl( fd
, code
, async_data
, blocking
);
665 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
666 obj_handle_t handle
, unsigned int access
)
669 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
670 return (struct pipe_server
*) obj
;
673 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
, unsigned int options
,
674 unsigned int pipe_flags
)
676 struct pipe_server
*server
;
678 server
= alloc_object( &pipe_server_ops
);
684 server
->client
= NULL
;
685 server
->flush_poll
= NULL
;
686 server
->options
= options
;
687 server
->pipe_flags
= pipe_flags
;
689 list_add_head( &pipe
->servers
, &server
->entry
);
691 if (!(server
->ioctl_fd
= alloc_pseudo_fd( &pipe_server_fd_ops
, &server
->obj
, options
)))
693 release_object( server
);
696 set_server_state( server
, ps_idle_server
);
700 static struct pipe_client
*create_pipe_client( unsigned int flags
, unsigned int pipe_flags
)
702 struct pipe_client
*client
;
704 client
= alloc_object( &pipe_client_ops
);
709 client
->server
= NULL
;
710 client
->flags
= flags
;
711 client
->pipe_flags
= pipe_flags
;
716 static struct pipe_server
*find_available_server( struct named_pipe
*pipe
)
718 struct pipe_server
*server
;
720 /* look for pipe servers that are listening */
721 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
723 if (server
->state
== ps_wait_open
)
724 return (struct pipe_server
*)grab_object( server
);
727 /* fall back to pipe servers that are idle */
728 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
730 if (server
->state
== ps_idle_server
)
731 return (struct pipe_server
*)grab_object( server
);
737 static int named_pipe_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
739 struct named_pipe_device
*dev
= (struct named_pipe_device
*)parent
;
741 if (parent
->ops
!= &named_pipe_device_ops
)
743 set_error( STATUS_OBJECT_NAME_INVALID
);
746 namespace_add( dev
->pipes
, name
);
747 name
->parent
= grab_object( parent
);
751 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
752 unsigned int sharing
, unsigned int options
)
754 struct named_pipe
*pipe
= (struct named_pipe
*)obj
;
755 struct pipe_server
*server
;
756 struct pipe_client
*client
;
757 unsigned int pipe_sharing
;
760 if (!(server
= find_available_server( pipe
)))
762 set_error( STATUS_PIPE_NOT_AVAILABLE
);
766 pipe_sharing
= server
->pipe
->sharing
;
767 if (((access
& GENERIC_READ
) && !(pipe_sharing
& FILE_SHARE_READ
)) ||
768 ((access
& GENERIC_WRITE
) && !(pipe_sharing
& FILE_SHARE_WRITE
)))
770 set_error( STATUS_ACCESS_DENIED
);
771 release_object( server
);
775 if ((client
= create_pipe_client( options
, pipe
->flags
)))
777 if (!socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
))
779 assert( !server
->fd
);
781 /* for performance reasons, only set nonblocking mode when using
782 * overlapped I/O. Otherwise, we will be doing too much busy
784 if (is_overlapped( options
)) fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
785 if (is_overlapped( server
->options
)) fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
789 setsockopt( fds
[0], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
790 setsockopt( fds
[1], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
794 setsockopt( fds
[0], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
795 setsockopt( fds
[1], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
798 client
->fd
= create_anonymous_fd( &pipe_client_fd_ops
, fds
[1], &client
->obj
, options
);
799 server
->fd
= create_anonymous_fd( &pipe_server_fd_ops
, fds
[0], &server
->obj
, server
->options
);
800 if (client
->fd
&& server
->fd
)
802 allow_fd_caching( client
->fd
);
803 allow_fd_caching( server
->fd
);
804 fd_copy_completion( server
->ioctl_fd
, server
->fd
);
805 if (server
->state
== ps_wait_open
)
806 fd_async_wake_up( server
->ioctl_fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
807 set_server_state( server
, ps_connected_server
);
808 server
->client
= client
;
809 client
->server
= server
;
813 release_object( client
);
820 release_object( client
);
824 release_object( server
);
828 static obj_handle_t
named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
,
829 const async_data_t
*async_data
, int blocking
)
831 struct named_pipe_device
*device
= get_fd_user( fd
);
835 case FSCTL_PIPE_WAIT
:
837 const FILE_PIPE_WAIT_FOR_BUFFER
*buffer
= get_req_data();
838 data_size_t size
= get_req_data_size();
839 obj_handle_t wait_handle
= 0;
840 struct named_pipe
*pipe
;
841 struct pipe_server
*server
;
842 struct unicode_str name
;
844 if (size
< sizeof(*buffer
) ||
845 size
< FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[buffer
->NameLength
/sizeof(WCHAR
)]))
847 set_error( STATUS_INVALID_PARAMETER
);
850 name
.str
= buffer
->Name
;
851 name
.len
= (buffer
->NameLength
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
852 if (!(pipe
= open_named_object( &device
->obj
, &named_pipe_ops
, &name
, 0 ))) return 0;
854 if (!(server
= find_available_server( pipe
)))
858 if (!pipe
->waiters
&& !(pipe
->waiters
= create_async_queue( NULL
))) goto done
;
860 if ((async
= create_async( current
, pipe
->waiters
, async_data
)))
862 timeout_t when
= buffer
->TimeoutSpecified
? buffer
->Timeout
.QuadPart
: pipe
->timeout
;
863 async_set_timeout( async
, when
, STATUS_IO_TIMEOUT
);
864 if (blocking
) wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
865 release_object( async
);
866 set_error( STATUS_PENDING
);
869 else release_object( server
);
872 release_object( pipe
);
877 return default_fd_ioctl( fd
, code
, async_data
, blocking
);
882 DECL_HANDLER(create_named_pipe
)
884 struct named_pipe
*pipe
;
885 struct pipe_server
*server
;
886 struct unicode_str name
;
888 const struct security_descriptor
*sd
;
889 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
891 if (!objattr
) return;
893 if (!req
->sharing
|| (req
->sharing
& ~(FILE_SHARE_READ
| FILE_SHARE_WRITE
)) ||
894 (!(req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
) && (req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
)))
896 if (root
) release_object( root
);
897 set_error( STATUS_INVALID_PARAMETER
);
901 if (!name
.len
) /* pipes need a root directory even without a name */
903 if (!objattr
->rootdir
)
905 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
908 if (!(root
= get_directory_obj( current
->process
, objattr
->rootdir
))) return;
911 pipe
= create_named_object( root
, &named_pipe_ops
, &name
, objattr
->attributes
| OBJ_OPENIF
, NULL
);
913 if (root
) release_object( root
);
916 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
918 /* initialize it if it didn't already exist */
920 pipe
->waiters
= NULL
;
921 list_init( &pipe
->servers
);
922 pipe
->insize
= req
->insize
;
923 pipe
->outsize
= req
->outsize
;
924 pipe
->maxinstances
= req
->maxinstances
;
925 pipe
->timeout
= req
->timeout
;
926 pipe
->flags
= req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
;
927 pipe
->sharing
= req
->sharing
;
931 if (pipe
->maxinstances
<= pipe
->instances
)
933 set_error( STATUS_INSTANCE_NOT_AVAILABLE
);
934 release_object( pipe
);
937 if (pipe
->sharing
!= req
->sharing
)
939 set_error( STATUS_ACCESS_DENIED
);
940 release_object( pipe
);
943 clear_error(); /* clear the name collision */
946 server
= create_pipe_server( pipe
, req
->options
, req
->flags
);
949 reply
->handle
= alloc_handle( current
->process
, server
, req
->access
, objattr
->attributes
);
950 server
->pipe
->instances
++;
951 if (sd
) default_set_sd( &server
->obj
, sd
, OWNER_SECURITY_INFORMATION
|
952 GROUP_SECURITY_INFORMATION
|
953 DACL_SECURITY_INFORMATION
|
954 SACL_SECURITY_INFORMATION
);
955 release_object( server
);
958 release_object( pipe
);
961 DECL_HANDLER(get_named_pipe_info
)
963 struct pipe_server
*server
;
964 struct pipe_client
*client
= NULL
;
966 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
);
969 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
973 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
974 0, &pipe_client_ops
);
976 server
= client
->server
;
979 reply
->flags
= client
? client
->pipe_flags
: server
->pipe_flags
;
982 reply
->sharing
= server
->pipe
->sharing
;
983 reply
->maxinstances
= server
->pipe
->maxinstances
;
984 reply
->instances
= server
->pipe
->instances
;
985 reply
->insize
= server
->pipe
->insize
;
986 reply
->outsize
= server
->pipe
->outsize
;
990 release_object(client
);
993 reply
->flags
|= NAMED_PIPE_SERVER_END
;
994 release_object(server
);
998 DECL_HANDLER(set_named_pipe_info
)
1000 struct pipe_server
*server
;
1001 struct pipe_client
*client
= NULL
;
1003 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
);
1006 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
1010 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
1011 0, &pipe_client_ops
);
1012 if (!client
) return;
1013 if (!(server
= client
->server
))
1015 release_object( client
);
1020 if ((req
->flags
& ~(NAMED_PIPE_MESSAGE_STREAM_READ
| NAMED_PIPE_NONBLOCKING_MODE
)) ||
1021 ((req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
) && !(server
->pipe
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
)))
1023 set_error( STATUS_INVALID_PARAMETER
);
1027 client
->pipe_flags
= server
->pipe
->flags
| req
->flags
;
1031 server
->pipe_flags
= server
->pipe
->flags
| req
->flags
;
1035 release_object(client
);
1037 release_object(server
);