mailslot.c 17.9 KB
Newer Older
Mike McCormack's avatar
Mike McCormack committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Server-side mailslot management
 *
 * Copyright (C) 1998 Alexandre Julliard
 * Copyright (C) 2005 Mike McCormack
 *
 * 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
Mike McCormack's avatar
Mike McCormack committed
20 21 22 23 24
 *
 */

#include "config.h"
#include "wine/port.h"
25 26
#include "ntstatus.h"
#define WIN32_NO_STATUS
Mike McCormack's avatar
Mike McCormack committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include "wine/unicode.h"

#include <assert.h>
#include <fcntl.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>

#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
44 45 46
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
Mike McCormack's avatar
Mike McCormack committed
47
#include "windef.h"
48
#include "winternl.h"
Mike McCormack's avatar
Mike McCormack committed
49 50 51 52 53 54 55 56 57 58

#include "file.h"
#include "handle.h"
#include "thread.h"
#include "request.h"

struct mailslot
{
    struct object       obj;
    struct fd          *fd;
59
    int                 write_fd;
Mike McCormack's avatar
Mike McCormack committed
60
    unsigned int        max_msgsize;
61
    timeout_t           read_timeout;
Mike McCormack's avatar
Mike McCormack committed
62 63 64 65 66 67
    struct list         writers;
};

/* mailslot functions */
static void mailslot_dump( struct object*, int );
static struct fd *mailslot_get_fd( struct object * );
68
static unsigned int mailslot_map_access( struct object *obj, unsigned int access );
69
static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent );
70 71
static struct object *mailslot_open_file( struct object *obj, unsigned int access,
                                          unsigned int sharing, unsigned int options );
Mike McCormack's avatar
Mike McCormack committed
72 73 74 75 76 77
static void mailslot_destroy( struct object * );

static const struct object_ops mailslot_ops =
{
    sizeof(struct mailslot),   /* size */
    mailslot_dump,             /* dump */
78
    file_get_type,             /* get_type */
79 80
    add_queue,                 /* add_queue */
    remove_queue,              /* remove_queue */
Mike McCormack's avatar
Mike McCormack committed
81 82
    default_fd_signaled,       /* signaled */
    no_satisfied,              /* satisfied */
83
    no_signal,                 /* signal */
Mike McCormack's avatar
Mike McCormack committed
84
    mailslot_get_fd,           /* get_fd */
85
    mailslot_map_access,       /* map_access */
86 87
    default_get_sd,            /* get_sd */
    default_set_sd,            /* set_sd */
88
    no_lookup_name,            /* lookup_name */
89 90
    mailslot_link_name,        /* link_name */
    default_unlink_name,       /* unlink_name */
91
    mailslot_open_file,        /* open_file */
92
    no_kernel_obj_list,        /* get_kernel_obj_list */
93
    fd_close_handle,           /* close_handle */
Mike McCormack's avatar
Mike McCormack committed
94 95 96
    mailslot_destroy           /* destroy */
};

97
static enum server_fd_type mailslot_get_fd_type( struct fd *fd );
98
static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count );
Mike McCormack's avatar
Mike McCormack committed
99 100 101

static const struct fd_ops mailslot_fd_ops =
{
102 103
    default_fd_get_poll_events, /* get_poll_events */
    default_poll_event,         /* poll_event */
104
    mailslot_get_fd_type,       /* get_fd_type */
105 106 107
    no_fd_read,                 /* read */
    no_fd_write,                /* write */
    no_fd_flush,                /* flush */
108
    default_fd_get_file_info,   /* get_file_info */
109
    no_fd_get_volume_info,      /* get_volume_info */
110
    default_fd_ioctl,           /* ioctl */
111
    mailslot_queue_async,       /* queue_async */
112
    default_fd_reselect_async   /* reselect_async */
Mike McCormack's avatar
Mike McCormack committed
113 114
};

115

