named_pipe.c 48.4 KB
Newer Older
1 2 3 4
/*
 * Server-side pipe management
 *
 * Copyright (C) 1998 Alexandre Julliard
5
 * Copyright (C) 2001 Mike McCormack
6
 * Copyright 2016 Jacek Caban for CodeWeavers
7
 *
8 9 10 11 12 13 14 15 16 17 18 19
 * 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
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 22 23
 */

#include "config.h"
24
#include "wine/port.h"
25 26 27

#include <assert.h>
#include <string.h>
28
#include <stdarg.h>
29 30 31
#include <stdio.h>
#include <stdlib.h>

32 33
#include "ntstatus.h"
#define WIN32_NO_STATUS
34
#include "windef.h"
35
#include "winternl.h"
36
#include "winioctl.h"
37

38
#include "file.h"
39 40 41
#include "handle.h"
#include "thread.h"
#include "request.h"
42
#include "security.h"
43
#include "process.h"
44 45 46

struct named_pipe;

47 48 49 50 51 52 53 54
struct pipe_message
{
    struct list          entry;      /* entry in message queue */
    data_size_t          read_pos;   /* already read bytes */
    struct iosb         *iosb;       /* message iosb */
    struct async        *async;      /* async of pending write */
};

55
struct pipe_end
56
{
57 58
    struct object        obj;        /* object header */
    struct fd           *fd;         /* pipe file descriptor */
59
    unsigned int         flags;      /* pipe flags */
60
    unsigned int         state;      /* pipe state */
61
    struct named_pipe   *pipe;
62
    struct pipe_end     *connection; /* the other end of the pipe */
63 64
    process_id_t         client_pid; /* process that created the client */
    process_id_t         server_pid; /* process that created the server */
65
    data_size_t          buffer_size;/* size of buffered data that doesn't block caller */
66
    struct list          message_queue;
67 68
    struct async_queue   read_q;     /* read queue */
    struct async_queue   write_q;    /* write queue */
69 70 71 72
};

struct pipe_server
{
73
    struct pipe_end      pipe_end;   /* common header for both pipe ends */
74
    struct list          entry;      /* entry in named pipe listeners list */
75
    unsigned int         options;    /* pipe options */
76
    struct async_queue   listen_q;   /* listen queue */
77 78
};

79 80 81
struct named_pipe
{
    struct object       obj;         /* object header */
82
    int                 message_mode;
83
    unsigned int        sharing;
84 85 86
    unsigned int        maxinstances;
    unsigned int        outsize;
    unsigned int        insize;
87
    unsigned int        instances;
88
    timeout_t           timeout;
89
    struct list         listeners;   /* list of servers listening on this pipe */
90
    struct async_queue  waiters;     /* list of clients waiting to connect */
91 92
};

93 94 95 96 97 98
struct named_pipe_device
{
    struct object       obj;         /* object header */
    struct namespace   *pipes;       /* named pipe namespace */
};

99 100 101 102 103 104 105
struct named_pipe_device_file
{
    struct object             obj;         /* object header */
    struct fd                *fd;          /* pseudo-fd for ioctls */
    struct named_pipe_device *device;      /* named pipe device */
};

106
static void named_pipe_dump( struct object *obj, int verbose );
107
static unsigned int named_pipe_map_access( struct object *obj, unsigned int access );
108
static int named_pipe_link_name( struct object *obj, struct object_name *name, struct object *parent );
109 110
static struct object *named_pipe_open_file( struct object *obj, unsigned int access,
                                            unsigned int sharing, unsigned int options );
111
static void named_pipe_destroy( struct object *obj );
112 113 114 115 116

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

136
/* common server and client pipe end functions */
137
static void pipe_end_destroy( struct object *obj );
138
static enum server_fd_type pipe_end_get_fd_type( struct fd *fd );
139
static struct fd *pipe_end_get_fd( struct object *obj );
140 141 142
static struct security_descriptor *pipe_end_get_sd( struct object *obj );
static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
                            unsigned int set_info );
143
static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos );
144
static int pipe_end_write( struct fd *fd, struct async *async_data, file_pos_t pos );
145
static int pipe_end_flush( struct fd *fd, struct async *async );
146
static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class );
147
static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue );
148
static void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class );
149

150 151 152
/* server end functions */
static void pipe_server_dump( struct object *obj, int verbose );
static void pipe_server_destroy( struct object *obj);
153
static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
154 155 156 157 158

static const struct object_ops pipe_server_ops =
{
    sizeof(struct pipe_server),   /* size */
    pipe_server_dump,             /* dump */
159
    file_get_type,                /* get_type */
160 161
    add_queue,                    /* add_queue */
    remove_queue,                 /* remove_queue */
162 163
    default_fd_signaled,          /* signaled */
    no_satisfied,                 /* satisfied */
164
    no_signal,                    /* signal */
165
    pipe_end_get_fd,              /* get_fd */
166
    default_fd_map_access,        /* map_access */
167 168
    pipe_end_get_sd,              /* get_sd */
    pipe_end_set_sd,              /* set_sd */
169
    no_lookup_name,               /* lookup_name */
170 171
    no_link_name,                 /* link_name */
    NULL,                         /* unlink_name */
172
    no_open_file,                 /* open_file */
173
    no_kernel_obj_list,           /* get_kernel_obj_list */
174
    fd_close_handle,              /* close_handle */
175 176 177 178 179
    pipe_server_destroy           /* destroy */
};

static const struct fd_ops pipe_server_fd_ops =
{
180
    default_fd_get_poll_events,   /* get_poll_events */
181
    default_poll_event,           /* poll_event */
182
    pipe_end_get_fd_type,         /* get_fd_type */
183
    pipe_end_read,                /* read */
184
    pipe_end_write,               /* write */
185
    pipe_end_flush,               /* flush */
186
    pipe_end_get_file_info,       /* get_file_info */
187
    pipe_end_get_volume_info,     /* get_volume_info */
188
    pipe_server_ioctl,            /* ioctl */
189
    no_fd_queue_async,            /* queue_async */
190
    pipe_end_reselect_async       /* reselect_async */
191
};
192

193 194
/* client end functions */
static void pipe_client_dump( struct object *obj, int verbose );
195
static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
196

