named_pipe.c 34.8 KB
Newer Older
1 2 3 4
/*
 * Server-side pipe management
 *
 * Copyright (C) 1998 Alexandre Julliard
5 6
 * Copyright (C) 2001 Mike McCormack
 *
7 8 9 10 11 12 13 14 15 16 17 18
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
20
 *
21
 * TODO:
22
 *   message mode
23 24 25
 */

#include "config.h"
26
#include "wine/port.h"
27 28 29 30

#include <assert.h>
#include <fcntl.h>
#include <string.h>
31
#include <stdarg.h>
32 33 34 35
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
Steven Edwards's avatar
Steven Edwards committed
36
#ifdef HAVE_SYS_SOCKET_H
37
#include <sys/socket.h>
Steven Edwards's avatar
Steven Edwards committed
38
#endif
39 40
#include <time.h>
#include <unistd.h>
41 42 43
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
44

45 46
#include "ntstatus.h"
#define WIN32_NO_STATUS
47
#include "windef.h"
48
#include "winternl.h"
49
#include "winioctl.h"
50

51
#include "file.h"
52 53 54 55
#include "handle.h"
#include "thread.h"
#include "request.h"

56 57 58 59 60
enum pipe_state
{
    ps_idle_server,
    ps_wait_open,
    ps_connected_server,
61 62 63 64 65
    ps_wait_disconnect,
    ps_disconnected_server,
    ps_wait_connect
};

66 67
struct named_pipe;

68 69
struct pipe_server
{
70 71
    struct object        obj;        /* object header */
    struct fd           *fd;         /* pipe file descriptor */
72
    struct fd           *ioctl_fd;   /* file descriptor for ioctls when not connected */
73 74 75
    struct list          entry;      /* entry in named pipe servers list */
    enum pipe_state      state;      /* server state */
    struct pipe_client  *client;     /* client that this server is connected to */
76 77 78
    struct named_pipe   *pipe;
    struct timeout_user *flush_poll;
    struct event        *event;
79
    unsigned int         options;    /* pipe options */
80
    unsigned int         pipe_flags;
81 82 83 84
};

struct pipe_client
{
85 86 87
    struct object        obj;        /* object header */
    struct fd           *fd;         /* pipe file descriptor */
    struct pipe_server  *server;     /* server that this client is connected to */
88
    unsigned int         flags;      /* file flags */
89
    unsigned int         pipe_flags;
90 91 92 93 94
};

struct named_pipe
{
    struct object       obj;         /* object header */
95
    unsigned int        flags;
96
    unsigned int        sharing;
97 98 99
    unsigned int        maxinstances;
    unsigned int        outsize;
    unsigned int        insize;
100
    unsigned int        instances;
101
    timeout_t           timeout;
102
    struct list         servers;     /* list of servers using this pipe */
103
    struct async_queue *waiters;     /* list of clients waiting to connect */
104 105
};

106 107 108
struct named_pipe_device
{
    struct object       obj;         /* object header */
109
    struct fd          *fd;          /* pseudo-fd for ioctls */
110 111 112
    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 116
static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
                                            unsigned int sharing, unsigned int options );
117
static void named_pipe_destroy( struct object *obj );
118 119 120 121 122

static const struct object_ops named_pipe_ops =
{
    sizeof(struct named_pipe),    /* size */
    named_pipe_dump,              /* dump */
123
    no_get_type,                  /* get_type */
124 125 126 127
    no_add_queue,                 /* add_queue */
    NULL,                         /* remove_queue */
    NULL,                         /* signaled */
    NULL,                         /* satisfied */
128
    no_signal,                    /* signal */
129
    no_get_fd,                    /* get_fd */
130
    named_pipe_map_access,        /* map_access */
131 132
    default_get_sd,               /* get_sd */
    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 137 138
    named_pipe_destroy            /* destroy */
};

139 140 141 142
/* server end functions */
static void pipe_server_dump( struct object *obj, int verbose );
static struct fd *pipe_server_get_fd( struct object *obj );
static void pipe_server_destroy( struct object *obj);
143
static void pipe_server_flush( struct fd *fd, struct event **event );
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,
146
                                       int blocking, const void *data, data_size_t size );
147 148 149 150 151