Mike McCormack's avatar
Mike McCormack committed
116 117 118
struct mail_writer
{
    struct object         obj;
119
    struct fd            *fd;
Mike McCormack's avatar
Mike McCormack committed
120 121
    struct mailslot      *mailslot;
    struct list           entry;
122 123
    unsigned int          access;
    unsigned int          sharing;
Mike McCormack's avatar
Mike McCormack committed
124 125 126 127
};

static void mail_writer_dump( struct object *obj, int verbose );
static struct fd *mail_writer_get_fd( struct object *obj );
128
static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
Mike McCormack's avatar
Mike McCormack committed
129 130 131 132 133 134
static void mail_writer_destroy( struct object *obj);

static const struct object_ops mail_writer_ops =
{
    sizeof(struct mail_writer), /* size */
    mail_writer_dump,           /* dump */
135
    file_get_type,              /* get_type */
Mike McCormack's avatar
Mike McCormack committed
136 137 138 139
    no_add_queue,               /* add_queue */
    NULL,                       /* remove_queue */
    NULL,                       /* signaled */
    NULL,                       /* satisfied */
140
    no_signal,                  /* signal */
Mike McCormack's avatar
Mike McCormack committed
141
    mail_writer_get_fd,         /* get_fd */
142
    mail_writer_map_access,     /* map_access */
143 144
    default_get_sd,             /* get_sd */
    default_set_sd,             /* set_sd */
145
    no_lookup_name,             /* lookup_name */
146 147
    no_link_name,               /* link_name */
    NULL,                       /* unlink_name */
148
    no_open_file,               /* open_file */
149
    no_kernel_obj_list,         /* get_kernel_obj_list */
150
    fd_close_handle,            /* close_handle */
Mike McCormack's avatar
Mike McCormack committed
151 152 153
    mail_writer_destroy         /* destroy */
};

154
static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
Mike McCormack's avatar
Mike McCormack committed
155 156 157

static const struct fd_ops mail_writer_fd_ops =
{
158 159
    default_fd_get_poll_events,  /* get_poll_events */
    default_poll_event,          /* poll_event */
160
    mail_writer_get_fd_type,     /* get_fd_type */
161 162 163
    no_fd_read,                  /* read */
    no_fd_write,                 /* write */
    no_fd_flush,                 /* flush */
164
    default_fd_get_file_info,    /* get_file_info */
165
    no_fd_get_volume_info,       /* get_volume_info */
166
    default_fd_ioctl,            /* ioctl */
167
    default_fd_queue_async,      /* queue_async */
168
    default_fd_reselect_async    /* reselect_async */
Mike McCormack's avatar
Mike McCormack committed
169 170
};

171 172 173 174 175 176 177 178

struct mailslot_device
{
    struct object       obj;         /* object header */
    struct fd          *fd;          /* pseudo-fd for ioctls */
    struct namespace   *mailslots;   /* mailslot namespace */
};

179
static void mailslot_device_dump( struct object *obj, int verbose );
180
static struct object_type *mailslot_device_get_type( struct object *obj );
181
static struct fd *mailslot_device_get_fd( struct object *obj );
182 183
static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
                                                   unsigned int attr );
184 185
static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
                                                 unsigned int sharing, unsigned int options );
186
static void mailslot_device_destroy( struct object *obj );
187
static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
188 189 190 191

static const struct object_ops mailslot_device_ops =
{
    sizeof(struct mailslot_device), /* size */
192
    mailslot_device_dump,           /* dump */
193
    mailslot_device_get_type,       /* get_type */
194 195 196 197 198 199
    no_add_queue,                   /* add_queue */
    NULL,                           /* remove_queue */
    NULL,                           /* signaled */
    no_satisfied,                   /* satisfied */
    no_signal,                      /* signal */
    mailslot_device_get_fd,         /* get_fd */
200
    no_map_access,                  /* map_access */
201 202
    default_get_sd,                 /* get_sd */
    default_set_sd,                 /* set_sd */
203
    mailslot_device_lookup_name,    /* lookup_name */
204 205
    directory_link_name,            /* link_name */
    default_unlink_name,            /* unlink_name */
206
    mailslot_device_open_file,      /* open_file */
207
    no_kernel_obj_list,             /* get_kernel_obj_list */
208
    fd_close_handle,                /* close_handle */
209 210 211 212 213
    mailslot_device_destroy         /* destroy */
};