197
static const struct object_ops pipe_client_ops =
198
{
199
    sizeof(struct pipe_end),      /* size */
200
    pipe_client_dump,             /* dump */
201
    file_get_type,                /* get_type */
202 203
    add_queue,                    /* add_queue */
    remove_queue,                 /* remove_queue */
204
    default_fd_signaled,          /* signaled */
205
    no_satisfied,                 /* satisfied */
206
    no_signal,                    /* signal */
207
    pipe_end_get_fd,              /* get_fd */
208
    default_fd_map_access,        /* map_access */
209 210
    pipe_end_get_sd,              /* get_sd */
    pipe_end_set_sd,              /* set_sd */
211
    no_lookup_name,               /* lookup_name */
212 213
    no_link_name,                 /* link_name */
    NULL,                         /* unlink_name */
214
    no_open_file,                 /* open_file */
215
    no_kernel_obj_list,           /* get_kernel_obj_list */
216
    fd_close_handle,              /* close_handle */
217
    pipe_end_destroy              /* destroy */
218 219
};

220
static const struct fd_ops pipe_client_fd_ops =
221
{
222
    default_fd_get_poll_events,   /* get_poll_events */
223
    default_poll_event,           /* poll_event */
224
    pipe_end_get_fd_type,         /* get_fd_type */
225
    pipe_end_read,                /* read */
226
    pipe_end_write,               /* write */
227
    pipe_end_flush,               /* flush */
228
    pipe_end_get_file_info,       /* get_file_info */
229
    pipe_end_get_volume_info,     /* get_volume_info */
230
    pipe_client_ioctl,            /* ioctl */
231
    no_fd_queue_async,            /* queue_async */
232
    pipe_end_reselect_async       /* reselect_async */
233 234
};

235
static void named_pipe_device_dump( struct object *obj, int verbose );
236
static struct object_type *named_pipe_device_get_type( struct object *obj );
237 238
static struct object *named_pipe_device_lookup_name( struct object *obj,
    struct unicode_str *name, unsigned int attr );
239 240
static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
                                                   unsigned int sharing, unsigned int options );
241 242 243 244 245 246
static void named_pipe_device_destroy( struct object *obj );

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
    no_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
    no_kernel_obj_list,               /* get_kernel_obj_list */
262
    no_close_handle,                  /* close_handle */
263 264 265
    named_pipe_device_destroy         /* destroy */
};

266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
static void named_pipe_device_file_dump( struct object *obj, int verbose );
static struct fd *named_pipe_device_file_get_fd( struct object *obj );
static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async );
static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd );
static void named_pipe_device_file_destroy( struct object *obj );

static const struct object_ops named_pipe_device_file_ops =
{
    sizeof(struct named_pipe_device_file),   /* size */
    named_pipe_device_file_dump,             /* dump */
    no_get_type,                             /* get_type */
    add_queue,                               /* add_queue */
    remove_queue,                            /* remove_queue */
    default_fd_signaled,                     /* signaled */
    no_satisfied,                            /* satisfied */
    no_signal,                               /* signal */
    named_pipe_device_file_get_fd,           /* get_fd */
    default_fd_map_access,                   /* map_access */
    default_get_sd,                          /* get_sd */
    default_set_sd,                          /* set_sd */
    no_lookup_name,                          /* lookup_name */
    no_link_name,                            /* link_name */
    NULL,                                    /* unlink_name */
    no_open_file,                            /* open_file */
290
    no_kernel_obj_list,                      /* get_kernel_obj_list */
291 292 293 294
    fd_close_handle,                         /* close_handle */
    named_pipe_device_file_destroy           /* destroy */
};

295 296
static const struct fd_ops named_pipe_device_fd_ops =
{
297 298 299 300 301 302 303 304 305 306 307
    default_fd_get_poll_events,              /* get_poll_events */
    default_poll_event,                      /* poll_event */
    named_pipe_device_file_get_fd_type,      /* get_fd_type */
    no_fd_read,                              /* read */
    no_fd_write,                             /* write */
    no_fd_flush,                             /* flush */
    default_fd_get_file_info,                /* get_file_info */
    no_fd_get_volume_info,                   /* get_volume_info */
    named_pipe_device_ioctl,                 /* ioctl */
    default_fd_queue_async,                  /* queue_async */
    default_fd_reselect_async                /* reselect_async */
308 309
};

310 311
static void named_pipe_dump( struct object *obj, int verbose )
{
312
    fputs( "Named pipe\n", stderr );
313 314
}

315 316 317 318 319 320 321 322 323
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);
}

324 325 326 327
static void pipe_server_dump( struct object *obj, int verbose )
{
    struct pipe_server *server = (struct pipe_server *) obj;
    assert( obj->ops == &pipe_server_ops );
328 329
    fprintf( stderr, "Named pipe server pipe=%p state=%d\n", server->pipe_end.pipe,
             server->pipe_end.state );
330 331 332
}

static void pipe_client_dump( struct object *obj, int verbose )
333
{
334
    struct pipe_end *client = (struct pipe_end *) obj;
335
    assert( obj->ops == &pipe_client_ops );
336
    fprintf( stderr, "Named pipe client server=%p\n", client->connection );
337 338
}

339 340 341 342
static void named_pipe_destroy( struct object *obj)
{
    struct named_pipe *pipe = (struct named_pipe *) obj;

343
    assert( list_empty( &pipe->listeners ) );
344
    assert( !pipe->instances );
345
    free_async_queue( &pipe->waiters );
346 347
}

348
static struct fd *pipe_end_get_fd( struct object *obj )
349
{
350 351
    struct pipe_end *pipe_end = (struct pipe_end *) obj;
    return (struct fd *) grab_object( pipe_end->fd );
352 353
}

354 355 356 357 358 359 360 361 362 363 364 365
static struct pipe_message *queue_message( struct pipe_end *pipe_end, struct iosb *iosb )
{
    struct pipe_message *message;

    if (!(message = mem_alloc( sizeof(*message) ))) return NULL;
    message->iosb = (struct iosb *)grab_object( iosb );
    message->async = NULL;
    message->read_pos = 0;
    list_add_tail( &pipe_end->message_queue, &message->entry );
    return message;
}

