named_pipe.c 34 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
#include "handle.h"
#include "thread.h"
#include "request.h"
55
#include "security.h"
56

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

67 68
struct named_pipe;

69 70
struct pipe_server
{
71 72
    struct object        obj;        /* object header */
    struct fd           *fd;         /* pipe file descriptor */
73
    struct fd           *ioctl_fd;   /* file descriptor for ioctls when not connected */
74 75 76
    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 */
77 78
    struct named_pipe   *pipe;
    struct timeout_user *flush_poll;
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
static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent );
116 117
static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
                                            unsigned int sharing, unsigned int options );
118
static void named_pipe_destroy( struct object *obj );
119 120 121 122 123

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

142 143 144 145
/* 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);
146
static obj_handle_t pipe_server_flush( struct fd *fd, const async_data_t *async, int blocking );
147
static enum server_fd_type pipe_server_get_fd_type( struct fd *fd );
148
static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async,
149
                                       int blocking );
150 151 152 153 154

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

static const struct fd_ops pipe_server_fd_ops =
{
175
    default_fd_get_poll_events,   /* get_poll_events */
176
    default_poll_event,           /* poll_event */
177
    pipe_server_get_fd_type,      /* get_fd_type */
178 179 180
    no_fd_read,                   /* read */
    no_fd_write,                  /* write */
    pipe_server_flush,            /* flush */
181
    pipe_server_ioctl,            /* ioctl */
182
    default_fd_queue_async,       /* queue_async */
183
    default_fd_reselect_async,    /* reselect_async */
184
    default_fd_cancel_async,      /* cancel_async */
185
};
186

187 188
/* client end functions */
static void pipe_client_dump( struct object *obj, int verbose );
189
static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry );
190 191
static struct fd *pipe_client_get_fd( struct object *obj );
static void pipe_client_destroy( struct object *obj );
192
static obj_handle_t pipe_client_flush( struct fd *fd, const async_data_t *async, int blocking );
193
static enum server_fd_type pipe_client_get_fd_type( struct fd *fd );
194

195
static const struct object_ops pipe_client_ops =
196
{
197 198
    sizeof(struct pipe_client),   /* size */
    pipe_client_dump,             /* dump */
199
    no_get_type,                  /* get_type */
200 201
    add_queue,                    /* add_queue */
    remove_queue,                 /* remove_queue */
202
    pipe_client_signaled,         /* signaled */
203
    no_satisfied,                 /* satisfied */
204
    no_signal,                    /* signal */
205
    pipe_client_get_fd,           /* get_fd */
206
    default_fd_map_access,        /* map_access */
207 208
    default_get_sd,               /* get_sd */
    default_set_sd,               /* set_sd */
209
    no_lookup_name,               /* lookup_name */
210 211
    no_link_name,                 /* link_name */
    NULL,                         /* unlink_name */
212
    no_open_file,                 /* open_file */
213
    fd_close_handle,              /* close_handle */
214
    pipe_client_destroy           /* destroy */
215 216
};

217
static const struct fd_ops pipe_client_fd_ops =
218
{
219
    default_fd_get_poll_events,   /* get_poll_events */
220
    default_poll_event,           /* poll_event */
221
    pipe_client_get_fd_type,      /* get_fd_type */
222 223 224
    no_fd_read,                   /* read */
    no_fd_write,                  /* write */
    pipe_client_flush,            /* flush */
225
    default_fd_ioctl,             /* ioctl */
226
    default_fd_queue_async,       /* queue_async */
227
    default_fd_reselect_async,    /* reselect_async */
228
    default_fd_cancel_async       /* cancel_async */
229 230
};

231
static void named_pipe_device_dump( struct object *obj, int verbose );
232
static struct object_type *named_pipe_device_get_type( struct object *obj );
233
static struct fd *named_pipe_device_get_fd( struct object *obj );
234 235
static struct object *named_pipe_device_lookup_name( struct object *obj,
    struct unicode_str *name, unsigned int attr );
236 237
static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
                                                   unsigned int sharing, unsigned int options );
238
static void named_pipe_device_destroy( struct object *obj );
239
static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd );
240 241
static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code,
                                             const async_data_t *async_data, int blocking );
