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
63 ps_disconnected_server
,
71 struct object obj
; /* object header */
72 struct fd
*fd
; /* pipe file descriptor */
73 struct fd
*ioctl_fd
; /* file descriptor for ioctls when not connected */
74 struct list entry
; /* entry in named pipe servers list */
75 enum pipe_state state
; /* server state */
76 struct pipe_client
*client
; /* client that this server is connected to */
77 struct named_pipe
*pipe
;
78 struct timeout_user
*flush_poll
;
79 unsigned int options
; /* pipe options */
80 unsigned int pipe_flags
;
85 struct object obj
; /* object header */
86 struct fd
*fd
; /* pipe file descriptor */
87 struct pipe_server
*server
; /* server that this client is connected to */
88 unsigned int flags
; /* file flags */
89 unsigned int pipe_flags
;
94 struct object obj
; /* object header */
97 unsigned int maxinstances
;
100 unsigned int instances
;
102 struct list servers
; /* list of servers using this pipe */
103 struct async_queue
*waiters
; /* list of clients waiting to connect */
106 struct named_pipe_device
108 struct object obj
; /* object header */
109 struct fd
*fd
; /* pseudo-fd for ioctls */
110 struct namespace *pipes
; /* named pipe namespace */
113 static void named_pipe_dump( struct object
*obj
, int verbose
);
114 static unsigned int named_pipe_map_access( struct object
*obj
, unsigned int access
);
115 static int named_pipe_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
);
116 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
117 unsigned int sharing
, unsigned int options
);
118 static void named_pipe_destroy( struct object
*obj
);
120 static const struct object_ops named_pipe_ops
=
122 sizeof(struct named_pipe
), /* size */
123 named_pipe_dump
, /* dump */
124 no_get_type
, /* get_type */
125 no_add_queue
, /* add_queue */
126 NULL
, /* remove_queue */
128 NULL
, /* satisfied */
129 no_signal
, /* signal */
130 no_get_fd
, /* get_fd */
131 named_pipe_map_access
, /* map_access */
132 default_get_sd
, /* get_sd */
133 default_set_sd
, /* set_sd */
134 no_lookup_name
, /* lookup_name */
135 named_pipe_link_name
, /* link_name */
136 default_unlink_name
, /* unlink_name */
137 named_pipe_open_file
, /* open_file */
138 no_close_handle
, /* close_handle */
139 named_pipe_destroy
/* destroy */
142 /* server end functions */
143 static void pipe_server_dump( struct object
*obj
, int verbose
);
144 static struct fd
*pipe_server_get_fd( struct object
*obj
);
145 static void pipe_server_destroy( struct object
*obj
);
146 static obj_handle_t
pipe_server_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
);
147 static enum server_fd_type
pipe_server_get_fd_type( struct fd
*fd
);
148 static obj_handle_t
pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async
,
151 static const struct object_ops pipe_server_ops
=
153 sizeof(struct pipe_server
), /* size */
154 pipe_server_dump
, /* dump */
155 no_get_type
, /* get_type */
156 add_queue
, /* add_queue */
157 remove_queue
, /* remove_queue */
158 default_fd_signaled
, /* signaled */
159 no_satisfied
, /* satisfied */
160 no_signal
, /* signal */
161 pipe_server_get_fd
, /* get_fd */
162 default_fd_map_access
, /* map_access */
163 default_get_sd
, /* get_sd */
164 default_set_sd
, /* set_sd */
165 no_lookup_name
, /* lookup_name */
166 no_link_name
, /* link_name */
167 NULL
, /* unlink_name */
168 no_open_file
, /* open_file */
169 fd_close_handle
, /* close_handle */
170 pipe_server_destroy
/* destroy */
173 static const struct fd_ops pipe_server_fd_ops
=
175 default_fd_get_poll_events
, /* get_poll_events */
176 default_poll_event
, /* poll_event */
177 pipe_server_get_fd_type
, /* get_fd_type */
178 no_fd_read
, /* read */
179 no_fd_write
, /* write */
180 pipe_server_flush
, /* flush */
181 pipe_server_ioctl
, /* ioctl */
182 default_fd_queue_async
, /* queue_async */
183 default_fd_reselect_async
, /* reselect_async */
184 default_fd_cancel_async
, /* cancel_async */
187 /* client end functions */
188 static void pipe_client_dump( struct object
*obj
, int verbose
);
189 static int pipe_client_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
190 static struct fd
*pipe_client_get_fd( struct object
*obj
);
191 static void pipe_client_destroy( struct object
*obj
);
192 static obj_handle_t
pipe_client_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
);
193 static enum server_fd_type
pipe_client_get_fd_type( struct fd
*fd
);
195 static const struct object_ops pipe_client_ops
=
197 sizeof(struct pipe_client
), /* size */
198 pipe_client_dump
, /* dump */
199 no_get_type
, /* get_type */
200 add_queue
, /* add_queue */
201 remove_queue
, /* remove_queue */
202 pipe_client_signaled
, /* signaled */
203 no_satisfied
, /* satisfied */
204 no_signal
, /* signal */
205 pipe_client_get_fd
, /* get_fd */
206 default_fd_map_access
, /* map_access */
207 default_get_sd
, /* get_sd */
208 default_set_sd
, /* set_sd */
209 no_lookup_name
, /* lookup_name */
210 no_link_name
, /* link_name */
211 NULL
, /* unlink_name */
212 no_open_file
, /* open_file */
213 fd_close_handle
, /* close_handle */
214 pipe_client_destroy
/* destroy */
217 static const struct fd_ops pipe_client_fd_ops
=
219 default_fd_get_poll_events
, /* get_poll_events */
220 default_poll_event
, /* poll_event */
221 pipe_client_get_fd_type
, /* get_fd_type */
222 no_fd_read
, /* read */
223 no_fd_write
, /* write */
224 pipe_client_flush
, /* flush */
225 default_fd_ioctl
, /* ioctl */
226 default_fd_queue_async
, /* queue_async */
227 default_fd_reselect_async
, /* reselect_async */
228 default_fd_cancel_async
/* cancel_async */
231 static void named_pipe_device_dump( struct object
*obj
, int verbose
);
232 static struct object_type
*named_pipe_device_get_type( struct object
*obj
);
233 static struct fd
*named_pipe_device_get_fd( struct object
*obj
);
234 static struct object
*named_pipe_device_lookup_name( struct object
*obj
,
235 struct unicode_str
*name
, unsigned int attr
);
236 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
237 unsigned int sharing
, unsigned int options
);
238 static void named_pipe_device_destroy( struct object
*obj
);
239 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
);
240 static obj_handle_t
named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
,
241 const async_data_t
*async_data
, int blocking
);
243 static const struct object_ops named_pipe_device_ops
=
245 sizeof(struct named_pipe_device
), /* size */
246 named_pipe_device_dump
, /* dump */
247 named_pipe_device_get_type
, /* get_type */
248 no_add_queue
, /* add_queue */
249 NULL
, /* remove_queue */
251 no_satisfied
, /* satisfied */
252 no_signal
, /* signal */
253 named_pipe_device_get_fd
, /* get_fd */
254 no_map_access
, /* map_access */
255 default_get_sd
, /* get_sd */
256 default_set_sd
, /* set_sd */
257 named_pipe_device_lookup_name
, /* lookup_name */
258 directory_link_name
, /* link_name */
259 default_unlink_name
, /* unlink_name */
260 named_pipe_device_open_file
, /* open_file */
261 fd_close_handle
, /* close_handle */
262 named_pipe_device_destroy
/* destroy */
265 static const struct fd_ops named_pipe_device_fd_ops
=
267 default_fd_get_poll_events
, /* get_poll_events */
268 default_poll_event
, /* poll_event */
269 named_pipe_device_get_fd_type
, /* get_fd_type */
270 no_fd_read
, /* read */
271 no_fd_write
, /* write */
272 no_fd_flush
, /* flush */
273 named_pipe_device_ioctl
, /* ioctl */
274 default_fd_queue_async
, /* queue_async */
275 default_fd_reselect_async
, /* reselect_async */
276 default_fd_cancel_async
/* cancel_async */
279 static void named_pipe_dump( struct object
*obj
, int verbose
)
281 fputs( "Named pipe\n", stderr
);
284 static unsigned int named_pipe_map_access( struct object
*obj
, unsigned int access
)
286 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
;
287 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| FILE_CREATE_PIPE_INSTANCE
;
288 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
289 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_ALL
;
290 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
293 static void pipe_server_dump( struct object
*obj
, int verbose
)
295 struct pipe_server
*server
= (struct pipe_server
*) obj
;
296 assert( obj
->ops
== &pipe_server_ops
);
297 fprintf( stderr
, "Named pipe server pipe=%p state=%d\n", server
->pipe
, server
->state
);
300 static void pipe_client_dump( struct object
*obj
, int verbose
)
302 struct pipe_client
*client
= (struct pipe_client
*) obj
;
303 assert( obj
->ops
== &pipe_client_ops
);
304 fprintf( stderr
, "Named pipe client server=%p\n", client
->server
);
307 static int pipe_client_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
309 struct pipe_client
*client
= (struct pipe_client
*) obj
;
311 return client
->fd
&& is_fd_signaled(client
->fd
);
314 static void named_pipe_destroy( struct object
*obj
)
316 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
318 assert( list_empty( &pipe
->servers
) );
319 assert( !pipe
->instances
);
320 free_async_queue( pipe
->waiters
);
323 static struct fd
*pipe_client_get_fd( struct object
*obj
)
325 struct pipe_client
*client
= (struct pipe_client
*) obj
;
327 return (struct fd
*) grab_object( client
->fd
);
328 set_error( STATUS_PIPE_DISCONNECTED
);
332 static void set_server_state( struct pipe_server
*server
, enum pipe_state state
)
334 server
->state
= state
;
338 case ps_connected_server
:
339 case ps_wait_disconnect
:
340 assert( server
->fd
);
344 assert( !server
->fd
);
345 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_LISTENING
);
347 case ps_disconnected_server
:
348 case ps_wait_connect
:
349 assert( !server
->fd
);
350 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_DISCONNECTED
);
355 static struct fd
*pipe_server_get_fd( struct object
*obj
)
357 struct pipe_server
*server
= (struct pipe_server
*) obj
;
359 return (struct fd
*)grab_object( server
->fd
? server
->fd
: server
->ioctl_fd
);
363 static void notify_empty( struct pipe_server
*server
)
365 if (!server
->flush_poll
)
367 assert( server
->state
== ps_connected_server
);
368 remove_timeout_user( server
->flush_poll
);
369 server
->flush_poll
= NULL
;
370 fd_async_wake_up( server
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
373 static void do_disconnect( struct pipe_server
*server
)
375 /* we may only have a server fd, if the client disconnected */
378 assert( server
->client
->server
== server
);
379 assert( server
->client
->fd
);
380 release_object( server
->client
->fd
);
381 server
->client
->fd
= NULL
;
383 assert( server
->fd
);
384 shutdown( get_unix_fd( server
->fd
), SHUT_RDWR
);
385 release_object( server
->fd
);
389 static void pipe_server_destroy( struct object
*obj
)
391 struct pipe_server
*server
= (struct pipe_server
*)obj
;
393 assert( obj
->ops
== &pipe_server_ops
);
397 notify_empty( server
);
398 do_disconnect( server
);
403 server
->client
->server
= NULL
;
404 server
->client
= NULL
;
407 assert( server
->pipe
->instances
);
408 server
->pipe
->instances
--;
410 if (server
->ioctl_fd
) release_object( server
->ioctl_fd
);
411 list_remove( &server
->entry
);
412 release_object( server
->pipe
);
415 static void pipe_client_destroy( struct object
*obj
)
417 struct pipe_client
*client
= (struct pipe_client
*)obj
;
418 struct pipe_server
*server
= client
->server
;
420 assert( obj
->ops
== &pipe_client_ops
);
424 notify_empty( server
);
426 switch(server
->state
)
428 case ps_connected_server
:
429 /* Don't destroy the server's fd here as we can't
430 do a successful flush without it. */
431 set_server_state( server
, ps_wait_disconnect
);
433 case ps_disconnected_server
:
434 set_server_state( server
, ps_wait_connect
);
438 case ps_wait_disconnect
:
439 case ps_wait_connect
:
442 assert( server
->client
);
443 server
->client
= NULL
;
444 client
->server
= NULL
;
446 if (client
->fd
) release_object( client
->fd
);
449 static void named_pipe_device_dump( struct object
*obj
, int verbose
)
451 fputs( "Named pipe device\n", stderr
);
454 static struct object_type
*named_pipe_device_get_type( struct object
*obj
)
456 static const WCHAR name
[] = {'D','e','v','i','c','e'};
457 static const struct unicode_str str
= { name
, sizeof(name
) };
458 return get_object_type( &str
);
461 static struct fd
*named_pipe_device_get_fd( struct object
*obj
)
463 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
464 return (struct fd
*)grab_object( device
->fd
);
467 static struct object
*named_pipe_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
470 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
471 struct object
*found
;
473 assert( obj
->ops
== &named_pipe_device_ops
);
474 assert( device
->pipes
);
476 if (!name
) return NULL
; /* open the device itself */
478 if ((found
= find_object( device
->pipes
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
484 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
485 unsigned int sharing
, unsigned int options
)
487 return grab_object( obj
);
490 static void named_pipe_device_destroy( struct object
*obj
)
492 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
493 assert( obj
->ops
== &named_pipe_device_ops
);
494 if (device
->fd
) release_object( device
->fd
);
495 free( device
->pipes
);
498 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
)
500 return FD_TYPE_DEVICE
;
503 struct object
*create_named_pipe_device( struct object
*root
, const struct unicode_str
*name
)
505 struct named_pipe_device
*dev
;
507 if ((dev
= create_named_object( root
, &named_pipe_device_ops
, name
, 0, NULL
)) &&
508 get_error() != STATUS_OBJECT_NAME_EXISTS
)
511 if (!(dev
->fd
= alloc_pseudo_fd( &named_pipe_device_fd_ops
, &dev
->obj
, 0 )) ||
512 !(dev
->pipes
= create_namespace( 7 )))
514 release_object( dev
);
521 static int pipe_data_remaining( struct pipe_server
*server
)
526 assert( server
->client
);
528 fd
= get_unix_fd( server
->client
->fd
);
535 if (0 > poll( &pfd
, 1, 0 ))
538 return pfd
.revents
&POLLIN
;
541 static void check_flushed( void *arg
)
543 struct pipe_server
*server
= (struct pipe_server
*) arg
;
545 if (pipe_data_remaining( server
))
547 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
551 server
->flush_poll
= NULL
;
552 fd_async_wake_up( server
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
556 static obj_handle_t
pipe_server_flush( struct fd
*fd
, const async_data_t
*async_data
, int blocking
)
558 struct pipe_server
*server
= get_fd_user( fd
);
559 obj_handle_t handle
= 0;
562 if (!server
|| server
->state
!= ps_connected_server
) return 0;
564 if (!pipe_data_remaining( server
)) return 0;
566 if ((async
= fd_queue_async( server
->fd
, async_data
, ASYNC_TYPE_WAIT
)))
568 /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
569 if (!server
->flush_poll
)
570 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
571 if (blocking
) handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
572 release_object( async
);
573 set_error( STATUS_PENDING
);
578 static obj_handle_t
pipe_client_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
)
580 /* FIXME: what do we have to do for this? */
584 static inline int is_overlapped( unsigned int options
)
586 return !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
589 static enum server_fd_type
pipe_server_get_fd_type( struct fd
*fd
)
594 static enum server_fd_type
pipe_client_get_fd_type( struct fd
*fd
)
599 static obj_handle_t
pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
602 struct pipe_server
*server
= get_fd_user( fd
);
604 obj_handle_t wait_handle
= 0;
608 case FSCTL_PIPE_LISTEN
:
609 switch(server
->state
)
612 case ps_wait_connect
:
613 if ((async
= fd_queue_async( server
->ioctl_fd
, async_data
, ASYNC_TYPE_WAIT
)))
615 if (blocking
) wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
616 set_server_state( server
, ps_wait_open
);
617 if (server
->pipe
->waiters
) async_wake_up( server
->pipe
->waiters
, STATUS_SUCCESS
);
618 release_object( async
);
619 set_error( STATUS_PENDING
);
623 case ps_connected_server
:
624 set_error( STATUS_PIPE_CONNECTED
);
626 case ps_disconnected_server
:
627 set_error( STATUS_PIPE_BUSY
);
629 case ps_wait_disconnect
:
630 set_error( STATUS_NO_DATA_DETECTED
);
633 set_error( STATUS_INVALID_HANDLE
);
638 case FSCTL_PIPE_DISCONNECT
:
639 switch(server
->state
)
641 case ps_connected_server
:
642 assert( server
->client
);
643 assert( server
->client
->fd
);
645 notify_empty( server
);
647 /* dump the client and server fds, but keep the pointers
648 around - client loses all waiting data */
649 do_disconnect( server
);
650 set_server_state( server
, ps_disconnected_server
);
652 case ps_wait_disconnect
:
653 assert( !server
->client
);
654 do_disconnect( server
);
655 set_server_state( server
, ps_wait_connect
);
659 set_error( STATUS_PIPE_LISTENING
);
661 case ps_disconnected_server
:
662 case ps_wait_connect
:
663 set_error( STATUS_PIPE_DISCONNECTED
);
669 return default_fd_ioctl( fd
, code
, async_data
, blocking
);
673 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
674 obj_handle_t handle
, unsigned int access
)
677 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
678 return (struct pipe_server
*) obj
;
681 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
, unsigned int options
,
682 unsigned int pipe_flags
)
684 struct pipe_server
*server
;
686 server
= alloc_object( &pipe_server_ops
);
692 server
->client
= NULL
;
693 server
->flush_poll
= NULL
;
694 server
->options
= options
;
695 server
->pipe_flags
= pipe_flags
;
697 list_add_head( &pipe
->servers
, &server
->entry
);
699 if (!(server
->ioctl_fd
= alloc_pseudo_fd( &pipe_server_fd_ops
, &server
->obj
, options
)))
701 release_object( server
);
704 set_server_state( server
, ps_idle_server
);
708 static struct pipe_client
*create_pipe_client( unsigned int flags
, unsigned int pipe_flags
)
710 struct pipe_client
*client
;
712 client
= alloc_object( &pipe_client_ops
);
717 client
->server
= NULL
;
718 client
->flags
= flags
;
719 client
->pipe_flags
= pipe_flags
;
724 static struct pipe_server
*find_available_server( struct named_pipe
*pipe
)
726 struct pipe_server
*server
;
728 /* look for pipe servers that are listening */
729 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
731 if (server
->state
== ps_wait_open
)
732 return (struct pipe_server
*)grab_object( server
);
735 /* fall back to pipe servers that are idle */
736 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
738 if (server
->state
== ps_idle_server
)
739 return (struct pipe_server
*)grab_object( server
);
745 static int named_pipe_link_name( struct object
*obj
, struct object_name
*name
, struct object
*parent
)
747 struct named_pipe_device
*dev
= (struct named_pipe_device
*)parent
;
749 if (parent
->ops
!= &named_pipe_device_ops
)
751 set_error( STATUS_OBJECT_NAME_INVALID
);
754 namespace_add( dev
->pipes
, name
);
755 name
->parent
= grab_object( parent
);
759 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
760 unsigned int sharing
, unsigned int options
)
762 struct named_pipe
*pipe
= (struct named_pipe
*)obj
;
763 struct pipe_server
*server
;
764 struct pipe_client
*client
;
765 unsigned int pipe_sharing
;
768 if (!(server
= find_available_server( pipe
)))
770 set_error( STATUS_PIPE_NOT_AVAILABLE
);
774 pipe_sharing
= server
->pipe
->sharing
;
775 if (((access
& GENERIC_READ
) && !(pipe_sharing
& FILE_SHARE_READ
)) ||
776 ((access
& GENERIC_WRITE
) && !(pipe_sharing
& FILE_SHARE_WRITE
)))
778 set_error( STATUS_ACCESS_DENIED
);
779 release_object( server
);
783 if ((client
= create_pipe_client( options
, pipe
->flags
)))
785 if (!socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
))
787 assert( !server
->fd
);
789 /* for performance reasons, only set nonblocking mode when using
790 * overlapped I/O. Otherwise, we will be doing too much busy
792 if (is_overlapped( options
)) fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
793 if (is_overlapped( server
->options
)) fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
797 setsockopt( fds
[0], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
798 setsockopt( fds
[1], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
802 setsockopt( fds
[0], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
803 setsockopt( fds
[1], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
806 client
->fd
= create_anonymous_fd( &pipe_client_fd_ops
, fds
[1], &client
->obj
, options
);
807 server
->fd
= create_anonymous_fd( &pipe_server_fd_ops
, fds
[0], &server
->obj
, server
->options
);
808 if (client
->fd
&& server
->fd
)
810 allow_fd_caching( client
->fd
);
811 allow_fd_caching( server
->fd
);
812 fd_copy_completion( server
->ioctl_fd
, server
->fd
);
813 if (server
->state
== ps_wait_open
)
814 fd_async_wake_up( server
->ioctl_fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
815 set_server_state( server
, ps_connected_server
);
816 server
->client
= client
;
817 client
->server
= server
;
821 release_object( client
);
828 release_object( client
);
832 release_object( server
);
836 static obj_handle_t
named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
,
837 const async_data_t
*async_data
, int blocking
)
839 struct named_pipe_device
*device
= get_fd_user( fd
);
843 case FSCTL_PIPE_WAIT
:
845 const FILE_PIPE_WAIT_FOR_BUFFER
*buffer
= get_req_data();
846 data_size_t size
= get_req_data_size();
847 obj_handle_t wait_handle
= 0;
848 struct named_pipe
*pipe
;
849 struct pipe_server
*server
;
850 struct unicode_str name
;
852 if (size
< sizeof(*buffer
) ||
853 size
< FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[buffer
->NameLength
/sizeof(WCHAR
)]))
855 set_error( STATUS_INVALID_PARAMETER
);
858 name
.str
= buffer
->Name
;
859 name
.len
= (buffer
->NameLength
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
860 if (!(pipe
= open_named_object( &device
->obj
, &named_pipe_ops
, &name
, 0 ))) return 0;
862 if (!(server
= find_available_server( pipe
)))
866 if (!pipe
->waiters
&& !(pipe
->waiters
= create_async_queue( NULL
))) goto done
;
868 if ((async
= create_async( current
, pipe
->waiters
, async_data
)))
870 timeout_t when
= buffer
->TimeoutSpecified
? buffer
->Timeout
.QuadPart
: pipe
->timeout
;
871 async_set_timeout( async
, when
, STATUS_IO_TIMEOUT
);
872 if (blocking
) wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
873 release_object( async
);
874 set_error( STATUS_PENDING
);
877 else release_object( server
);
880 release_object( pipe
);
885 return default_fd_ioctl( fd
, code
, async_data
, blocking
);
890 DECL_HANDLER(create_named_pipe
)
892 struct named_pipe
*pipe
;
893 struct pipe_server
*server
;
894 struct unicode_str name
;
896 const struct security_descriptor
*sd
;
897 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
, &root
);
899 if (!objattr
) return;
901 if (!req
->sharing
|| (req
->sharing
& ~(FILE_SHARE_READ
| FILE_SHARE_WRITE
)) ||
902 (!(req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
) && (req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
)))
904 if (root
) release_object( root
);
905 set_error( STATUS_INVALID_PARAMETER
);
909 if (!name
.len
) /* pipes need a root directory even without a name */
911 if (!objattr
->rootdir
)
913 set_error( STATUS_OBJECT_PATH_SYNTAX_BAD
);
916 if (!(root
= get_directory_obj( current
->process
, objattr
->rootdir
))) return;
919 pipe
= create_named_object( root
, &named_pipe_ops
, &name
, objattr
->attributes
| OBJ_OPENIF
, NULL
);
921 if (root
) release_object( root
);
924 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
926 /* initialize it if it didn't already exist */
928 pipe
->waiters
= NULL
;
929 list_init( &pipe
->servers
);
930 pipe
->insize
= req
->insize
;
931 pipe
->outsize
= req
->outsize
;
932 pipe
->maxinstances
= req
->maxinstances
;
933 pipe
->timeout
= req
->timeout
;
934 pipe
->flags
= req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
;
935 pipe
->sharing
= req
->sharing
;
939 if (pipe
->maxinstances
<= pipe
->instances
)
941 set_error( STATUS_INSTANCE_NOT_AVAILABLE
);
942 release_object( pipe
);
945 if (pipe
->sharing
!= req
->sharing
)
947 set_error( STATUS_ACCESS_DENIED
);
948 release_object( pipe
);
951 clear_error(); /* clear the name collision */
954 server
= create_pipe_server( pipe
, req
->options
, req
->flags
);
957 reply
->handle
= alloc_handle( current
->process
, server
, req
->access
, objattr
->attributes
);
958 server
->pipe
->instances
++;
959 if (sd
) default_set_sd( &server
->obj
, sd
, OWNER_SECURITY_INFORMATION
|
960 GROUP_SECURITY_INFORMATION
|
961 DACL_SECURITY_INFORMATION
|
962 SACL_SECURITY_INFORMATION
);
963 release_object( server
);
966 release_object( pipe
);
969 DECL_HANDLER(get_named_pipe_info
)
971 struct pipe_server
*server
;
972 struct pipe_client
*client
= NULL
;
974 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
);
977 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
981 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
982 0, &pipe_client_ops
);
984 server
= client
->server
;
987 reply
->flags
= client
? client
->pipe_flags
: server
->pipe_flags
;
990 reply
->sharing
= server
->pipe
->sharing
;
991 reply
->maxinstances
= server
->pipe
->maxinstances
;
992 reply
->instances
= server
->pipe
->instances
;
993 reply
->insize
= server
->pipe
->insize
;
994 reply
->outsize
= server
->pipe
->outsize
;
998 release_object(client
);
1001 reply
->flags
|= NAMED_PIPE_SERVER_END
;
1002 release_object(server
);
1006 DECL_HANDLER(set_named_pipe_info
)
1008 struct pipe_server
*server
;
1009 struct pipe_client
*client
= NULL
;
1011 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
);
1014 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
1018 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
1019 0, &pipe_client_ops
);
1020 if (!client
) return;
1021 if (!(server
= client
->server
))
1023 release_object( client
);
1028 if ((req
->flags
& ~(NAMED_PIPE_MESSAGE_STREAM_READ
| NAMED_PIPE_NONBLOCKING_MODE
)) ||
1029 ((req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
) && !(server
->pipe
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
)))
1031 set_error( STATUS_INVALID_PARAMETER
);
1035 client
->pipe_flags
= server
->pipe
->flags
| req
->flags
;
1039 server
->pipe_flags
= server
->pipe
->flags
| req
->flags
;
1043 release_object(client
);
1045 release_object(server
);