named_pipe.c 32.3 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 95 96
    unsigned int        maxinstances;
    unsigned int        outsize;
    unsigned int        insize;
97
    unsigned int        instances;
98
    timeout_t           timeout;
99
    struct list         servers;     /* list of servers using this pipe */
100
    struct async_queue *waiters;     /* list of clients waiting to connect */
101 102
};

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

292 293 294 295 296 297
static void named_pipe_destroy( struct object *obj)
{
    struct named_pipe *pipe = (struct named_pipe *) obj;

    assert( list_empty( &pipe->servers ) );
    assert( !pipe->instances );
298
    free_async_queue( pipe->waiters );
299 300 301 302 303
}

static struct fd *pipe_client_get_fd( struct object *obj )
{
    struct pipe_client *client = (struct pipe_client *) obj;
304
    if (client->fd)
305
        return (struct fd *) grab_object( client->fd );
306 307
    set_error( STATUS_PIPE_DISCONNECTED );
    return NULL;
308 309
}

310
static void set_server_state( struct pipe_server *server, enum pipe_state state )
311
{
312
    server->state = state;
313

314
    switch(state)
315 316 317 318
    {
    case ps_connected_server:
    case ps_wait_disconnect:
        assert( server->fd );
319
        break;
320 321
    case ps_wait_open:
    case ps_idle_server:
322 323
        assert( !server->fd );
        set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
324 325 326
        break;
    case ps_disconnected_server:
    case ps_wait_connect:
327 328
        assert( !server->fd );
        set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
329 330
        break;
    }
331 332 333 334 335 336 337
}

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 );
338 339 340 341 342
}


static void notify_empty( struct pipe_server *server )
{
343
    if (!server->flush_poll)
344 345 346 347 348 349 350 351 352 353 354 355 356
        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 */
357
    if (server->client)
358 359 360 361 362 363 364
    {
        assert( server->client->server == server );
        assert( server->client->fd );
        release_object( server->client->fd );
        server->client->fd = NULL;
    }
    assert( server->fd );
365
    shutdown( get_unix_fd( server->fd ), SHUT_RDWR );
366 367 368 369 370 371 372 373 374 375
    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 );

376
    if (server->fd)
377 378 379 380 381
    {
        notify_empty( server );
        do_disconnect( server );
    }

382
    if (server->client)
383 384 385 386 387 388 389 390
    {
        server->client->server = NULL;
        server->client = NULL;
    }

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

391
    if (server->ioctl_fd) release_object( server->ioctl_fd );
392
    list_remove( &server->entry );
393 394 395 396
    release_object( server->pipe );
}

static void pipe_client_destroy( struct object *obj)
397
{
398 399
    struct pipe_client *client = (struct pipe_client *)obj;
    struct pipe_server *server = client->server;
400

401
    assert( obj->ops == &pipe_client_ops );
402

403
    if (server)
404
    {
405 406
        notify_empty( server );

407
        switch(server->state)
408 409
        {
        case ps_connected_server:
410 411
            /* Don't destroy the server's fd here as we can't
               do a successful flush without it. */
412
            set_server_state( server, ps_wait_disconnect );
413
            break;
414
        case ps_disconnected_server:
415
            set_server_state( server, ps_wait_connect );
416
            break;
417 418 419 420
        case ps_idle_server:
        case ps_wait_open:
        case ps_wait_disconnect:
        case ps_wait_connect:
421
            assert( 0 );
422
        }
423 424 425
        assert( server->client );
        server->client = NULL;
        client->server = NULL;
426
    }
427
    if (client->fd) release_object( client->fd );
428 429
}

430 431 432 433 434 435
static void named_pipe_device_dump( struct object *obj, int verbose )
{
    assert( obj->ops == &named_pipe_device_ops );
    fprintf( stderr, "Named pipe device\n" );
}

436 437 438 439 440 441 442
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 );
}

443 444 445
static struct fd *named_pipe_device_get_fd( struct object *obj )
{
    struct named_pipe_device *device = (struct named_pipe_device *)obj;
446
    return (struct fd *)grab_object( device->fd );
447 448
}

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
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;
}

464 465 466 467 468 469
static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
                                                   unsigned int sharing, unsigned int options )
{
    return grab_object( obj );
}