366
static void wake_message( struct pipe_message *message, data_size_t result )
367 368 369 370
{
    struct async *async = message->async;

    message->async = NULL;
371 372
    if (!async) return;

373
    message->iosb->status = STATUS_SUCCESS;
374
    message->iosb->result = result;
375 376
    async_terminate( async, message->iosb->result ? STATUS_ALERTED : STATUS_SUCCESS );
    release_object( async );
377 378
}

379 380 381 382 383 384 385
static void free_message( struct pipe_message *message )
{
    list_remove( &message->entry );
    if (message->iosb) release_object( message->iosb );
    free( message );
}

386 387 388
static void pipe_end_disconnect( struct pipe_end *pipe_end, unsigned int status )
{
    struct pipe_end *connection = pipe_end->connection;
389 390
    struct pipe_message *message, *next;
    struct async *async;
391 392 393

    pipe_end->connection = NULL;

394 395
    pipe_end->state = status == STATUS_PIPE_DISCONNECTED
        ? FILE_PIPE_DISCONNECTED_STATE : FILE_PIPE_CLOSING_STATE;
396
    fd_async_wake_up( pipe_end->fd, ASYNC_TYPE_WAIT, status );
397 398
    async_wake_up( &pipe_end->read_q, status );
    LIST_FOR_EACH_ENTRY_SAFE( message, next, &pipe_end->message_queue, struct pipe_message, entry )
399
    {
400 401 402 403 404
        async = message->async;
        if (async || status == STATUS_PIPE_DISCONNECTED) free_message( message );
        if (!async) continue;
        async_terminate( async, status );
        release_object( async );
405
    }
406 407
    if (status == STATUS_PIPE_DISCONNECTED) set_fd_signaled( pipe_end->fd, 0 );

408 409 410 411 412 413 414
    if (connection)
    {
        connection->connection = NULL;
        pipe_end_disconnect( connection, status );
    }
}

415
static void pipe_end_destroy( struct object *obj )
416
{
417
    struct pipe_end *pipe_end = (struct pipe_end *)obj;
418 419
    struct pipe_message *message;

420 421
    pipe_end_disconnect( pipe_end, STATUS_PIPE_BROKEN );

422 423 424 425 426 427
    while (!list_empty( &pipe_end->message_queue ))
    {
        message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
        assert( !message->async );
        free_message( message );
    }
428

429 430
    free_async_queue( &pipe_end->read_q );
    free_async_queue( &pipe_end->write_q );
431
    if (pipe_end->fd) release_object( pipe_end->fd );
432
    if (pipe_end->pipe) release_object( pipe_end->pipe );
433 434
}

435
static void pipe_server_destroy( struct object *obj )
436 437
{
    struct pipe_server *server = (struct pipe_server *)obj;
438
    struct named_pipe *pipe = server->pipe_end.pipe;
439 440 441

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

442 443
    assert( pipe->instances );
    if (!--pipe->instances) unlink_named_object( &pipe->obj );
444 445
    if (server->pipe_end.state == FILE_PIPE_LISTENING_STATE)
        list_remove( &server->entry );
446

447
    free_async_queue( &server->listen_q );
448
    pipe_end_destroy( obj );
449 450
}

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

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

463 464 465 466 467 468 469 470 471
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 );

472 473
    if (!name) return NULL;  /* open the device itself */

474 475 476 477 478 479
    if ((found = find_object( device->pipes, name, attr | OBJ_CASE_INSENSITIVE )))
        name->len = 0;

    return found;
}

480 481 482
static struct object *named_pipe_device_open_file( struct object *obj, unsigned int access,
                                                   unsigned int sharing, unsigned int options )
{
483 484 485 486 487 488 489 490 491 492 493
    struct named_pipe_device_file *file;

    if (!(file = alloc_object( &named_pipe_device_file_ops ))) return NULL;
    file->device = (struct named_pipe_device *)grab_object( obj );
    if (!(file->fd = alloc_pseudo_fd( &named_pipe_device_fd_ops, obj, options )))
    {
        release_object( file );
        return NULL;
    }
    allow_fd_caching( file->fd );
    return &file->obj;
494 495
}

496 497 498 499
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 );
500
    free( device->pipes );
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->pipes = create_namespace( 7 )))
512 513 514 515 516
        {
            release_object( dev );
            dev = NULL;
        }
    }
517
    return &dev->obj;
518 519
}

520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
static void named_pipe_device_file_dump( struct object *obj, int verbose )
{
    struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;

    fprintf( stderr, "File on named pipe device %p\n", file->device );
}

static struct fd *named_pipe_device_file_get_fd( struct object *obj )
{
    struct named_pipe_device_file *file = (struct named_pipe_device_file *)obj;
    return (struct fd *)grab_object( file->fd );
}

static enum server_fd_type named_pipe_device_file_get_fd_type( struct fd *fd )
{
    return FD_TYPE_DEVICE;
}

static void named_pipe_device_file_destroy( struct object *obj )
{
    struct named_pipe_device_file *file = (struct named_pipe_device_file*)obj;
    assert( obj->ops == &named_pipe_device_file_ops );
    if (file->fd) release_object( file->fd );
    release_object( file->device );
}

546
static int pipe_end_flush( struct fd *fd, struct async *async )
547
{
548
    struct pipe_end *pipe_end = get_fd_user( fd );
549

550 551 552 553 554 555
    if (!pipe_end->pipe)
    {
        set_error( STATUS_PIPE_DISCONNECTED );
        return 0;
    }

556
    if (pipe_end->connection && !list_empty( &pipe_end->connection->message_queue ))
557
    {
558 559
        fd_queue_async( pipe_end->fd, async, ASYNC_TYPE_WAIT );
        set_error( STATUS_PENDING );
560
    }
561
    return 1;
562 563
}

