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 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_open_file
, /* open_file */
135 no_close_handle
, /* close_handle */
136 named_pipe_destroy
/* destroy */
139 /* server end functions */
140 static void pipe_server_dump( struct object
*obj
, int verbose
);
141 static struct fd
*pipe_server_get_fd( struct object
*obj
);
142 static void pipe_server_destroy( struct object
*obj
);
143 static obj_handle_t
pipe_server_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
);
144 static enum server_fd_type
pipe_server_get_fd_type( struct fd
*fd
);
145 static obj_handle_t
pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async
,
148 static const struct object_ops pipe_server_ops
=
150 sizeof(struct pipe_server
), /* size */
151 pipe_server_dump
, /* dump */
152 no_get_type
, /* get_type */
153 add_queue
, /* add_queue */
154 remove_queue
, /* remove_queue */
155 default_fd_signaled
, /* signaled */
156 no_satisfied
, /* satisfied */
157 no_signal
, /* signal */
158 pipe_server_get_fd
, /* get_fd */
159 default_fd_map_access
, /* map_access */
160 default_get_sd
, /* get_sd */
161 default_set_sd
, /* set_sd */
162 no_lookup_name
, /* lookup_name */
163 no_open_file
, /* open_file */
164 fd_close_handle
, /* close_handle */
165 pipe_server_destroy
/* destroy */
168 static const struct fd_ops pipe_server_fd_ops
=
170 default_fd_get_poll_events
, /* get_poll_events */
171 default_poll_event
, /* poll_event */
172 pipe_server_get_fd_type
, /* get_fd_type */
173 no_fd_read
, /* read */
174 no_fd_write
, /* write */
175 pipe_server_flush
, /* flush */
176 pipe_server_ioctl
, /* ioctl */
177 default_fd_queue_async
, /* queue_async */
178 default_fd_reselect_async
, /* reselect_async */
179 default_fd_cancel_async
, /* cancel_async */
182 /* client end functions */
183 static void pipe_client_dump( struct object
*obj
, int verbose
);
184 static int pipe_client_signaled( struct object
*obj
, struct wait_queue_entry
*entry
);
185 static struct fd
*pipe_client_get_fd( struct object
*obj
);
186 static void pipe_client_destroy( struct object
*obj
);
187 static obj_handle_t
pipe_client_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
);
188 static enum server_fd_type
pipe_client_get_fd_type( struct fd
*fd
);
190 static const struct object_ops pipe_client_ops
=
192 sizeof(struct pipe_client
), /* size */
193 pipe_client_dump
, /* dump */
194 no_get_type
, /* get_type */
195 add_queue
, /* add_queue */
196 remove_queue
, /* remove_queue */
197 pipe_client_signaled
, /* signaled */
198 no_satisfied
, /* satisfied */
199 no_signal
, /* signal */
200 pipe_client_get_fd
, /* get_fd */
201 default_fd_map_access
, /* map_access */
202 default_get_sd
, /* get_sd */
203 default_set_sd
, /* set_sd */
204 no_lookup_name
, /* lookup_name */
205 no_open_file
, /* open_file */
206 fd_close_handle
, /* close_handle */
207 pipe_client_destroy
/* destroy */
210 static const struct fd_ops pipe_client_fd_ops
=
212 default_fd_get_poll_events
, /* get_poll_events */
213 default_poll_event
, /* poll_event */
214 pipe_client_get_fd_type
, /* get_fd_type */
215 no_fd_read
, /* read */
216 no_fd_write
, /* write */
217 pipe_client_flush
, /* flush */
218 default_fd_ioctl
, /* ioctl */
219 default_fd_queue_async
, /* queue_async */
220 default_fd_reselect_async
, /* reselect_async */
221 default_fd_cancel_async
/* cancel_async */
224 static void named_pipe_device_dump( struct object
*obj
, int verbose
);
225 static struct object_type
*named_pipe_device_get_type( struct object
*obj
);
226 static struct fd
*named_pipe_device_get_fd( struct object
*obj
);
227 static struct object
*named_pipe_device_lookup_name( struct object
*obj
,
228 struct unicode_str
*name
, unsigned int attr
);
229 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
230 unsigned int sharing
, unsigned int options
);
231 static void named_pipe_device_destroy( struct object
*obj
);
232 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
);
233 static obj_handle_t
named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
,
234 const async_data_t
*async_data
, int blocking
);
236 static const struct object_ops named_pipe_device_ops
=
238 sizeof(struct named_pipe_device
), /* size */
239 named_pipe_device_dump
, /* dump */
240 named_pipe_device_get_type
, /* get_type */
241 no_add_queue
, /* add_queue */
242 NULL
, /* remove_queue */
244 no_satisfied
, /* satisfied */
245 no_signal
, /* signal */
246 named_pipe_device_get_fd
, /* get_fd */
247 no_map_access
, /* map_access */
248 default_get_sd
, /* get_sd */
249 default_set_sd
, /* set_sd */
250 named_pipe_device_lookup_name
, /* lookup_name */
251 named_pipe_device_open_file
, /* open_file */
252 fd_close_handle
, /* close_handle */
253 named_pipe_device_destroy
/* destroy */
256 static const struct fd_ops named_pipe_device_fd_ops
=
258 default_fd_get_poll_events
, /* get_poll_events */
259 default_poll_event
, /* poll_event */
260 named_pipe_device_get_fd_type
, /* get_fd_type */
261 no_fd_read
, /* read */
262 no_fd_write
, /* write */
263 no_fd_flush
, /* flush */
264 named_pipe_device_ioctl
, /* ioctl */
265 default_fd_queue_async
, /* queue_async */
266 default_fd_reselect_async
, /* reselect_async */
267 default_fd_cancel_async
/* cancel_async */
270 static void named_pipe_dump( struct object
*obj
, int verbose
)
272 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
273 assert( obj
->ops
== &named_pipe_ops
);
274 fprintf( stderr
, "Named pipe " );
275 dump_object_name( &pipe
->obj
);
276 fprintf( stderr
, "\n" );
279 static unsigned int named_pipe_map_access( struct object
*obj
, unsigned int access
)
281 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
;
282 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| FILE_CREATE_PIPE_INSTANCE
;
283 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
284 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_ALL
;
285 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
288 static void pipe_server_dump( struct object
*obj
, int verbose
)
290 struct pipe_server
*server
= (struct pipe_server
*) obj
;
291 assert( obj
->ops
== &pipe_server_ops
);
292 fprintf( stderr
, "Named pipe server pipe=%p state=%d\n", server
->pipe
, server
->state
);
295 static void pipe_client_dump( struct object
*obj
, int verbose
)
297 struct pipe_client
*client
= (struct pipe_client
*) obj
;
298 assert( obj
->ops
== &pipe_client_ops
);
299 fprintf( stderr
, "Named pipe client server=%p\n", client
->server
);
302 static int pipe_client_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
304 struct pipe_client
*client
= (struct pipe_client
*) obj
;
306 return client
->fd
&& is_fd_signaled(client
->fd
);
309 static void named_pipe_destroy( struct object
*obj
)
311 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
313 assert( list_empty( &pipe
->servers
) );
314 assert( !pipe
->instances
);
315 free_async_queue( pipe
->waiters
);
318 static struct fd
*pipe_client_get_fd( struct object
*obj
)
320 struct pipe_client
*client
= (struct pipe_client
*) obj
;
322 return (struct fd
*) grab_object( client
->fd
);
323 set_error( STATUS_PIPE_DISCONNECTED
);
327 static void set_server_state( struct pipe_server
*server
, enum pipe_state state
)
329 server
->state
= state
;
333 case ps_connected_server
:
334 case ps_wait_disconnect
:
335 assert( server
->fd
);
339 assert( !server
->fd
);
340 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_LISTENING
);
342 case ps_disconnected_server
:
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
);
428 case ps_disconnected_server
:
429 set_server_state( server
, ps_wait_connect
);
433 case ps_wait_disconnect
:
434 case ps_wait_connect
:
437 assert( server
->client
);
438 server
->client
= NULL
;
439 client
->server
= NULL
;
441 if (client
->fd
) release_object( client
->fd
);
444 static void named_pipe_device_dump( struct object
*obj
, int verbose
)
446 assert( obj
->ops
== &named_pipe_device_ops
);
447 fprintf( stderr
, "Named pipe device\n" );
450 static struct object_type
*named_pipe_device_get_type( struct object
*obj
)
452 static const WCHAR name
[] = {'D','e','v','i','c','e'};
453 static const struct unicode_str str
= { name
, sizeof(name
) };
454 return get_object_type( &str
);
457 static struct fd
*named_pipe_device_get_fd( struct object
*obj
)
459 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
460 return (struct fd
*)grab_object( device
->fd
);
463 static struct object
*named_pipe_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
466 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
467 struct object
*found
;
469 assert( obj
->ops
== &named_pipe_device_ops
);
470 assert( device
->pipes
);
472 if ((found
= find_object( device
->pipes
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
478 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
479 unsigned int sharing
, unsigned int options
)
481 return grab_object( obj
);
484 static void named_pipe_device_destroy( struct object
*obj
)
486 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
487 assert( obj
->ops
== &named_pipe_device_ops
);
488 if (device
->fd
) release_object( device
->fd
);
489 free( device
->pipes
);
492 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
)
494 return FD_TYPE_DEVICE
;
497 void create_named_pipe_device( struct directory
*root
, const struct unicode_str
*name
)
499 struct named_pipe_device
*dev
;
501 if ((dev
= create_named_object_dir( root
, name
, 0, &named_pipe_device_ops
)) &&
502 get_error() != STATUS_OBJECT_NAME_EXISTS
)
505 if (!(dev
->fd
= alloc_pseudo_fd( &named_pipe_device_fd_ops
, &dev
->obj
, 0 )) ||
506 !(dev
->pipes
= create_namespace( 7 )))
508 release_object( dev
);
512 if (dev
) make_object_static( &dev
->obj
);
515 static int pipe_data_remaining( struct pipe_server
*server
)
520 assert( server
->client
);
522 fd
= get_unix_fd( server
->client
->fd
);
529 if (0 > poll( &pfd
, 1, 0 ))
532 return pfd
.revents
&POLLIN
;
535 static void check_flushed( void *arg
)
537 struct pipe_server
*server
= (struct pipe_server
*) arg
;
539 if (pipe_data_remaining( server
))
541 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
545 server
->flush_poll
= NULL
;
546 fd_async_wake_up( server
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
550 static obj_handle_t
pipe_server_flush( struct fd
*fd
, const async_data_t
*async_data
, int blocking
)
552 struct pipe_server
*server
= get_fd_user( fd
);
553 obj_handle_t handle
= 0;
556 if (!server
|| server
->state
!= ps_connected_server
) return 0;
558 if (!pipe_data_remaining( server
)) return 0;
560 if ((async
= fd_queue_async( server
->fd
, async_data
, ASYNC_TYPE_WAIT
)))
562 /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
563 if (!server
->flush_poll
)
564 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
565 if (blocking
) handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
566 release_object( async
);
567 set_error( STATUS_PENDING
);
572 static obj_handle_t
pipe_client_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
)
574 /* FIXME: what do we have to do for this? */
578 static inline int is_overlapped( unsigned int options
)
580 return !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
583 static enum server_fd_type
pipe_server_get_fd_type( struct fd
*fd
)
588 static enum server_fd_type
pipe_client_get_fd_type( struct fd
*fd
)
593 static obj_handle_t
pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
596 struct pipe_server
*server
= get_fd_user( fd
);
598 obj_handle_t wait_handle
= 0;
602 case FSCTL_PIPE_LISTEN
:
603 switch(server
->state
)
606 case ps_wait_connect
:
607 if ((async
= fd_queue_async( server
->ioctl_fd
, async_data
, ASYNC_TYPE_WAIT
)))
609 if (blocking
) wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
610 set_server_state( server
, ps_wait_open
);
611 if (server
->pipe
->waiters
) async_wake_up( server
->pipe
->waiters
, STATUS_SUCCESS
);
612 release_object( async
);
613 set_error( STATUS_PENDING
);
617 case ps_connected_server
:
618 set_error( STATUS_PIPE_CONNECTED
);
620 case ps_disconnected_server
:
621 set_error( STATUS_PIPE_BUSY
);
623 case ps_wait_disconnect
:
624 set_error( STATUS_NO_DATA_DETECTED
);
627 set_error( STATUS_INVALID_HANDLE
);
632 case FSCTL_PIPE_DISCONNECT
:
633 switch(server
->state
)
635 case ps_connected_server
:
636 assert( server
->client
);
637 assert( server
->client
->fd
);
639 notify_empty( server
);
641 /* dump the client and server fds, but keep the pointers
642 around - client loses all waiting data */
643 do_disconnect( server
);
644 set_server_state( server
, ps_disconnected_server
);
646 case ps_wait_disconnect
:
647 assert( !server
->client
);
648 do_disconnect( server
);
649 set_server_state( server
, ps_wait_connect
);
653 set_error( STATUS_PIPE_LISTENING
);
655 case ps_disconnected_server
:
656 case ps_wait_connect
:
657 set_error( STATUS_PIPE_DISCONNECTED
);
663 return default_fd_ioctl( fd
, code
, async_data
, blocking
);
667 static struct named_pipe
*create_named_pipe( struct directory
*root
, const struct unicode_str
*name
,
668 unsigned int attr
, const struct security_descriptor
*sd
)
671 struct named_pipe
*pipe
= NULL
;
672 struct unicode_str new_name
;
674 if (!name
|| !name
->len
)
676 if ((pipe
= alloc_object( &named_pipe_ops
))) clear_error();
680 if (!(obj
= find_object_dir( root
, name
, attr
, &new_name
)))
682 set_error( STATUS_OBJECT_NAME_INVALID
);
687 if (attr
& OBJ_OPENIF
&& obj
->ops
== &named_pipe_ops
)
688 set_error( STATUS_OBJECT_NAME_EXISTS
);
691 release_object( obj
);
693 if (attr
& OBJ_OPENIF
)
694 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
696 set_error( STATUS_OBJECT_NAME_COLLISION
);
698 return (struct named_pipe
*)obj
;
701 if (obj
->ops
!= &named_pipe_device_ops
)
702 set_error( STATUS_OBJECT_NAME_INVALID
);
705 struct named_pipe_device
*dev
= (struct named_pipe_device
*)obj
;
706 if ((pipe
= create_object( dev
->pipes
, &named_pipe_ops
, &new_name
, NULL
)))
710 release_object( obj
);
714 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
715 obj_handle_t handle
, unsigned int access
)
718 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
719 return (struct pipe_server
*) obj
;
722 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
, unsigned int options
,
723 unsigned int pipe_flags
)
725 struct pipe_server
*server
;
727 server
= alloc_object( &pipe_server_ops
);
733 server
->client
= NULL
;
734 server
->flush_poll
= NULL
;
735 server
->options
= options
;
736 server
->pipe_flags
= pipe_flags
;
738 list_add_head( &pipe
->servers
, &server
->entry
);
740 if (!(server
->ioctl_fd
= alloc_pseudo_fd( &pipe_server_fd_ops
, &server
->obj
, options
)))
742 release_object( server
);
745 set_server_state( server
, ps_idle_server
);
749 static struct pipe_client
*create_pipe_client( unsigned int flags
, unsigned int pipe_flags
)
751 struct pipe_client
*client
;
753 client
= alloc_object( &pipe_client_ops
);
758 client
->server
= NULL
;
759 client
->flags
= flags
;
760 client
->pipe_flags
= pipe_flags
;
765 static struct pipe_server
*find_available_server( struct named_pipe
*pipe
)
767 struct pipe_server
*server
;
769 /* look for pipe servers that are listening */
770 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
772 if (server
->state
== ps_wait_open
)
773 return (struct pipe_server
*)grab_object( server
);
776 /* fall back to pipe servers that are idle */
777 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
779 if (server
->state
== ps_idle_server
)
780 return (struct pipe_server
*)grab_object( server
);
786 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
787 unsigned int sharing
, unsigned int options
)
789 struct named_pipe
*pipe
= (struct named_pipe
*)obj
;
790 struct pipe_server
*server
;
791 struct pipe_client
*client
;
792 unsigned int pipe_sharing
;
795 if (!(server
= find_available_server( pipe
)))
797 set_error( STATUS_PIPE_NOT_AVAILABLE
);
801 pipe_sharing
= server
->pipe
->sharing
;
802 if (((access
& GENERIC_READ
) && !(pipe_sharing
& FILE_SHARE_READ
)) ||
803 ((access
& GENERIC_WRITE
) && !(pipe_sharing
& FILE_SHARE_WRITE
)))
805 set_error( STATUS_ACCESS_DENIED
);
806 release_object( server
);
810 if ((client
= create_pipe_client( options
, pipe
->flags
)))
812 if (!socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
))
814 assert( !server
->fd
);
816 /* for performance reasons, only set nonblocking mode when using
817 * overlapped I/O. Otherwise, we will be doing too much busy
819 if (is_overlapped( options
)) fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
820 if (is_overlapped( server
->options
)) fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
824 setsockopt( fds
[0], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
825 setsockopt( fds
[1], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
829 setsockopt( fds
[0], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
830 setsockopt( fds
[1], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
833 client
->fd
= create_anonymous_fd( &pipe_client_fd_ops
, fds
[1], &client
->obj
, options
);
834 server
->fd
= create_anonymous_fd( &pipe_server_fd_ops
, fds
[0], &server
->obj
, server
->options
);
835 if (client
->fd
&& server
->fd
)
837 allow_fd_caching( client
->fd
);
838 allow_fd_caching( server
->fd
);
839 fd_copy_completion( server
->ioctl_fd
, server
->fd
);
840 if (server
->state
== ps_wait_open
)
841 fd_async_wake_up( server
->ioctl_fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
842 set_server_state( server
, ps_connected_server
);
843 server
->client
= client
;
844 client
->server
= server
;
848 release_object( client
);
855 release_object( client
);
859 release_object( server
);
863 static obj_handle_t
named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
,
864 const async_data_t
*async_data
, int blocking
)
866 struct named_pipe_device
*device
= get_fd_user( fd
);
870 case FSCTL_PIPE_WAIT
:
872 const FILE_PIPE_WAIT_FOR_BUFFER
*buffer
= get_req_data();
873 data_size_t size
= get_req_data_size();
874 obj_handle_t wait_handle
= 0;
875 struct named_pipe
*pipe
;
876 struct pipe_server
*server
;
877 struct unicode_str name
;
879 if (size
< sizeof(*buffer
) ||
880 size
< FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[buffer
->NameLength
/sizeof(WCHAR
)]))
882 set_error( STATUS_INVALID_PARAMETER
);
885 name
.str
= buffer
->Name
;
886 name
.len
= (buffer
->NameLength
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
887 if (!(pipe
= (struct named_pipe
*)find_object( device
->pipes
, &name
, OBJ_CASE_INSENSITIVE
)))
889 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
892 if (!(server
= find_available_server( pipe
)))
896 if (!pipe
->waiters
&& !(pipe
->waiters
= create_async_queue( NULL
))) goto done
;
898 if ((async
= create_async( current
, pipe
->waiters
, async_data
)))
900 timeout_t when
= buffer
->TimeoutSpecified
? buffer
->Timeout
.QuadPart
: pipe
->timeout
;
901 async_set_timeout( async
, when
, STATUS_IO_TIMEOUT
);
902 if (blocking
) wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
903 release_object( async
);
904 set_error( STATUS_PENDING
);
907 else release_object( server
);
910 release_object( pipe
);
915 return default_fd_ioctl( fd
, code
, async_data
, blocking
);
920 DECL_HANDLER(create_named_pipe
)
922 struct named_pipe
*pipe
;
923 struct pipe_server
*server
;
924 struct unicode_str name
;
925 struct directory
*root
= NULL
;
926 const struct object_attributes
*objattr
= get_req_data();
927 const struct security_descriptor
*sd
;
929 if (!req
->sharing
|| (req
->sharing
& ~(FILE_SHARE_READ
| FILE_SHARE_WRITE
)) ||
930 (!(req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
) && (req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
)))
932 set_error( STATUS_INVALID_PARAMETER
);
938 if (!objattr_is_valid( objattr
, get_req_data_size() ))
941 sd
= objattr
->sd_len
? (const struct security_descriptor
*)(objattr
+ 1) : NULL
;
942 objattr_get_name( objattr
, &name
);
944 if (objattr
->rootdir
&& !(root
= get_directory_obj( current
->process
, objattr
->rootdir
, 0 )))
947 pipe
= create_named_pipe( root
, &name
, req
->attributes
| OBJ_OPENIF
, sd
);
949 if (root
) release_object( root
);
952 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
954 /* initialize it if it didn't already exist */
956 pipe
->waiters
= NULL
;
957 list_init( &pipe
->servers
);
958 pipe
->insize
= req
->insize
;
959 pipe
->outsize
= req
->outsize
;
960 pipe
->maxinstances
= req
->maxinstances
;
961 pipe
->timeout
= req
->timeout
;
962 pipe
->flags
= req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
;
963 pipe
->sharing
= req
->sharing
;
967 if (pipe
->maxinstances
<= pipe
->instances
)
969 set_error( STATUS_INSTANCE_NOT_AVAILABLE
);
970 release_object( pipe
);
973 if (pipe
->sharing
!= req
->sharing
)
975 set_error( STATUS_ACCESS_DENIED
);
976 release_object( pipe
);
979 clear_error(); /* clear the name collision */
982 server
= create_pipe_server( pipe
, req
->options
, req
->flags
);
985 reply
->handle
= alloc_handle( current
->process
, server
, req
->access
, req
->attributes
);
986 server
->pipe
->instances
++;
987 if (sd
) default_set_sd( &server
->obj
, sd
, OWNER_SECURITY_INFORMATION
|
988 GROUP_SECURITY_INFORMATION
|
989 DACL_SECURITY_INFORMATION
|
990 SACL_SECURITY_INFORMATION
);
991 release_object( server
);
994 release_object( pipe
);
997 DECL_HANDLER(get_named_pipe_info
)
999 struct pipe_server
*server
;
1000 struct pipe_client
*client
= NULL
;
1002 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
);
1005 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
1009 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
1010 0, &pipe_client_ops
);
1011 if (!client
) return;
1012 server
= client
->server
;
1015 reply
->flags
= client
? client
->pipe_flags
: server
->pipe_flags
;
1016 reply
->sharing
= server
->pipe
->sharing
;
1017 reply
->maxinstances
= server
->pipe
->maxinstances
;
1018 reply
->instances
= server
->pipe
->instances
;
1019 reply
->insize
= server
->pipe
->insize
;
1020 reply
->outsize
= server
->pipe
->outsize
;
1023 release_object(client
);
1026 reply
->flags
|= NAMED_PIPE_SERVER_END
;
1027 release_object(server
);
1031 DECL_HANDLER(set_named_pipe_info
)
1033 struct pipe_server
*server
;
1034 struct pipe_client
*client
= NULL
;
1036 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
);
1039 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
1043 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
1044 0, &pipe_client_ops
);
1045 if (!client
) return;
1046 server
= client
->server
;
1049 if ((req
->flags
& ~(NAMED_PIPE_MESSAGE_STREAM_READ
| NAMED_PIPE_NONBLOCKING_MODE
)) ||
1050 ((req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
) && !(server
->pipe
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
)))
1052 set_error( STATUS_INVALID_PARAMETER
);
1056 client
->pipe_flags
= server
->pipe
->flags
| req
->flags
;
1060 server
->pipe_flags
= server
->pipe
->flags
| req
->flags
;
1064 release_object(client
);
1066 release_object(server
);