470 471 472 473
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 );
474
    if (device->fd) release_object( device->fd );
475
    free( device->pipes );
476 477
}

478
static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
479
{
480
    return FD_TYPE_DEVICE;
481 482
}

483
void create_named_pipe_device( struct directory *root, const struct unicode_str *name )
484 485 486
{
    struct named_pipe_device *dev;

487
    if ((dev = create_named_object_dir( root, name, 0, &named_pipe_device_ops )) &&
488 489
        get_error() != STATUS_OBJECT_NAME_EXISTS)
    {
490
        dev->pipes = NULL;
491
        if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
492
            !(dev->pipes = create_namespace( 7 )))
493 494 495 496 497
        {
            release_object( dev );
            dev = NULL;
        }
    }
498
    if (dev) make_object_static( &dev->obj );
499 500
}

501 502 503 504 505 506 507 508
static int pipe_data_remaining( struct pipe_server *server )
{
    struct pollfd pfd;
    int fd;

    assert( server->client );

    fd = get_unix_fd( server->client->fd );
509
    if (fd < 0)
510 511 512 513 514
        return 0;
    pfd.fd = fd;
    pfd.events = POLLIN;
    pfd.revents = 0;

515
    if (0 > poll( &pfd, 1, 0 ))
516 517 518 519 520 521 522 523 524 525
        return 0;
 
    return pfd.revents&POLLIN;
}

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

    assert( server->event );
526
    if (pipe_data_remaining( server ))
527
    {
528
        server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
529 530
    }
    else
531 532 533 534 535 536 537
    {
        /* notify_empty( server ); */
        server->flush_poll = NULL;
        set_event( server->event );
        release_object( server->event );
        server->event = NULL;
    }
538 539
}

540
static void pipe_server_flush( struct fd *fd, struct event **event )
541 542 543
{
    struct pipe_server *server = get_fd_user( fd );

544
    if (!server || server->state != ps_connected_server) return;
545 546 547

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

550
    if (pipe_data_remaining( server ))
551
    {
552
        /* this kind of sux -
553
           there's no unix way to be alerted when a pipe becomes empty */
554
        server->event = create_event( NULL, NULL, 0, 0, 0, NULL );
555
        if (!server->event) return;
556
        server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
557 558 559 560
        *event = server->event;
    }
}

561
static void pipe_client_flush( struct fd *fd, struct event **event )
562 563 564 565
{
    /* FIXME: what do we have to do for this? */
}

566
static inline int is_overlapped( unsigned int options )
567
{
568 569 570
    return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
}

571
static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
572
{
573
    return FD_TYPE_PIPE;
574 575
}

576
static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
577
{
578
    return FD_TYPE_PIPE;
579 580
}

581 582 583
static obj_handle_t alloc_wait_event( struct process *process )
{
    obj_handle_t handle = 0;
584
    struct event *event = create_event( NULL, NULL, 0, 1, 0, NULL );
585 586 587 588 589 590 591 592 593

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

594 595
static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
                                       const void *data, data_size_t size )
596 597
{
    struct pipe_server *server = get_fd_user( fd );
598
    struct async *async;
599
    obj_handle_t wait_handle = 0;
600 601 602

    switch(code)
    {
603 604 605 606 607
    case FSCTL_PIPE_LISTEN:
        switch(server->state)
        {
        case ps_idle_server:
        case ps_wait_connect:
608 609 610 611 612 613 614 615 616 617 618 619 620 621
            if (!async_data->event && !async_data->apc)
            {
                async_data_t new_data = *async_data;
                if (!(wait_handle = alloc_wait_event( current->process ))) break;
                new_data.event = wait_handle;
                if (!(async = fd_queue_async( server->ioctl_fd, &new_data, ASYNC_TYPE_WAIT, 0 )))
                {
                    close_handle( current->process, wait_handle );
                    break;
                }
            }
            else async = fd_queue_async( server->ioctl_fd, async_data, ASYNC_TYPE_WAIT, 0 );

            if (async)
622
            {
623
                set_server_state( server, ps_wait_open );
624 625 626
                if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
                release_object( async );
                set_error( STATUS_PENDING );
627
                return wait_handle;
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
            }
            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;
        }
643
        return 0;
644

645 646 647 648 649 650 651 652 653 654 655 656
    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 );
657
            set_server_state( server, ps_disconnected_server );
658 659 660 661
            break;
        case ps_wait_disconnect:
            assert( !server->client );
            do_disconnect( server );
662
            set_server_state( server, ps_wait_connect );
663 664 665
            break;
        case ps_idle_server:
        case ps_wait_open:
666 667
            set_error( STATUS_PIPE_LISTENING );
            break;
668 669
        case ps_disconnected_server:
        case ps_wait_connect:
670
            set_error( STATUS_PIPE_DISCONNECTED );
671 672
            break;
        }