static const struct object_ops pipe_server_ops =
{
    sizeof(struct pipe_server),   /* size */
    pipe_server_dump,             /* dump */
152
    no_get_type,                  /* get_type */
153 154
    add_queue,                    /* add_queue */
    remove_queue,                 /* remove_queue */
155 156
    default_fd_signaled,          /* signaled */
    no_satisfied,                 /* satisfied */
157
    no_signal,                    /* signal */
158
    pipe_server_get_fd,           /* get_fd */
159
    default_fd_map_access,        /* map_access */
160 161
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
162
    no_lookup_name,               /* lookup_name */
163
    no_open_file,                 /* open_file */
164
    fd_close_handle,              /* close_handle */
165 166 167 168 169
    pipe_server_destroy           /* destroy */
};

static const struct fd_ops pipe_server_fd_ops =
{
170
    default_fd_get_poll_events,   /* get_poll_events */
171 172
    default_poll_event,           /* poll_event */
    pipe_server_flush,            /* flush */
173
    pipe_server_get_fd_type,      /* get_fd_type */
174
    pipe_server_ioctl,            /* ioctl */
175
    default_fd_queue_async,       /* queue_async */
176
    default_fd_reselect_async,    /* reselect_async */
177
    default_fd_cancel_async,      /* cancel_async */
178
};
179

180 181
/* client end functions */
static void pipe_client_dump( struct object *obj, int verbose );
182
static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry );
183 184
static struct fd *pipe_client_get_fd( struct object *obj );
static void pipe_client_destroy( struct object *obj );
185
static void pipe_client_flush( struct fd *fd, struct event **event );
186
static enum server_fd_type pipe_client_get_fd_type( struct fd *fd );
187

188
static const struct object_ops pipe_client_ops =
189
{
190 191
    sizeof(struct pipe_client),   /* size */
    pipe_client_dump,             /* dump */
192
    no_get_type,                  /* get_type */
193 194
    add_queue,                    /* add_queue */
    remove_queue,                 /* remove_queue */
195
    pipe_client_signaled,         /* signaled */
196
    no_satisfied,                 /* satisfied */
197
    no_signal,                    /* signal */
198
    pipe_client_get_fd,           /* get_fd */
199
    default_fd_map_access,        /* map_access */
200 201
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
202
    no_lookup_name,               /* lookup_name */
203
    no_open_file,                 /* open_file */
204
    fd_close_handle,              /* close_handle */
205
    pipe_client_destroy           /* destroy */
206 207
};

208
static const struct fd_ops pipe_client_fd_ops =
209
{
210
    default_fd_get_poll_events,   /* get_poll_events */
211
    default_poll_event,           /* poll_event */
212
    pipe_client_flush,            /* flush */
213
    pipe_client_get_fd_type,      /* get_fd_type */
214
    default_fd_ioctl,             /* ioctl */
215
    default_fd_queue_async,       /* queue_async */
216
    default_fd_reselect_async,    /* reselect_async */
217
    default_fd_cancel_async       /* cancel_async */
218 219
};

220
static void named_pipe_device_dump( struct object *obj, int verbose );
221
static struct object_type *named_pipe_device_get_type( struct object *obj );
222
static struct fd *named_pipe_device_get_fd( struct object *obj );
223 224
static struct object *named_pipe_device_lookup_name( struct object *obj,
    struct unicode_str *name, unsigned int attr );
225 226
static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
                                                   unsigned int sharing, unsigned int options );
227
static void named_pipe_device_destroy( struct object *obj );
228
static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
229
static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
230
                                             int blocking, const void *data, data_size_t size );
231 232 233 234 235

static const struct object_ops named_pipe_device_ops =
{
    sizeof(struct named_pipe_device), /* size */
    named_pipe_device_dump,           /* dump */
236
    named_pipe_device_get_type,       /* get_type */
237 238 239 240 241
    no_add_queue,                     /* add_queue */
    NULL,                             /* remove_queue */
    NULL,                             /* signaled */
    no_satisfied,                     /* satisfied */
    no_signal,                        /* signal */
242
    named_pipe_device_get_fd,         /* get_fd */
243
    no_map_access,                    /* map_access */
244 245
    default_get_sd,                   /* get_sd */
    default_set_sd,                   /* set_sd */
246
    named_pipe_device_lookup_name,    /* lookup_name */
247
    named_pipe_device_open_file,      /* open_file */
248
    fd_close_handle,                  /* close_handle */
249 250 251
    named_pipe_device_destroy         /* destroy */
};