static const struct fd_ops mailslot_device_fd_ops =
{
214 215
    default_fd_get_poll_events,     /* get_poll_events */
    default_poll_event,             /* poll_event */
216
    mailslot_device_get_fd_type,    /* get_fd_type */
217 218 219
    no_fd_read,                     /* read */
    no_fd_write,                    /* write */
    no_fd_flush,                    /* flush */
220
    default_fd_get_file_info,       /* get_file_info */
221
    no_fd_get_volume_info,          /* get_volume_info */
222
    default_fd_ioctl,               /* ioctl */
223
    default_fd_queue_async,         /* queue_async */
224
    default_fd_reselect_async       /* reselect_async */
225 226
};

Mike McCormack's avatar
Mike McCormack committed
227 228 229 230 231 232
static void mailslot_destroy( struct object *obj)
{
    struct mailslot *mailslot = (struct mailslot *) obj;

    assert( mailslot->fd );

233 234 235 236 237
    if (mailslot->write_fd != -1)
    {
        shutdown( mailslot->write_fd, SHUT_RDWR );
        close( mailslot->write_fd );
    }
Mike McCormack's avatar
Mike McCormack committed
238 239 240 241 242 243 244 245
    release_object( mailslot->fd );
}

static void mailslot_dump( struct object *obj, int verbose )
{
    struct mailslot *mailslot = (struct mailslot *) obj;

    assert( obj->ops == &mailslot_ops );
246 247
    fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
             mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
Mike McCormack's avatar
Mike McCormack committed
248 249
}

250
static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
Mike McCormack's avatar
Mike McCormack committed
251
{
252
    return FD_TYPE_MAILSLOT;
Mike McCormack's avatar
Mike McCormack committed
253 254 255 256 257 258 259 260 261
}

static struct fd *mailslot_get_fd( struct object *obj )
{
    struct mailslot *mailslot = (struct mailslot *) obj;

    return (struct fd *)grab_object( mailslot->fd );
}

262 263
static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
{
264
    /* mailslots can only be read */
265
    if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
266
    if (access & GENERIC_ALL)     access |= FILE_GENERIC_READ;
267 268 269
    return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
}

270 271 272 273 274 275 276 277 278 279 280 281 282 283
static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent )
{
    struct mailslot_device *dev = (struct mailslot_device *)parent;

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

284 285 286 287 288
static struct object *mailslot_open_file( struct object *obj, unsigned int access,
                                          unsigned int sharing, unsigned int options )
{
    struct mailslot *mailslot = (struct mailslot *)obj;
    struct mail_writer *writer;
289
    int unix_fd;
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

    if (!(sharing & FILE_SHARE_READ))
    {
        set_error( STATUS_SHARING_VIOLATION );
        return NULL;
    }

    if (!list_empty( &mailslot->writers ))
    {
        /* Readers and writers cannot be mixed.
         * If there's more than one writer, all writers must open with FILE_SHARE_WRITE
         */
        writer = LIST_ENTRY( list_head(&mailslot->writers), struct mail_writer, entry );

        if (((access & (GENERIC_WRITE|FILE_WRITE_DATA)) || (writer->access & FILE_WRITE_DATA)) &&
           !((sharing & FILE_SHARE_WRITE) && (writer->sharing & FILE_SHARE_WRITE)))
        {
            set_error( STATUS_SHARING_VIOLATION );
            return NULL;
        }
    }

312 313 314 315 316 317 318 319 320 321
    if ((unix_fd = dup( mailslot->write_fd )) == -1)
    {
        file_set_error();
        return NULL;
    }
    if (!(writer = alloc_object( &mail_writer_ops )))
    {
        close( unix_fd );
        return NULL;
    }
322 323 324 325 326
    grab_object( mailslot );
    writer->mailslot = mailslot;
    writer->access   = mail_writer_map_access( &writer->obj, access );
    writer->sharing  = sharing;
    list_add_head( &mailslot->writers, &writer->entry );
327 328 329 330 331 332

    if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
    {
        release_object( writer );
        return NULL;
    }
333
    allow_fd_caching( writer->fd );
334 335 336
    return &writer->obj;
}

337
static void mailslot_queue_async( struct fd *fd, struct async *async, int type, int count )
Mike McCormack's avatar
Mike McCormack committed
338 339 340 341 342
{
    struct mailslot *mailslot = get_fd_user( fd );

    assert(mailslot->obj.ops == &mailslot_ops);

343 344 345 346
    fd_queue_async( fd, async, type );
    async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
                       STATUS_IO_TIMEOUT );
    set_error( STATUS_PENDING );
Mike McCormack's avatar
Mike McCormack committed
347 348
}