673
        return 0;
674

675
    default:
676
        return default_fd_ioctl( fd, code, async_data, data, size );
677 678 679
    }
}

680 681
static struct named_pipe *create_named_pipe( struct directory *root, const struct unicode_str *name,
                                             unsigned int attr )
682
{
683 684 685
    struct object *obj;
    struct named_pipe *pipe = NULL;
    struct unicode_str new_name;
686

687 688
    if (!name || !name->len) return alloc_object( &named_pipe_ops );

689 690 691 692 693
    if (!(obj = find_object_dir( root, name, attr, &new_name )))
    {
        set_error( STATUS_OBJECT_NAME_INVALID );
        return NULL;
    }
694
    if (!new_name.len)
695
    {
696 697 698
        if (attr & OBJ_OPENIF && obj->ops == &named_pipe_ops)
            set_error( STATUS_OBJECT_NAME_EXISTS );
        else
699
        {
700 701 702 703 704 705
            release_object( obj );
            obj = NULL;
            if (attr & OBJ_OPENIF)
                set_error( STATUS_OBJECT_TYPE_MISMATCH );
            else
                set_error( STATUS_OBJECT_NAME_COLLISION );
706
        }
707
        return (struct named_pipe *)obj;
708
    }
709

710
    if (obj->ops != &named_pipe_device_ops)
711
        set_error( STATUS_OBJECT_NAME_INVALID );
712 713 714 715 716
    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();
717 718
    }

719 720
    release_object( obj );
    return pipe;
721 722
}

723 724
static struct pipe_server *get_pipe_server_obj( struct process *process,
                                obj_handle_t handle, unsigned int access )
725
{
726 727 728
    struct object *obj;
    obj = get_handle_obj( process, handle, access, &pipe_server_ops );
    return (struct pipe_server *) obj;
729 730
}

731
static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options )
732
{
733
    struct pipe_server *server;
734

735
    server = alloc_object( &pipe_server_ops );
736
    if (!server)
737 738
        return NULL;

739 740 741 742
    server->fd = NULL;
    server->pipe = pipe;
    server->client = NULL;
    server->flush_poll = NULL;
743
    server->options = options;
744

745
    list_add_head( &pipe->servers, &server->entry );
746
    grab_object( pipe );
747
    if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->obj, options )))
748 749
    {
        release_object( server );
750
        return NULL;
751 752
    }
    set_server_state( server, ps_idle_server );
753
    return server;
754 755
}

756
static struct pipe_client *create_pipe_client( unsigned int flags )
757
{
758
    struct pipe_client *client;
759

760
    client = alloc_object( &pipe_client_ops );
761
    if (!client)
762 763 764
        return NULL;

    client->fd = NULL;
765
    client->server = NULL;
766
    client->flags = flags;
767 768 769 770

    return client;
}

771
static struct pipe_server *find_available_server( struct named_pipe *pipe )
772
{
773
    struct pipe_server *server;
774

775
    /* look for pipe servers that are listening */
776 777
    LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
    {
778
        if (server->state == ps_wait_open)
779 780
            return (struct pipe_server *)grab_object( server );
    }
781 782 783 784 785 786 787 788

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

789
    return NULL;
790 791
}

792 793 794 795 796 797 798 799
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;
    int fds[2];

800
    if (!(server = find_available_server( pipe )))
801 802 803 804 805 806 807 808 809 810 811 812 813 814
    {
        set_error( STATUS_PIPE_NOT_AVAILABLE );
        return NULL;
    }

    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 */
815 816
            if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
            if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
817 818 819 820 821 822 823 824 825 826 827 828

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

829 830
            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 );
831
            if (client->fd && server->fd)