252 253
static const struct fd_ops named_pipe_device_fd_ops =
{
254 255 256
    default_fd_get_poll_events,       /* get_poll_events */
    default_poll_event,               /* poll_event */
    no_flush,                         /* flush */
257
    named_pipe_device_get_fd_type,    /* get_fd_type */
258
    named_pipe_device_ioctl,          /* ioctl */
259
    default_fd_queue_async,           /* queue_async */
260
    default_fd_reselect_async,        /* reselect_async */
261
    default_fd_cancel_async           /* cancel_async */
262 263
};

264 265
static void named_pipe_dump( struct object *obj, int verbose )
{
266
    struct named_pipe *pipe = (struct named_pipe *) obj;
267
    assert( obj->ops == &named_pipe_ops );
268 269 270
    fprintf( stderr, "Named pipe " );
    dump_object_name( &pipe->obj );
    fprintf( stderr, "\n" );
271 272
}

273 274 275 276 277 278 279 280 281
static unsigned int named_pipe_map_access( struct object *obj, unsigned int access )
{
    if (access & GENERIC_READ)    access |= STANDARD_RIGHTS_READ;
    if (access & GENERIC_WRITE)   access |= STANDARD_RIGHTS_WRITE | FILE_CREATE_PIPE_INSTANCE;
    if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
    if (access & GENERIC_ALL)     access |= STANDARD_RIGHTS_ALL;
    return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
}

282 283 284 285
static void pipe_server_dump( struct object *obj, int verbose )
{
    struct pipe_server *server = (struct pipe_server *) obj;
    assert( obj->ops == &pipe_server_ops );
286
    fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
287 288 289
}

static void pipe_client_dump( struct object *obj, int verbose )
290
{
291
    struct pipe_client *client = (struct pipe_client *) obj;
292 293
    assert( obj->ops == &pipe_client_ops );
    fprintf( stderr, "Named pipe client server=%p\n", client->server );
294 295
}

296
static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry )
297 298 299 300 301 302
{
    struct pipe_client *client = (struct pipe_client *) obj;

    return client->fd && is_fd_signaled(client->fd);
}

303 304 305 306 307 308
static void named_pipe_destroy( struct object *obj)
{
    struct named_pipe *pipe = (struct named_pipe *) obj;

    assert( list_empty( &pipe->servers ) );
    assert( !pipe->instances );
309
    free_async_queue( pipe->waiters );
310 311 312 313 314
}

static struct fd *pipe_client_get_fd( struct object *obj )
{
    struct pipe_client *client = (struct pipe_client *) obj;
315
    if (client->fd)
316
        return (struct fd *) grab_object( client->fd );
317 318
    set_error( STATUS_PIPE_DISCONNECTED );
    return NULL;
319 320
}

321
static void set_server_state( struct pipe_server *server, enum pipe_state state )
322
{
323
    server->state = state;
324

325
    switch(state)
326 327 328 329
    {
    case ps_connected_server:
    case ps_wait_disconnect:
        assert( server->fd );
330
        break;
331 332
    case ps_wait_open:
    case ps_idle_server:
333 334
        assert( !server->fd );
        set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
335 336 337
        break;
    case ps_disconnected_server:
    case ps_wait_connect:
338 339
        assert( !server->fd );
        set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
340 341
        break;
    }
342 343 344 345 346 347 348
}

static struct fd *pipe_server_get_fd( struct object *obj )
{
    struct pipe_server *server = (struct pipe_server *) obj;

    return (struct fd *)grab_object( server->fd ? server->fd : server->ioctl_fd );
349 350 351 352 353
}


static void notify_empty( struct pipe_server *server )
{
354
    if (!server->flush_poll)
355 356 357 358 359 360 361 362 363 364 365 366 367
        return;
    assert( server->state == ps_connected_server );
    assert( server->event );
    remove_timeout_user( server->flush_poll );
    server->flush_poll = NULL;
    set_event( server->event );
    release_object( server->event );
    server->event = NULL;
}

