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
, struct async
*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
, struct async
*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
, struct async
*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 struct async
*async
, 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
, struct async
*async
, int blocking
)
550 struct pipe_server
*server
= get_fd_user( fd
);
551 obj_handle_t handle
= 0;
553 if (!server
|| server
->state
!= ps_connected_server
) return 0;
555 if (!pipe_data_remaining( server
)) return 0;
557 if ((async
= fd_queue_async( server
->fd
, async_get_data( async
), NULL
, ASYNC_TYPE_WAIT
)))
559 /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
560 if (!server
->flush_poll
)
561 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
562 if (blocking
) handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
563 release_object( async
);
564 set_error( STATUS_PENDING
);
569 static obj_handle_t
pipe_client_flush( struct fd
*fd
, struct async
*async
, int blocking
)
571 /* FIXME: what do we have to do for this? */
575 static inline int is_overlapped( unsigned int options
)
577 return !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
580 static enum server_fd_type
pipe_server_get_fd_type( struct fd
*fd
)
585 static enum server_fd_type
pipe_client_get_fd_type( struct fd
*fd
)
590 static obj_handle_t
pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, struct async
*async
,
593 struct pipe_server
*server
= get_fd_user( fd
);
594 obj_handle_t wait_handle
= 0;
598 case FSCTL_PIPE_LISTEN
:
599 switch(server
->state
)
602 case ps_wait_connect
:
603 if ((async
= fd_queue_async( server
->ioctl_fd
, async_get_data( async
), NULL
, ASYNC_TYPE_WAIT
)))
605 if (blocking
) wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
606 set_server_state( server
, ps_wait_open
);
607 if (server
->pipe
->waiters
) async_wake_up( server
->pipe
->waiters
, STATUS_SUCCESS
);
608 release_object( async
);
609 set_error( STATUS_PENDING
);
613 case ps_connected_server
:
614 set_error( STATUS_PIPE_CONNECTED
);
616 case ps_wait_disconnect
:
617 set_error( STATUS_NO_DATA_DETECTED
);
620 set_error( STATUS_INVALID_HANDLE
);
625 case FSCTL_PIPE_DISCONNECT
:
626 switch(server
->state
)
628 case ps_connected_server
:
629 assert( server
->client
);
630 assert( server
->client
->fd
);
632 notify_empty( server
);
634 /* dump the client and server fds - client loses all waiting data */
635 do_disconnect( server
);
636 server
->client
->server
= NULL
;
637 server
->client
= NULL
;
638 set_server_state( server
, ps_wait_connect
);
640 case ps_wait_disconnect
:
641 assert( !server
->client
);
642 do_disconnect( server
);
643 set_server_state( server
, ps_wait_connect
);
647 set_error( STATUS_PIPE_LISTENING
);
649 case ps_wait_connect
:
650 set_error( STATUS_PIPE_DISCONNECTED
);
656 return default_fd_ioctl( fd
, code
, async
, blocking
);
660 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
661 obj_handle_t handle
, unsigned int access
)
664 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
665 return (struct pipe_server
*) obj
;
668 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
, unsigned int options
,
669 unsigned int pipe_flags
)
671 struct pipe_server
*server
;
673 server
= alloc_object( &pipe_server_ops
);
679 server
->client
= NULL
;
680 server
->flush_poll
= NULL
;
681 server
->options
= options
;
682 server
->pipe_flags
= pipe_flags
;
684 list_add_head( &pipe
->servers
, &server
->entry
);
686 if (!(server
->ioctl_fd
= alloc_pseudo_fd( &pipe_server_fd_ops
, &server
->obj
, options
)))
688 release_object( server
);
691 set_fd_signaled( server
->ioctl_fd
, 1 );
692 set_server_state( server
, ps_idle_server
);
696 static struct pipe_client
*create_pipe_client( unsigned int flags
, unsigned int pipe_flags
)
698 struct pipe_client
*client
;
700 client
= alloc_object( &pipe_client_ops
);
705 client
->server
= NULL
;
706 client
->flags
= flags
;
707 client
->pipe_flags
= pipe_flags
;
712 static struct pipe_server
*find_available_server( struct named_pipe
*pipe
)
714 struct pipe_server
*server
;
716 /* look for pipe servers that are listening */
717 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
719 if (server
->state
== ps_wait_open
)
720 return (struct pipe_server
*)grab_object( server
);
723 /* fall back to pipe servers that are idle */
724 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
726 if (server
->state
== ps_idle_server
)
727 return (struct pipe_server
*)grab_object( server
);
733 static int named_pipe_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
735 struct named_pipe_device
*dev
= (struct named_pipe_device
*)parent
;
737 if (parent
->ops
!= &named_pipe_device_ops
)
739 set_error( STATUS_OBJECT_NAME_INVALID
);
742 namespace_add( dev
->pipes
, name
);
743 name
->parent
= grab_object( parent
);
747 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
748 unsigned int sharing
, unsigned int options
)
750 struct named_pipe
*pipe
= (struct named_pipe
*)obj
;
751 struct pipe_server
*server
;
752 struct pipe_client
*client
;
753 unsigned int pipe_sharing
;
756 if (!(server
= find_available_server( pipe
)))
758 set_error( STATUS_PIPE_NOT_AVAILABLE
);
762 pipe_sharing
= server
->pipe
->sharing
;
763 if (((access
& GENERIC_READ
) && !(pipe_sharing
& FILE_SHARE_READ
)) ||
764 ((access
& GENERIC_WRITE
) && !(pipe_sharing
& FILE_SHARE_WRITE
)))
766 set_error( STATUS_ACCESS_DENIED
);
767 release_object( server
);
771 if ((client
= create_pipe_client( options
, pipe
->flags
)))
773 if (!socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
))
775 assert( !server
->fd
);
777 /* for performance reasons, only set nonblocking mode when using
778 * overlapped I/O. Otherwise, we will be doing too much busy
780 if (is_overlapped( options
)) fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
781 if (is_overlapped( server
->options
)) fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
785 setsockopt( fds
[0], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
786 setsockopt( fds
[1], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
790 setsockopt( fds
[0], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
791 setsockopt( fds
[1], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
794 client
->fd
= create_anonymous_fd( &pipe_client_fd_ops
, fds
[1], &client
->obj
, options
);
795 server
->fd
= create_anonymous_fd( &pipe_server_fd_ops
, fds
[0], &server
->obj
, server
->options
);
796 if (client
->fd
&& server
->fd
)
798 allow_fd_caching( client
->fd
);
799 allow_fd_caching( server
->fd
);
800 fd_copy_completion( server
->ioctl_fd
, server
->fd
);
801 if (server
->state
== ps_wait_open
)
802 fd_async_wake_up( server
->ioctl_fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
803 set_server_state( server
, ps_connected_server
);
804 server
->client
= client
;
805 client
->server
= server
;
809 release_object( client
);
816 release_object( client
);
820 release_object( server
);
824 static obj_handle_t
named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
,
825 struct async
*async
, int blocking
)
827 struct named_pipe_device
*device
= get_fd_user( fd
);
831 case FSCTL_PIPE_WAIT
:
833 const FILE_PIPE_WAIT_FOR_BUFFER
*buffer
= get_req_data();
834 data_size_t size
= get_req_data_size();
835 obj_handle_t wait_handle
= 0;
836 struct named_pipe
*pipe
;
837 struct pipe_server
*server
;
838 struct unicode_str name
;
841 if (size
< sizeof(*buffer
) ||
842 size
< FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[buffer
->NameLength
/sizeof(WCHAR
)]))
844 set_error( STATUS_INVALID_PARAMETER
);
847 name
.str
= buffer
->Name
;
848 name
.len
= (buffer
->NameLength
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
849 if (!(pipe
= open_named_object( &device
->obj
, &named_pipe_ops
, &name
, 0 ))) return 0;
851 if (!(server
= find_available_server( pipe
)))
853 if (!pipe
->waiters
&& !(pipe
->waiters
= create_async_queue( NULL
))) goto done
;
855 queue_async( pipe
->waiters
, async
);
856 when
= buffer
->TimeoutSpecified
? buffer
->Timeout
.QuadPart
: pipe
->timeout
;
857 async_set_timeout( async
, when
, STATUS_IO_TIMEOUT
);
858 if (blocking
) wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
859 set_error( STATUS_PENDING
);
861 else release_object( server
);
864 release_object( pipe
);
869 return default_fd_ioctl( fd
, code
, async
, blocking
);
874 DECL_HANDLER(create_named_pipe
)
876 struct named_pipe
*pipe
;
877 struct pipe_server
*server
;
878 struct unicode_str name
;
880 const struct security_descriptor
*sd
;
881 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
883 if (!objattr
) return;
885 if (!req
->sharing
|| (req
->sharing
& ~(FILE_SHARE_READ
| FILE_SHARE_WRITE
)) ||
886 (!(req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
) && (req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
)))
888 if (root
) release_object( root
);
889 set_error( STATUS_INVALID_PARAMETER
);
893 if (!name
.len
) /* pipes need a root directory even without a name */
895 if (!objattr
->rootdir
)
897 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
900 if (!(root
= get_directory_obj( current
->process
, objattr
->rootdir
))) return;
903 pipe
= create_named_object( root
, &named_pipe_ops
, &name
, objattr
->attributes
| OBJ_OPENIF
, NULL
);
905 if (root
) release_object( root
);
908 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
910 /* initialize it if it didn't already exist */
912 pipe
->waiters
= NULL
;
913 list_init( &pipe
->servers
);
914 pipe
->insize
= req
->insize
;
915 pipe
->outsize
= req
->outsize
;
916 pipe
->maxinstances
= req
->maxinstances
;
917 pipe
->timeout
= req
->timeout
;
918 pipe
->flags
= req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
;
919 pipe
->sharing
= req
->sharing
;
923 if (pipe
->maxinstances
<= pipe
->instances
)
925 set_error( STATUS_INSTANCE_NOT_AVAILABLE
);
926 release_object( pipe
);
929 if (pipe
->sharing
!= req
->sharing
)
931 set_error( STATUS_ACCESS_DENIED
);
932 release_object( pipe
);
935 clear_error(); /* clear the name collision */
938 server
= create_pipe_server( pipe
, req
->options
, req
->flags
);
941 reply
->handle
= alloc_handle( current
->process
, server
, req
->access
, objattr
->attributes
);
942 server
->pipe
->instances
++;
943 if (sd
) default_set_sd( &server
->obj
, sd
, OWNER_SECURITY_INFORMATION
|
944 GROUP_SECURITY_INFORMATION
|
945 DACL_SECURITY_INFORMATION
|
946 SACL_SECURITY_INFORMATION
);
947 release_object( server
);
950 release_object( pipe
);
953 DECL_HANDLER(get_named_pipe_info
)
955 struct pipe_server
*server
;
956 struct pipe_client
*client
= NULL
;
958 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
);
961 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
965 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
966 0, &pipe_client_ops
);
968 server
= client
->server
;
971 reply
->flags
= client
? client
->pipe_flags
: server
->pipe_flags
;
974 reply
->sharing
= server
->pipe
->sharing
;
975 reply
->maxinstances
= server
->pipe
->maxinstances
;
976 reply
->instances
= server
->pipe
->instances
;
977 reply
->insize
= server
->pipe
->insize
;
978 reply
->outsize
= server
->pipe
->outsize
;
982 release_object(client
);
985 reply
->flags
|= NAMED_PIPE_SERVER_END
;
986 release_object(server
);
990 DECL_HANDLER(set_named_pipe_info
)
992 struct pipe_server
*server
;
993 struct pipe_client
*client
= NULL
;
995 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
);
998 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
1002 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
1003 0, &pipe_client_ops
);
1004 if (!client
) return;
1005 if (!(server
= client
->server
))
1007 release_object( client
);
1012 if ((req
->flags
& ~(NAMED_PIPE_MESSAGE_STREAM_READ
| NAMED_PIPE_NONBLOCKING_MODE
)) ||
1013 ((req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
) && !(server
->pipe
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
)))
1015 set_error( STATUS_INVALID_PARAMETER
);
1019 client
->pipe_flags
= server
->pipe
->flags
| req
->flags
;
1023 server
->pipe_flags
= server
->pipe
->flags
| req
->flags
;
1027 release_object(client
);
1029 release_object(server
);