564
static void pipe_end_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
565
{
566 567 568
    struct pipe_end *pipe_end = get_fd_user( fd );
    struct named_pipe *pipe = pipe_end->pipe;

569 570 571 572 573 574 575 576 577 578 579 580 581 582
    switch (info_class)
    {
    case FileNameInformation:
        {
            FILE_NAME_INFORMATION *name_info;
            data_size_t name_len, reply_size;
            const WCHAR *name;

            if (get_reply_max_size() < sizeof(*name_info))
            {
                set_error( STATUS_INFO_LENGTH_MISMATCH );
                return;
            }

583
            /* FIXME: We should be able to return on unlinked pipe */
584
            if (!pipe || !(name = get_object_name( &pipe->obj, &name_len )))
585 586 587 588
            {
                set_error( STATUS_PIPE_DISCONNECTED );
                return;
            }
589

590 591 592 593 594 595 596 597 598 599 600 601 602 603
            reply_size = offsetof( FILE_NAME_INFORMATION, FileName[name_len/sizeof(WCHAR) + 1] );
            if (reply_size > get_reply_max_size())
            {
                reply_size = get_reply_max_size();
                set_error( STATUS_BUFFER_OVERFLOW );
            }

            if (!(name_info = set_reply_data_size( reply_size ))) return;
            name_info->FileNameLength = name_len + sizeof(WCHAR);
            name_info->FileName[0] = '\\';
            reply_size -= offsetof( FILE_NAME_INFORMATION, FileName[1] );
            if (reply_size) memcpy( &name_info->FileName[1], name, reply_size );
            break;
        }
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
    case FilePipeInformation:
    {
            FILE_PIPE_INFORMATION *pipe_info;

            if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
            {
                set_error( STATUS_ACCESS_DENIED );
                return;
            }

            if (get_reply_max_size() < sizeof(*pipe_info))
            {
                set_error( STATUS_INFO_LENGTH_MISMATCH );
                return;
            }

620 621 622 623 624 625
            if (!pipe)
            {
                set_error( STATUS_PIPE_DISCONNECTED );
                return;
            }

626 627 628 629 630 631 632
            if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
            pipe_info->ReadMode       = (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
                ? FILE_PIPE_MESSAGE_MODE : FILE_PIPE_BYTE_STREAM_MODE;
            pipe_info->CompletionMode = (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE)
                ? FILE_PIPE_COMPLETE_OPERATION : FILE_PIPE_QUEUE_OPERATION;
            break;
        }
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    case FilePipeLocalInformation:
        {
            FILE_PIPE_LOCAL_INFORMATION *pipe_info;

            if (!(get_handle_access( current->process, handle) & FILE_READ_ATTRIBUTES))
            {
                set_error( STATUS_ACCESS_DENIED );
                return;
            }

            if (get_reply_max_size() < sizeof(*pipe_info))
            {
                set_error( STATUS_INFO_LENGTH_MISMATCH );
                return;
            }

649 650 651 652 653 654
            if (!pipe)
            {
                set_error( STATUS_PIPE_DISCONNECTED );
                return;
            }

655
            if (!(pipe_info = set_reply_data_size( sizeof(*pipe_info) ))) return;
656
            pipe_info->NamedPipeType = pipe->message_mode;
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674
            switch (pipe->sharing)
            {
            case FILE_SHARE_READ:
                pipe_info->NamedPipeConfiguration = FILE_PIPE_OUTBOUND;
                break;
            case FILE_SHARE_WRITE:
                pipe_info->NamedPipeConfiguration = FILE_PIPE_INBOUND;
                break;
            case FILE_SHARE_READ | FILE_SHARE_WRITE:
                pipe_info->NamedPipeConfiguration = FILE_PIPE_FULL_DUPLEX;
                break;
            }
            pipe_info->MaximumInstances    = pipe->maxinstances;
            pipe_info->CurrentInstances    = pipe->instances;
            pipe_info->InboundQuota        = pipe->insize;
            pipe_info->ReadDataAvailable   = 0; /* FIXME */
            pipe_info->OutboundQuota       = pipe->outsize;
            pipe_info->WriteQuotaAvailable = 0; /* FIXME */
675
            pipe_info->NamedPipeState      = pipe_end->state;
676 677 678 679
            pipe_info->NamedPipeEnd        = pipe_end->obj.ops == &pipe_server_ops
                ? FILE_PIPE_SERVER_END : FILE_PIPE_CLIENT_END;
            break;
        }
680
    default:
681
        default_fd_get_file_info( fd, handle, info_class );
682 683 684
    }
}

685
static struct security_descriptor *pipe_end_get_sd( struct object *obj )
686
{
687 688
    struct pipe_end *pipe_end = (struct pipe_end *) obj;
    if (pipe_end->pipe) return default_get_sd( &pipe_end->pipe->obj );
689 690 691 692
    set_error( STATUS_PIPE_DISCONNECTED );
    return NULL;
}

693 694
static int pipe_end_set_sd( struct object *obj, const struct security_descriptor *sd,
                            unsigned int set_info )
695
{
696 697
    struct pipe_end *pipe_end = (struct pipe_end *) obj;
    if (pipe_end->pipe) return default_set_sd( &pipe_end->pipe->obj, sd, set_info );
698 699 700 701
    set_error( STATUS_PIPE_DISCONNECTED );
    return 0;
}

702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
static void pipe_end_get_volume_info( struct fd *fd, unsigned int info_class )
{
    switch (info_class)
    {
    case FileFsDeviceInformation:
        {
            static const FILE_FS_DEVICE_INFORMATION device_info =
            {
                FILE_DEVICE_NAMED_PIPE,
                FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL
            };
            if (get_reply_max_size() >= sizeof(device_info))
                set_reply_data( &device_info, sizeof(device_info) );
            else
                set_error( STATUS_BUFFER_TOO_SMALL );
            break;
        }
    default:
        set_error( STATUS_NOT_IMPLEMENTED );
    }
}

724 725 726 727
static void message_queue_read( struct pipe_end *pipe_end, struct iosb *iosb )
{
    struct pipe_message *message;

728
    if (pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ)
729
    {
730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
        message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
        iosb->out_size = min( iosb->out_size, message->iosb->in_size - message->read_pos );
        iosb->status = message->read_pos + iosb->out_size < message->iosb->in_size
            ? STATUS_BUFFER_OVERFLOW : STATUS_SUCCESS;
    }
    else
    {
        data_size_t avail = 0;
        LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
        {
            avail += message->iosb->in_size - message->read_pos;
            if (avail >= iosb->out_size) break;
        }
        iosb->out_size = min( iosb->out_size, avail );
        iosb->status = STATUS_SUCCESS;
745 746 747 748 749 750 751
    }

    message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
    if (!message->read_pos && message->iosb->in_size == iosb->out_size) /* fast path */
    {
        iosb->out_data = message->iosb->in_data;
        message->iosb->in_data = NULL;
752
        wake_message( message, message->iosb->in_size );
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
        free_message( message );
    }
    else
    {
        data_size_t write_pos = 0, writing;
        char *buf = NULL;

        if (iosb->out_size && !(buf = iosb->out_data = malloc( iosb->out_size )))
        {
            iosb->out_size = 0;
            iosb->status = STATUS_NO_MEMORY;
            return;
        }

        do
        {
            message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
            writing = min( iosb->out_size - write_pos, message->iosb->in_size - message->read_pos );
            if (writing) memcpy( buf + write_pos, (const char *)message->iosb->in_data + message->read_pos, writing );
            write_pos += writing;
            message->read_pos += writing;
            if (message->read_pos == message->iosb->in_size)
            {
776
                wake_message(message, message->iosb->in_size);
777 778 779 780 781 782 783
                free_message(message);
            }
        } while (write_pos < iosb->out_size);
    }
    iosb->result = iosb->out_size;
}

784 785 786 787
/* We call async_terminate in our reselect implementation, which causes recursive reselect.
 * We're not interested in such reselect calls, so we ignore them. */
static int ignore_reselect;

788 789
static void reselect_write_queue( struct pipe_end *pipe_end );

790
static void reselect_read_queue( struct pipe_end *pipe_end, int reselect_write )
791 792 793 794 795
{
    struct async *async;
    struct iosb *iosb;

    ignore_reselect = 1;
796
    while (!list_empty( &pipe_end->message_queue ) && (async = find_pending_async( &pipe_end->read_q )))
797 798 799 800 801 802
    {
        iosb = async_get_iosb( async );
        message_queue_read( pipe_end, iosb );
        async_terminate( async, iosb->result ? STATUS_ALERTED : iosb->status );
        release_object( async );
        release_object( iosb );
803
        reselect_write = 1;
804 805 806 807 808 809 810
    }
    ignore_reselect = 0;

    if (pipe_end->connection)
    {
        if (list_empty( &pipe_end->message_queue ))
            fd_async_wake_up( pipe_end->connection->fd, ASYNC_TYPE_WAIT, STATUS_SUCCESS );
811
        else if (reselect_write)
812 813 814 815
            reselect_write_queue( pipe_end->connection );
    }
}

816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
static void reselect_write_queue( struct pipe_end *pipe_end )
{
    struct pipe_message *message, *next;
    struct pipe_end *reader = pipe_end->connection;
    data_size_t avail = 0;

    if (!reader) return;

    ignore_reselect = 1;

    LIST_FOR_EACH_ENTRY_SAFE( message, next, &reader->message_queue, struct pipe_message, entry )
    {
        if (message->async && message->iosb->status != STATUS_PENDING)
        {
            release_object( message->async );
            message->async = NULL;
            free_message( message );
        }
        else
        {
            avail += message->iosb->in_size - message->read_pos;
837
            if (message->async && (avail <= reader->buffer_size || !message->iosb->in_size))
838 839 840 841 842 843 844 845
            {
                wake_message( message, message->iosb->in_size );
            }
            else if (message->async && (pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE))
            {
                wake_message( message, message->read_pos );
                free_message( message );
            }
846 847 848 849
        }
    }

    ignore_reselect = 0;
850
    reselect_read_queue( reader, 0 );
851 852
}

853
static int pipe_end_read( struct fd *fd, struct async *async, file_pos_t pos )
854 855 856
{
    struct pipe_end *pipe_end = get_fd_user( fd );

857
    switch (pipe_end->state)
858
    {
859
    case FILE_PIPE_CONNECTED_STATE:
860 861 862 863 864
        if ((pipe_end->flags & NAMED_PIPE_NONBLOCKING_MODE) && list_empty( &pipe_end->message_queue ))
        {
            set_error( STATUS_PIPE_EMPTY );
            return 0;
        }
865 866 867 868 869 870 871 872 873
        break;
    case FILE_PIPE_DISCONNECTED_STATE:
        set_error( STATUS_PIPE_DISCONNECTED );
        return 0;
    case FILE_PIPE_LISTENING_STATE:
        set_error( STATUS_PIPE_LISTENING );
        return 0;
    case FILE_PIPE_CLOSING_STATE:
        if (!list_empty( &pipe_end->message_queue )) break;
874 875 876 877
        set_error( STATUS_PIPE_BROKEN );
        return 0;
    }

878
    queue_async( &pipe_end->read_q, async );
879
    reselect_read_queue( pipe_end, 0 );
880
    set_error( STATUS_PENDING );
881
    return 1;
882 883
}

884
static int pipe_end_write( struct fd *fd, struct async *async, file_pos_t pos )
885
{
886
    struct pipe_end *pipe_end = get_fd_user( fd );
887
    struct pipe_message *message;
888
    struct iosb *iosb;
889

890
    switch (pipe_end->state)
891
    {
892 893 894
    case FILE_PIPE_CONNECTED_STATE:
        break;
    case FILE_PIPE_DISCONNECTED_STATE:
895 896
        set_error( STATUS_PIPE_DISCONNECTED );
        return 0;
897 898 899 900 901 902
    case FILE_PIPE_LISTENING_STATE:
        set_error( STATUS_PIPE_LISTENING );
        return 0;
    case FILE_PIPE_CLOSING_STATE:
        set_error( STATUS_PIPE_CLOSING );
        return 0;
903 904
    }

905
    if (!pipe_end->pipe->message_mode && !get_req_data_size()) return 1;
906

907 908 909 910
    iosb = async_get_iosb( async );
    message = queue_message( pipe_end->connection, iosb );
    release_object( iosb );
    if (!message) return 0;
911

912 913
    message->async = (struct async *)grab_object( async );
    queue_async( &pipe_end->write_q, async );
914
    reselect_read_queue( pipe_end->connection, 1 );
915
    set_error( STATUS_PENDING );
916
    return 1;
917 918
}

919 920 921 922 923 924
static void pipe_end_reselect_async( struct fd *fd, struct async_queue *queue )
{
    struct pipe_end *pipe_end = get_fd_user( fd );

    if (ignore_reselect) return;

925
    if (&pipe_end->write_q == queue)
926
        reselect_write_queue( pipe_end );
927
    else if (&pipe_end->read_q == queue)
928
        reselect_read_queue( pipe_end, 0 );
929 930
}

931
static enum server_fd_type pipe_end_get_fd_type( struct fd *fd )
932
{
933
    return FD_TYPE_PIPE;
934 935
}

936
static int pipe_end_peek( struct pipe_end *pipe_end )
937 938 939 940 941
{
    unsigned reply_size = get_reply_max_size();
    FILE_PIPE_PEEK_BUFFER *buffer;
    struct pipe_message *message;
    data_size_t avail = 0;
942
    data_size_t message_length = 0;
943 944 945 946

    if (reply_size < offsetof( FILE_PIPE_PEEK_BUFFER, Data ))
    {
        set_error( STATUS_INFO_LENGTH_MISMATCH );
947
        return 0;
948 949 950
    }
    reply_size -= offsetof( FILE_PIPE_PEEK_BUFFER, Data );

951
    switch (pipe_end->state)
952
    {
953 954 955 956
    case FILE_PIPE_CONNECTED_STATE:
        break;
    case FILE_PIPE_CLOSING_STATE:
        if (!list_empty( &pipe_end->message_queue )) break;
957 958
        set_error( STATUS_PIPE_BROKEN );
        return 0;
959
    default:
960
        set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
961
        return 0;
962 963
    }

964 965
    LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
        avail += message->iosb->in_size - message->read_pos;
966
    reply_size = min( reply_size, avail );
967

968
    if (avail && pipe_end->pipe->message_mode)
969 970
    {
        message = LIST_ENTRY( list_head(&pipe_end->message_queue), struct pipe_message, entry );
971 972
        message_length = message->iosb->in_size - message->read_pos;
        reply_size = min( reply_size, message_length );
973 974
    }

975
    if (!(buffer = set_reply_data_size( offsetof( FILE_PIPE_PEEK_BUFFER, Data[reply_size] )))) return 0;
976
    buffer->NamedPipeState    = pipe_end->state;
977 978
    buffer->ReadDataAvailable = avail;
    buffer->NumberOfMessages  = 0;  /* FIXME */
979
    buffer->MessageLength     = message_length;
980 981 982 983 984 985 986 987 988 989 990 991 992

    if (reply_size)
    {
        data_size_t write_pos = 0, writing;
        LIST_FOR_EACH_ENTRY( message, &pipe_end->message_queue, struct pipe_message, entry )
        {
            writing = min( reply_size - write_pos, message->iosb->in_size - message->read_pos );
            memcpy( buffer->Data + write_pos, (const char *)message->iosb->in_data + message->read_pos,
                    writing );
            write_pos += writing;
            if (write_pos == reply_size) break;
        }
    }
993
    if (message_length > reply_size) set_error( STATUS_BUFFER_OVERFLOW );
994
    return 1;
995 996
}

997 998 999 1000 1001
static int pipe_end_transceive( struct pipe_end *pipe_end, struct async *async )
{
    struct pipe_message *message;
    struct iosb *iosb;

1002
    if (!pipe_end->connection)
1003
    {
1004
        set_error( pipe_end->pipe ? STATUS_INVALID_PIPE_STATE : STATUS_PIPE_DISCONNECTED );
1005 1006 1007
        return 0;
    }

1008
    if (!(pipe_end->flags & NAMED_PIPE_MESSAGE_STREAM_READ))
1009
    {
1010
        set_error( STATUS_INVALID_READ_MODE );
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
        return 0;
    }

    /* not allowed if we already have read data buffered */
    if (!list_empty( &pipe_end->message_queue ))
    {
        set_error( STATUS_PIPE_BUSY );
        return 0;
    }

    iosb = async_get_iosb( async );
    /* ignore output buffer copy transferred because of METHOD_NEITHER */
    iosb->in_size -= iosb->out_size;
    /* transaction never blocks on write, so just queue a message without async */
    message = queue_message( pipe_end->connection, iosb );
    release_object( iosb );
    if (!message) return 0;
1028
    reselect_read_queue( pipe_end->connection, 0 );
1029 1030

    queue_async( &pipe_end->read_q, async );
1031
    reselect_read_queue( pipe_end, 0 );
1032 1033 1034 1035
    set_error( STATUS_PENDING );
    return 1;
}

1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
static int pipe_end_get_connection_attribute( struct pipe_end *pipe_end )
{
    const char *attr = get_req_data();
    data_size_t value_size, attr_size = get_req_data_size();
    void *value;

    if (attr_size == sizeof("ClientProcessId") && !memcmp( attr, "ClientProcessId", attr_size ))
    {
        value = &pipe_end->client_pid;
        value_size = sizeof(pipe_end->client_pid);
    }
    else if (attr_size == sizeof("ServerProcessId") && !memcmp( attr, "ServerProcessId", attr_size ))
    {
        value = &pipe_end->server_pid;
        value_size = sizeof(pipe_end->server_pid);
    }
    else
    {
        set_error( STATUS_ILLEGAL_FUNCTION );
        return 0;
    }

    if (get_reply_max_size() < value_size)
    {
        set_error( STATUS_INFO_LENGTH_MISMATCH );
        return 0;
    }

    set_reply_data( value, value_size );
    return 1;
}

1068 1069 1070 1071
static int pipe_end_ioctl( struct pipe_end *pipe_end, ioctl_code_t code, struct async *async )
{
    switch(code)
    {
1072 1073 1074
    case FSCTL_PIPE_GET_CONNECTION_ATTRIBUTE:
        return pipe_end_get_connection_attribute( pipe_end );

1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
    case FSCTL_PIPE_PEEK:
        return pipe_end_peek( pipe_end );

    case FSCTL_PIPE_TRANSCEIVE:
        return pipe_end_transceive( pipe_end, async );

    default:
        return default_fd_ioctl( pipe_end->fd, code, async );
    }
}

1086
static int pipe_server_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1087 1088 1089 1090 1091
{
    struct pipe_server *server = get_fd_user( fd );

    switch(code)
    {
1092
    case FSCTL_PIPE_LISTEN:
1093
        switch(server->pipe_end.state)
1094
        {
1095
        case FILE_PIPE_LISTENING_STATE:
1096
            break;
1097
        case FILE_PIPE_DISCONNECTED_STATE:
1098 1099
            server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
            list_add_tail( &server->pipe_end.pipe->listeners, &server->entry );
1100
            break;
1101 1102 1103 1104 1105 1106
        case FILE_PIPE_CONNECTED_STATE:
            set_error( STATUS_PIPE_CONNECTED );
            return 0;
        case FILE_PIPE_CLOSING_STATE:
            set_error( STATUS_PIPE_CLOSING );
            return 0;
1107
        }
1108

1109 1110 1111 1112 1113
        if (server->pipe_end.flags & NAMED_PIPE_NONBLOCKING_MODE)
        {
            set_error( STATUS_PIPE_LISTENING );
            return 0;
        }
1114
        queue_async( &server->listen_q, async );
1115 1116 1117
        async_wake_up( &server->pipe_end.pipe->waiters, STATUS_SUCCESS );
        set_error( STATUS_PENDING );
        return 1;
1118

1119
    case FSCTL_PIPE_DISCONNECT:
1120
        switch(server->pipe_end.state)
1121
        {
1122
        case FILE_PIPE_CONNECTED_STATE:
1123
            /* dump the client connection - all data is lost */
1124
            assert( server->pipe_end.connection );
1125 1126
            release_object( server->pipe_end.connection->pipe );
            server->pipe_end.connection->pipe = NULL;
1127
            break;
1128
        case FILE_PIPE_CLOSING_STATE:
1129
            break;
1130
        case FILE_PIPE_LISTENING_STATE:
1131
            set_error( STATUS_PIPE_LISTENING );
1132
            return 0;
1133
        case FILE_PIPE_DISCONNECTED_STATE:
1134
            set_error( STATUS_PIPE_DISCONNECTED );
1135
            return 0;
1136
        }
1137 1138

        pipe_end_disconnect( &server->pipe_end, STATUS_PIPE_DISCONNECTED );
1139
        return 1;
1140

1141
    default:
1142
        return pipe_end_ioctl( &server->pipe_end, code, async );
1143 1144 1145
    }
}

1146
static int pipe_client_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1147
{
1148
    struct pipe_end *client = get_fd_user( fd );
1149 1150 1151

    switch(code)
    {
1152 1153 1154 1155
    case FSCTL_PIPE_LISTEN:
        set_error( STATUS_ILLEGAL_FUNCTION );
        return 0;

1156
    default:
1157
        return pipe_end_ioctl( client, code, async );
1158 1159 1160
    }
}

1161 1162
static void init_pipe_end( struct pipe_end *pipe_end, struct named_pipe *pipe,
                           unsigned int pipe_flags, data_size_t buffer_size )
1163
{
1164
    pipe_end->pipe = (struct named_pipe *)grab_object( pipe );
1165 1166
    pipe_end->fd = NULL;
    pipe_end->flags = pipe_flags;
1167
    pipe_end->connection = NULL;
1168
    pipe_end->buffer_size = buffer_size;
1169 1170
    init_async_queue( &pipe_end->read_q );
    init_async_queue( &pipe_end->write_q );
1171
    list_init( &pipe_end->message_queue );
1172 1173
}

1174 1175
static struct pipe_server *create_pipe_server( struct named_pipe *pipe, unsigned int options,
                                               unsigned int pipe_flags )
1176
{
1177
    struct pipe_server *server;
1178

1179
    server = alloc_object( &pipe_server_ops );
1180
    if (!server)
1181 1182
        return NULL;

1183
    server->options = options;
1184
    init_pipe_end( &server->pipe_end, pipe, pipe_flags, pipe->insize );
1185
    server->pipe_end.state = FILE_PIPE_LISTENING_STATE;
1186
    server->pipe_end.server_pid = get_process_id( current->process );
1187
    init_async_queue( &server->listen_q );
1188

1189
    list_add_tail( &pipe->listeners, &server->entry );
1190
    if (!(server->pipe_end.fd = alloc_pseudo_fd( &pipe_server_fd_ops, &server->pipe_end.obj, options )))
1191 1192
    {
        release_object( server );
1193
        return NULL;
1194
    }
1195
    allow_fd_caching( server->pipe_end.fd );
1196
    set_fd_signaled( server->pipe_end.fd, 1 );
1197
    async_wake_up( &pipe->waiters, STATUS_SUCCESS );
1198
    return server;
1199 1200
}

1201
static struct pipe_end *create_pipe_client( struct named_pipe *pipe, data_size_t buffer_size, unsigned int options )
1202
{
1203
    struct pipe_end *client;
1204

1205
    client = alloc_object( &pipe_client_ops );
1206
    if (!client)
1207 1208
        return NULL;

1209 1210 1211
    init_pipe_end( client, pipe, 0, buffer_size );
    client->state = FILE_PIPE_CONNECTED_STATE;
    client->client_pid = get_process_id( current->process );
1212

1213 1214
    client->fd = alloc_pseudo_fd( &pipe_client_fd_ops, &client->obj, options );
    if (!client->fd)
1215 1216 1217 1218
    {
        release_object( client );
        return NULL;
    }
1219 1220
    allow_fd_caching( client->fd );
    set_fd_signaled( client->fd, 1 );
1221

1222 1223 1224
    return client;
}

1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
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;
}