static void do_disconnect( struct pipe_server *server )
{
    /* we may only have a server fd, if the client disconnected */
368
    if (server->client)
369 370 371 372 373 374 375
    {
        assert( server->client->server == server );
        assert( server->client->fd );
        release_object( server->client->fd );
        server->client->fd = NULL;
    }
    assert( server->fd );
376
    shutdown( get_unix_fd( server->fd ), SHUT_RDWR );
377 378 379 380 381 382 383 384 385 386
    release_object( server->fd );
    server->fd = NULL;
}

static void pipe_server_destroy( struct object *obj)
{
    struct pipe_server *server = (struct pipe_server *)obj;

    assert( obj->ops == &pipe_server_ops );

387
    if (server->fd)
388 389 390 391 392
    {
        notify_empty( server );
        do_disconnect( server );
    }

393
    if (server->client)
394 395 396 397 398 399 400 401
    {
        server->client->server = NULL;
        server->client = NULL;
    }

    assert( server->pipe->instances );
    server->pipe->instances--;

402
    if (server->ioctl_fd) release_object( server->ioctl_fd );
403
    list_remove( &server->entry );
404 405 406 407
    release_object( server->pipe );
}

static void pipe_client_destroy( struct object *obj)
408
{
409 410
    struct pipe_client *client = (struct pipe_client *)obj;
    struct pipe_server *server = client->server;
411

412
    assert( obj->ops == &pipe_client_ops );
413

414
    if (server)
415
    {
416 417
        notify_empty( server );

418
        switch(server->state)
419 420
        {
        case ps_connected_server:
421 422
            /* Don't destroy the server's fd here as we can't
               do a successful flush without it. */
423
            set_server_state( server, ps_wait_disconnect );
424
            break;
425
        case ps_disconnected_server:
426
            set_server_state( server, ps_wait_connect );
427
            break;
428 429 430 431
        case ps_idle_server:
        case ps_wait_open:
        case ps_wait_disconnect:
        case ps_wait_connect:
432
            assert( 0 );
433
        }
434 435 436
        assert( server->client );
        server->client = NULL;
        client->server = NULL;
437
    }
438
    if (client->fd) release_object( client->fd );
439 440
}

441 442 443 444 445 446
static void named_pipe_device_dump( struct object *obj, int verbose )
{
    assert( obj->ops == &named_pipe_device_ops );
    fprintf( stderr, "Named pipe device\n" );
}

447 448 449 450 451 452 453
static struct object_type *named_pipe_device_get_type( struct object *obj )
{
    static const WCHAR name[] = {'D','e','v','i','c','e'};
    static const struct unicode_str str = { name, sizeof(name) };
    return get_object_type( &str );
}

454 455 456
static struct fd *named_pipe_device_get_fd( struct object *obj )
{
    struct named_pipe_device *device = (struct named_pipe_device *)obj;
457
    return (struct fd *)grab_object( device->fd );
458 459
}

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
static struct object *named_pipe_device_lookup_name( struct object *obj, struct unicode_str *name,
                                                     unsigned int attr )
{
    struct named_pipe_device *device = (struct named_pipe_device*)obj;
    struct object *found;

    assert( obj->ops == &named_pipe_device_ops );
    assert( device->pipes );

    if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
        name->len = 0;

    return found;
}

475 476 477 478 479 480
static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
                                                   unsigned int sharing, unsigned int options )
{
    return grab_object( obj );
}

481 482 483 484
static void named_pipe_device_destroy( struct object *obj )
{
    struct named_pipe_device *device = (struct named_pipe_device*)obj;
    assert( obj->ops == &named_pipe_device_ops );
485
    if (device->fd) release_object( device->fd );
486
    free( device->pipes );
487 488
}

489
static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
490
{
491
    return FD_TYPE_DEVICE;
492 493
}

494
void create_named_pipe_device( struct directory *root, const struct unicode_str *name )
495 496 497
{
    struct named_pipe_device *dev;

498
    if ((dev = create_named_object_dir( root, name, 0, &named_pipe_device_ops )) &&
499 500
        get_error() != STATUS_OBJECT_NAME_EXISTS)
    {
501
        dev->pipes = NULL;
502
        if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
503
            !(dev->pipes = create_namespace( 7 )))
504 505 506 507 508
        {
            release_object( dev );
            dev = NULL;
        }
    }
509
    if (dev) make_object_static( &dev->obj );
