named_pipe.c 33.4 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 81 82 83
};

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

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

104 105 106
struct named_pipe_device
{
    struct object       obj;         /* object header */
107
    struct fd          *fd;          /* pseudo-fd for ioctls */
108 109 110
    struct namespace   *pipes;       /* named pipe namespace */
};

111
static void named_pipe_dump( struct object *obj, int verbose );
112
static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
113 114
static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
                                            unsigned int sharing, unsigned int options );
115
static void named_pipe_destroy( struct object *obj );
116 117 118 119 120

static const struct object_ops named_pipe_ops =
{
    sizeof(struct named_pipe),    /* size */
    named_pipe_dump,              /* dump */
121
    no_get_type,                  /* get_type */
122 123 124 125
    no_add_queue,                 /* add_queue */
    NULL,                         /* remove_queue */
    NULL,                         /* signaled */
    NULL,                         /* satisfied */
126
    no_signal,                    /* signal */
127
    no_get_fd,                    /* get_fd */
128
    named_pipe_map_access,        /* map_access */
129 130
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
131
    no_lookup_name,               /* lookup_name */
132
    named_pipe_open_file,         /* open_file */
133
    no_close_handle,              /* close_handle */
134 135 136
    named_pipe_destroy            /* destroy */
};

137 138 139 140
/* 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);
141
static void pipe_server_flush( struct fd *fd, struct event **event );
142
static enum server_fd_type pipe_server_get_fd_type( struct fd *fd );
143
static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
144
                                       int blocking, const void *data, data_size_t size );
145 146 147 148 149

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

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

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

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

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

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

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

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

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

271 272 273 274 275 276 277 278 279
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);
}

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

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

294 295 296 297 298 299 300
static int pipe_client_signaled( struct object *obj, struct thread *thread )
{
    struct pipe_client *client = (struct pipe_client *) obj;

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

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

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

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

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

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

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 );
347 348 349 350 351
}


static void notify_empty( struct pipe_server *server )
{
352
    if (!server->flush_poll)
353 354 355 356 357 358 359 360 361 362 363 364 365
        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 */
366
    if (server->client)
367 368 369 370 371 372 373
    {
        assert( server->client->server == server );
        assert( server->client->fd );
        release_object( server->client->fd );
        server->client->fd = NULL;
    }
    assert( server->fd );
374
    shutdown( get_unix_fd( server->fd ), SHUT_RDWR );
375 376 377 378 379 380 381 382 383 384
    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 );

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

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

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

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

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

410
    assert( obj->ops == &pipe_client_ops );
411

412
    if (server)
413
    {
414 415
        notify_empty( server );

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

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

445 446 447 448 449 450 451
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 );
}

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

458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
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;
}

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

479 480 481 482
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 );
483
    if (device->fd) release_object( device->fd );
484
    free( device->pipes );
485 486
}

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

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

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

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

    assert( server->client );

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

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

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

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

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

553
    if (!server || server->state != ps_connected_server) return;
554 555 556

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

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

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

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

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

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

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

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

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

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

            if (async)
631
            {
632
                set_server_state( server, ps_wait_open );
633 634 635
                if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
                release_object( async );
                set_error( STATUS_PENDING );
636
                return wait_handle;
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
            }
            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;
        }
652
        return 0;
653

654 655 656 657 658 659 660 661 662 663 664 665
    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 );
666
            set_server_state( server, ps_disconnected_server );
667 668 669 670
            break;
        case ps_wait_disconnect:
            assert( !server->client );
            do_disconnect( server );
671
            set_server_state( server, ps_wait_connect );
672 673 674
            break;
        case ps_idle_server:
        case ps_wait_open:
675 676
            set_error( STATUS_PIPE_LISTENING );
            break;
677 678
        case ps_disconnected_server:
        case ps_wait_connect:
679
            set_error( STATUS_PIPE_DISCONNECTED );
680 681
            break;
        }
682
        return 0;
683

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

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

696 697
    if (!name || !name->len) return alloc_object( &named_pipe_ops );

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

719
    if (obj->ops != &named_pipe_device_ops)
720
        set_error( STATUS_OBJECT_NAME_INVALID );
721 722 723 724 725
    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();
726 727
    }

728 729
    release_object( obj );
    return pipe;
730 731
}

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