1239 1240 1241 1242 1243
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;
1244
    struct pipe_end *client;
1245
    unsigned int pipe_sharing;
1246

1247
    if (list_empty( &pipe->listeners ))
1248 1249 1250 1251
    {
        set_error( STATUS_PIPE_NOT_AVAILABLE );
        return NULL;
    }
1252
    server = LIST_ENTRY( list_head( &pipe->listeners ), struct pipe_server, entry );
1253

1254
    pipe_sharing = pipe->sharing;
1255 1256 1257 1258 1259 1260 1261
    if (((access & GENERIC_READ) && !(pipe_sharing & FILE_SHARE_READ)) ||
        ((access & GENERIC_WRITE) && !(pipe_sharing & FILE_SHARE_WRITE)))
    {
        set_error( STATUS_ACCESS_DENIED );
        return NULL;
    }

1262
    if ((client = create_pipe_client( pipe, pipe->outsize, options )))
1263
    {
1264
        async_wake_up( &server->listen_q, STATUS_SUCCESS );
1265
        server->pipe_end.state = FILE_PIPE_CONNECTED_STATE;
1266 1267 1268 1269
        server->pipe_end.connection = client;
        client->connection = &server->pipe_end;
        server->pipe_end.client_pid = client->client_pid;
        client->server_pid = server->pipe_end.server_pid;
1270
        list_remove( &server->entry );
1271
    }
