blob: ef298bb6fa908954018b91303e2ab3dc1c08483a [file] [log] [blame]
Mike McCormackde7c3002001-07-10 19:16:49 +00001/*
2 * Server-side pipe management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Mike McCormackbf554572001-08-23 23:29:20 +00005 * Copyright (C) 2001 Mike McCormack
6 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00007 * 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.
11 *
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.
16 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
Mike McCormackbf554572001-08-23 23:29:20 +000021 * TODO:
Mike McCormackef8b9462003-05-15 04:22:45 +000022 * message mode
Mike McCormackde7c3002001-07-10 19:16:49 +000023 */
24
25#include "config.h"
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000026#include "wine/port.h"
Mike McCormackde7c3002001-07-10 19:16:49 +000027
28#include <assert.h>
29#include <fcntl.h>
30#include <string.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000031#include <stdarg.h>
Mike McCormackde7c3002001-07-10 19:16:49 +000032#include <stdio.h>
33#include <stdlib.h>
34#include <sys/time.h>
35#include <sys/types.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000036#ifdef HAVE_SYS_SOCKET_H
Mike McCormackde7c3002001-07-10 19:16:49 +000037#include <sys/socket.h>
Steven Edwards037c8a12003-02-11 22:27:13 +000038#endif
Mike McCormackde7c3002001-07-10 19:16:49 +000039#include <time.h>
40#include <unistd.h>
Steven Edwards57279182005-03-04 12:38:36 +000041#ifdef HAVE_POLL_H
42#include <poll.h>
43#endif
Mike McCormackde7c3002001-07-10 19:16:49 +000044
Ge van Geldorp1a1583a2005-11-28 17:32:54 +010045#include "ntstatus.h"
46#define WIN32_NO_STATUS
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000047#include "windef.h"
Robert Shearmane51dd362005-06-08 19:11:46 +000048#include "winternl.h"
Mike McCormackde7c3002001-07-10 19:16:49 +000049
Alexandre Julliard863637b2003-01-30 00:26:44 +000050#include "file.h"
Mike McCormackde7c3002001-07-10 19:16:49 +000051#include "handle.h"
52#include "thread.h"
53#include "request.h"
54
Mike McCormackbf554572001-08-23 23:29:20 +000055enum pipe_state
56{
Mike McCormackbf554572001-08-23 23:29:20 +000057 ps_idle_server,
58 ps_wait_open,
Mike McCormackbf554572001-08-23 23:29:20 +000059 ps_connected_server,
Mike McCormackef8b9462003-05-15 04:22:45 +000060 ps_wait_disconnect,
61 ps_disconnected_server,
62 ps_wait_connect
63};
64
Mike McCormackde7c3002001-07-10 19:16:49 +000065struct named_pipe;
66
Mike McCormackef8b9462003-05-15 04:22:45 +000067struct pipe_server
Mike McCormackde7c3002001-07-10 19:16:49 +000068{
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +000069 struct object obj; /* object header */
70 struct fd *fd; /* pipe file descriptor */
71 struct list entry; /* entry in named pipe servers list */
72 enum pipe_state state; /* server state */
73 struct pipe_client *client; /* client that this server is connected to */
Mike McCormackef8b9462003-05-15 04:22:45 +000074 struct named_pipe *pipe;
Mike McCormackef8b9462003-05-15 04:22:45 +000075 struct timeout_user *flush_poll;
76 struct event *event;
Eric Pouech5a2591d2005-04-18 14:57:04 +000077 struct list wait_q; /* only a single one can be queued */
Robert Shearmane51dd362005-06-08 19:11:46 +000078 unsigned int options; /* pipe options */
Mike McCormackef8b9462003-05-15 04:22:45 +000079};
80
81struct pipe_client
82{
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +000083 struct object obj; /* object header */
84 struct fd *fd; /* pipe file descriptor */
85 struct pipe_server *server; /* server that this client is connected to */
Robert Shearmane51dd362005-06-08 19:11:46 +000086 unsigned int flags; /* file flags */
Mike McCormackde7c3002001-07-10 19:16:49 +000087};
88
89struct named_pipe
90{
91 struct object obj; /* object header */
Eric Pouech5a2591d2005-04-18 14:57:04 +000092 unsigned int flags;
Mike McCormackde7c3002001-07-10 19:16:49 +000093 unsigned int maxinstances;
94 unsigned int outsize;
95 unsigned int insize;
96 unsigned int timeout;
Mike McCormackef8b9462003-05-15 04:22:45 +000097 unsigned int instances;
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +000098 struct list servers; /* list of servers using this pipe */
99 struct list waiters; /* list of clients waiting to connect */
Mike McCormackde7c3002001-07-10 19:16:49 +0000100};
101
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100102struct named_pipe_device
103{
104 struct object obj; /* object header */
Alexandre Julliard26e60d82005-12-12 14:30:06 +0100105 struct fd *fd; /* pseudo-fd for ioctls */
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100106 struct namespace *pipes; /* named pipe namespace */
107};
108
Mike McCormackde7c3002001-07-10 19:16:49 +0000109static void named_pipe_dump( struct object *obj, int verbose );
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100110static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
Mike McCormackef8b9462003-05-15 04:22:45 +0000111static void named_pipe_destroy( struct object *obj );
Mike McCormackde7c3002001-07-10 19:16:49 +0000112
113static const struct object_ops named_pipe_ops =
114{
115 sizeof(struct named_pipe), /* size */
116 named_pipe_dump, /* dump */
117 no_add_queue, /* add_queue */
118 NULL, /* remove_queue */
119 NULL, /* signaled */
120 NULL, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000121 no_signal, /* signal */
Mike McCormackde7c3002001-07-10 19:16:49 +0000122 no_get_fd, /* get_fd */
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100123 named_pipe_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000124 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000125 no_close_handle, /* close_handle */
Mike McCormackde7c3002001-07-10 19:16:49 +0000126 named_pipe_destroy /* destroy */
127};
128
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100129/* functions common to server and client */
130static unsigned int pipe_map_access( struct object *obj, unsigned int access );
131
Mike McCormackef8b9462003-05-15 04:22:45 +0000132/* server end functions */
133static void pipe_server_dump( struct object *obj, int verbose );
134static struct fd *pipe_server_get_fd( struct object *obj );
135static void pipe_server_destroy( struct object *obj);
136static int pipe_server_flush( struct fd *fd, struct event **event );
Robert Shearmane51dd362005-06-08 19:11:46 +0000137static int pipe_server_get_info( struct fd *fd );
Mike McCormackde7c3002001-07-10 19:16:49 +0000138
Mike McCormackef8b9462003-05-15 04:22:45 +0000139static const struct object_ops pipe_server_ops =
Mike McCormackde7c3002001-07-10 19:16:49 +0000140{
Mike McCormackef8b9462003-05-15 04:22:45 +0000141 sizeof(struct pipe_server), /* size */
142 pipe_server_dump, /* dump */
Alexandre Julliard863637b2003-01-30 00:26:44 +0000143 default_fd_add_queue, /* add_queue */
144 default_fd_remove_queue, /* remove_queue */
145 default_fd_signaled, /* signaled */
Mike McCormackde7c3002001-07-10 19:16:49 +0000146 no_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000147 no_signal, /* signal */
Mike McCormackef8b9462003-05-15 04:22:45 +0000148 pipe_server_get_fd, /* get_fd */
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100149 pipe_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000150 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000151 no_close_handle, /* close_handle */
Mike McCormackef8b9462003-05-15 04:22:45 +0000152 pipe_server_destroy /* destroy */
Alexandre Julliard863637b2003-01-30 00:26:44 +0000153};
154
Mike McCormackef8b9462003-05-15 04:22:45 +0000155static const struct fd_ops pipe_server_fd_ops =
Alexandre Julliard863637b2003-01-30 00:26:44 +0000156{
Robert Shearmane51dd362005-06-08 19:11:46 +0000157 default_fd_get_poll_events, /* get_poll_events */
Mike McCormackde7c3002001-07-10 19:16:49 +0000158 default_poll_event, /* poll_event */
Mike McCormackef8b9462003-05-15 04:22:45 +0000159 pipe_server_flush, /* flush */
Robert Shearmane51dd362005-06-08 19:11:46 +0000160 pipe_server_get_info, /* get_file_info */
161 default_fd_queue_async, /* queue_async */
162 default_fd_cancel_async, /* cancel_async */
Mike McCormackef8b9462003-05-15 04:22:45 +0000163};
164
165/* client end functions */
166static void pipe_client_dump( struct object *obj, int verbose );
167static struct fd *pipe_client_get_fd( struct object *obj );
168static void pipe_client_destroy( struct object *obj );
169static int pipe_client_flush( struct fd *fd, struct event **event );
Robert Shearmane51dd362005-06-08 19:11:46 +0000170static int pipe_client_get_info( struct fd *fd );
Mike McCormackef8b9462003-05-15 04:22:45 +0000171
172static const struct object_ops pipe_client_ops =
173{
174 sizeof(struct pipe_client), /* size */
175 pipe_client_dump, /* dump */
176 default_fd_add_queue, /* add_queue */
177 default_fd_remove_queue, /* remove_queue */
178 default_fd_signaled, /* signaled */
179 no_satisfied, /* satisfied */
Mike McCormackf92fff62005-04-24 17:35:52 +0000180 no_signal, /* signal */
Mike McCormackef8b9462003-05-15 04:22:45 +0000181 pipe_client_get_fd, /* get_fd */
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100182 pipe_map_access, /* map_access */
Vitaliy Margolenbaffcb92005-11-22 14:55:42 +0000183 no_lookup_name, /* lookup_name */
Alexandre Julliardb9b1ea92005-06-09 15:39:52 +0000184 no_close_handle, /* close_handle */
Mike McCormackef8b9462003-05-15 04:22:45 +0000185 pipe_client_destroy /* destroy */
186};
187
188static const struct fd_ops pipe_client_fd_ops =
189{
Robert Shearmane51dd362005-06-08 19:11:46 +0000190 default_fd_get_poll_events, /* get_poll_events */
Mike McCormackef8b9462003-05-15 04:22:45 +0000191 default_poll_event, /* poll_event */
192 pipe_client_flush, /* flush */
Robert Shearmane51dd362005-06-08 19:11:46 +0000193 pipe_client_get_info, /* get_file_info */
194 default_fd_queue_async, /* queue_async */
195 default_fd_cancel_async /* cancel_async */
Mike McCormackde7c3002001-07-10 19:16:49 +0000196};
197
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100198static void named_pipe_device_dump( struct object *obj, int verbose );
Alexandre Julliard26e60d82005-12-12 14:30:06 +0100199static struct fd *named_pipe_device_get_fd( struct object *obj );
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100200static struct object *named_pipe_device_lookup_name( struct object *obj,
201 struct unicode_str *name, unsigned int attr );
202static void named_pipe_device_destroy( struct object *obj );
Alexandre Julliard1acb9302005-12-13 12:00:11 +0100203static int named_pipe_device_get_file_info( struct fd *fd );
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100204
205static const struct object_ops named_pipe_device_ops =
206{
207 sizeof(struct named_pipe_device), /* size */
208 named_pipe_device_dump, /* dump */
209 no_add_queue, /* add_queue */
210 NULL, /* remove_queue */
211 NULL, /* signaled */
212 no_satisfied, /* satisfied */
213 no_signal, /* signal */
Alexandre Julliard26e60d82005-12-12 14:30:06 +0100214 named_pipe_device_get_fd, /* get_fd */
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100215 pipe_map_access, /* map_access */
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100216 named_pipe_device_lookup_name, /* lookup_name */
217 no_close_handle, /* close_handle */
218 named_pipe_device_destroy /* destroy */
219};
220
Alexandre Julliard26e60d82005-12-12 14:30:06 +0100221static const struct fd_ops named_pipe_device_fd_ops =
222{
Alexandre Julliard1acb9302005-12-13 12:00:11 +0100223 default_fd_get_poll_events, /* get_poll_events */
224 default_poll_event, /* poll_event */
225 no_flush, /* flush */
226 named_pipe_device_get_file_info, /* get_file_info */
227 default_fd_queue_async, /* queue_async */
228 default_fd_cancel_async /* cancel_async */
Alexandre Julliard26e60d82005-12-12 14:30:06 +0100229};
230
Mike McCormackde7c3002001-07-10 19:16:49 +0000231static void named_pipe_dump( struct object *obj, int verbose )
232{
Mike McCormackef8b9462003-05-15 04:22:45 +0000233 struct named_pipe *pipe = (struct named_pipe *) obj;
Mike McCormackde7c3002001-07-10 19:16:49 +0000234 assert( obj->ops == &named_pipe_ops );
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000235 fprintf( stderr, "Named pipe " );
236 dump_object_name( &pipe->obj );
237 fprintf( stderr, "\n" );
Mike McCormackde7c3002001-07-10 19:16:49 +0000238}
239
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100240static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
241{
242 if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ;
243 if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
244 if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
245 if (access & GENERIC_ALL) access |= STANDARD_RIGHTS_ALL;
246 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
247}
248
Mike McCormackef8b9462003-05-15 04:22:45 +0000249static void pipe_server_dump( struct object *obj, int verbose )
Mike McCormackde7c3002001-07-10 19:16:49 +0000250{
Mike McCormackef8b9462003-05-15 04:22:45 +0000251 struct pipe_server *server = (struct pipe_server *) obj;
252 assert( obj->ops == &pipe_server_ops );
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000253 fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
Mike McCormackef8b9462003-05-15 04:22:45 +0000254}
255
256static void pipe_client_dump( struct object *obj, int verbose )
257{
258 struct pipe_client *client = (struct pipe_client *) obj;
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000259 assert( obj->ops == &pipe_client_ops );
260 fprintf( stderr, "Named pipe client server=%p\n", client->server );
Mike McCormackde7c3002001-07-10 19:16:49 +0000261}
262
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000263static void named_pipe_destroy( struct object *obj)
264{
265 struct named_pipe *pipe = (struct named_pipe *) obj;
266
267 assert( list_empty( &pipe->servers ) );
268 assert( !pipe->instances );
Eric Pouech5a2591d2005-04-18 14:57:04 +0000269 async_terminate_queue( &pipe->waiters, STATUS_HANDLES_CLOSED );
Mike McCormackef8b9462003-05-15 04:22:45 +0000270}
271
272static struct fd *pipe_client_get_fd( struct object *obj )
273{
274 struct pipe_client *client = (struct pipe_client *) obj;
Robert Shearman37957092005-06-10 19:54:46 +0000275 if (client->fd)
Mike McCormackef8b9462003-05-15 04:22:45 +0000276 return (struct fd *) grab_object( client->fd );
Bill Medland3f7c3ff2003-04-17 02:14:04 +0000277 set_error( STATUS_PIPE_DISCONNECTED );
278 return NULL;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000279}
280
Mike McCormackef8b9462003-05-15 04:22:45 +0000281static struct fd *pipe_server_get_fd( struct object *obj )
Mike McCormackde7c3002001-07-10 19:16:49 +0000282{
Mike McCormackef8b9462003-05-15 04:22:45 +0000283 struct pipe_server *server = (struct pipe_server *) obj;
Mike McCormackde7c3002001-07-10 19:16:49 +0000284
Mike McCormackef8b9462003-05-15 04:22:45 +0000285 switch(server->state)
Mike McCormackbf554572001-08-23 23:29:20 +0000286 {
Mike McCormackef8b9462003-05-15 04:22:45 +0000287 case ps_connected_server:
288 case ps_wait_disconnect:
289 assert( server->fd );
290 return (struct fd *) grab_object( server->fd );
Mike McCormackde7c3002001-07-10 19:16:49 +0000291
Mike McCormackef8b9462003-05-15 04:22:45 +0000292 case ps_wait_open:
293 case ps_idle_server:
294 set_error( STATUS_PIPE_LISTENING );
295 break;
296
297 case ps_disconnected_server:
298 case ps_wait_connect:
299 set_error( STATUS_PIPE_DISCONNECTED );
300 break;
Mike McCormackef8b9462003-05-15 04:22:45 +0000301 }
302 return NULL;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000303}
304
Mike McCormackef8b9462003-05-15 04:22:45 +0000305
306static void notify_empty( struct pipe_server *server )
307{
Robert Shearman37957092005-06-10 19:54:46 +0000308 if (!server->flush_poll)
Mike McCormackef8b9462003-05-15 04:22:45 +0000309 return;
310 assert( server->state == ps_connected_server );
311 assert( server->event );
312 remove_timeout_user( server->flush_poll );
313 server->flush_poll = NULL;
314 set_event( server->event );
315 release_object( server->event );
316 server->event = NULL;
317}
318
319static void do_disconnect( struct pipe_server *server )
320{
321 /* we may only have a server fd, if the client disconnected */
Robert Shearman37957092005-06-10 19:54:46 +0000322 if (server->client)
Mike McCormackef8b9462003-05-15 04:22:45 +0000323 {
324 assert( server->client->server == server );
325 assert( server->client->fd );
326 release_object( server->client->fd );
327 server->client->fd = NULL;
328 }
329 assert( server->fd );
330 release_object( server->fd );
331 server->fd = NULL;
332}
333
Alexandre Julliarda510a7e2005-12-12 16:46:17 +0100334static unsigned int pipe_map_access( struct object *obj, unsigned int access )
335{
336 if (access & GENERIC_READ) access |= FILE_GENERIC_READ;
337 if (access & GENERIC_WRITE) access |= FILE_GENERIC_WRITE;
338 if (access & GENERIC_EXECUTE) access |= FILE_GENERIC_EXECUTE;
339 if (access & GENERIC_ALL) access |= FILE_ALL_ACCESS;
340 return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
341}
342
Mike McCormackef8b9462003-05-15 04:22:45 +0000343static void pipe_server_destroy( struct object *obj)
344{
345 struct pipe_server *server = (struct pipe_server *)obj;
346
347 assert( obj->ops == &pipe_server_ops );
348
Robert Shearman37957092005-06-10 19:54:46 +0000349 if (server->fd)
Mike McCormackef8b9462003-05-15 04:22:45 +0000350 {
351 notify_empty( server );
352 do_disconnect( server );
353 }
354
Robert Shearman37957092005-06-10 19:54:46 +0000355 if (server->client)
Mike McCormackef8b9462003-05-15 04:22:45 +0000356 {
357 server->client->server = NULL;
358 server->client = NULL;
359 }
360
Eric Pouech5a2591d2005-04-18 14:57:04 +0000361 async_terminate_head( &server->wait_q, STATUS_HANDLES_CLOSED );
Mike McCormackef8b9462003-05-15 04:22:45 +0000362
363 assert( server->pipe->instances );
364 server->pipe->instances--;
365
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000366 list_remove( &server->entry );
Mike McCormackef8b9462003-05-15 04:22:45 +0000367 release_object( server->pipe );
368}
369
370static void pipe_client_destroy( struct object *obj)
371{
372 struct pipe_client *client = (struct pipe_client *)obj;
373 struct pipe_server *server = client->server;
374
375 assert( obj->ops == &pipe_client_ops );
376
Robert Shearman37957092005-06-10 19:54:46 +0000377 if (server)
Mike McCormackef8b9462003-05-15 04:22:45 +0000378 {
379 notify_empty( server );
380
Robert Shearman37957092005-06-10 19:54:46 +0000381 switch(server->state)
Mike McCormackef8b9462003-05-15 04:22:45 +0000382 {
383 case ps_connected_server:
384 /* Don't destroy the server's fd here as we can't
385 do a successful flush without it. */
386 server->state = ps_wait_disconnect;
387 release_object( client->fd );
388 client->fd = NULL;
389 break;
390 case ps_disconnected_server:
391 server->state = ps_wait_connect;
392 break;
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000393 case ps_idle_server:
394 case ps_wait_open:
395 case ps_wait_disconnect:
396 case ps_wait_connect:
Mike McCormackef8b9462003-05-15 04:22:45 +0000397 assert( 0 );
398 }
399 assert( server->client );
400 server->client = NULL;
401 client->server = NULL;
402 }
403 assert( !client->fd );
404}
405
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100406static void named_pipe_device_dump( struct object *obj, int verbose )
407{
408 assert( obj->ops == &named_pipe_device_ops );
409 fprintf( stderr, "Named pipe device\n" );
410}
411
Alexandre Julliard26e60d82005-12-12 14:30:06 +0100412static struct fd *named_pipe_device_get_fd( struct object *obj )
413{
414 struct named_pipe_device *device = (struct named_pipe_device *)obj;
Alexandre Julliard5bcd0882005-12-13 11:22:28 +0100415 return (struct fd *)grab_object( device->fd );
Alexandre Julliard26e60d82005-12-12 14:30:06 +0100416}
417
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100418static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
419 unsigned int attr )
420{
421 struct named_pipe_device *device = (struct named_pipe_device*)obj;
422 struct object *found;
423
424 assert( obj->ops == &named_pipe_device_ops );
425 assert( device->pipes );
426
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100427 if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
428 name->len = 0;
429
430 return found;
431}
432
433static void named_pipe_device_destroy( struct object *obj )
434{
435 struct named_pipe_device *device = (struct named_pipe_device*)obj;
436 assert( obj->ops == &named_pipe_device_ops );
Alexandre Julliard26e60d82005-12-12 14:30:06 +0100437 if (device->fd) release_object( device->fd );
438 if (device->pipes) free( device->pipes );
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100439}
440
Alexandre Julliard1acb9302005-12-13 12:00:11 +0100441static int named_pipe_device_get_file_info( struct fd *fd )
442{
443 return 0;
444}
445
Alexandre Julliardd13095b2005-12-05 14:52:02 +0100446struct named_pipe_device *create_named_pipe_device( struct directory *root,
447 const struct unicode_str *name )
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100448{
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100449 struct named_pipe_device *dev;
450
Alexandre Julliardd13095b2005-12-05 14:52:02 +0100451 if ((dev = create_named_object_dir( root, name, 0, &named_pipe_device_ops )) &&
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100452 get_error() != STATUS_OBJECT_NAME_EXISTS)
453 {
Alexandre Julliard26e60d82005-12-12 14:30:06 +0100454 dev->pipes = NULL;
455 if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj )) ||
456 !(dev->pipes = create_namespace( 7 )))
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100457 {
458 release_object( dev );
459 dev = NULL;
460 }
461 }
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100462 return dev;
463}
464
Mike McCormackef8b9462003-05-15 04:22:45 +0000465static int pipe_data_remaining( struct pipe_server *server )
466{
467 struct pollfd pfd;
468 int fd;
469
470 assert( server->client );
471
472 fd = get_unix_fd( server->client->fd );
Robert Shearman37957092005-06-10 19:54:46 +0000473 if (fd < 0)
Mike McCormackef8b9462003-05-15 04:22:45 +0000474 return 0;
475 pfd.fd = fd;
476 pfd.events = POLLIN;
477 pfd.revents = 0;
478
Robert Shearman37957092005-06-10 19:54:46 +0000479 if (0 > poll( &pfd, 1, 0 ))
Mike McCormackef8b9462003-05-15 04:22:45 +0000480 return 0;
481
482 return pfd.revents&POLLIN;
483}
484
485static void check_flushed( void *arg )
486{
487 struct pipe_server *server = (struct pipe_server*) arg;
488
489 assert( server->event );
Robert Shearman37957092005-06-10 19:54:46 +0000490 if (pipe_data_remaining( server ))
Mike McCormackef8b9462003-05-15 04:22:45 +0000491 {
492 struct timeval tv;
493
Robert Shearmanc5165712005-05-25 18:41:09 +0000494 gettimeofday( &tv, NULL );
Mike McCormackef8b9462003-05-15 04:22:45 +0000495 add_timeout( &tv, 100 );
496 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
497 }
498 else
Mike McCormack99a6efa2004-07-15 18:59:58 +0000499 {
500 /* notify_empty( server ); */
501 server->flush_poll = NULL;
502 set_event( server->event );
503 release_object( server->event );
504 server->event = NULL;
505 }
Mike McCormackef8b9462003-05-15 04:22:45 +0000506}
507
508static int pipe_server_flush( struct fd *fd, struct event **event )
509{
510 struct pipe_server *server = get_fd_user( fd );
511
Robert Shearman37957092005-06-10 19:54:46 +0000512 if (!server)
Mike McCormackef8b9462003-05-15 04:22:45 +0000513 return 0;
514
Robert Shearman37957092005-06-10 19:54:46 +0000515 if (server->state != ps_connected_server)
Mike McCormackef8b9462003-05-15 04:22:45 +0000516 return 0;
517
518 /* FIXME: if multiple threads flush the same pipe,
519 maybe should create a list of processes to notify */
Robert Shearman37957092005-06-10 19:54:46 +0000520 if (server->flush_poll)
Mike McCormackef8b9462003-05-15 04:22:45 +0000521 return 0;
522
Robert Shearman37957092005-06-10 19:54:46 +0000523 if (pipe_data_remaining( server ))
Mike McCormackef8b9462003-05-15 04:22:45 +0000524 {
525 struct timeval tv;
526
527 /* this kind of sux -
528 there's no unix way to be alerted when a pipe becomes empty */
Vitaliy Margolenf676bc82005-12-02 15:55:48 +0100529 server->event = create_event( NULL, NULL, 0, 0, 0 );
Robert Shearman37957092005-06-10 19:54:46 +0000530 if (!server->event)
Mike McCormackef8b9462003-05-15 04:22:45 +0000531 return 0;
Robert Shearmanc5165712005-05-25 18:41:09 +0000532 gettimeofday( &tv, NULL );
Mike McCormackef8b9462003-05-15 04:22:45 +0000533 add_timeout( &tv, 100 );
534 server->flush_poll = add_timeout_user( &tv, check_flushed, server );
535 *event = server->event;
536 }
537
538 return 0;
539}
540
541static int pipe_client_flush( struct fd *fd, struct event **event )
542{
543 /* FIXME: what do we have to do for this? */
544 return 0;
545}
546
Robert Shearmane51dd362005-06-08 19:11:46 +0000547static inline int is_overlapped( unsigned int options )
Alexandre Julliard646d6212001-10-05 19:45:45 +0000548{
Robert Shearmane51dd362005-06-08 19:11:46 +0000549 return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
550}
551
552static int pipe_server_get_info( struct fd *fd )
553{
554 struct pipe_server *server = get_fd_user( fd );
555 int flags = FD_FLAG_AVAILABLE;
556
557 if (is_overlapped( server->options )) flags |= FD_FLAG_OVERLAPPED;
558
559 return flags;
560}
561
562static int pipe_client_get_info( struct fd *fd )
563{
564 struct pipe_client *client = get_fd_user( fd );
565 int flags = FD_FLAG_AVAILABLE;
566
567 if (is_overlapped( client->flags )) flags |= FD_FLAG_OVERLAPPED;
568
569 return flags;
Alexandre Julliard646d6212001-10-05 19:45:45 +0000570}
571
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100572static struct named_pipe *create_named_pipe( struct directory *root, const struct unicode_str *name,
573 unsigned int attr )
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000574{
575 struct object *obj;
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100576 struct named_pipe *pipe = NULL;
577 struct unicode_str new_name;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000578
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100579 if (!name || !name->len) return alloc_object( &named_pipe_ops );
580
581 if (!(obj = find_object_dir( root, name, attr, &new_name ))) return NULL;
582 if (!new_name.len)
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000583 {
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100584 if (attr & OBJ_OPENIF && obj->ops == &named_pipe_ops)
585 set_error( STATUS_OBJECT_NAME_EXISTS );
586 else
587 {
588 release_object( obj );
589 obj = NULL;
590 if (attr & OBJ_OPENIF)
591 set_error( STATUS_OBJECT_TYPE_MISMATCH );
592 else
593 set_error( STATUS_OBJECT_NAME_COLLISION );
594 }
595 return (struct named_pipe *)obj;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000596 }
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000597
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100598 if (obj->ops != &named_pipe_device_ops)
599 set_error( STATUS_OBJECT_TYPE_MISMATCH );
600 else
601 {
602 struct named_pipe_device *dev = (struct named_pipe_device *)obj;
603 if ((pipe = create_object( dev->pipes, &named_pipe_ops, &new_name, NULL )))
604 clear_error();
605 }
606
607 release_object( obj );
608 return pipe;
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000609}
610
Mike McCormackef8b9462003-05-15 04:22:45 +0000611static struct pipe_server *get_pipe_server_obj( struct process *process,
612 obj_handle_t handle, unsigned int access )
Mike McCormackde7c3002001-07-10 19:16:49 +0000613{
Mike McCormackef8b9462003-05-15 04:22:45 +0000614 struct object *obj;
615 obj = get_handle_obj( process, handle, access, &pipe_server_ops );
616 return (struct pipe_server *) obj;
Mike McCormackde7c3002001-07-10 19:16:49 +0000617}
618
Robert Shearmane51dd362005-06-08 19:11:46 +0000619static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options )
Mike McCormackde7c3002001-07-10 19:16:49 +0000620{
Mike McCormackef8b9462003-05-15 04:22:45 +0000621 struct pipe_server *server;
Mike McCormackde7c3002001-07-10 19:16:49 +0000622
Mike McCormackef8b9462003-05-15 04:22:45 +0000623 server = alloc_object( &pipe_server_ops );
Robert Shearman37957092005-06-10 19:54:46 +0000624 if (!server)
Mike McCormackde7c3002001-07-10 19:16:49 +0000625 return NULL;
Mike McCormackde7c3002001-07-10 19:16:49 +0000626
Mike McCormackef8b9462003-05-15 04:22:45 +0000627 server->fd = NULL;
628 server->pipe = pipe;
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000629 server->state = ps_idle_server;
Mike McCormackef8b9462003-05-15 04:22:45 +0000630 server->client = NULL;
631 server->flush_poll = NULL;
Robert Shearmane51dd362005-06-08 19:11:46 +0000632 server->options = options;
Eric Pouech5a2591d2005-04-18 14:57:04 +0000633 list_init( &server->wait_q );
Mike McCormackde7c3002001-07-10 19:16:49 +0000634
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000635 list_add_head( &pipe->servers, &server->entry );
Mike McCormackef8b9462003-05-15 04:22:45 +0000636 grab_object( pipe );
Mike McCormackde7c3002001-07-10 19:16:49 +0000637
Mike McCormackef8b9462003-05-15 04:22:45 +0000638 return server;
Mike McCormackde7c3002001-07-10 19:16:49 +0000639}
640
Robert Shearmane51dd362005-06-08 19:11:46 +0000641static struct pipe_client *create_pipe_client( struct pipe_server *server, unsigned int flags )
Mike McCormackde7c3002001-07-10 19:16:49 +0000642{
Mike McCormackef8b9462003-05-15 04:22:45 +0000643 struct pipe_client *client;
Mike McCormackde7c3002001-07-10 19:16:49 +0000644
Mike McCormackef8b9462003-05-15 04:22:45 +0000645 client = alloc_object( &pipe_client_ops );
Robert Shearman37957092005-06-10 19:54:46 +0000646 if (!client)
Mike McCormackbf554572001-08-23 23:29:20 +0000647 return NULL;
648
Mike McCormackef8b9462003-05-15 04:22:45 +0000649 client->fd = NULL;
650 client->server = server;
Robert Shearmane51dd362005-06-08 19:11:46 +0000651 client->flags = flags;
Mike McCormackef8b9462003-05-15 04:22:45 +0000652
653 return client;
654}
655
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000656static inline struct pipe_server *find_server( struct named_pipe *pipe, enum pipe_state state )
Mike McCormackef8b9462003-05-15 04:22:45 +0000657{
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000658 struct pipe_server *server;
Mike McCormackef8b9462003-05-15 04:22:45 +0000659
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000660 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
661 {
662 if (server->state == state) return (struct pipe_server *)grab_object( server );
663 }
664 return NULL;
665}
Mike McCormackef8b9462003-05-15 04:22:45 +0000666
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000667static inline struct pipe_server *find_server2( struct named_pipe *pipe,
668 enum pipe_state state1, enum pipe_state state2 )
669{
670 struct pipe_server *server;
Mike McCormackef8b9462003-05-15 04:22:45 +0000671
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000672 LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
673 {
674 if (server->state == state1 || server->state == state2)
675 return (struct pipe_server *)grab_object( server );
676 }
677 return NULL;
Mike McCormackde7c3002001-07-10 19:16:49 +0000678}
679
680DECL_HANDLER(create_named_pipe)
681{
682 struct named_pipe *pipe;
Mike McCormackef8b9462003-05-15 04:22:45 +0000683 struct pipe_server *server;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000684 struct unicode_str name;
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100685 struct directory *root = NULL;
Mike McCormackde7c3002001-07-10 19:16:49 +0000686
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000687 reply->handle = 0;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000688 get_req_unicode_str( &name );
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100689 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
690 return;
691
692 pipe = create_named_pipe( root, &name, req->attributes | OBJ_OPENIF );
693
694 if (root) release_object( root );
695 if (!pipe) return;
Mike McCormackde7c3002001-07-10 19:16:49 +0000696
Vitaliy Margolen893987b2005-11-21 16:27:03 +0000697 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
Mike McCormackf2e7ce72001-08-27 19:03:42 +0000698 {
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100699 /* initialize it if it didn't already exist */
700 pipe->instances = 0;
701 list_init( &pipe->servers );
702 list_init( &pipe->waiters );
Mike McCormackf2e7ce72001-08-27 19:03:42 +0000703 pipe->insize = req->insize;
704 pipe->outsize = req->outsize;
705 pipe->maxinstances = req->maxinstances;
706 pipe->timeout = req->timeout;
Eric Pouech5a2591d2005-04-18 14:57:04 +0000707 pipe->flags = req->flags;
Mike McCormackf2e7ce72001-08-27 19:03:42 +0000708 }
Mike McCormackef8b9462003-05-15 04:22:45 +0000709 else
Mike McCormackde7c3002001-07-10 19:16:49 +0000710 {
Robert Shearman37957092005-06-10 19:54:46 +0000711 if (pipe->maxinstances <= pipe->instances)
Mike McCormackef8b9462003-05-15 04:22:45 +0000712 {
Vitaliy Margolenecca1042005-11-01 21:37:30 +0000713 set_error( STATUS_INSTANCE_NOT_AVAILABLE );
Mike McCormackef8b9462003-05-15 04:22:45 +0000714 release_object( pipe );
715 return;
716 }
Robert Shearman37957092005-06-10 19:54:46 +0000717 if ((pipe->maxinstances != req->maxinstances) ||
718 (pipe->timeout != req->timeout) ||
719 (pipe->flags != req->flags))
Mike McCormackef8b9462003-05-15 04:22:45 +0000720 {
721 set_error( STATUS_ACCESS_DENIED );
722 release_object( pipe );
723 return;
724 }
Vitaliy Margolenecca1042005-11-01 21:37:30 +0000725 set_error( 0 ); /* clear the name collision */
Mike McCormackef8b9462003-05-15 04:22:45 +0000726 }
727
Robert Shearmane51dd362005-06-08 19:11:46 +0000728 server = create_pipe_server( pipe, req->options );
Robert Shearman37957092005-06-10 19:54:46 +0000729 if (server)
Mike McCormackef8b9462003-05-15 04:22:45 +0000730 {
Alexandre Julliard24560e72005-12-09 13:58:25 +0100731 reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
Mike McCormackef8b9462003-05-15 04:22:45 +0000732 server->pipe->instances++;
733 release_object( server );
Mike McCormackde7c3002001-07-10 19:16:49 +0000734 }
735
736 release_object( pipe );
737}
738
739DECL_HANDLER(open_named_pipe)
740{
Mike McCormackef8b9462003-05-15 04:22:45 +0000741 struct pipe_server *server;
742 struct pipe_client *client;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000743 struct unicode_str name;
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100744 struct directory *root = NULL;
Mike McCormackde7c3002001-07-10 19:16:49 +0000745 struct named_pipe *pipe;
Mike McCormackef8b9462003-05-15 04:22:45 +0000746 int fds[2];
Mike McCormackde7c3002001-07-10 19:16:49 +0000747
Alexandre Julliardead9b062005-11-18 16:31:18 +0000748 get_req_unicode_str( &name );
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100749 if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
750 return;
751
752 pipe = open_object_dir( root, &name, req->attributes, &named_pipe_ops );
753
754 if (root) release_object( root );
755 if (!pipe) return;
Mike McCormackef8b9462003-05-15 04:22:45 +0000756
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000757 server = find_server2( pipe, ps_idle_server, ps_wait_open );
Mike McCormackef8b9462003-05-15 04:22:45 +0000758 release_object( pipe );
759
Robert Shearman37957092005-06-10 19:54:46 +0000760 if (!server)
Mike McCormackde7c3002001-07-10 19:16:49 +0000761 {
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000762 set_error( STATUS_PIPE_NOT_AVAILABLE );
763 return;
764 }
Mike McCormackbf554572001-08-23 23:29:20 +0000765
Robert Shearmane51dd362005-06-08 19:11:46 +0000766 client = create_pipe_client( server, req->flags );
Robert Shearman37957092005-06-10 19:54:46 +0000767 if (client)
Mike McCormackef8b9462003-05-15 04:22:45 +0000768 {
Robert Shearmand6810222005-06-09 09:46:55 +0000769 if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
Mike McCormackde7c3002001-07-10 19:16:49 +0000770 {
Robert Shearmand6810222005-06-09 09:46:55 +0000771 int res = 0;
772
Mike McCormackef8b9462003-05-15 04:22:45 +0000773 assert( !client->fd );
774 assert( !server->fd );
Robert Shearmand6810222005-06-09 09:46:55 +0000775
776 /* for performance reasons, only set nonblocking mode when using
777 * overlapped I/O. Otherwise, we will be doing too much busy
778 * looping */
779 if (is_overlapped( req->flags ))
780 res = fcntl( fds[1], F_SETFL, O_NONBLOCK );
781 if ((res != -1) && is_overlapped( server->options ))
782 res = fcntl( fds[0], F_SETFL, O_NONBLOCK );
783
Eric Pouechf479eef2005-05-09 09:25:45 +0000784 client->fd = create_anonymous_fd( &pipe_client_fd_ops,
Mike McCormackef8b9462003-05-15 04:22:45 +0000785 fds[1], &client->obj );
786 server->fd = create_anonymous_fd( &pipe_server_fd_ops,
787 fds[0], &server->obj );
Robert Shearmand6810222005-06-09 09:46:55 +0000788 if (client->fd && server->fd && res != 1)
Mike McCormackde7c3002001-07-10 19:16:49 +0000789 {
Robert Shearman37957092005-06-10 19:54:46 +0000790 if (server->state == ps_wait_open)
Eric Pouech5a2591d2005-04-18 14:57:04 +0000791 async_terminate_head( &server->wait_q, STATUS_SUCCESS );
792 assert( list_empty( &server->wait_q ) );
Mike McCormackef8b9462003-05-15 04:22:45 +0000793 server->state = ps_connected_server;
794 server->client = client;
795 client->server = server;
Alexandre Julliard24560e72005-12-09 13:58:25 +0100796 reply->handle = alloc_handle( current->process, client, req->access, req->attributes );
Mike McCormackde7c3002001-07-10 19:16:49 +0000797 }
Mike McCormackde7c3002001-07-10 19:16:49 +0000798 }
Mike McCormackef8b9462003-05-15 04:22:45 +0000799 else
800 file_set_error();
Mike McCormackde7c3002001-07-10 19:16:49 +0000801
Mike McCormackef8b9462003-05-15 04:22:45 +0000802 release_object( client );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000803 }
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000804 release_object( server );
Mike McCormackde7c3002001-07-10 19:16:49 +0000805}
806
807DECL_HANDLER(connect_named_pipe)
808{
Mike McCormackef8b9462003-05-15 04:22:45 +0000809 struct pipe_server *server;
Mike McCormackde7c3002001-07-10 19:16:49 +0000810
Mike McCormackef8b9462003-05-15 04:22:45 +0000811 server = get_pipe_server_obj(current->process, req->handle, 0);
Robert Shearman37957092005-06-10 19:54:46 +0000812 if (!server)
Mike McCormackde7c3002001-07-10 19:16:49 +0000813 return;
814
Robert Shearman37957092005-06-10 19:54:46 +0000815 switch(server->state)
Mike McCormackde7c3002001-07-10 19:16:49 +0000816 {
Mike McCormackef8b9462003-05-15 04:22:45 +0000817 case ps_idle_server:
818 case ps_wait_connect:
819 assert( !server->fd );
820 server->state = ps_wait_open;
Eric Pouech5a2591d2005-04-18 14:57:04 +0000821 create_async( current, NULL, &server->wait_q,
Ivan Leo Puotif0db7ab2005-09-06 14:09:42 +0000822 req->func, req->event, NULL );
Eric Pouech5a2591d2005-04-18 14:57:04 +0000823 async_terminate_queue( &server->pipe->waiters, STATUS_SUCCESS );
Mike McCormackef8b9462003-05-15 04:22:45 +0000824 break;
825 case ps_connected_server:
826 assert( server->fd );
827 set_error( STATUS_PIPE_CONNECTED );
828 break;
829 case ps_disconnected_server:
830 set_error( STATUS_PIPE_BUSY );
831 break;
832 case ps_wait_disconnect:
833 set_error( STATUS_NO_DATA_DETECTED );
834 break;
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000835 case ps_wait_open:
Mike McCormackef8b9462003-05-15 04:22:45 +0000836 set_error( STATUS_INVALID_HANDLE );
837 break;
Mike McCormackde7c3002001-07-10 19:16:49 +0000838 }
839
Mike McCormackef8b9462003-05-15 04:22:45 +0000840 release_object(server);
Mike McCormackde7c3002001-07-10 19:16:49 +0000841}
Mike McCormackbf554572001-08-23 23:29:20 +0000842
843DECL_HANDLER(wait_named_pipe)
844{
Vitaliy Margolenb05340a2005-12-14 11:14:02 +0100845 struct named_pipe_device *device;
Mike McCormackbf554572001-08-23 23:29:20 +0000846 struct named_pipe *pipe;
Mike McCormackef8b9462003-05-15 04:22:45 +0000847 struct pipe_server *server;
Alexandre Julliardead9b062005-11-18 16:31:18 +0000848 struct unicode_str name;
Mike McCormackbf554572001-08-23 23:29:20 +0000849
Vitaliy Margolenb05340a2005-12-14 11:14:02 +0100850 device = (struct named_pipe_device *)get_handle_obj( current->process, req->handle,
851 FILE_READ_ATTRIBUTES, &named_pipe_device_ops );
852 if (!device) return;
853
Alexandre Julliardead9b062005-11-18 16:31:18 +0000854 get_req_unicode_str( &name );
Vitaliy Margolenb05340a2005-12-14 11:14:02 +0100855 pipe = (struct named_pipe *)find_object( device->pipes, &name, OBJ_CASE_INSENSITIVE );
856 release_object( device );
Vitaliy Margolenbabfa792005-12-05 13:09:35 +0100857 if (!pipe)
Mike McCormackbf554572001-08-23 23:29:20 +0000858 {
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000859 set_error( STATUS_PIPE_NOT_AVAILABLE );
860 return;
Mike McCormackbf554572001-08-23 23:29:20 +0000861 }
Mike McCormackef8b9462003-05-15 04:22:45 +0000862 server = find_server( pipe, ps_wait_open );
Robert Shearman37957092005-06-10 19:54:46 +0000863 if (server)
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000864 {
Mike McCormackef8b9462003-05-15 04:22:45 +0000865 /* there's already a server waiting for a client to connect */
Eric Pouech5a2591d2005-04-18 14:57:04 +0000866 thread_queue_apc( current, NULL, req->func, APC_ASYNC_IO,
Vitaliy Margolenb05340a2005-12-14 11:14:02 +0100867 1, req->event, NULL, (void *)STATUS_SUCCESS );
Mike McCormackef8b9462003-05-15 04:22:45 +0000868 release_object( server );
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000869 }
870 else
Robert Shearman795e9de2005-02-08 12:54:23 +0000871 {
Mike McCormacke659f1e2005-08-09 10:37:50 +0000872 int timeout;
Robert Shearman795e9de2005-02-08 12:54:23 +0000873 if (req->timeout == NMPWAIT_USE_DEFAULT_WAIT)
874 timeout = pipe->timeout;
875 else
876 timeout = req->timeout;
877
878 if (req->timeout == NMPWAIT_WAIT_FOREVER)
Eric Pouech5a2591d2005-04-18 14:57:04 +0000879 create_async( current, NULL, &pipe->waiters,
Vitaliy Margolenb05340a2005-12-14 11:14:02 +0100880 req->func, req->event, NULL );
Robert Shearman795e9de2005-02-08 12:54:23 +0000881 else
Eric Pouech5a2591d2005-04-18 14:57:04 +0000882 create_async( current, &timeout, &pipe->waiters,
Vitaliy Margolenb05340a2005-12-14 11:14:02 +0100883 req->func, req->event, NULL );
Robert Shearman795e9de2005-02-08 12:54:23 +0000884 }
Alexandre Julliarde66207e2003-02-19 00:33:32 +0000885
Mike McCormackef8b9462003-05-15 04:22:45 +0000886 release_object( pipe );
Mike McCormackbf554572001-08-23 23:29:20 +0000887}
888
889DECL_HANDLER(disconnect_named_pipe)
890{
Mike McCormackef8b9462003-05-15 04:22:45 +0000891 struct pipe_server *server;
Mike McCormackbf554572001-08-23 23:29:20 +0000892
Mike McCormackef8b9462003-05-15 04:22:45 +0000893 reply->fd = -1;
894 server = get_pipe_server_obj( current->process, req->handle, 0 );
Robert Shearman37957092005-06-10 19:54:46 +0000895 if (!server)
Mike McCormackbf554572001-08-23 23:29:20 +0000896 return;
Robert Shearman37957092005-06-10 19:54:46 +0000897 switch(server->state)
Mike McCormackbf554572001-08-23 23:29:20 +0000898 {
Mike McCormackef8b9462003-05-15 04:22:45 +0000899 case ps_connected_server:
900 assert( server->fd );
901 assert( server->client );
902 assert( server->client->fd );
Mike McCormackbf554572001-08-23 23:29:20 +0000903
Mike McCormackef8b9462003-05-15 04:22:45 +0000904 notify_empty( server );
Mike McCormackef8b9462003-05-15 04:22:45 +0000905
906 /* Dump the client and server fds, but keep the pointers
907 around - client loses all waiting data */
908 server->state = ps_disconnected_server;
909 do_disconnect( server );
910 reply->fd = flush_cached_fd( current->process, req->handle );
911 break;
912
913 case ps_wait_disconnect:
914 assert( !server->client );
915 assert( server->fd );
916 do_disconnect( server );
917 server->state = ps_wait_connect;
918 reply->fd = flush_cached_fd( current->process, req->handle );
919 break;
920
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000921 case ps_idle_server:
922 case ps_wait_open:
923 case ps_disconnected_server:
924 case ps_wait_connect:
Mike McCormackef8b9462003-05-15 04:22:45 +0000925 set_error( STATUS_PIPE_DISCONNECTED );
Alexandre Julliard7c1cf8f2005-02-03 10:48:23 +0000926 break;
Mike McCormackbf554572001-08-23 23:29:20 +0000927 }
Mike McCormackef8b9462003-05-15 04:22:45 +0000928 release_object( server );
Mike McCormackbf554572001-08-23 23:29:20 +0000929}
Mike McCormackf2e7ce72001-08-27 19:03:42 +0000930
931DECL_HANDLER(get_named_pipe_info)
932{
Mike McCormackef8b9462003-05-15 04:22:45 +0000933 struct pipe_server *server;
Mike McCormackf2e7ce72001-08-27 19:03:42 +0000934
Mike McCormackef8b9462003-05-15 04:22:45 +0000935 server = get_pipe_server_obj( current->process, req->handle, 0 );
Robert Shearman37957092005-06-10 19:54:46 +0000936 if (!server)
Mike McCormackf2e7ce72001-08-27 19:03:42 +0000937 return;
938
Eric Pouech5a2591d2005-04-18 14:57:04 +0000939 reply->flags = server->pipe->flags;
Mike McCormackef8b9462003-05-15 04:22:45 +0000940 reply->maxinstances = server->pipe->maxinstances;
941 reply->insize = server->pipe->insize;
942 reply->outsize = server->pipe->outsize;
Mike McCormackf2e7ce72001-08-27 19:03:42 +0000943
Mike McCormackef8b9462003-05-15 04:22:45 +0000944 release_object(server);
Mike McCormackf2e7ce72001-08-27 19:03:42 +0000945}