242 243 244 245 246

static const struct object_ops named_pipe_device_ops =
{
    sizeof(struct named_pipe_device), /* size */
    named_pipe_device_dump,           /* dump */
247
    named_pipe_device_get_type,       /* get_type */
248 249 250 251 252
    no_add_queue,                     /* add_queue */
    NULL,                             /* remove_queue */
    NULL,                             /* signaled */
    no_satisfied,                     /* satisfied */
    no_signal,                        /* signal */
253
    named_pipe_device_get_fd,         /* get_fd */
254
    no_map_access,                    /* map_access */
255 256
    default_get_sd,                   /* get_sd */
    default_set_sd,                   /* set_sd */
257
    named_pipe_device_lookup_name,    /* lookup_name */
258 259
    directory_link_name,              /* link_name */
    default_unlink_name,              /* unlink_name */
260
    named_pipe_device_open_file,      /* open_file */
261
    fd_close_handle,                  /* close_handle */
262 263 264
    named_pipe_device_destroy         /* destroy */
};

265 266
static const struct fd_ops named_pipe_device_fd_ops =
{
267 268
    default_fd_get_poll_events,       /* get_poll_events */
    default_poll_event,               /* poll_event */
269
    named_pipe_device_get_fd_type,    /* get_fd_type */
270 271 272
    no_fd_read,                       /* read */
    no_fd_write,                      /* write */
    no_fd_flush,                      /* flush */
273
    named_pipe_device_ioctl,          /* ioctl */
274
    default_fd_queue_async,           /* queue_async */
275
    default_fd_reselect_async,        /* reselect_async */
276
    default_fd_cancel_async           /* cancel_async */
277 278
};

279 280
static void named_pipe_dump( struct object *obj, int verbose )
{
281
    fputs( "Named pipe\n", stderr );
282 283
}

284 285 286 287 288 289 290 291 292
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);
}

293 294 295 296
static void pipe_server_dump( struct object *obj, int verbose )
{
    struct pipe_server *server = (struct pipe_server *) obj;
    assert( obj->ops == &pipe_server_ops );
297
    fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe, server->state );
298 299 300
}

static void pipe_client_dump( struct object *obj, int verbose )
301
{
302
    struct pipe_client *client = (struct pipe_client *) obj;
303 304
    assert( obj->ops == &pipe_client_ops );
    fprintf( stderr, "Named pipe client server=%p\n", client->server );
305 306
}

307
static int pipe_client_signaled( struct object *obj, struct wait_queue_entry *entry )
308 309 310 311 312 313
{
    struct pipe_client *client = (struct pipe_client *) obj;

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

314 315 316 317 318 319
static void named_pipe_destroy( struct object *obj)
{
    struct named_pipe *pipe = (struct named_pipe *) obj;

    assert( list_empty( &pipe->servers ) );
    assert( !pipe->instances );
320
    free_async_queue( pipe->waiters );
321 322 323 324 325
}

static struct fd *pipe_client_get_fd( struct object *obj )
{
    struct pipe_client *client = (struct pipe_client *) obj;
326
    if (client->fd)
327
        return (struct fd *) grab_object( client->fd );
328 329
    set_error( STATUS_PIPE_DISCONNECTED );
    return NULL;
330 331
}

332
static void set_server_state( struct pipe_server *server, enum pipe_state state )
333
{
334
    server->state = state;
335

336
    switch(state)
337 338 339 340
    {
    case ps_connected_server:
    case ps_wait_disconnect:
        assert( server->fd );
341
        break;
342 343
    case ps_wait_open:
    case ps_idle_server:
344 345
        assert( !server->fd );
        set_no_fd_status( server->ioctl_fd, STATUS_PIPE_LISTENING );
346 347 348
        break;
    case ps_disconnected_server:
    case ps_wait_connect:
349 350
        assert( !server->fd );
        set_no_fd_status( server->ioctl_fd, STATUS_PIPE_DISCONNECTED );
351 352
        break;
    }
353 354 355 356 357 358 359
}

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 );
360 361 362 363 364
}