349 350
static void mailslot_device_dump( struct object *obj, int verbose )
{
351
    fputs( "Mailslot device\n", stderr );
352 353
}

354 355 356 357 358 359 360
static struct object_type *mailslot_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 );
}

361 362 363
static struct fd *mailslot_device_get_fd( struct object *obj )
{
    struct mailslot_device *device = (struct mailslot_device *)obj;
364
    return (struct fd *)grab_object( device->fd );
365 366
}

367 368 369 370 371 372 373 374
static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
                                                   unsigned int attr )
{
    struct mailslot_device *device = (struct mailslot_device*)obj;
    struct object *found;

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

375 376
    if (!name) return NULL;  /* open the device itself */

377 378 379 380 381 382
    if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
        name->len = 0;

    return found;
}

383 384 385 386 387 388
static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
                                                 unsigned int sharing, unsigned int options )
{
    return grab_object( obj );
}

389 390 391 392
static void mailslot_device_destroy( struct object *obj )
{
    struct mailslot_device *device = (struct mailslot_device*)obj;
    assert( obj->ops == &mailslot_device_ops );
393
    if (device->fd) release_object( device->fd );
394
    free( device->mailslots );
395 396
}

397
static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
398
{
399
    return FD_TYPE_DEVICE;
400 401
}

402
struct object *create_mailslot_device( struct object *root, const struct unicode_str *name )
403 404 405
{
    struct mailslot_device *dev;

406
    if ((dev = create_named_object( root, &mailslot_device_ops, name, 0, NULL )) &&
407 408
        get_error() != STATUS_OBJECT_NAME_EXISTS)
    {
409
        dev->mailslots = NULL;
410
        if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj, 0 )) ||
411
            !(dev->mailslots = create_namespace( 7 )))
412 413 414 415 416
        {
            release_object( dev );
            dev = NULL;
        }
    }
417
    return &dev->obj;
418 419
}

420
static struct mailslot *create_mailslot( struct object *root,
421
                                         const struct unicode_str *name, unsigned int attr,
422 423
                                         int max_msgsize, timeout_t read_timeout,
                                         const struct security_descriptor *sd )
Mike McCormack's avatar
Mike McCormack committed
424 425 426 427
{
    struct mailslot *mailslot;
    int fds[2];

428
    if (!(mailslot = create_named_object( root, &mailslot_ops, name, attr, sd ))) return NULL;
429

Mike McCormack's avatar
Mike McCormack committed
430
    mailslot->fd = NULL;
431
    mailslot->write_fd = -1;
Mike McCormack's avatar
Mike McCormack committed
432 433 434 435
    mailslot->max_msgsize = max_msgsize;
    mailslot->read_timeout = read_timeout;
    list_init( &mailslot->writers );

436
    if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
Mike McCormack's avatar
Mike McCormack committed
437 438 439
    {
        fcntl( fds[0], F_SETFL, O_NONBLOCK );
        fcntl( fds[1], F_SETFL, O_NONBLOCK );
440
        shutdown( fds[0], SHUT_RD );
441
        mailslot->write_fd = fds[0];
442 443 444 445 446 447
        if ((mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
                                                 FILE_SYNCHRONOUS_IO_NONALERT )))
        {
            allow_fd_caching( mailslot->fd );
            return mailslot;
        }
Mike McCormack's avatar
Mike McCormack committed
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
    }
    else file_set_error();

    release_object( mailslot );
    return NULL;
}

