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
62 ps_disconnected_server
,
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
;
79 unsigned int options
; /* pipe options */
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 */
92 struct object obj
; /* object header */
94 unsigned int maxinstances
;
97 unsigned int instances
;
99 struct list servers
; /* list of servers using this pipe */
100 struct async_queue
*waiters
; /* list of clients waiting to connect */
103 struct named_pipe_device
105 struct object obj
; /* object header */
106 struct fd
*fd
; /* pseudo-fd for ioctls */
107 struct namespace *pipes
; /* named pipe namespace */
110 static void named_pipe_dump( struct object
*obj
, int verbose
);
111 static unsigned int named_pipe_map_access( struct object
*obj
, unsigned int access
);
112 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
113 unsigned int sharing
, unsigned int options
);
114 static void named_pipe_destroy( struct object
*obj
);
116 static const struct object_ops named_pipe_ops
=
118 sizeof(struct named_pipe
), /* size */
119 named_pipe_dump
, /* dump */
120 no_get_type
, /* get_type */
121 no_add_queue
, /* add_queue */
122 NULL
, /* remove_queue */
124 NULL
, /* satisfied */
125 no_signal
, /* signal */
126 no_get_fd
, /* get_fd */
127 named_pipe_map_access
, /* map_access */
128 default_get_sd
, /* get_sd */
129 default_set_sd
, /* set_sd */
130 no_lookup_name
, /* lookup_name */
131 named_pipe_open_file
, /* open_file */
132 no_close_handle
, /* close_handle */
133 named_pipe_destroy
/* destroy */
136 /* server end functions */
137 static void pipe_server_dump( struct object
*obj
, int verbose
);
138 static struct fd
*pipe_server_get_fd( struct object
*obj
);
139 static void pipe_server_destroy( struct object
*obj
);
140 static void pipe_server_flush( struct fd
*fd
, struct event
**event
);
141 static enum server_fd_type
pipe_server_get_fd_type( struct fd
*fd
);
142 static obj_handle_t
pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async
,
143 int blocking
, const void *data
, data_size_t size
);
145 static const struct object_ops pipe_server_ops
=
147 sizeof(struct pipe_server
), /* size */
148 pipe_server_dump
, /* dump */
149 no_get_type
, /* get_type */
150 add_queue
, /* add_queue */
151 remove_queue
, /* remove_queue */
152 default_fd_signaled
, /* signaled */
153 no_satisfied
, /* satisfied */
154 no_signal
, /* signal */
155 pipe_server_get_fd
, /* get_fd */
156 default_fd_map_access
, /* map_access */
157 default_get_sd
, /* get_sd */
158 default_set_sd
, /* set_sd */
159 no_lookup_name
, /* lookup_name */
160 no_open_file
, /* open_file */
161 fd_close_handle
, /* close_handle */
162 pipe_server_destroy
/* destroy */
165 static const struct fd_ops pipe_server_fd_ops
=
167 default_fd_get_poll_events
, /* get_poll_events */
168 default_poll_event
, /* poll_event */
169 pipe_server_flush
, /* flush */
170 pipe_server_get_fd_type
, /* get_fd_type */
171 pipe_server_ioctl
, /* ioctl */
172 default_fd_queue_async
, /* queue_async */
173 default_fd_reselect_async
, /* reselect_async */
174 default_fd_cancel_async
, /* cancel_async */
177 /* client end functions */
178 static void pipe_client_dump( struct object
*obj
, int verbose
);
179 static struct fd
*pipe_client_get_fd( struct object
*obj
);
180 static void pipe_client_destroy( struct object
*obj
);
181 static void pipe_client_flush( struct fd
*fd
, struct event
**event
);
182 static enum server_fd_type
pipe_client_get_fd_type( struct fd
*fd
);
184 static const struct object_ops pipe_client_ops
=
186 sizeof(struct pipe_client
), /* size */
187 pipe_client_dump
, /* dump */
188 no_get_type
, /* get_type */
189 add_queue
, /* add_queue */
190 remove_queue
, /* remove_queue */
191 default_fd_signaled
, /* signaled */
192 no_satisfied
, /* satisfied */
193 no_signal
, /* signal */
194 pipe_client_get_fd
, /* get_fd */
195 default_fd_map_access
, /* map_access */
196 default_get_sd
, /* get_sd */
197 default_set_sd
, /* set_sd */
198 no_lookup_name
, /* lookup_name */
199 no_open_file
, /* open_file */
200 fd_close_handle
, /* close_handle */
201 pipe_client_destroy
/* destroy */
204 static const struct fd_ops pipe_client_fd_ops
=
206 default_fd_get_poll_events
, /* get_poll_events */
207 default_poll_event
, /* poll_event */
208 pipe_client_flush
, /* flush */
209 pipe_client_get_fd_type
, /* get_fd_type */
210 default_fd_ioctl
, /* ioctl */
211 default_fd_queue_async
, /* queue_async */
212 default_fd_reselect_async
, /* reselect_async */
213 default_fd_cancel_async
/* cancel_async */
216 static void named_pipe_device_dump( struct object
*obj
, int verbose
);
217 static struct object_type
*named_pipe_device_get_type( struct object
*obj
);
218 static struct fd
*named_pipe_device_get_fd( struct object
*obj
);
219 static struct object
*named_pipe_device_lookup_name( struct object
*obj
,
220 struct unicode_str
*name
, unsigned int attr
);
221 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
222 unsigned int sharing
, unsigned int options
);
223 static void named_pipe_device_destroy( struct object
*obj
);
224 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
);
225 static obj_handle_t
named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
226 int blocking
, const void *data
, data_size_t size
);
228 static const struct object_ops named_pipe_device_ops
=
230 sizeof(struct named_pipe_device
), /* size */
231 named_pipe_device_dump
, /* dump */
232 named_pipe_device_get_type
, /* get_type */
233 no_add_queue
, /* add_queue */
234 NULL
, /* remove_queue */
236 no_satisfied
, /* satisfied */
237 no_signal
, /* signal */
238 named_pipe_device_get_fd
, /* get_fd */
239 no_map_access
, /* map_access */
240 default_get_sd
, /* get_sd */
241 default_set_sd
, /* set_sd */
242 named_pipe_device_lookup_name
, /* lookup_name */
243 named_pipe_device_open_file
, /* open_file */
244 fd_close_handle
, /* close_handle */
245 named_pipe_device_destroy
/* destroy */
248 static const struct fd_ops named_pipe_device_fd_ops
=
250 default_fd_get_poll_events
, /* get_poll_events */
251 default_poll_event
, /* poll_event */
252 no_flush
, /* flush */
253 named_pipe_device_get_fd_type
, /* get_fd_type */
254 named_pipe_device_ioctl
, /* ioctl */
255 default_fd_queue_async
, /* queue_async */
256 default_fd_reselect_async
, /* reselect_async */
257 default_fd_cancel_async
/* cancel_async */
260 static void named_pipe_dump( struct object
*obj
, int verbose
)
262 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
263 assert( obj
->ops
== &named_pipe_ops
);
264 fprintf( stderr
, "Named pipe " );
265 dump_object_name( &pipe
->obj
);
266 fprintf( stderr
, "\n" );
269 static unsigned int named_pipe_map_access( struct object
*obj
, unsigned int access
)
271 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
;
272 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| FILE_CREATE_PIPE_INSTANCE
;
273 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
274 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_ALL
;
275 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
278 static void pipe_server_dump( struct object
*obj
, int verbose
)
280 struct pipe_server
*server
= (struct pipe_server
*) obj
;
281 assert( obj
->ops
== &pipe_server_ops
);
282 fprintf( stderr
, "Named pipe server pipe=%p state=%d\n", server
->pipe
, server
->state
);
285 static void pipe_client_dump( struct object
*obj
, int verbose
)
287 struct pipe_client
*client
= (struct pipe_client
*) obj
;
288 assert( obj
->ops
== &pipe_client_ops
);
289 fprintf( stderr
, "Named pipe client server=%p\n", client
->server
);
292 static void named_pipe_destroy( struct object
*obj
)
294 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
296 assert( list_empty( &pipe
->servers
) );
297 assert( !pipe
->instances
);
298 free_async_queue( pipe
->waiters
);
301 static struct fd
*pipe_client_get_fd( struct object
*obj
)
303 struct pipe_client
*client
= (struct pipe_client
*) obj
;
305 return (struct fd
*) grab_object( client
->fd
);
306 set_error( STATUS_PIPE_DISCONNECTED
);
310 static void set_server_state( struct pipe_server
*server
, enum pipe_state state
)
312 server
->state
= state
;
316 case ps_connected_server
:
317 case ps_wait_disconnect
:
318 assert( server
->fd
);
322 assert( !server
->fd
);
323 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_LISTENING
);
325 case ps_disconnected_server
:
326 case ps_wait_connect
:
327 assert( !server
->fd
);
328 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_DISCONNECTED
);
333 static struct fd
*pipe_server_get_fd( struct object
*obj
)
335 struct pipe_server
*server
= (struct pipe_server
*) obj
;
337 return (struct fd
*)grab_object( server
->fd
? server
->fd
: server
->ioctl_fd
);
341 static void notify_empty( struct pipe_server
*server
)
343 if (!server
->flush_poll
)
345 assert( server
->state
== ps_connected_server
);
346 assert( server
->event
);
347 remove_timeout_user( server
->flush_poll
);
348 server
->flush_poll
= NULL
;
349 set_event( server
->event
);
350 release_object( server
->event
);
351 server
->event
= NULL
;
354 static void do_disconnect( struct pipe_server
*server
)
356 /* we may only have a server fd, if the client disconnected */
359 assert( server
->client
->server
== server
);
360 assert( server
->client
->fd
);
361 release_object( server
->client
->fd
);
362 server
->client
->fd
= NULL
;
364 assert( server
->fd
);
365 shutdown( get_unix_fd( server
->fd
), SHUT_RDWR
);
366 release_object( server
->fd
);
370 static void pipe_server_destroy( struct object
*obj
)
372 struct pipe_server
*server
= (struct pipe_server
*)obj
;
374 assert( obj
->ops
== &pipe_server_ops
);
378 notify_empty( server
);
379 do_disconnect( server
);
384 server
->client
->server
= NULL
;
385 server
->client
= NULL
;
388 assert( server
->pipe
->instances
);
389 server
->pipe
->instances
--;
391 if (server
->ioctl_fd
) release_object( server
->ioctl_fd
);
392 list_remove( &server
->entry
);
393 release_object( server
->pipe
);
396 static void pipe_client_destroy( struct object
*obj
)
398 struct pipe_client
*client
= (struct pipe_client
*)obj
;
399 struct pipe_server
*server
= client
->server
;
401 assert( obj
->ops
== &pipe_client_ops
);
405 notify_empty( server
);
407 switch(server
->state
)
409 case ps_connected_server
:
410 /* Don't destroy the server's fd here as we can't
411 do a successful flush without it. */
412 set_server_state( server
, ps_wait_disconnect
);
414 case ps_disconnected_server
:
415 set_server_state( server
, ps_wait_connect
);
419 case ps_wait_disconnect
:
420 case ps_wait_connect
:
423 assert( server
->client
);
424 server
->client
= NULL
;
425 client
->server
= NULL
;
427 if (client
->fd
) release_object( client
->fd
);
430 static void named_pipe_device_dump( struct object
*obj
, int verbose
)
432 assert( obj
->ops
== &named_pipe_device_ops
);
433 fprintf( stderr
, "Named pipe device\n" );
436 static struct object_type
*named_pipe_device_get_type( struct object
*obj
)
438 static const WCHAR name
[] = {'D','e','v','i','c','e'};
439 static const struct unicode_str str
= { name
, sizeof(name
) };
440 return get_object_type( &str
);
443 static struct fd
*named_pipe_device_get_fd( struct object
*obj
)
445 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
446 return (struct fd
*)grab_object( device
->fd
);
449 static struct object
*named_pipe_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
452 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
453 struct object
*found
;
455 assert( obj
->ops
== &named_pipe_device_ops
);
456 assert( device
->pipes
);
458 if ((found
= find_object( device
->pipes
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
464 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
465 unsigned int sharing
, unsigned int options
)
467 return grab_object( obj
);
470 static void named_pipe_device_destroy( struct object
*obj
)
472 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
473 assert( obj
->ops
== &named_pipe_device_ops
);
474 if (device
->fd
) release_object( device
->fd
);
475 free( device
->pipes
);
478 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
)
480 return FD_TYPE_DEVICE
;
483 void create_named_pipe_device( struct directory
*root
, const struct unicode_str
*name
)
485 struct named_pipe_device
*dev
;
487 if ((dev
= create_named_object_dir( root
, name
, 0, &named_pipe_device_ops
)) &&
488 get_error() != STATUS_OBJECT_NAME_EXISTS
)
491 if (!(dev
->fd
= alloc_pseudo_fd( &named_pipe_device_fd_ops
, &dev
->obj
, 0 )) ||
492 !(dev
->pipes
= create_namespace( 7 )))
494 release_object( dev
);
498 if (dev
) make_object_static( &dev
->obj
);
501 static int pipe_data_remaining( struct pipe_server
*server
)
506 assert( server
->client
);
508 fd
= get_unix_fd( server
->client
->fd
);
515 if (0 > poll( &pfd
, 1, 0 ))
518 return pfd
.revents
&POLLIN
;
521 static void check_flushed( void *arg
)
523 struct pipe_server
*server
= (struct pipe_server
*) arg
;
525 assert( server
->event
);
526 if (pipe_data_remaining( server
))
528 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
532 /* notify_empty( server ); */
533 server
->flush_poll
= NULL
;
534 set_event( server
->event
);
535 release_object( server
->event
);
536 server
->event
= NULL
;
540 static void pipe_server_flush( struct fd
*fd
, struct event
**event
)
542 struct pipe_server
*server
= get_fd_user( fd
);
544 if (!server
|| server
->state
!= ps_connected_server
) return;
546 /* FIXME: if multiple threads flush the same pipe,
547 maybe should create a list of processes to notify */
548 if (server
->flush_poll
) return;
550 if (pipe_data_remaining( server
))
552 /* this kind of sux -
553 there's no unix way to be alerted when a pipe becomes empty */
554 server
->event
= create_event( NULL
, NULL
, 0, 0, 0, NULL
);
555 if (!server
->event
) return;
556 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
557 *event
= server
->event
;
561 static void pipe_client_flush( struct fd
*fd
, struct event
**event
)
563 /* FIXME: what do we have to do for this? */
566 static inline int is_overlapped( unsigned int options
)
568 return !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
571 static enum server_fd_type
pipe_server_get_fd_type( struct fd
*fd
)
576 static enum server_fd_type
pipe_client_get_fd_type( struct fd
*fd
)
581 static obj_handle_t
alloc_wait_event( struct process
*process
)
583 obj_handle_t handle
= 0;
584 struct event
*event
= create_event( NULL
, NULL
, 0, 1, 0, NULL
);
588 handle
= alloc_handle( process
, event
, EVENT_ALL_ACCESS
, 0 );
589 release_object( event
);
594 static obj_handle_t
pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
595 int blocking
, const void *data
, data_size_t size
)
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 (blocking
&& !is_overlapped( get_fd_options(fd
) ))
610 async_data_t new_data
= *async_data
;
611 if (!(wait_handle
= alloc_wait_event( current
->process
))) break;
612 new_data
.event
= wait_handle
;
613 if (!(async
= fd_queue_async( server
->ioctl_fd
, &new_data
, ASYNC_TYPE_WAIT
)))
615 close_handle( current
->process
, wait_handle
);
619 else async
= fd_queue_async( server
->ioctl_fd
, async_data
, ASYNC_TYPE_WAIT
);
623 set_server_state( server
, ps_wait_open
);
624 if (server
->pipe
->waiters
) async_wake_up( server
->pipe
->waiters
, STATUS_SUCCESS
);
625 release_object( async
);
626 set_error( STATUS_PENDING
);
630 case ps_connected_server
:
631 set_error( STATUS_PIPE_CONNECTED
);
633 case ps_disconnected_server
:
634 set_error( STATUS_PIPE_BUSY
);
636 case ps_wait_disconnect
:
637 set_error( STATUS_NO_DATA_DETECTED
);
640 set_error( STATUS_INVALID_HANDLE
);
645 case FSCTL_PIPE_DISCONNECT
:
646 switch(server
->state
)
648 case ps_connected_server
:
649 assert( server
->client
);
650 assert( server
->client
->fd
);
652 notify_empty( server
);
654 /* dump the client and server fds, but keep the pointers
655 around - client loses all waiting data */
656 do_disconnect( server
);
657 set_server_state( server
, ps_disconnected_server
);
659 case ps_wait_disconnect
:
660 assert( !server
->client
);
661 do_disconnect( server
);
662 set_server_state( server
, ps_wait_connect
);
666 set_error( STATUS_PIPE_LISTENING
);
668 case ps_disconnected_server
:
669 case ps_wait_connect
:
670 set_error( STATUS_PIPE_DISCONNECTED
);
676 return default_fd_ioctl( fd
, code
, async_data
, blocking
, data
, size
);
680 static struct named_pipe
*create_named_pipe( struct directory
*root
, const struct unicode_str
*name
,
684 struct named_pipe
*pipe
= NULL
;
685 struct unicode_str new_name
;
687 if (!name
|| !name
->len
) return alloc_object( &named_pipe_ops
);
689 if (!(obj
= find_object_dir( root
, name
, attr
, &new_name
)))
691 set_error( STATUS_OBJECT_NAME_INVALID
);
696 if (attr
& OBJ_OPENIF
&& obj
->ops
== &named_pipe_ops
)
697 set_error( STATUS_OBJECT_NAME_EXISTS
);
700 release_object( obj
);
702 if (attr
& OBJ_OPENIF
)
703 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
705 set_error( STATUS_OBJECT_NAME_COLLISION
);
707 return (struct named_pipe
*)obj
;
710 if (obj
->ops
!= &named_pipe_device_ops
)
711 set_error( STATUS_OBJECT_NAME_INVALID
);
714 struct named_pipe_device
*dev
= (struct named_pipe_device
*)obj
;
715 if ((pipe
= create_object( dev
->pipes
, &named_pipe_ops
, &new_name
, NULL
)))
719 release_object( obj
);
723 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
724 obj_handle_t handle
, unsigned int access
)
727 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
728 return (struct pipe_server
*) obj
;
731 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
, unsigned int options
)
733 struct pipe_server
*server
;
735 server
= alloc_object( &pipe_server_ops
);
741 server
->client
= NULL
;
742 server
->flush_poll
= NULL
;
743 server
->options
= options
;
745 list_add_head( &pipe
->servers
, &server
->entry
);
747 if (!(server
->ioctl_fd
= alloc_pseudo_fd( &pipe_server_fd_ops
, &server
->obj
, options
)))
749 release_object( server
);
752 set_server_state( server
, ps_idle_server
);
756 static struct pipe_client
*create_pipe_client( unsigned int flags
)
758 struct pipe_client
*client
;
760 client
= alloc_object( &pipe_client_ops
);
765 client
->server
= NULL
;
766 client
->flags
= flags
;
771 static struct pipe_server
*find_available_server( struct named_pipe
*pipe
)
773 struct pipe_server
*server
;
775 /* look for pipe servers that are listening */
776 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
778 if (server
->state
== ps_wait_open
)
779 return (struct pipe_server
*)grab_object( server
);
782 /* fall back to pipe servers that are idle */
783 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
785 if (server
->state
== ps_idle_server
)
786 return (struct pipe_server
*)grab_object( server
);
792 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
793 unsigned int sharing
, unsigned int options
)
795 struct named_pipe
*pipe
= (struct named_pipe
*)obj
;
796 struct pipe_server
*server
;
797 struct pipe_client
*client
;
800 if (!(server
= find_available_server( pipe
)))
802 set_error( STATUS_PIPE_NOT_AVAILABLE
);
806 if ((client
= create_pipe_client( options
)))
808 if (!socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
))
810 assert( !server
->fd
);
812 /* for performance reasons, only set nonblocking mode when using
813 * overlapped I/O. Otherwise, we will be doing too much busy
815 if (is_overlapped( options
)) fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
816 if (is_overlapped( server
->options
)) fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
820 setsockopt( fds
[0], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
821 setsockopt( fds
[1], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
825 setsockopt( fds
[0], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
826 setsockopt( fds
[1], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
829 client
->fd
= create_anonymous_fd( &pipe_client_fd_ops
, fds
[1], &client
->obj
, options
);
830 server
->fd
= create_anonymous_fd( &pipe_server_fd_ops
, fds
[0], &server
->obj
, server
->options
);
831 if (client
->fd
&& server
->fd
)
833 fd_copy_completion( server
->ioctl_fd
, server
->fd
);
834 if (server
->state
== ps_wait_open
)
835 fd_async_wake_up( server
->ioctl_fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
836 set_server_state( server
, ps_connected_server
);
837 server
->client
= client
;
838 client
->server
= server
;
842 release_object( client
);
849 release_object( client
);
853 release_object( server
);
857 static obj_handle_t
named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
858 int blocking
, const void *data
, data_size_t size
)
860 struct named_pipe_device
*device
= get_fd_user( fd
);
864 case FSCTL_PIPE_WAIT
:
866 const FILE_PIPE_WAIT_FOR_BUFFER
*buffer
= data
;
867 obj_handle_t wait_handle
= 0;
868 struct named_pipe
*pipe
;
869 struct pipe_server
*server
;
870 struct unicode_str name
;
872 if (size
< sizeof(*buffer
) ||
873 size
< FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[buffer
->NameLength
/sizeof(WCHAR
)]))
875 set_error( STATUS_INVALID_PARAMETER
);
878 name
.str
= buffer
->Name
;
879 name
.len
= (buffer
->NameLength
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
880 if (!(pipe
= (struct named_pipe
*)find_object( device
->pipes
, &name
, OBJ_CASE_INSENSITIVE
)))
882 set_error( STATUS_PIPE_NOT_AVAILABLE
);
885 if (!(server
= find_available_server( pipe
)))
889 if (!pipe
->waiters
&& !(pipe
->waiters
= create_async_queue( NULL
))) goto done
;
893 async_data_t new_data
= *async_data
;
894 if (!(wait_handle
= alloc_wait_event( current
->process
))) goto done
;
895 new_data
.event
= wait_handle
;
896 if (!(async
= create_async( current
, pipe
->waiters
, &new_data
)))
898 close_handle( current
->process
, wait_handle
);
902 else async
= create_async( current
, pipe
->waiters
, async_data
);
906 timeout_t when
= buffer
->TimeoutSpecified
? buffer
->Timeout
.QuadPart
: pipe
->timeout
;
907 async_set_timeout( async
, when
, STATUS_IO_TIMEOUT
);
908 release_object( async
);
909 set_error( STATUS_PENDING
);
912 else release_object( server
);
915 release_object( pipe
);
920 return default_fd_ioctl( fd
, code
, async_data
, blocking
, data
, size
);
925 DECL_HANDLER(create_named_pipe
)
927 struct named_pipe
*pipe
;
928 struct pipe_server
*server
;
929 struct unicode_str name
;
930 struct directory
*root
= NULL
;
933 get_req_unicode_str( &name
);
934 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
937 pipe
= create_named_pipe( root
, &name
, req
->attributes
| OBJ_OPENIF
);
939 if (root
) release_object( root
);
942 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
944 /* initialize it if it didn't already exist */
946 pipe
->waiters
= NULL
;
947 list_init( &pipe
->servers
);
948 pipe
->insize
= req
->insize
;
949 pipe
->outsize
= req
->outsize
;
950 pipe
->maxinstances
= req
->maxinstances
;
951 pipe
->timeout
= req
->timeout
;
952 pipe
->flags
= req
->flags
;
956 if (pipe
->maxinstances
<= pipe
->instances
)
958 set_error( STATUS_INSTANCE_NOT_AVAILABLE
);
959 release_object( pipe
);
962 if ((pipe
->maxinstances
!= req
->maxinstances
) ||
963 (pipe
->timeout
!= req
->timeout
) ||
964 (pipe
->flags
!= req
->flags
))
966 set_error( STATUS_ACCESS_DENIED
);
967 release_object( pipe
);
970 clear_error(); /* clear the name collision */
973 server
= create_pipe_server( pipe
, req
->options
);
976 reply
->handle
= alloc_handle( current
->process
, server
, req
->access
, req
->attributes
);
977 server
->pipe
->instances
++;
978 release_object( server
);
981 release_object( pipe
);
984 DECL_HANDLER(get_named_pipe_info
)
986 struct pipe_server
*server
;
987 struct pipe_client
*client
= NULL
;
989 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
);
993 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
994 FILE_READ_ATTRIBUTES
, &pipe_client_ops
);
996 server
= client
->server
;
999 reply
->flags
= server
->pipe
->flags
;
1000 reply
->maxinstances
= server
->pipe
->maxinstances
;
1001 reply
->instances
= server
->pipe
->instances
;
1002 reply
->insize
= server
->pipe
->insize
;
1003 reply
->outsize
= server
->pipe
->outsize
;
1006 release_object(client
);
1009 reply
->flags
|= NAMED_PIPE_SERVER_END
;
1010 release_object(server
);