510 511
}

512 513 514 515 516 517 518 519
static int pipe_data_remaining( struct pipe_server *server )
{
    struct pollfd pfd;
    int fd;

    assert( server->client );

    fd = get_unix_fd( server->client->fd );
520
    if (fd < 0)
521 522 523 524 525
        return 0;
    pfd.fd = fd;
    pfd.events = POLLIN;
    pfd.revents = 0;

526
    if (0 > poll( &pfd, 1, 0 ))
527 528 529 530 531 532 533 534 535 536
        return 0;
 
    return pfd.revents&POLLIN;
}

static void check_flushed( void *arg )
{
    struct pipe_server *server = (struct pipe_server*) arg;

    assert( server->event );
537
    if (pipe_data_remaining( server ))
538
    {
539
        server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
540 541
    }
    else
542 543 544 545 546 547 548
    {
        /* notify_empty( server ); */
        server->flush_poll = NULL;
        set_event( server->event );
        release_object( server->event );
        server->event = NULL;
    }
549 550
}

551
static void pipe_server_flush( struct fd *fd, struct event **event )
552 553 554
{
    struct pipe_server *server = get_fd_user( fd );

555
    if (!server || server->state != ps_connected_server) return;
556 557 558

    /* FIXME: if multiple threads flush the same pipe,
              maybe should create a list of processes to notify */
559
    if (server->flush_poll) return;
560

561
    if (pipe_data_remaining( server ))
562
    {
563
        /* this kind of sux -
564
           there's no unix way to be alerted when a pipe becomes empty */
565
        server->event = create_event( NULL, NULL, 0, 0, 0, NULL );
566
        if (!server->event) return;
567
        server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
568 569 570 571
        *event = server->event;
    }
}

572
static void pipe_client_flush( struct fd *fd, struct event **event )
573 574 575 576
{
    /* FIXME: what do we have to do for this? */
}

577
static inline int is_overlapped( unsigned int options )
578
{
579 580 581
    return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
}

582
static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
583
{
584
    return FD_TYPE_PIPE;
585 586
}

587
static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
588
{
589
    return FD_TYPE_PIPE;
590 591
}

592 593 594
static obj_handle_t alloc_wait_event( struct process *process )
{
    obj_handle_t handle = 0;
595
    struct event *event = create_event( NULL, NULL, 0, 1, 0, NULL );
596 597 598 599 600 601 602 603 604

    if (event)
    {
        handle = alloc_handle( process, event, EVENT_ALL_ACCESS, 0 );
        release_object( event );
    }
    return handle;
}

605
static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
606
                                       int blocking, const void *data, data_size_t size )
607 608
{
    struct pipe_server *server = get_fd_user( fd );
609
    struct async *async;
610
    obj_handle_t wait_handle = 0;
611 612 613

    switch(code)
    {
614 615 616 617 618
    case FSCTL_PIPE_LISTEN:
        switch(server->state)
        {
        case ps_idle_server:
        case ps_wait_connect:
619
            if (blocking)
620 621 622 623
            {
                async_data_t new_data = *async_data;
                if (!(wait_handle = alloc_wait_event( current->process ))) break;
                new_data.event = wait_handle;
624
                if (!(async = fd_queue_async( server->ioctl_fd, &new_data, ASYNC_TYPE_WAIT )))
625 626 627 628 629
                {
                    close_handle( current->process, wait_handle );
                    break;
                }
            }
630
            else async = fd_queue_async( server->ioctl_fd, async_data, ASYNC_TYPE_WAIT );
631 632

            if (async)
633
            {
634
                set_server_state( server, ps_wait_open );
635 636 637
                if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
                release_object( async );
                set_error( STATUS_PENDING );
638
                return wait_handle;
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653
            }
            break;
        case ps_connected_server:
            set_error( STATUS_PIPE_CONNECTED );
            break;
        case ps_disconnected_server:
            set_error( STATUS_PIPE_BUSY );
            break;
        case ps_wait_disconnect:
            set_error( STATUS_NO_DATA_DETECTED );
            break;
        case ps_wait_open:
            set_error( STATUS_INVALID_HANDLE );
            break;
        }
654
        return 0;
655

656 657 658 659 660 661 662 663 664 665 666 667
    case FSCTL_PIPE_DISCONNECT:
        switch(server->state)
        {
        case ps_connected_server:
            assert( server->client );
            assert( server->client->fd );

            notify_empty( server );

            /* dump the client and server fds, but keep the pointers
               around - client loses all waiting data */
            do_disconnect( server );
668
            set_server_state( server, ps_disconnected_server );
669 670 671 672
            break;
        case ps_wait_disconnect:
            assert( !server->client );
            do_disconnect( server );
673
            set_server_state( server, ps_wait_connect );
674 675 676
            break;
        case ps_idle_server:
        case ps_wait_open:
677 678
            set_error( STATUS_PIPE_LISTENING );
            break;
679 680
        case ps_disconnected_server:
        case ps_wait_connect:
681
            set_error( STATUS_PIPE_DISCONNECTED );
682 683
            break;
        }
684
        return 0;
685

686
    default:
687
        return default_fd_ioctl( fd, code, async_data, blocking, data, size );
688 689 690
    }
}