static void notify_empty( struct pipe_server *server )
{
365
    if (!server->flush_poll)
366 367 368 369
        return;
    assert( server->state == ps_connected_server );
    remove_timeout_user( server->flush_poll );
    server->flush_poll = NULL;
370
    fd_async_wake_up( server->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
371 372 373 374 375
}

static void do_disconnect( struct pipe_server *server )
{
    /* we may only have a server fd, if the client disconnected */
376
    if (server->client)
377 378 379 380 381 382 383
    {
        assert( server->client->server == server );
        assert( server->client->fd );
        release_object( server->client->fd );
        server->client->fd = NULL;
    }
    assert( server->fd );
384
    shutdown( get_unix_fd( server->fd ), SHUT_RDWR );
385 386 387 388 389 390 391 392 393 394
    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 );

395
    if (server->fd)
396 397 398 399 400
    {
        notify_empty( server );
        do_disconnect( server );
    }

401
    if (server->client)
402 403 404 405 406 407 408 409
    {
        server->client->server = NULL;
        server->client = NULL;
    }

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

410
    if (server->ioctl_fd) release_object( server->ioctl_fd );
411
    list_remove( &server->entry );
412 413 414 415
    release_object( server->pipe );
}

static void pipe_client_destroy( struct object *obj)
416
{
417 418
    struct pipe_client *client = (struct pipe_client *)obj;
    struct pipe_server *server = client->server;
419

420
    assert( obj->ops == &pipe_client_ops );
421

422
    if (server)
423
    {
424 425
        notify_empty( server );

426
        switch(server->state)
427 428
        {
        case ps_connected_server:
429 430
            /* Don't destroy the server's fd here as we can't
               do a successful flush without it. */
431
            set_server_state( server, ps_wait_disconnect );
432
            break;
433
        case ps_disconnected_server:
434
            set_server_state( server, ps_wait_connect );
435
            break;
436 437 438 439
        case ps_idle_server:
        case ps_wait_open:
        case ps_wait_disconnect:
        case ps_wait_connect:
440
            assert( 0 );
441
        }
442 443 444
        assert( server->client );
        server->client = NULL;
        client->server = NULL;
445
    }
446
    if (client->fd) release_object( client->fd );
447 448
}

449 450
static void named_pipe_device_dump( struct object *obj, int verbose )
{
451
    fputs( "Named pipe device\n", stderr );
452 453
}

454 455 456 457 458 459 460
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 );
}

461 462 463
static struct fd *named_pipe_device_get_fd( struct object *obj )
{
    struct named_pipe_device *device = (struct named_pipe_device *)obj;
464
    return (struct fd *)grab_object( device->fd );
465 466
}

467 468 469 470 471 472 473 474 475
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 );

476 477
    if (!name) return NULL;  /* open the device itself */

478 479 480 481 482 483
    if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
        name->len = 0;

    return found;
}

484 485 486 487 488 489
static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
                                                   unsigned int sharing, unsigned int options )
{
    return grab_object( obj );
}

490 491 492 493
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 );
494
    if (device->fd) release_object( device->fd );
495
    free( device->pipes );
496 497
}

498
static enum server_fd_type named_pipe_device_get_fd_type( struct fd *fd )
499
{
500
    return FD_TYPE_DEVICE;
501 502
}

503
struct object *create_named_pipe_device( struct object *root, const struct unicode_str *name )
504 505 506
{
    struct named_pipe_device *dev;

507
    if ((dev = create_named_object( root, &named_pipe_device_ops, name, 0, NULL )) &&
508 509
        get_error() != STATUS_OBJECT_NAME_EXISTS)
    {
510
        dev->pipes = NULL;
511
        if (!(dev->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, &dev->obj, 0 )) ||
512
            !(dev->pipes = create_namespace( 7 )))
513 514 515 516 517
        {
            release_object( dev );
            dev = NULL;
        }
    }
518
    return &dev->obj;
519 520
}

521 522 523 524 525 526 527 528
static int pipe_data_remaining( struct pipe_server *server )
{
    struct pollfd pfd;
    int fd;

    assert( server->client );

    fd = get_unix_fd( server->client->fd );
529
    if (fd < 0)
530 531 532 533 534
        return 0;
    pfd.fd = fd;
    pfd.events = POLLIN;
    pfd.revents = 0;

535
    if (0 > poll( &pfd, 1, 0 ))
536 537 538 539 540 541 542 543 544
        return 0;
 
    return pfd.revents&POLLIN;
}

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