1272
    return &client->obj;
1273 1274
}

1275
static int named_pipe_device_ioctl( struct fd *fd, ioctl_code_t code, struct async *async )
1276 1277 1278 1279 1280 1281 1282
{
    struct named_pipe_device *device = get_fd_user( fd );

    switch(code)
    {
    case FSCTL_PIPE_WAIT:
        {
1283 1284
            const FILE_PIPE_WAIT_FOR_BUFFER *buffer = get_req_data();
            data_size_t size = get_req_data_size();
1285 1286
            struct named_pipe *pipe;
            struct unicode_str name;
1287
            timeout_t when;
1288 1289 1290 1291 1292

            if (size < sizeof(*buffer) ||
                size < FIELD_OFFSET(FILE_PIPE_WAIT_FOR_BUFFER, Name[buffer->NameLength/sizeof(WCHAR)]))
            {
                set_error( STATUS_INVALID_PARAMETER );
1293
                return 0;
1294 1295 1296
            }
            name.str = buffer->Name;
            name.len = (buffer->NameLength / sizeof(WCHAR)) * sizeof(WCHAR);
1297 1298
            if (!(pipe = open_named_object( &device->obj, &named_pipe_ops, &name, 0 ))) return 0;

1299
            if (list_empty( &pipe->listeners ))
1300
            {
1301
                queue_async( &pipe->waiters, async );
1302 1303 1304
                when = buffer->TimeoutSpecified ? buffer->Timeout.QuadPart : pipe->timeout;
                async_set_timeout( async, when, STATUS_IO_TIMEOUT );
                set_error( STATUS_PENDING );
1305 1306 1307
            }

            release_object( pipe );
1308
            return 1;
1309 1310 1311
        }

    default:
1312
        return default_fd_ioctl( fd, code, async );
1313 1314 1315 1316
    }
}