static void mail_writer_dump( struct object *obj, int verbose )
{
    fprintf( stderr, "Mailslot writer\n" );
}

static void mail_writer_destroy( struct object *obj)
{
    struct mail_writer *writer = (struct mail_writer *) obj;

464
    if (writer->fd) release_object( writer->fd );
Mike McCormack's avatar
Mike McCormack committed
465 466 467 468
    list_remove( &writer->entry );
    release_object( writer->mailslot );
}

469
static enum server_fd_type mail_writer_get_fd_type( struct fd *fd )
Mike McCormack's avatar
Mike McCormack committed
470
{
471
    return FD_TYPE_MAILSLOT;
Mike McCormack's avatar
Mike McCormack committed
472 473 474 475 476
}

static struct fd *mail_writer_get_fd( struct object *obj )
{
    struct mail_writer *writer = (struct mail_writer *) obj;
477
    return (struct fd *)grab_object( writer->fd );
Mike McCormack's avatar
Mike McCormack committed
478 479
}

480 481
static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
{
482
    /* mailslot writers can only get write access */
483
    if (access & GENERIC_WRITE)   access |= FILE_GENERIC_WRITE;
484
    if (access & GENERIC_ALL)     access |= FILE_GENERIC_WRITE;
485 486 487
    return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
}

Mike McCormack's avatar
Mike McCormack committed
488 489 490
static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
                                          unsigned int access )
{
491
    return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
Mike McCormack's avatar
Mike McCormack committed
492 493 494 495 496 497 498
}


/* create a mailslot */
DECL_HANDLER(create_mailslot)
{
    struct mailslot *mailslot;
499
    struct unicode_str name;
500
    struct object *root;
501
    const struct security_descriptor *sd;
502
    const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
Mike McCormack's avatar
Mike McCormack committed
503

504
    if (!objattr) return;
505 506 507 508 509 510 511 512

    if (!name.len)  /* mailslots need a root directory even without a name */
    {
        if (!objattr->rootdir)
        {
            set_error( STATUS_OBJECT_PATH_SYNTAX_BAD );
            return;
        }
513
        if (!(root = get_directory_obj( current->process, objattr->rootdir ))) return;
514
    }
515

516 517
    if ((mailslot = create_mailslot( root, &name, objattr->attributes, req->max_msgsize,
                                     req->read_timeout, sd )))
Mike McCormack's avatar
Mike McCormack committed
518
    {
519
        reply->handle = alloc_handle( current->process, mailslot, req->access, objattr->attributes );
Mike McCormack's avatar
Mike McCormack committed
520 521
        release_object( mailslot );
    }
522 523

    if (root) release_object( root );
Mike McCormack's avatar
Mike McCormack committed
524 525 526 527 528 529 530 531
}


/* set mailslot information */
DECL_HANDLER(set_mailslot_info)
{
    struct mailslot *mailslot = get_mailslot_obj( current->process, req->handle, 0 );

532
    if (mailslot)
Mike McCormack's avatar
Mike McCormack committed
533
    {
534
        if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
Mike McCormack's avatar
Mike McCormack committed
535 536 537 538 539 540
            mailslot->read_timeout = req->read_timeout;
        reply->max_msgsize = mailslot->max_msgsize;
        reply->read_timeout = mailslot->read_timeout;
        release_object( mailslot );
    }
}