545
    if (pipe_data_remaining( server ))
546
    {
547
        server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
548 549
    }
    else
550 551
    {
        server->flush_poll = NULL;
552
        fd_async_wake_up( server->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
553
    }
554 555
}

556
static obj_handle_t pipe_server_flush( struct fd *fd, const async_data_t *async_data, int blocking )
557 558
{
    struct pipe_server *server = get_fd_user( fd );
559
    obj_handle_t handle = 0;
560
    struct async *async;
561

562
    if (!server || server->state != ps_connected_server) return 0;
563

564
    if (!pipe_data_remaining( server )) return 0;
565

566
    if ((async = fd_queue_async( server->fd, async_data, ASYNC_TYPE_WAIT )))
567
    {
568 569 570 571
        /* there's no unix way to be alerted when a pipe becomes empty, so resort to polling */
        if (!server->flush_poll)
            server->flush_poll = add_timeout_user( -TICKS_PER_SEC / 10, check_flushed, server );
        if (blocking) handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
572
        release_object( async );
573
        set_error( STATUS_PENDING );
574
    }
575
    return handle;
576 577
}

578
static obj_handle_t pipe_client_flush( struct fd *fd, const async_data_t *async, int blocking )
579 580
{
    /* FIXME: what do we have to do for this? */
581
    return 0;
582 583
}

584
static inline int is_overlapped( unsigned int options )
585
{
586 587 588
    return !(options & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT));
}

589
static enum server_fd_type pipe_server_get_fd_type( struct fd *fd )
590
{
591
    return FD_TYPE_PIPE;
592 593
}

594
static enum server_fd_type pipe_client_get_fd_type( struct fd *fd )
595
{
596
    return FD_TYPE_PIPE;
597 598
}

599
static obj_handle_t pipe_server_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
600
                                       int blocking )
601 602
{
    struct pipe_server *server = get_fd_user( fd );
603
    struct async *async;
604
    obj_handle_t wait_handle = 0;
605 606 607

    switch(code)
    {
608 609 610 611 612
    case FSCTL_PIPE_LISTEN:
        switch(server->state)
        {
        case ps_idle_server:
        case ps_wait_connect:
613
            if ((async = fd_queue_async( server->ioctl_fd, async_data, ASYNC_TYPE_WAIT )))
614
            {
615
                if (blocking) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
616
                set_server_state( server, ps_wait_open );
617 618 619
                if (server->pipe->waiters) async_wake_up( server->pipe->waiters, STATUS_SUCCESS );
                release_object( async );
                set_error( STATUS_PENDING );
620
                return wait_handle;
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
            }
            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;
        }
636
        return 0;
637

638 639 640 641 642 643 644 645 646 647 648 649
    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 );
650
            set_server_state( server, ps_disconnected_server );
651 652 653 654
            break;
        case ps_wait_disconnect:
            assert( !server->client );
            do_disconnect( server );
655
            set_server_state( server, ps_wait_connect );
656 657 658
            break;
        case ps_idle_server:
        case ps_wait_open:
659 660
            set_error( STATUS_PIPE_LISTENING );
            break;
661 662
        case ps_disconnected_server:
        case ps_wait_connect:
663
            set_error( STATUS_PIPE_DISCONNECTED );
664 665
            break;
        }
666
        return 0;
667

668
    default:
669
        return default_fd_ioctl( fd, code, async_data, blocking );
670 671 672
    }
}

673 674
static struct pipe_server *get_pipe_server_obj( struct process *process,
                                obj_handle_t handle, unsigned int access )
675
{
676 677 678
    struct object *obj;
    obj = get_handle_obj( process, handle, access, &pipe_server_ops );
    return (struct pipe_server *) obj;
679 680
}

681 682
static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
                                               unsigned int pipe_flags )