691 692
static struct named_pipe *create_named_pipe( struct directory *root, const struct unicode_str *name,
                                             unsigned int attr )
693
{
694 695 696
    struct object *obj;
    struct named_pipe *pipe = NULL;
    struct unicode_str new_name;
697

698 699
    if (!name || !name->len) return alloc_object( &named_pipe_ops );

700 701 702 703 704
    if (!(obj = find_object_dir( root, name, attr, &new_name )))
    {
        set_error( STATUS_OBJECT_NAME_INVALID );
        return NULL;
    }
705
    if (!new_name.len)
706
    {
707 708 709
        if (attr & OBJ_OPENIF && obj->ops == &named_pipe_ops)
            set_error( STATUS_OBJECT_NAME_EXISTS );
        else
710
        {
711 712 713 714 715 716
            release_object( obj );
            obj = NULL;
            if (attr & OBJ_OPENIF)
                set_error( STATUS_OBJECT_TYPE_MISMATCH );
            else
                set_error( STATUS_OBJECT_NAME_COLLISION );
717
        }
718
        return (struct named_pipe *)obj;
719
    }
720

721
    if (obj->ops != &named_pipe_device_ops)
722
        set_error( STATUS_OBJECT_NAME_INVALID );
723 724 725 726 727
    else
    {
        struct named_pipe_device *dev = (struct named_pipe_device *)obj;
        if ((pipe = create_object( dev->pipes, &named_pipe_ops, &new_name, NULL )))
            clear_error();
728 729
    }

730 731
    release_object( obj );
    return pipe;
732 733
}

734 735
static struct pipe_server *get_pipe_server_obj( struct process *process,
                                obj_handle_t handle, unsigned int access )
736
{
737 738 739
    struct object *obj;
    obj = get_handle_obj( process, handle, access, &pipe_server_ops );
    return (struct pipe_server *) obj;
740 741
}

742 743
static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
                                               unsigned int pipe_flags )
744
{
745
    struct pipe_server *server;
746

747
    server = alloc_object( &pipe_server_ops );
748
    if (!server)
749 750
        return NULL;

751 752 753 754
    server->fd = NULL;
    server->pipe = pipe;
    server->client = NULL;
    server->flush_poll = NULL;
755
    server->options = options;
756
    server->pipe_flags = pipe_flags;
757

758
    list_add_head( &pipe->servers, &server->entry );
759
    grab_object( pipe );
760
    if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->obj, options )))
761 762
    {
        release_object( server );
763
        return NULL;
764 765
    }
    set_server_state( server, ps_idle_server );
766
    return server;
767 768
}

769
static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags )
770
{
771
    struct pipe_client *client;
772

773
    client = alloc_object( &pipe_client_ops );
774
    if (!client)
775 776 777
        return NULL;

    client->fd = NULL;
778
    client->server = NULL;
779
    client->flags = flags;
780
    client->pipe_flags = pipe_flags;
781 782 783 784

    return client;
}

785
static struct pipe_server *find_available_server( struct named_pipe *pipe )
786
{
787
    struct pipe_server *server;
788

789
    /* look for pipe servers that are listening */
790 791
    LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
    {
792
        if (server->state == ps_wait_open)
793 794
            return (struct pipe_server *)grab_object( server );
    }
795 796 797 798 799 800 801 802

    /* fall back to pipe servers that are idle */
    LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
    {
        if (server->state == ps_idle_server)
            return (struct pipe_server *)grab_object( server );
    }

803
    return NULL;
804 805
}