740
static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options )
741
{
742
    struct pipe_server *server;
743

744
    server = alloc_object( &pipe_server_ops );
745
    if (!server)
746 747
        return NULL;

748 749 750 751
    server->fd = NULL;
    server->pipe = pipe;
    server->client = NULL;
    server->flush_poll = NULL;
752
    server->options = options;
753

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

765
static struct pipe_client *create_pipe_client( unsigned int flags )
766
{
767
    struct pipe_client *client;
768

769
    client = alloc_object( &pipe_client_ops );
770
    if (!client)
771 772 773
        return NULL;

    client->fd = NULL;
774
    client->server = NULL;
775
    client->flags = flags;
776 777 778 779

    return client;
}

780
static struct pipe_server *find_available_server( struct named_pipe *pipe )
781
{
782
    struct pipe_server *server;
783

784
    /* look for pipe servers that are listening */
785 786
    LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
    {
787
        if (server->state == ps_wait_open)
788 789
            return (struct pipe_server *)grab_object( server );
    }
790 791 792 793 794 795 796 797

    /* 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 );
    }

798
    return NULL;
799 800
}

801 802 803 804 805 806
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;
807
    unsigned int pipe_sharing;
808 809
    int fds[2];

810
    if (!(server = find_available_server( pipe )))
811 812 813 814 815
    {
        set_error( STATUS_PIPE_NOT_AVAILABLE );
        return NULL;
    }

816 817 818 819 820 821 822 823 824
    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;
    }

825 826 827 828 829 830 831 832 833
    if ((client = create_pipe_client( options )))
    {
        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 */
834 835
            if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
            if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
836 837 838 839 840 841 842 843 844 845 846 847

            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) );
            }

848 849
            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 );
850
            if (client->fd && server->fd)
851
            {
852 853
                allow_fd_caching( client->fd );
                allow_fd_caching( server->fd );
854
                fd_copy_completion( server->ioctl_fd, server->fd );
855
                if (server->state == ps_wait_open)
856
                    fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
857
                set_server_state( server, ps_connected_server );
858 859 860
                server->client = client;
                client->server = server;
            }
861 862 863 864 865
            else
            {
                release_object( client );
                client = NULL;
            }
866 867
        }
        else
868
        {
869
            file_set_error();
870 871 872
            release_object( client );
            client = NULL;
        }
873 874 875 876 877
    }
    release_object( server );
    return &client->obj;
}

878
static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
879
                                             int blocking, const void *data, data_size_t size )
880 881 882 883 884 885 886 887
{
    struct named_pipe_device *device = get_fd_user( fd );

    switch(code)
    {
    case FSCTL_PIPE_WAIT:
        {
            const FILE_PIPE_WAIT_FOR_BUFFER *buffer = data;
888
            obj_handle_t wait_handle = 0;
889 890 891 892 893 894 895 896
            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 );
897
                return 0;
898 899 900 901 902 903
            }
            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 )))
            {
                set_error( STATUS_PIPE_NOT_AVAILABLE );
904
                return 0;
905 906 907 908 909
            }
            if (!(server = find_available_server( pipe )))
            {
                struct async *async;

910 911
                if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;

912
                if (blocking)
913
                {
914 915 916 917 918 919 920 921
                    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;
                    }
922
                }
923
                else async = create_async( current, pipe->waiters, async_data );
924

925
                if (async)
926 927 928 929 930 931 932 933 934
                {
                    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 );

935
        done:
936
            release_object( pipe );
937
            return wait_handle;
938 939 940
        }

    default:
941
        return default_fd_ioctl( fd, code, async_data, blocking, data, size );
942 943 944 945
    }
}


946 947 948
DECL_HANDLER(create_named_pipe)
{
    struct named_pipe *pipe;
949
    struct pipe_server *server;
950
    struct unicode_str name;
951
    struct directory *root = NULL;
952

953 954
    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)))
955 956 957 958 959
    {
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }

960
    reply->handle = 0;
961
    get_req_unicode_str( &name );
962 963 964 965 966 967 968
    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;
969

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

1000
    server = create_pipe_server( pipe, req->options );
1001
    if (server)
1002
    {
1003
        reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
1004 1005
        server->pipe->instances++;
        release_object( server );
1006 1007 1008 1009 1010
    }

    release_object( pipe );
}

1011 1012
DECL_HANDLER(get_named_pipe_info)
{
1013
    struct pipe_server *server;
1014
    struct pipe_client *client = NULL;
1015

1016
    server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
1017
    if (!server)
1018
    {
1019 1020 1021
        if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
            return;

1022 1023
        clear_error();
        client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
1024
                                                       0, &pipe_client_ops );
1025 1026 1027
        if (!client) return;
        server = client->server;
    }
1028

1029
    reply->flags        = server->pipe->flags;
1030
    reply->sharing      = server->pipe->sharing;
1031
    reply->maxinstances = server->pipe->maxinstances;
1032
    reply->instances    = server->pipe->instances;
1033 1034
    reply->insize       = server->pipe->insize;
    reply->outsize      = server->pipe->outsize;
1035

1036 1037 1038 1039 1040 1041 1042
    if (client)
        release_object(client);
    else
    {
        reply->flags |= NAMED_PIPE_SERVER_END;
        release_object(server);
    }
1043
}