1317 1318 1319
DECL_HANDLER(create_named_pipe)
{
    struct named_pipe *pipe;
1320
    struct pipe_server *server;
1321
    struct unicode_str name;
1322
    struct object *root;
1323
    const struct security_descriptor *sd;
1324
    const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
1325 1326

    if (!objattr) return;
1327

1328 1329
    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)))
1330
    {
1331
        if (root) release_object( root );
1332 1333 1334 1335
        set_error( STATUS_INVALID_PARAMETER );
        return;
    }

1336 1337 1338 1339 1340 1341 1342
    if (!name.len)  /* pipes need a root directory even without a name */
    {
        if (!objattr->rootdir)
        {
            set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
            return;
        }
1343
        if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
1344
    }
1345

1346
    pipe = create_named_object( root, &named_pipe_ops, &name, objattr->attributes | OBJ_OPENIF, NULL );
1347 1348 1349

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

1351
    if (get_error() != STATUS_OBJECT_NAME_EXISTS)
1352
    {
1353 1354
        /* initialize it if it didn't already exist */
        pipe->instances = 0;
1355
        init_async_queue( &pipe->waiters );
1356
        list_init( &pipe->listeners );
1357 1358 1359 1360
        pipe->insize = req->insize;
        pipe->outsize = req->outsize;
        pipe->maxinstances = req->maxinstances;
        pipe->timeout = req->timeout;
1361
        pipe->message_mode = (req->flags & NAMED_PIPE_MESSAGE_STREAM_WRITE) != 0;
1362
        pipe->sharing = req->sharing;
1363 1364 1365 1366
        if (sd) default_set_sd( &pipe->obj, sd, OWNER_SECURITY_INFORMATION |
                                                GROUP_SECURITY_INFORMATION |
                                                DACL_SECURITY_INFORMATION |
                                                SACL_SECURITY_INFORMATION );
1367
    }
