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 */
185 /* client end functions */
186 static void pipe_client_dump( struct object
*obj
, int verbose
);
187 static int pipe_client_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
188 static struct fd
*pipe_client_get_fd( struct object
*obj
);
189 static void pipe_client_destroy( struct object
*obj
);
190 static obj_handle_t
pipe_client_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
);
191 static enum server_fd_type
pipe_client_get_fd_type( struct fd
*fd
);
193 static const struct object_ops pipe_client_ops
=
195 sizeof(struct pipe_client
), /* size */
196 pipe_client_dump
, /* dump */
197 no_get_type
, /* get_type */
198 add_queue
, /* add_queue */
199 remove_queue
, /* remove_queue */
200 pipe_client_signaled
, /* signaled */
201 no_satisfied
, /* satisfied */
202 no_signal
, /* signal */
203 pipe_client_get_fd
, /* get_fd */
204 default_fd_map_access
, /* map_access */
205 default_get_sd
, /* get_sd */
206 default_set_sd
, /* set_sd */
207 no_lookup_name
, /* lookup_name */
208 no_link_name
, /* link_name */
209 NULL
, /* unlink_name */
210 no_open_file
, /* open_file */
211 fd_close_handle
, /* close_handle */
212 pipe_client_destroy
/* destroy */
215 static const struct fd_ops pipe_client_fd_ops
=
217 default_fd_get_poll_events
, /* get_poll_events */
218 default_poll_event
, /* poll_event */
219 pipe_client_get_fd_type
, /* get_fd_type */
220 no_fd_read
, /* read */
221 no_fd_write
, /* write */
222 pipe_client_flush
, /* flush */
223 default_fd_ioctl
, /* ioctl */
224 default_fd_queue_async
, /* queue_async */
225 default_fd_reselect_async
/* reselect_async */
228 static void named_pipe_device_dump( struct object
*obj
, int verbose
);
229 static struct object_type
*named_pipe_device_get_type( struct object
*obj
);
230 static struct fd
*named_pipe_device_get_fd( struct object
*obj
);
231 static struct object
*named_pipe_device_lookup_name( struct object
*obj
,
232 struct unicode_str
*name
, unsigned int attr
);
233 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
234 unsigned int sharing
, unsigned int options
);
235 static void named_pipe_device_destroy( struct object
*obj
);
236 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
);
237 static obj_handle_t
named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
,
238 const async_data_t
*async_data
, int blocking
);
240 static const struct object_ops named_pipe_device_ops
=
242 sizeof(struct named_pipe_device
), /* size */
243 named_pipe_device_dump
, /* dump */
244 named_pipe_device_get_type
, /* get_type */
245 no_add_queue
, /* add_queue */
246 NULL
, /* remove_queue */
248 no_satisfied
, /* satisfied */
249 no_signal
, /* signal */
250 named_pipe_device_get_fd
, /* get_fd */
251 no_map_access
, /* map_access */
252 default_get_sd
, /* get_sd */
253 default_set_sd
, /* set_sd */
254 named_pipe_device_lookup_name
, /* lookup_name */
255 directory_link_name
, /* link_name */
256 default_unlink_name
, /* unlink_name */
257 named_pipe_device_open_file
, /* open_file */
258 fd_close_handle
, /* close_handle */
259 named_pipe_device_destroy
/* destroy */
262 static const struct fd_ops named_pipe_device_fd_ops
=
264 default_fd_get_poll_events
, /* get_poll_events */
265 default_poll_event
, /* poll_event */
266 named_pipe_device_get_fd_type
, /* get_fd_type */
267 no_fd_read
, /* read */
268 no_fd_write
, /* write */
269 no_fd_flush
, /* flush */
270 named_pipe_device_ioctl
, /* ioctl */
271 default_fd_queue_async
, /* queue_async */
272 default_fd_reselect_async
/* reselect_async */
275 static void named_pipe_dump( struct object
*obj
, int verbose
)
277 fputs( "Named pipe\n", stderr
);
280 static unsigned int named_pipe_map_access( struct object
*obj
, unsigned int access
)
282 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
;
283 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| FILE_CREATE_PIPE_INSTANCE
;
284 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
285 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_ALL
;
286 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
289 static void pipe_server_dump( struct object
*obj
, int verbose
)
291 struct pipe_server
*server
= (struct pipe_server
*) obj
;
292 assert( obj
->ops
== &pipe_server_ops
);
293 fprintf( stderr
, "Named pipe server pipe=%p state=%d\n", server
->pipe
, server
->state
);
296 static void pipe_client_dump( struct object
*obj
, int verbose
)
298 struct pipe_client
*client
= (struct pipe_client
*) obj
;
299 assert( obj
->ops
== &pipe_client_ops
);
300 fprintf( stderr
, "Named pipe client server=%p\n", client
->server
);
303 static int pipe_client_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
305 struct pipe_client
*client
= (struct pipe_client
*) obj
;
307 return client
->fd
&& is_fd_signaled(client
->fd
);
310 static void named_pipe_destroy( struct object
*obj
)
312 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
314 assert( list_empty( &pipe
->servers
) );
315 assert( !pipe
->instances
);
316 free_async_queue( pipe
->waiters
);
319 static struct fd
*pipe_client_get_fd( struct object
*obj
)
321 struct pipe_client
*client
= (struct pipe_client
*) obj
;
323 return (struct fd
*) grab_object( client
->fd
);
324 set_error( STATUS_PIPE_DISCONNECTED
);
328 static void set_server_state( struct pipe_server
*server
, enum pipe_state state
)
330 server
->state
= state
;
334 case ps_connected_server
:
335 case ps_wait_disconnect
:
336 assert( server
->fd
);
340 assert( !server
->fd
);
341 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_LISTENING
);
343 case ps_wait_connect
:
344 assert( !server
->fd
);
345 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_DISCONNECTED
);
350 static struct fd
*pipe_server_get_fd( struct object
*obj
)
352 struct pipe_server
*server
= (struct pipe_server
*) obj
;
354 return (struct fd
*)grab_object( server
->fd
? server
->fd
: server
->ioctl_fd
);
358 static void notify_empty( struct pipe_server
*server
)
360 if (!server
->flush_poll
)
362 assert( server
->state
== ps_connected_server
);
363 remove_timeout_user( server
->flush_poll
);
364 server
->flush_poll
= NULL
;
365 fd_async_wake_up( server
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
368 static void do_disconnect( struct pipe_server
*server
)
370 /* we may only have a server fd, if the client disconnected */
373 assert( server
->client
->server
== server
);
374 assert( server
->client
->fd
);
375 release_object( server
->client
->fd
);
376 server
->client
->fd
= NULL
;
378 assert( server
->fd
);
379 shutdown( get_unix_fd( server
->fd
), SHUT_RDWR
);
380 release_object( server
->fd
);
384 static void pipe_server_destroy( struct object
*obj
)
386 struct pipe_server
*server
= (struct pipe_server
*)obj
;
388 assert( obj
->ops
== &pipe_server_ops
);
392 notify_empty( server
);
393 do_disconnect( server
);
398 server
->client
->server
= NULL
;
399 server
->client
= NULL
;
402 assert( server
->pipe
->instances
);
403 server
->pipe
->instances
--;
405 if (server
->ioctl_fd
) release_object( server
->ioctl_fd
);
406 list_remove( &server
->entry
);
407 release_object( server
->pipe
);
410 static void pipe_client_destroy( struct object
*obj
)
412 struct pipe_client
*client
= (struct pipe_client
*)obj
;
413 struct pipe_server
*server
= client
->server
;
415 assert( obj
->ops
== &pipe_client_ops
);
419 notify_empty( server
);
421 switch(server
->state
)
423 case ps_connected_server
:
424 /* Don't destroy the server's fd here as we can't
425 do a successful flush without it. */
426 set_server_state( server
, ps_wait_disconnect
);
430 case ps_wait_disconnect
:
431 case ps_wait_connect
:
434 assert( server
->client
);
435 server
->client
= NULL
;
436 client
->server
= NULL
;
438 if (client
->fd
) release_object( client
->fd
);
441 static void named_pipe_device_dump( struct object
*obj
, int verbose
)
443 fputs( "Named pipe device\n", stderr
);
446 static struct object_type
*named_pipe_device_get_type( struct object
*obj
)
448 static const WCHAR name
[] = {'D','e','v','i','c','e'};
449 static const struct unicode_str str
= { name
, sizeof(name
) };
450 return get_object_type( &str
);
453 static struct fd
*named_pipe_device_get_fd( struct object
*obj
)
455 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
456 return (struct fd
*)grab_object( device
->fd
);
459 static struct object
*named_pipe_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
462 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
463 struct object
*found
;
465 assert( obj
->ops
== &named_pipe_device_ops
);
466 assert( device
->pipes
);
468 if (!name
) return NULL
; /* open the device itself */
470 if ((found
= find_object( device
->pipes
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
476 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
477 unsigned int sharing
, unsigned int options
)
479 return grab_object( obj
);
482 static void named_pipe_device_destroy( struct object
*obj
)
484 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
485 assert( obj
->ops
== &named_pipe_device_ops
);
486 if (device
->fd
) release_object( device
->fd
);
487 free( device
->pipes
);
490 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
)
492 return FD_TYPE_DEVICE
;
495 struct object
*create_named_pipe_device( struct object
*root
, const struct unicode_str
*name
)
497 struct named_pipe_device
*dev
;
499 if ((dev
= create_named_object( root
, &named_pipe_device_ops
, name
, 0, NULL
)) &&
500 get_error() != STATUS_OBJECT_NAME_EXISTS
)
503 if (!(dev
->fd
= alloc_pseudo_fd( &named_pipe_device_fd_ops
, &dev
->obj
, 0 )) ||
504 !(dev
->pipes
= create_namespace( 7 )))
506 release_object( dev
);
513 static int pipe_data_remaining( struct pipe_server
*server
)
518 assert( server
->client
);
520 fd
= get_unix_fd( server
->client
->fd
);
527 if (0 > poll( &pfd
, 1, 0 ))
530 return pfd
.revents
&POLLIN
;
533 static void check_flushed( void *arg
)
535 struct pipe_server
*server
= (struct pipe_server
*) arg
;
537 if (pipe_data_remaining( server
))
539 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
543 server
->flush_poll
= NULL
;
544 fd_async_wake_up( server
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
548 static obj_handle_t
pipe_server_flush( struct fd
*fd
, const async_data_t
*async_data
, int blocking
)
550 struct pipe_server
*server
= get_fd_user( fd
);
551 obj_handle_t handle
= 0;
554 if (!server
|| server
->state
!= ps_connected_server
) return 0;
556 if (!pipe_data_remaining( server
)) return 0;
558 if ((async
= fd_queue_async( server
->fd
, async_data
, NULL
, ASYNC_TYPE_WAIT
)))
560 /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
561 if (!server
->flush_poll
)
562 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
563 if (blocking
) handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
564 release_object( async
);
565 set_error( STATUS_PENDING
);
570 static obj_handle_t
pipe_client_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
)
572 /* FIXME: what do we have to do for this? */
576 static inline int is_overlapped( unsigned int options
)
578 return !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
581 static enum server_fd_type
pipe_server_get_fd_type( struct fd
*fd
)
586 static enum server_fd_type
pipe_client_get_fd_type( struct fd
*fd
)
591 static obj_handle_t
pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
594 struct pipe_server
*server
= get_fd_user( fd
);
596 obj_handle_t wait_handle
= 0;
600 case FSCTL_PIPE_LISTEN
:
601 switch(server
->state
)
604 case ps_wait_connect
:
605 if ((async
= fd_queue_async( server
->ioctl_fd
, async_data
, NULL
, ASYNC_TYPE_WAIT
)))
607 if (blocking
) wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
608 set_server_state( server
, ps_wait_open
);
609 if (server
->pipe
->waiters
) async_wake_up( server
->pipe
->waiters
, STATUS_SUCCESS
);
610 release_object( async
);
611 set_error( STATUS_PENDING
);
615 case ps_connected_server
:
616 set_error( STATUS_PIPE_CONNECTED
);
618 case ps_wait_disconnect
:
619 set_error( STATUS_NO_DATA_DETECTED
);
622 set_error( STATUS_INVALID_HANDLE
);
627 case FSCTL_PIPE_DISCONNECT
:
628 switch(server
->state
)
630 case ps_connected_server
:
631 assert( server
->client
);
632 assert( server
->client
->fd
);
634 notify_empty( server
);
636 /* dump the client and server fds - client loses all waiting data */
637 do_disconnect( server
);
638 server
->client
->server
= NULL
;
639 server
->client
= NULL
;
640 set_server_state( server
, ps_wait_connect
);
642 case ps_wait_disconnect
:
643 assert( !server
->client
);
644 do_disconnect( server
);
645 set_server_state( server
, ps_wait_connect
);
649 set_error( STATUS_PIPE_LISTENING
);
651 case ps_wait_connect
:
652 set_error( STATUS_PIPE_DISCONNECTED
);
658 return default_fd_ioctl( fd
, code
, async_data
, blocking
);
662 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
663 obj_handle_t handle
, unsigned int access
)
666 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
667 return (struct pipe_server
*) obj
;
670 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
, unsigned int options
,
671 unsigned int pipe_flags
)
673 struct pipe_server
*server
;
675 server
= alloc_object( &pipe_server_ops
);
681 server
->client
= NULL
;
682 server
->flush_poll
= NULL
;
683 server
->options
= options
;
684 server
->pipe_flags
= pipe_flags
;
686 list_add_head( &pipe
->servers
, &server
->entry
);
688 if (!(server
->ioctl_fd
= alloc_pseudo_fd( &pipe_server_fd_ops
, &server
->obj
, options
)))
690 release_object( server
);
693 set_fd_signaled( server
->ioctl_fd
, 1 );
694 set_server_state( server
, ps_idle_server
);
698 static struct pipe_client
*create_pipe_client( unsigned int flags
, unsigned int pipe_flags
)
700 struct pipe_client
*client
;
702 client
= alloc_object( &pipe_client_ops
);
707 client
->server
= NULL
;
708 client
->flags
= flags
;
709 client
->pipe_flags
= pipe_flags
;
714 static struct pipe_server
*find_available_server( struct named_pipe
*pipe
)
716 struct pipe_server
*server
;
718 /* look for pipe servers that are listening */
719 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
721 if (server
->state
== ps_wait_open
)
722 return (struct pipe_server
*)grab_object( server
);
725 /* fall back to pipe servers that are idle */
726 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
728 if (server
->state
== ps_idle_server
)
729 return (struct pipe_server
*)grab_object( server
);
735 static int named_pipe_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
737 struct named_pipe_device
*dev
= (struct named_pipe_device
*)parent
;
739 if (parent
->ops
!= &named_pipe_device_ops
)
741 set_error( STATUS_OBJECT_NAME_INVALID
);
744 namespace_add( dev
->pipes
, name
);
745 name
->parent
= grab_object( parent
);
749 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
750 unsigned int sharing
, unsigned int options
)
752 struct named_pipe
*pipe
= (struct named_pipe
*)obj
;
753 struct pipe_server
*server
;
754 struct pipe_client
*client
;
755 unsigned int pipe_sharing
;
758 if (!(server
= find_available_server( pipe
)))
760 set_error( STATUS_PIPE_NOT_AVAILABLE
);
764 pipe_sharing
= server
->pipe
->sharing
;
765 if (((access
& GENERIC_READ
) && !(pipe_sharing
& FILE_SHARE_READ
)) ||
766 ((access
& GENERIC_WRITE
) && !(pipe_sharing
& FILE_SHARE_WRITE
)))
768 set_error( STATUS_ACCESS_DENIED
);
769 release_object( server
);
773 if ((client
= create_pipe_client( options
, pipe
->flags
)))
775 if (!socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
))
777 assert( !server
->fd
);
779 /* for performance reasons, only set nonblocking mode when using
780 * overlapped I/O. Otherwise, we will be doing too much busy
782 if (is_overlapped( options
)) fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
783 if (is_overlapped( server
->options
)) fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
787 setsockopt( fds
[0], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
788 setsockopt( fds
[1], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
792 setsockopt( fds
[0], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
793 setsockopt( fds
[1], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
796 client
->fd
= create_anonymous_fd( &pipe_client_fd_ops
, fds
[1], &client
->obj
, options
);
797 server
->fd
= create_anonymous_fd( &pipe_server_fd_ops
, fds
[0], &server
->obj
, server
->options
);
798 if (client
->fd
&& server
->fd
)
800 allow_fd_caching( client
->fd
);
801 allow_fd_caching( server
->fd
);
802 fd_copy_completion( server
->ioctl_fd
, server
->fd
);
803 if (server
->state
== ps_wait_open
)
804 fd_async_wake_up( server
->ioctl_fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
805 set_server_state( server
, ps_connected_server
);
806 server
->client
= client
;
807 client
->server
= server
;
811 release_object( client
);
818 release_object( client
);
822 release_object( server
);
826 static obj_handle_t
named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
,
827 const async_data_t
*async_data
, int blocking
)
829 struct named_pipe_device
*device
= get_fd_user( fd
);
833 case FSCTL_PIPE_WAIT
:
835 const FILE_PIPE_WAIT_FOR_BUFFER
*buffer
= get_req_data();
836 data_size_t size
= get_req_data_size();
837 obj_handle_t wait_handle
= 0;
838 struct named_pipe
*pipe
;
839 struct pipe_server
*server
;
840 struct unicode_str name
;
842 if (size
< sizeof(*buffer
) ||
843 size
< FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[buffer
->NameLength
/sizeof(WCHAR
)]))
845 set_error( STATUS_INVALID_PARAMETER
);
848 name
.str
= buffer
->Name
;
849 name
.len
= (buffer
->NameLength
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
850 if (!(pipe
= open_named_object( &device
->obj
, &named_pipe_ops
, &name
, 0 ))) return 0;
852 if (!(server
= find_available_server( pipe
)))
856 if (!pipe
->waiters
&& !(pipe
->waiters
= create_async_queue( NULL
))) goto done
;
858 if ((async
= create_async( current
, pipe
->waiters
, async_data
, NULL
)))
860 timeout_t when
= buffer
->TimeoutSpecified
? buffer
->Timeout
.QuadPart
: pipe
->timeout
;
861 async_set_timeout( async
, when
, STATUS_IO_TIMEOUT
);
862 if (blocking
) wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
863 release_object( async
);
864 set_error( STATUS_PENDING
);
867 else release_object( server
);
870 release_object( pipe
);
875 return default_fd_ioctl( fd
, code
, async_data
, blocking
);
880 DECL_HANDLER(create_named_pipe
)
882 struct named_pipe
*pipe
;
883 struct pipe_server
*server
;
884 struct unicode_str name
;
886 const struct security_descriptor
*sd
;
887 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
889 if (!objattr
) return;
891 if (!req
->sharing
|| (req
->sharing
& ~(FILE_SHARE_READ
| FILE_SHARE_WRITE
)) ||
892 (!(req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
) && (req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
)))
894 if (root
) release_object( root
);
895 set_error( STATUS_INVALID_PARAMETER
);
899 if (!name
.len
) /* pipes need a root directory even without a name */
901 if (!objattr
->rootdir
)
903 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
906 if (!(root
= get_directory_obj( current
->process
, objattr
->rootdir
))) return;
909 pipe
= create_named_object( root
, &named_pipe_ops
, &name
, objattr
->attributes
| OBJ_OPENIF
, NULL
);
911 if (root
) release_object( root
);
914 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
916 /* initialize it if it didn't already exist */
918 pipe
->waiters
= NULL
;
919 list_init( &pipe
->servers
);
920 pipe
->insize
= req
->insize
;
921 pipe
->outsize
= req
->outsize
;
922 pipe
->maxinstances
= req
->maxinstances
;
923 pipe
->timeout
= req
->timeout
;
924 pipe
->flags
= req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
;
925 pipe
->sharing
= req
->sharing
;
929 if (pipe
->maxinstances
<= pipe
->instances
)
931 set_error( STATUS_INSTANCE_NOT_AVAILABLE
);
932 release_object( pipe
);
935 if (pipe
->sharing
!= req
->sharing
)
937 set_error( STATUS_ACCESS_DENIED
);
938 release_object( pipe
);
941 clear_error(); /* clear the name collision */
944 server
= create_pipe_server( pipe
, req
->options
, req
->flags
);
947 reply
->handle
= alloc_handle( current
->process
, server
, req
->access
, objattr
->attributes
);
948 server
->pipe
->instances
++;
949 if (sd
) default_set_sd( &server
->obj
, sd
, OWNER_SECURITY_INFORMATION
|
950 GROUP_SECURITY_INFORMATION
|
951 DACL_SECURITY_INFORMATION
|
952 SACL_SECURITY_INFORMATION
);
953 release_object( server
);
956 release_object( pipe
);
959 DECL_HANDLER(get_named_pipe_info
)
961 struct pipe_server
*server
;
962 struct pipe_client
*client
= NULL
;
964 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
);
967 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
971 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
972 0, &pipe_client_ops
);
974 server
= client
->server
;
977 reply
->flags
= client
? client
->pipe_flags
: server
->pipe_flags
;
980 reply
->sharing
= server
->pipe
->sharing
;
981 reply
->maxinstances
= server
->pipe
->maxinstances
;
982 reply
->instances
= server
->pipe
->instances
;
983 reply
->insize
= server
->pipe
->insize
;
984 reply
->outsize
= server
->pipe
->outsize
;
988 release_object(client
);
991 reply
->flags
|= NAMED_PIPE_SERVER_END
;
992 release_object(server
);
996 DECL_HANDLER(set_named_pipe_info
)
998 struct pipe_server
*server
;
999 struct pipe_client
*client
= NULL
;
1001 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
);
1004 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
1008 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
1009 0, &pipe_client_ops
);
1010 if (!client
) return;
1011 if (!(server
= client
->server
))
1013 release_object( client
);
1018 if ((req
->flags
& ~(NAMED_PIPE_MESSAGE_STREAM_READ
| NAMED_PIPE_NONBLOCKING_MODE
)) ||
1019 ((req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
) && !(server
->pipe
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
)))
1021 set_error( STATUS_INVALID_PARAMETER
);
1025 client
->pipe_flags
= server
->pipe
->flags
| req
->flags
;
1029 server
->pipe_flags
= server
->pipe
->flags
| req
->flags
;
1033 release_object(client
);
1035 release_object(server
);