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 fputs( "Named pipe\n", stderr
);
275 static unsigned int named_pipe_map_access( struct object
*obj
, unsigned int access
)
277 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
;
278 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| FILE_CREATE_PIPE_INSTANCE
;
279 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
280 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_ALL
;
281 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
284 static void pipe_server_dump( struct object
*obj
, int verbose
)
286 struct pipe_server
*server
= (struct pipe_server
*) obj
;
287 assert( obj
->ops
== &pipe_server_ops
);
288 fprintf( stderr
, "Named pipe server pipe=%p state=%d\n", server
->pipe
, server
->state
);
291 static void pipe_client_dump( struct object
*obj
, int verbose
)
293 struct pipe_client
*client
= (struct pipe_client
*) obj
;
294 assert( obj
->ops
== &pipe_client_ops
);
295 fprintf( stderr
, "Named pipe client server=%p\n", client
->server
);
298 static int pipe_client_signaled( struct object
*obj
, struct wait_queue_entry
*entry
)
300 struct pipe_client
*client
= (struct pipe_client
*) obj
;
302 return client
->fd
&& is_fd_signaled(client
->fd
);
305 static void named_pipe_destroy( struct object
*obj
)
307 struct named_pipe
*pipe
= (struct named_pipe
*) obj
;
309 assert( list_empty( &pipe
->servers
) );
310 assert( !pipe
->instances
);
311 free_async_queue( pipe
->waiters
);
314 static struct fd
*pipe_client_get_fd( struct object
*obj
)
316 struct pipe_client
*client
= (struct pipe_client
*) obj
;
318 return (struct fd
*) grab_object( client
->fd
);
319 set_error( STATUS_PIPE_DISCONNECTED
);
323 static void set_server_state( struct pipe_server
*server
, enum pipe_state state
)
325 server
->state
= state
;
329 case ps_connected_server
:
330 case ps_wait_disconnect
:
331 assert( server
->fd
);
335 assert( !server
->fd
);
336 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_LISTENING
);
338 case ps_disconnected_server
:
339 case ps_wait_connect
:
340 assert( !server
->fd
);
341 set_no_fd_status( server
->ioctl_fd
, STATUS_PIPE_DISCONNECTED
);
346 static struct fd
*pipe_server_get_fd( struct object
*obj
)
348 struct pipe_server
*server
= (struct pipe_server
*) obj
;
350 return (struct fd
*)grab_object( server
->fd
? server
->fd
: server
->ioctl_fd
);
354 static void notify_empty( struct pipe_server
*server
)
356 if (!server
->flush_poll
)
358 assert( server
->state
== ps_connected_server
);
359 remove_timeout_user( server
->flush_poll
);
360 server
->flush_poll
= NULL
;
361 fd_async_wake_up( server
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
364 static void do_disconnect( struct pipe_server
*server
)
366 /* we may only have a server fd, if the client disconnected */
369 assert( server
->client
->server
== server
);
370 assert( server
->client
->fd
);
371 release_object( server
->client
->fd
);
372 server
->client
->fd
= NULL
;
374 assert( server
->fd
);
375 shutdown( get_unix_fd( server
->fd
), SHUT_RDWR
);
376 release_object( server
->fd
);
380 static void pipe_server_destroy( struct object
*obj
)
382 struct pipe_server
*server
= (struct pipe_server
*)obj
;
384 assert( obj
->ops
== &pipe_server_ops
);
388 notify_empty( server
);
389 do_disconnect( server
);
394 server
->client
->server
= NULL
;
395 server
->client
= NULL
;
398 assert( server
->pipe
->instances
);
399 server
->pipe
->instances
--;
401 if (server
->ioctl_fd
) release_object( server
->ioctl_fd
);
402 list_remove( &server
->entry
);
403 release_object( server
->pipe
);
406 static void pipe_client_destroy( struct object
*obj
)
408 struct pipe_client
*client
= (struct pipe_client
*)obj
;
409 struct pipe_server
*server
= client
->server
;
411 assert( obj
->ops
== &pipe_client_ops
);
415 notify_empty( server
);
417 switch(server
->state
)
419 case ps_connected_server
:
420 /* Don't destroy the server's fd here as we can't
421 do a successful flush without it. */
422 set_server_state( server
, ps_wait_disconnect
);
424 case ps_disconnected_server
:
425 set_server_state( server
, ps_wait_connect
);
429 case ps_wait_disconnect
:
430 case ps_wait_connect
:
433 assert( server
->client
);
434 server
->client
= NULL
;
435 client
->server
= NULL
;
437 if (client
->fd
) release_object( client
->fd
);
440 static void named_pipe_device_dump( struct object
*obj
, int verbose
)
442 fputs( "Named pipe device\n", stderr
);
445 static struct object_type
*named_pipe_device_get_type( struct object
*obj
)
447 static const WCHAR name
[] = {'D','e','v','i','c','e'};
448 static const struct unicode_str str
= { name
, sizeof(name
) };
449 return get_object_type( &str
);
452 static struct fd
*named_pipe_device_get_fd( struct object
*obj
)
454 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
455 return (struct fd
*)grab_object( device
->fd
);
458 static struct object
*named_pipe_device_lookup_name( struct object
*obj
, struct unicode_str
*name
,
461 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
462 struct object
*found
;
464 assert( obj
->ops
== &named_pipe_device_ops
);
465 assert( device
->pipes
);
467 if ((found
= find_object( device
->pipes
, name
, attr
| OBJ_CASE_INSENSITIVE
)))
473 static struct object
*named_pipe_device_open_file( struct object
*obj
, unsigned int access
,
474 unsigned int sharing
, unsigned int options
)
476 return grab_object( obj
);
479 static void named_pipe_device_destroy( struct object
*obj
)
481 struct named_pipe_device
*device
= (struct named_pipe_device
*)obj
;
482 assert( obj
->ops
== &named_pipe_device_ops
);
483 if (device
->fd
) release_object( device
->fd
);
484 free( device
->pipes
);
487 static enum server_fd_type
named_pipe_device_get_fd_type( struct fd
*fd
)
489 return FD_TYPE_DEVICE
;
492 void create_named_pipe_device( struct directory
*root
, const struct unicode_str
*name
)
494 struct named_pipe_device
*dev
;
496 if ((dev
= create_named_object_dir( root
, name
, 0, &named_pipe_device_ops
)) &&
497 get_error() != STATUS_OBJECT_NAME_EXISTS
)
500 if (!(dev
->fd
= alloc_pseudo_fd( &named_pipe_device_fd_ops
, &dev
->obj
, 0 )) ||
501 !(dev
->pipes
= create_namespace( 7 )))
503 release_object( dev
);
507 if (dev
) make_object_static( &dev
->obj
);
510 static int pipe_data_remaining( struct pipe_server
*server
)
515 assert( server
->client
);
517 fd
= get_unix_fd( server
->client
->fd
);
524 if (0 > poll( &pfd
, 1, 0 ))
527 return pfd
.revents
&POLLIN
;
530 static void check_flushed( void *arg
)
532 struct pipe_server
*server
= (struct pipe_server
*) arg
;
534 if (pipe_data_remaining( server
))
536 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
540 server
->flush_poll
= NULL
;
541 fd_async_wake_up( server
->fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
545 static obj_handle_t
pipe_server_flush( struct fd
*fd
, const async_data_t
*async_data
, int blocking
)
547 struct pipe_server
*server
= get_fd_user( fd
);
548 obj_handle_t handle
= 0;
551 if (!server
|| server
->state
!= ps_connected_server
) return 0;
553 if (!pipe_data_remaining( server
)) return 0;
555 if ((async
= fd_queue_async( server
->fd
, async_data
, ASYNC_TYPE_WAIT
)))
557 /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
558 if (!server
->flush_poll
)
559 server
->flush_poll
= add_timeout_user( -TICKS_PER_SEC
/ 10, check_flushed
, server
);
560 if (blocking
) handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
561 release_object( async
);
562 set_error( STATUS_PENDING
);
567 static obj_handle_t
pipe_client_flush( struct fd
*fd
, const async_data_t
*async
, int blocking
)
569 /* FIXME: what do we have to do for this? */
573 static inline int is_overlapped( unsigned int options
)
575 return !(options
& (FILE_SYNCHRONOUS_IO_ALERT
| FILE_SYNCHRONOUS_IO_NONALERT
));
578 static enum server_fd_type
pipe_server_get_fd_type( struct fd
*fd
)
583 static enum server_fd_type
pipe_client_get_fd_type( struct fd
*fd
)
588 static obj_handle_t
pipe_server_ioctl( struct fd
*fd
, ioctl_code_t code
, const async_data_t
*async_data
,
591 struct pipe_server
*server
= get_fd_user( fd
);
593 obj_handle_t wait_handle
= 0;
597 case FSCTL_PIPE_LISTEN
:
598 switch(server
->state
)
601 case ps_wait_connect
:
602 if ((async
= fd_queue_async( server
->ioctl_fd
, async_data
, ASYNC_TYPE_WAIT
)))
604 if (blocking
) wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
605 set_server_state( server
, ps_wait_open
);
606 if (server
->pipe
->waiters
) async_wake_up( server
->pipe
->waiters
, STATUS_SUCCESS
);
607 release_object( async
);
608 set_error( STATUS_PENDING
);
612 case ps_connected_server
:
613 set_error( STATUS_PIPE_CONNECTED
);
615 case ps_disconnected_server
:
616 set_error( STATUS_PIPE_BUSY
);
618 case ps_wait_disconnect
:
619 set_error( STATUS_NO_DATA_DETECTED
);
622 set_error( STATUS_INVALID_HANDLE
);
627 case FSCTL_PIPE_DISCONNECT
:
628 switch(server
->state
)
630 case ps_connected_server
:
631 assert( server
->client
);
632 assert( server
->client
->fd
);
634 notify_empty( server
);
636 /* dump the client and server fds, but keep the pointers
637 around - client loses all waiting data */
638 do_disconnect( server
);
639 set_server_state( server
, ps_disconnected_server
);
641 case ps_wait_disconnect
:
642 assert( !server
->client
);
643 do_disconnect( server
);
644 set_server_state( server
, ps_wait_connect
);
648 set_error( STATUS_PIPE_LISTENING
);
650 case ps_disconnected_server
:
651 case ps_wait_connect
:
652 set_error( STATUS_PIPE_DISCONNECTED
);
658 return default_fd_ioctl( fd
, code
, async_data
, blocking
);
662 static struct named_pipe
*create_named_pipe( struct directory
*root
, const struct unicode_str
*name
,
663 unsigned int attr
, const struct security_descriptor
*sd
)
666 struct named_pipe
*pipe
= NULL
;
667 struct unicode_str new_name
;
669 if (!name
|| !name
->len
)
671 if ((pipe
= alloc_object( &named_pipe_ops
))) clear_error();
675 if (!(obj
= find_object_dir( root
, name
, attr
, &new_name
)))
677 set_error( STATUS_OBJECT_NAME_INVALID
);
682 if (attr
& OBJ_OPENIF
&& obj
->ops
== &named_pipe_ops
)
683 set_error( STATUS_OBJECT_NAME_EXISTS
);
686 release_object( obj
);
688 if (attr
& OBJ_OPENIF
)
689 set_error( STATUS_OBJECT_TYPE_MISMATCH
);
691 set_error( STATUS_OBJECT_NAME_COLLISION
);
693 return (struct named_pipe
*)obj
;
696 if (obj
->ops
!= &named_pipe_device_ops
)
697 set_error( STATUS_OBJECT_NAME_INVALID
);
700 struct named_pipe_device
*dev
= (struct named_pipe_device
*)obj
;
701 if ((pipe
= create_object( dev
->pipes
, &named_pipe_ops
, &new_name
, NULL
)))
705 release_object( obj
);
709 static struct pipe_server
*get_pipe_server_obj( struct process
*process
,
710 obj_handle_t handle
, unsigned int access
)
713 obj
= get_handle_obj( process
, handle
, access
, &pipe_server_ops
);
714 return (struct pipe_server
*) obj
;
717 static struct pipe_server
*create_pipe_server( struct named_pipe
*pipe
, unsigned int options
,
718 unsigned int pipe_flags
)
720 struct pipe_server
*server
;
722 server
= alloc_object( &pipe_server_ops
);
728 server
->client
= NULL
;
729 server
->flush_poll
= NULL
;
730 server
->options
= options
;
731 server
->pipe_flags
= pipe_flags
;
733 list_add_head( &pipe
->servers
, &server
->entry
);
735 if (!(server
->ioctl_fd
= alloc_pseudo_fd( &pipe_server_fd_ops
, &server
->obj
, options
)))
737 release_object( server
);
740 set_server_state( server
, ps_idle_server
);
744 static struct pipe_client
*create_pipe_client( unsigned int flags
, unsigned int pipe_flags
)
746 struct pipe_client
*client
;
748 client
= alloc_object( &pipe_client_ops
);
753 client
->server
= NULL
;
754 client
->flags
= flags
;
755 client
->pipe_flags
= pipe_flags
;
760 static struct pipe_server
*find_available_server( struct named_pipe
*pipe
)
762 struct pipe_server
*server
;
764 /* look for pipe servers that are listening */
765 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
767 if (server
->state
== ps_wait_open
)
768 return (struct pipe_server
*)grab_object( server
);
771 /* fall back to pipe servers that are idle */
772 LIST_FOR_EACH_ENTRY( server
, &pipe
->servers
, struct pipe_server
, entry
)
774 if (server
->state
== ps_idle_server
)
775 return (struct pipe_server
*)grab_object( server
);
781 static struct object
*named_pipe_open_file( struct object
*obj
, unsigned int access
,
782 unsigned int sharing
, unsigned int options
)
784 struct named_pipe
*pipe
= (struct named_pipe
*)obj
;
785 struct pipe_server
*server
;
786 struct pipe_client
*client
;
787 unsigned int pipe_sharing
;
790 if (!(server
= find_available_server( pipe
)))
792 set_error( STATUS_PIPE_NOT_AVAILABLE
);
796 pipe_sharing
= server
->pipe
->sharing
;
797 if (((access
& GENERIC_READ
) && !(pipe_sharing
& FILE_SHARE_READ
)) ||
798 ((access
& GENERIC_WRITE
) && !(pipe_sharing
& FILE_SHARE_WRITE
)))
800 set_error( STATUS_ACCESS_DENIED
);
801 release_object( server
);
805 if ((client
= create_pipe_client( options
, pipe
->flags
)))
807 if (!socketpair( PF_UNIX
, SOCK_STREAM
, 0, fds
))
809 assert( !server
->fd
);
811 /* for performance reasons, only set nonblocking mode when using
812 * overlapped I/O. Otherwise, we will be doing too much busy
814 if (is_overlapped( options
)) fcntl( fds
[1], F_SETFL
, O_NONBLOCK
);
815 if (is_overlapped( server
->options
)) fcntl( fds
[0], F_SETFL
, O_NONBLOCK
);
819 setsockopt( fds
[0], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
820 setsockopt( fds
[1], SOL_SOCKET
, SO_RCVBUF
, &pipe
->insize
, sizeof(pipe
->insize
) );
824 setsockopt( fds
[0], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
825 setsockopt( fds
[1], SOL_SOCKET
, SO_SNDBUF
, &pipe
->outsize
, sizeof(pipe
->outsize
) );
828 client
->fd
= create_anonymous_fd( &pipe_client_fd_ops
, fds
[1], &client
->obj
, options
);
829 server
->fd
= create_anonymous_fd( &pipe_server_fd_ops
, fds
[0], &server
->obj
, server
->options
);
830 if (client
->fd
&& server
->fd
)
832 allow_fd_caching( client
->fd
);
833 allow_fd_caching( server
->fd
);
834 fd_copy_completion( server
->ioctl_fd
, server
->fd
);
835 if (server
->state
== ps_wait_open
)
836 fd_async_wake_up( server
->ioctl_fd
, ASYNC_TYPE_WAIT
, STATUS_SUCCESS
);
837 set_server_state( server
, ps_connected_server
);
838 server
->client
= client
;
839 client
->server
= server
;
843 release_object( client
);
850 release_object( client
);
854 release_object( server
);
858 static obj_handle_t
named_pipe_device_ioctl( struct fd
*fd
, ioctl_code_t code
,
859 const async_data_t
*async_data
, int blocking
)
861 struct named_pipe_device
*device
= get_fd_user( fd
);
865 case FSCTL_PIPE_WAIT
:
867 const FILE_PIPE_WAIT_FOR_BUFFER
*buffer
= get_req_data();
868 data_size_t size
= get_req_data_size();
869 obj_handle_t wait_handle
= 0;
870 struct named_pipe
*pipe
;
871 struct pipe_server
*server
;
872 struct unicode_str name
;
874 if (size
< sizeof(*buffer
) ||
875 size
< FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER
, Name
[buffer
->NameLength
/sizeof(WCHAR
)]))
877 set_error( STATUS_INVALID_PARAMETER
);
880 name
.str
= buffer
->Name
;
881 name
.len
= (buffer
->NameLength
/ sizeof(WCHAR
)) * sizeof(WCHAR
);
882 if (!(pipe
= (struct named_pipe
*)find_object( device
->pipes
, &name
, OBJ_CASE_INSENSITIVE
)))
884 set_error( STATUS_OBJECT_NAME_NOT_FOUND
);
887 if (!(server
= find_available_server( pipe
)))
891 if (!pipe
->waiters
&& !(pipe
->waiters
= create_async_queue( NULL
))) goto done
;
893 if ((async
= create_async( current
, pipe
->waiters
, async_data
)))
895 timeout_t when
= buffer
->TimeoutSpecified
? buffer
->Timeout
.QuadPart
: pipe
->timeout
;
896 async_set_timeout( async
, when
, STATUS_IO_TIMEOUT
);
897 if (blocking
) wait_handle
= alloc_handle( current
->process
, async
, SYNCHRONIZE
, 0 );
898 release_object( async
);
899 set_error( STATUS_PENDING
);
902 else release_object( server
);
905 release_object( pipe
);
910 return default_fd_ioctl( fd
, code
, async_data
, blocking
);
915 DECL_HANDLER(create_named_pipe
)
917 struct named_pipe
*pipe
;
918 struct pipe_server
*server
;
919 struct unicode_str name
;
920 struct directory
*root
= NULL
;
921 const struct security_descriptor
*sd
;
922 const struct object_attributes
*objattr
= get_req_object_attributes( &sd
, &name
);
924 if (!objattr
) return;
926 if (!req
->sharing
|| (req
->sharing
& ~(FILE_SHARE_READ
| FILE_SHARE_WRITE
)) ||
927 (!(req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
) && (req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
)))
929 set_error( STATUS_INVALID_PARAMETER
);
933 if (objattr
->rootdir
&& !(root
= get_directory_obj( current
->process
, objattr
->rootdir
, 0 )))
936 pipe
= create_named_pipe( root
, &name
, objattr
->attributes
| OBJ_OPENIF
, sd
);
938 if (root
) release_object( root
);
941 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
943 /* initialize it if it didn't already exist */
945 pipe
->waiters
= NULL
;
946 list_init( &pipe
->servers
);
947 pipe
->insize
= req
->insize
;
948 pipe
->outsize
= req
->outsize
;
949 pipe
->maxinstances
= req
->maxinstances
;
950 pipe
->timeout
= req
->timeout
;
951 pipe
->flags
= req
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
;
952 pipe
->sharing
= req
->sharing
;
956 if (pipe
->maxinstances
<= pipe
->instances
)
958 set_error( STATUS_INSTANCE_NOT_AVAILABLE
);
959 release_object( pipe
);
962 if (pipe
->sharing
!= req
->sharing
)
964 set_error( STATUS_ACCESS_DENIED
);
965 release_object( pipe
);
968 clear_error(); /* clear the name collision */
971 server
= create_pipe_server( pipe
, req
->options
, req
->flags
);
974 reply
->handle
= alloc_handle( current
->process
, server
, req
->access
, objattr
->attributes
);
975 server
->pipe
->instances
++;
976 if (sd
) default_set_sd( &server
->obj
, sd
, OWNER_SECURITY_INFORMATION
|
977 GROUP_SECURITY_INFORMATION
|
978 DACL_SECURITY_INFORMATION
|
979 SACL_SECURITY_INFORMATION
);
980 release_object( server
);
983 release_object( pipe
);
986 DECL_HANDLER(get_named_pipe_info
)
988 struct pipe_server
*server
;
989 struct pipe_client
*client
= NULL
;
991 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_READ_ATTRIBUTES
);
994 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
998 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
999 0, &pipe_client_ops
);
1000 if (!client
) return;
1001 server
= client
->server
;
1004 reply
->flags
= client
? client
->pipe_flags
: server
->pipe_flags
;
1007 reply
->sharing
= server
->pipe
->sharing
;
1008 reply
->maxinstances
= server
->pipe
->maxinstances
;
1009 reply
->instances
= server
->pipe
->instances
;
1010 reply
->insize
= server
->pipe
->insize
;
1011 reply
->outsize
= server
->pipe
->outsize
;
1015 release_object(client
);
1018 reply
->flags
|= NAMED_PIPE_SERVER_END
;
1019 release_object(server
);
1023 DECL_HANDLER(set_named_pipe_info
)
1025 struct pipe_server
*server
;
1026 struct pipe_client
*client
= NULL
;
1028 server
= get_pipe_server_obj( current
->process
, req
->handle
, FILE_WRITE_ATTRIBUTES
);
1031 if (get_error() != STATUS_OBJECT_TYPE_MISMATCH
)
1035 client
= (struct pipe_client
*)get_handle_obj( current
->process
, req
->handle
,
1036 0, &pipe_client_ops
);
1037 if (!client
) return;
1038 if (!(server
= client
->server
))
1040 release_object( client
);
1045 if ((req
->flags
& ~(NAMED_PIPE_MESSAGE_STREAM_READ
| NAMED_PIPE_NONBLOCKING_MODE
)) ||
1046 ((req
->flags
& NAMED_PIPE_MESSAGE_STREAM_READ
) && !(server
->pipe
->flags
& NAMED_PIPE_MESSAGE_STREAM_WRITE
)))
1048 set_error( STATUS_INVALID_PARAMETER
);
1052 client
->pipe_flags
= server
->pipe
->flags
| req
->flags
;
1056 server
->pipe_flags
= server
->pipe
->flags
| req
->flags
;
1060 release_object(client
);
1062 release_object(server
);