1368 1369
    else
    {
1370
        if (pipe->maxinstances <= pipe->instances)
1371
        {
1372
            set_error( STATUS_INSTANCE_NOT_AVAILABLE );
1373 1374 1375
            release_object( pipe );
            return;
        }
1376
        if (pipe->sharing != req->sharing)
1377 1378 1379 1380 1381
        {
            set_error( STATUS_ACCESS_DENIED );
            release_object( pipe );
            return;
        }
1382
        clear_error(); /* clear the name collision */
1383
    }
1384

1385
    server = create_pipe_server( pipe, req->options, req->flags );
1386
    if (server)
1387
    {
1388
        reply->handle = alloc_handle( current->process, server, req->access, objattr->attributes );
1389
        pipe->instances++;
1390
        release_object( server );
1391 1392 1393 1394 1395
    }

    release_object( pipe );
}

1396 1397
DECL_HANDLER(set_named_pipe_info)
{
1398
    struct pipe_end *pipe_end;
1399

1400 1401 1402
    pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
                                                  FILE_WRITE_ATTRIBUTES, &pipe_server_ops );
    if (!pipe_end)
1403 1404 1405 1406 1407
    {
        if (get_error() != STATUS_OBJECT_TYPE_MISMATCH)
            return;

        clear_error();
1408 1409 1410
        pipe_end = (struct pipe_end *)get_handle_obj( current->process, req->handle,
                                                      0, &pipe_client_ops );
        if (!pipe_end) return;
1411 1412
    }

1413 1414 1415 1416 1417
    if (!pipe_end->pipe)
    {
        set_error( STATUS_PIPE_DISCONNECTED );
    }
    else if ((req->flags & ~(NAMED_PIPE_MESSAGE_STREAM_READ | NAMED_PIPE_NONBLOCKING_MODE)) ||
1418
            ((req->flags & NAMED_PIPE_MESSAGE_STREAM_READ) && !pipe_end->pipe->message_mode))
1419 1420 1421 1422 1423
    {
        set_error( STATUS_INVALID_PARAMETER );
    }
    else
    {
1424
        pipe_end->flags = req->flags;
1425 1426
    }

1427
    release_object( pipe_end );
1428
}