683
{
684
    struct pipe_server *server;
685

686
    server = alloc_object( &pipe_server_ops );
687
    if (!server)
688 689
        return NULL;

690 691 692 693
    server->fd = NULL;
    server->pipe = pipe;
    server->client = NULL;
    server->flush_poll = NULL;
694
    server->options = options;
695
    server->pipe_flags = pipe_flags;
696

697
    list_add_head( &pipe->servers, &server->entry );
698
    grab_object( pipe );
699
    if (!(server->ioctl_fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->obj, options )))
700 701
    {
        release_object( server );
702
        return NULL;
703 704
    }
    set_server_state( server, ps_idle_server );
705
    return server;
706 707
}

708
static struct pipe_client *create_pipe_client( unsigned int flags, unsigned int pipe_flags )
709
{
710
    struct pipe_client *client;
711

712
    client = alloc_object( &pipe_client_ops );
713
    if (!client)
714 715 716
        return NULL;

    client->fd = NULL;
717
    client->server = NULL;
718
    client->flags = flags;
719
    client->pipe_flags = pipe_flags;
720 721 722 723

    return client;
}

724
static struct pipe_server *find_available_server( struct named_pipe *pipe )
725
{
726
    struct pipe_server *server;
727

728
    /* look for pipe servers that are listening */
729 730
    LIST_FOR_EACH_ENTRY( server, &pipe->servers, struct pipe_server, entry )
    {
731
        if (server->state == ps_wait_open)
732 733
            return (struct pipe_server *)grab_object( server );
    }
734 735 736 737 738 739 740 741

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

742
    return NULL;
743 744
}

745 746 747 748 749 750 751 752 753 754 755 756 757 758
static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent )
{
    struct named_pipe_device *dev = (struct named_pipe_device *)parent;

    if (parent->ops != &named_pipe_device_ops)
    {
        set_error( STATUS_OBJECT_NAME_INVALID );
        return 0;
    }
    namespace_add( dev->pipes, name );
    name->parent = grab_object( parent );
    return 1;
}

759 760 761 762 763 764
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;
765
    unsigned int pipe_sharing;
766 767
    int fds[2];

768
    if (!(server = find_available_server( pipe )))
769 770 771 772 773
    {
        set_error( STATUS_PIPE_NOT_AVAILABLE );
        return NULL;
    }

774 775 776 777 778 779 780 781 782
    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;
    }

783
    if ((client = create_pipe_client( options, pipe->flags )))
784 785 786 787 788 789 790 791
    {
        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 */
792 793
            if (is_overlapped( options )) fcntl( fds[1], F_SETFL, O_NONBLOCK );
            if (is_overlapped( server->options )) fcntl( fds[0], F_SETFL, O_NONBLOCK );
794 795 796 797 798 799 800 801 802 803 804 805

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

806 807
            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 );
808
            if (client->fd && server->fd)
809
            {
810 811
                allow_fd_caching( client->fd );
                allow_fd_caching( server->fd );
812
                fd_copy_completion( server->ioctl_fd, server->fd );
813
                if (server->state == ps_wait_open)
814
                    fd_async_wake_up( server->ioctl_fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
815
                set_server_state( server, ps_connected_server );
816 817 818
                server->client = client;
                client->server = server;
            }
819 820 821 822 823
            else
            {
                release_object( client );
                client = NULL;
            }
824 825
        }
        else
826
        {
827
            file_set_error();
828 829 830
            release_object( client );
            client = NULL;
        }
831 832 833 834 835
    }
    release_object( server );
    return &client->obj;
}

836 837
static obj_handle_t named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code,
                                             const async_data_t *async_data, int blocking )
838 839 840 841 842 843 844
{
    struct named_pipe_device *device = get_fd_user( fd );

    switch(code)
    {
    case FSCTL_PIPE_WAIT:
        {
845 846
            const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
            data_size_t size = get_req_data_size();
847
            obj_handle_t wait_handle = 0;
848 849 850 851 852 853 854 855
            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 );
856
                return 0;
857 858 859
            }
            name.str = buffer->Name;
            name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
860 861
            if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;

862 863 864 865
            if (!(server = find_available_server( pipe )))
            {
                struct async *async;

866 867
                if (!pipe->waiters && !(pipe->waiters = create_async_queue( NULL ))) goto done;

868
                if ((async = create_async( current, pipe->waiters, async_data )))
869 870 871
                {
                    timeout_t when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
                    async_set_timeout( async, when, STATUS_IO_TIMEOUT );
872
                    if (blocking) wait_handle = alloc_handle( current->process, async, SYNCHRONIZE, 0 );
873 874 875 876 877 878
                    release_object( async );
                    set_error( STATUS_PENDING );
                }
            }
            else release_object( server );

879
        done:
880
            release_object( pipe );
881
            return wait_handle;
882 883 884
        }

    default:
885
        return default_fd_ioctl( fd, code, async_data, blocking );
886 887 888 889
    }
}


890 891 892
DECL_HANDLER(create_named_pipe)
{
    struct named_pipe *pipe;
893
    struct pipe_server *server;
894
    struct unicode_str name;
895
    struct object *root;
896
    const struct security_descriptor *sd;
897
    const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
898 899

    if (!objattr) return;
900

901 902
    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)))
903
    {
904
        if (root) release_object( root );
905 906 907 908
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }

909 910 911 912 913 914 915
    if (!name.len)  /* pipes need a root directory even without a name */
    {
        if (!objattr->rootdir)
        {
            set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
            return;
        }
916
        if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
917
    }
918

919
    pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
920 921 922

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

924
    if (get_error() != STATUS_OBJECT_NAME_EXISTS)
925
    {
926 927
        /* initialize it if it didn't already exist */
        pipe->instances = 0;
928
        pipe->waiters = NULL;
929
        list_init( &pipe->servers );
930 931 932 933
        pipe->insize = req->insize;
        pipe->outsize = req->outsize;
        pipe->maxinstances = req->maxinstances;
        pipe->timeout = req->timeout;
934
        pipe->flags = req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE;
935
        pipe->sharing = req->sharing;
936
    }
937 938
    else
    {
939
        if (pipe->maxinstances <= pipe->instances)
940
        {
941
            set_error( STATUS_INSTANCE_NOT_AVAILABLE );
942 943 944
            release_object( pipe );
            return;
        }
945
        if (pipe->sharing != req->sharing)
946 947 948 949 950
        {
            set_error( STATUS_ACCESS_DENIED );
            release_object( pipe );
            return;
        }
951
        clear_error(); /* clear the name collision */
952
    }
953

954
    server = create_pipe_server( pipe, req->options, req->flags );
955
    if (server)
956
    {
957
        reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
958
        server->pipe->instances++;
959 960 961 962
        if (sd) default_set_sd( &server->obj, sd, OWNER_SECURITY_INFORMATION |
                                                  GROUP_SECURITY_INFORMATION |
                                                  DACL_SECURITY_INFORMATION |
                                                  SACL_SECURITY_INFORMATION );
963
        release_object( server );
964 965 966 967 968
    }

    release_object( pipe );
}

969 970
DECL_HANDLER(get_named_pipe_info)
{
971
    struct pipe_server *server;
972
    struct pipe_client *client = NULL;
973

974
    server = get_pipe_server_obj( current->process, req->handle, FILE_READ_ATTRIBUTES );
975
    if (!server)
976
    {
977 978 979
        if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
            return;

980 981
        clear_error();
        client = (struct pipe_client *)get_handle_obj( current->process, req->handle,
982
                                                       0, &pipe_client_ops );
983 984 985
        if (!client) return;
        server = client->server;
    }
986

987
    reply->flags        = client ? client->pipe_flags : server->pipe_flags;
988 989 990 991 992 993 994 995
    if (server)
    {
        reply->sharing      = server->pipe->sharing;
        reply->maxinstances = server->pipe->maxinstances;
        reply->instances    = server->pipe->instances;
        reply->insize       = server->pipe->insize;
        reply->outsize      = server->pipe->outsize;
    }
996

997 998 999 1000 1001 1002 1003
    if (client)
        release_object(client);
    else
    {
        reply->flags |= NAMED_PIPE_SERVER_END;
        release_object(server);
    }
1004
}
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

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;
1021 1022 1023 1024 1025
        if (!(server = client->server))
        {
            release_object( client );
            return;
        }
1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
    }

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