832 833
            {
                if (server->state == ps_wait_open)
834
                    fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
835
                set_server_state( server, ps_connected_server );
836 837 838
                server->client = client;
                client->server = server;
            }
839 840 841 842 843
            else
            {
                release_object( client );
                client = NULL;
            }
844 845
        }
        else
846
        {
847
            file_set_error();
848 849 850
            release_object( client );
            client = NULL;
        }
851 852 853 854 855
    }
    release_object( server );
    return &client->obj;
}

856 857
static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
                                             const void *data, data_size_t size )
858 859 860 861 862 863 864 865
{
    struct named_pipe_device *device = get_fd_user( fd );

    switch(code)
    {
    case FSCTL_PIPE_WAIT:
        {
            const FILE_PIPE_WAIT_FOR_BUFFER *buffer = data;
866
            obj_handle_t wait_handle = 0;
867 868 869 870 871 872 873 874
            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 );
875
                return 0;
876 877 878 879 880 881
            }
            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 );
882
                return 0;
883 884 885 886 887
            }
            if (!(server = find_available_server( pipe )))
            {
                struct async *async;

888 889 890
                if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;

                if (!async_data->event && !async_data->apc)
891
                {
892 893 894 895 896 897 898 899
                    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;
                    }
900
                }
901
                else async = create_async( current, pipe->waiters, async_data );
902

903
                if (async)
904 905 906 907 908 909 910 911 912
                {
                    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 );

913
        done:
914
            release_object( pipe );
915
            return wait_handle;
916 917 918
        }

    default:
919
        return default_fd_ioctl( fd, code, async_data, data, size );
920 921 922 923
    }
}


924 925 926
DECL_HANDLER(create_named_pipe)
{
    struct named_pipe *pipe;
927
    struct pipe_server *server;
928
    struct unicode_str name;
929
    struct directory *root = NULL;
930

931
    reply->handle = 0;
932
    get_req_unicode_str( &name );
933 934 935 936 937 938 939
    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;
940

941
    if (get_error() != STATUS_OBJECT_NAME_EXISTS)
942
    {
943 944
        /* initialize it if it didn't already exist */
        pipe->instances = 0;
945
        pipe->waiters = NULL;
946
        list_init( &pipe->servers );
947 948 949 950
        pipe->insize = req->insize;
        pipe->outsize = req->outsize;
        pipe->maxinstances = req->maxinstances;
        pipe->timeout = req->timeout;
951
        pipe->flags = req->flags;
952
    }
953 954
    else
    {
955
        if (pipe->maxinstances <= pipe->instances)
956
        {
957
            set_error( STATUS_INSTANCE_NOT_AVAILABLE );
958 959 960
            release_object( pipe );
            return;
        }
961 962 963
        if ((pipe->maxinstances != req->maxinstances) ||
            (pipe->timeout != req->timeout) ||
            (pipe->flags != req->flags))
964 965 966 967 968
        {
            set_error( STATUS_ACCESS_DENIED );
            release_object( pipe );
            return;
        }
969
        clear_error(); /* clear the name collision */
970
    }
971

972
    server = create_pipe_server( pipe, req->options );
973
    if (server)
974
    {
975
        reply->handle = alloc_handle( current->process, server, req->access, req->attributes );
976 977
        server->pipe->instances++;
        release_object( server );
978 979 980 981 982
    }

    release_object( pipe );
}

983 984
DECL_HANDLER(get_named_pipe_info)
{
985
    struct pipe_server *server;
986
    struct pipe_client *client = NULL;
987

988
    server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
989
    if (!server)
990 991 992 993 994 995 996
    {
        clear_error();
        client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
                                                       FILE_READ_ATTRIBUTES, &pipe_client_ops );
        if (!client) return;
        server = client->server;
    }
997

998
    reply->flags        = server->pipe->flags;
999
    reply->maxinstances = server->pipe->maxinstances;
1000
    reply->instances    = server->pipe->instances;
1001 1002
    reply->insize       = server->pipe->insize;
    reply->outsize      = server->pipe->outsize;
1003

1004 1005 1006 1007 1008 1009 1010
    if (client)
        release_object(client);
    else
    {
        reply->flags |= NAMED_PIPE_SERVER_END;
        release_object(server);
    }
1011
}