806 807 808 809 810 811
static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
                                            unsigned int sharing, unsigned int options )
{
    struct named_pipe *pipe = (struct named_pipe *)obj;
    struct pipe_server *server;
    struct pipe_client *client;
812
    unsigned int pipe_sharing;
813 814
    int fds[2];

815
    if (!(server = find_available_server( pipe )))
816 817 818 819 820
    {
        set_error( STATUS_PIPE_NOT_AVAILABLE );
        return NULL;
    }

821 822 823 824 825 826 827 828 829
    pipe_sharing = server->pipe->sharing;
    if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
        ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
    {
        set_error( STATUS_ACCESS_DENIED );
        release_object( server );
        return NULL;
    }

830
    if ((client = create_pipe_client( options, pipe->flags )))
831 832 833 834 835 836 837 838
    {
        if (!socketpair( PF_UNIX, SOCK_STREAM, 0, fds ))
        {
            assert( !server->fd );

            /* for performance reasons, only set nonblocking mode when using
             * overlapped I/O. Otherwise, we will be doing too much busy
             * looping */
839 840
            if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
            if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
841 842 843 844 845 846 847 848 849 850 851 852

            if (pipe->insize)
            {
                setsockopt( fds[0], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
                setsockopt( fds[1], SOL_SOCKET, SO_RCVBUF, &pipe->insize, sizeof(pipe->insize) );
            }
            if (pipe->outsize)
            {
                setsockopt( fds[0], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
                setsockopt( fds[1], SOL_SOCKET, SO_SNDBUF, &pipe->outsize, sizeof(pipe->outsize) );
            }

853 854
            client->fd = create_anonymous_fd( &pipe_client_fd_ops, fds[1], &client->obj, options );
            server->fd = create_anonymous_fd( &pipe_server_fd_ops, fds[0], &server->obj, server->options );
855
            if (client->fd && server->fd)
856
            {
857 858
                allow_fd_caching( client->fd );
                allow_fd_caching( server->fd );
859
                fd_copy_completion( server->ioctl_fd, server->fd );
860
                if (server->state == ps_wait_open)
861
                    fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
862
                set_server_state( server, ps_connected_server );
863 864 865
                server->client = client;
                client->server = server;
            }
866 867 868 869 870
            else
            {
                release_object( client );
                client = NULL;
            }
871 872
        }
        else
873
        {
874
            file_set_error();
875 876 877
            release_object( client );
            client = NULL;
        }
878 879 880 881 882
    }
    release_object( server );
    return &client->obj;
}

883
static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
884
                                             int blocking, const void *data, data_size_t size )
885 886 887 888 889 890 891 892
{
    struct named_pipe_device *device = get_fd_user( fd );

    switch(code)
    {
    case FSCTL_PIPE_WAIT:
        {
            const FILE_PIPE_WAIT_FOR_BUFFER *buffer = data;
893
            obj_handle_t wait_handle = 0;
894 895 896 897 898 899 900 901
            struct named_pipe *pipe;
            struct pipe_server *server;
            struct unicode_str name;

            if (size < sizeof(*buffer) ||
                size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
            {
                set_error( STATUS_INVALID_PARAMETER );
902
                return 0;
903 904 905 906 907
            }
            name.str = buffer->Name;
            name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
            if (!(pipe = (struct named_pipe *)find_object( device->pipes, &name, OBJ_CASE_INSENSITIVE )))
            {
908
                set_error( STATUS_OBJECT_NAME_NOT_FOUND );
909
                return 0;
910 911 912 913 914
            }
            if (!(server = find_available_server( pipe )))
            {
                struct async *async;

915 916
                if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;

917
                if (blocking)
918
                {
919 920 921 922 923 924 925 926
                    async_data_t new_data = *async_data;
                    if (!(wait_handle = alloc_wait_event( current->process ))) goto done;
                    new_data.event = wait_handle;
                    if (!(async = create_async( current, pipe->waiters, &new_data )))
                    {
                        close_handle( current->process, wait_handle );
                        wait_handle = 0;
                    }
927
                }
928
                else async = create_async( current, pipe->waiters, async_data );
929

930
                if (async)
931 932 933 934 935 936 937 938 939
                {
                    timeout_t when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
                    async_set_timeout( async, when, STATUS_IO_TIMEOUT );
                    release_object( async );
                    set_error( STATUS_PENDING );
                }
            }
            else release_object( server );

940
        done:
941
            release_object( pipe );
942
            return wait_handle;
943 944 945
        }

    default:
946
        return default_fd_ioctl( fd, code, async_data, blocking, data, size );
947 948 949 950
    }
}


951 952 953
DECL_HANDLER(create_named_pipe)
{
    struct named_pipe *pipe;
954
    struct pipe_server *server;
955
    struct unicode_str name;
956
    struct directory *root = NULL;
957

958 959
    if (!req->sharing || (req->sharing & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) ||
        (!(req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) && (req->flags & NAMED_PIPE_MESSAGE_STREAM_READ)))
960 961 962 963 964
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }

965
    reply->handle = 0;
966
    get_req_unicode_str( &name );
967 968 969 970 971 972 973
    if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
        return;

    pipe = create_named_pipe( root, &name, req->attributes | OBJ_OPENIF );

    if (root) release_object( root );
    if (!pipe) return;
974

975
    if (get_error() != STATUS_OBJECT_NAME_EXISTS)
976
    {
977 978
        /* initialize it if it didn't already exist */
        pipe->instances = 0;
979
        pipe->waiters = NULL;
980
        list_init( &pipe->servers );
981 982 983 984
        pipe->insize = req->insize;
        pipe->outsize = req->outsize;
        pipe->maxinstances = req->maxinstances;
        pipe->timeout = req->timeout;
985
        pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
986
        pipe->sharing = req->sharing;
987
    }
988 989
    else
    {
990
        if (pipe->maxinstances <= pipe->instances)
991
        {
992
            set_error( STATUS_INSTANCE_NOT_AVAILABLE );
993 994 995
            release_object( pipe );
            return;
        }
996
        if (pipe->sharing != req->sharing)
997 998 999 1000 1001
        {
            set_error( STATUS_ACCESS_DENIED );
            release_object( pipe );
            return;
        }
1002
        clear_error(); /* clear the name collision */
1003
    }
1004

1005
    server = create_pipe_server( pipe, req->options, req->flags );
1006
    if (server)
1007
    {
1008
        reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
1009 1010
        server->pipe->instances++;
        release_object( server );
1011 1012 1013 1014 1015
    }

    release_object( pipe );
}

1016 1017
DECL_HANDLER(get_named_pipe_info)
{
1018
    struct pipe_server *server;
1019
    struct pipe_client *client = NULL;
1020

1021
    server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1022
    if (!server)
1023
    {
1024 1025 1026
        if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
            return;

1027 1028
        clear_error();
        client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1029
                                                       0, &pipe_client_ops );
1030 1031 1032
        if (!client) return;
        server = client->server;
    }
1033

1034
    reply->flags        = client ? client->pipe_flags : server->pipe_flags;
1035
    reply->sharing      = server->pipe->sharing;
1036
    reply->maxinstances = server->pipe->maxinstances;
1037
    reply->instances    = server->pipe->instances;
1038 1039
    reply->insize       = server->pipe->insize;
    reply->outsize      = server->pipe->outsize;
1040

1041 1042 1043 1044 1045 1046 1047
    if (client)
        release_object(client);
    else
    {
        reply->flags |= NAMED_PIPE_SERVER_END;
        release_object(server);
    }
1048
}
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086

DECL_HANDLER(set_named_pipe_info)
{
    struct pipe_server *server;
    struct pipe_client *client = NULL;

    server = get_pipe_server_obj( current->process, req->handle, FILE_WRITE_ATTRIBUTES );
    if (!server)
    {
        if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
            return;

        clear_error();
        client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
                                                       0, &pipe_client_ops );
        if (!client) return;
        server = client->server;
    }

    if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
            ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !(server->pipe->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE)))
    {
        set_error( STATUS_INVALID_PARAMETER );
    }
    else if (client)
    {
        client->pipe_flags = server->pipe->flags | req->flags;
    }
    else
    {
        server->pipe_flags = server->pipe->flags | req->flags;
    }

    if (client)
        release_object(client);
    else
        release_object(server);
}