mailslot.c 17.1 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 70
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
71 72 73 74 75 76
static void mailslot_destroy( struct object * );

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

93
static enum server_fd_type mailslot_get_fd_type( struct fd *fd );
94
static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count );
Mike McCormack's avatar
Mike McCormack committed
95 96 97

static const struct fd_ops mailslot_fd_ops =
{
98 99 100
    default_fd_get_poll_events, /* get_poll_events */
    default_poll_event,         /* poll_event */
    no_flush,                   /* flush */
101
    mailslot_get_fd_type,       /* get_fd_type */
102
    default_fd_ioctl,           /* ioctl */
103
    mailslot_queue_async,       /* queue_async */
104
    default_fd_reselect_async,  /* reselect_async */
105
    default_fd_cancel_async     /* cancel_async */
Mike McCormack's avatar
Mike McCormack committed
106 107
};

108

Mike McCormack's avatar
Mike McCormack committed
109 110 111
struct mail_writer
{
    struct object         obj;
112
    struct fd            *fd;
Mike McCormack's avatar
Mike McCormack committed
113 114
    struct mailslot      *mailslot;
    struct list           entry;
115 116
    unsigned int          access;
    unsigned int          sharing;
Mike McCormack's avatar
Mike McCormack committed
117 118 119 120
};

static void mail_writer_dump( struct object *obj, int verbose );
static struct fd *mail_writer_get_fd( struct object *obj );
121
static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
Mike McCormack's avatar
Mike McCormack committed
122 123 124 125 126 127
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 */
128
    no_get_type,                /* get_type */
Mike McCormack's avatar
Mike McCormack committed
129 130 131 132
    no_add_queue,               /* add_queue */
    NULL,                       /* remove_queue */
    NULL,                       /* signaled */
    NULL,                       /* satisfied */
133
    no_signal,                  /* signal */
Mike McCormack's avatar
Mike McCormack committed
134
    mail_writer_get_fd,         /* get_fd */
135
    mail_writer_map_access,     /* map_access */
136 137
    default_get_sd,             /* get_sd */
    default_set_sd,             /* set_sd */
138
    no_lookup_name,             /* lookup_name */
139
    no_open_file,               /* open_file */
140
    fd_close_handle,            /* close_handle */
Mike McCormack's avatar
Mike McCormack committed
141 142 143
    mail_writer_destroy         /* destroy */
};

144
static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
Mike McCormack's avatar
Mike McCormack committed
145 146 147

static const struct fd_ops mail_writer_fd_ops =
{
148 149
    default_fd_get_poll_events,  /* get_poll_events */
    default_poll_event,          /* poll_event */
Mike McCormack's avatar
Mike McCormack committed
150
    no_flush,                    /* flush */
151
    mail_writer_get_fd_type,     /* get_fd_type */
152
    default_fd_ioctl,            /* ioctl */
153 154 155
    default_fd_queue_async,      /* queue_async */
    default_fd_reselect_async,   /* reselect_async */
    default_fd_cancel_async      /* cancel_async */
Mike McCormack's avatar
Mike McCormack committed
156 157
};

158 159 160 161 162 163 164 165

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

166
static void mailslot_device_dump( struct object *obj, int verbose );
167
static struct object_type *mailslot_device_get_type( struct object *obj );
168
static struct fd *mailslot_device_get_fd( struct object *obj );
169 170
static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
                                                   unsigned int attr );
171 172
static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
                                                 unsigned int sharing, unsigned int options );
173
static void mailslot_device_destroy( struct object *obj );
174
static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd );
175 176 177 178

static const struct object_ops mailslot_device_ops =
{
    sizeof(struct mailslot_device), /* size */
179
    mailslot_device_dump,           /* dump */
180
    mailslot_device_get_type,       /* get_type */
181 182 183 184 185 186
    no_add_queue,                   /* add_queue */
    NULL,                           /* remove_queue */
    NULL,                           /* signaled */
    no_satisfied,                   /* satisfied */
    no_signal,                      /* signal */
    mailslot_device_get_fd,         /* get_fd */
187
    no_map_access,                  /* map_access */
188 189
    default_get_sd,                 /* get_sd */
    default_set_sd,                 /* set_sd */
190
    mailslot_device_lookup_name,    /* lookup_name */
191
    mailslot_device_open_file,      /* open_file */
192
    fd_close_handle,                /* close_handle */
193 194 195 196 197
    mailslot_device_destroy         /* destroy */
};

static const struct fd_ops mailslot_device_fd_ops =
{
198 199 200
    default_fd_get_poll_events,     /* get_poll_events */
    default_poll_event,             /* poll_event */
    no_flush,                       /* flush */
201
    mailslot_device_get_fd_type,    /* get_fd_type */
202
    default_fd_ioctl,               /* ioctl */
203
    default_fd_queue_async,         /* queue_async */
204
    default_fd_reselect_async,      /* reselect_async */
205
    default_fd_cancel_async         /* cancel_async */
206 207
};

Mike McCormack's avatar
Mike McCormack committed
208 209 210 211 212 213
static void mailslot_destroy( struct object *obj)
{
    struct mailslot *mailslot = (struct mailslot *) obj;

    assert( mailslot->fd );

214 215 216 217 218
    if (mailslot->write_fd != -1)
    {
        shutdown( mailslot->write_fd, SHUT_RDWR );
        close( mailslot->write_fd );
    }
Mike McCormack's avatar
Mike McCormack committed
219 220 221 222 223 224 225 226
    release_object( mailslot->fd );
}

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

    assert( obj->ops == &mailslot_ops );
227 228
    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
229 230
}

231
static enum server_fd_type mailslot_get_fd_type( struct fd *fd )
Mike McCormack's avatar
Mike McCormack committed
232
{
233
    return FD_TYPE_MAILSLOT;
Mike McCormack's avatar
Mike McCormack committed
234 235 236 237 238 239 240 241 242
}

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

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

243 244
static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
{
245
    /* mailslots can only be read */
246
    if (access & GENERIC_READ)    access |= FILE_GENERIC_READ;
247
    if (access & GENERIC_ALL)     access |= FILE_GENERIC_READ;
248 249 250
    return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
}

251 252 253 254 255
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;
256
    int unix_fd;
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278

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

279 280 281 282 283 284 285 286 287 288
    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;
    }
289 290 291 292 293
    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 );
294 295 296 297 298 299

    if (!(writer->fd = create_anonymous_fd( &mail_writer_fd_ops, unix_fd, &writer->obj, options )))
    {
        release_object( writer );
        return NULL;
    }
300 301 302
    return &writer->obj;
}

303
static void mailslot_queue_async( struct fd *fd, const async_data_t *data, int type, int count )
Mike McCormack's avatar
Mike McCormack committed
304 305
{
    struct mailslot *mailslot = get_fd_user( fd );
306
    struct async *async;
Mike McCormack's avatar
Mike McCormack committed
307 308 309

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

310
    if ((async = fd_queue_async( fd, data, type )))
311
    {
312 313
        async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1,
                           STATUS_IO_TIMEOUT );
314 315
        release_object( async );
        set_error( STATUS_PENDING );
316
    }
Mike McCormack's avatar
Mike McCormack committed
317 318
}

319 320 321 322 323 324
static void mailslot_device_dump( struct object *obj, int verbose )
{
    assert( obj->ops == &mailslot_device_ops );
    fprintf( stderr, "Mail slot device\n" );
}

325 326 327 328 329 330 331
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 );
}

332 333 334
static struct fd *mailslot_device_get_fd( struct object *obj )
{
    struct mailslot_device *device = (struct mailslot_device *)obj;
335
    return (struct fd *)grab_object( device->fd );
336 337
}

338 339 340 341 342 343 344 345 346 347 348 349 350 351
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 );

    if ((found = find_object( device->mailslots, name, attr | OBJ_CASE_INSENSITIVE )))
        name->len = 0;

    return found;
}

352 353 354 355 356 357
static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
                                                 unsigned int sharing, unsigned int options )
{
    return grab_object( obj );
}

358 359 360 361
static void mailslot_device_destroy( struct object *obj )
{
    struct mailslot_device *device = (struct mailslot_device*)obj;
    assert( obj->ops == &mailslot_device_ops );
362
    if (device->fd) release_object( device->fd );
363
    free( device->mailslots );
364 365
}

366
static enum server_fd_type mailslot_device_get_fd_type( struct fd *fd )
367
{
368
    return FD_TYPE_DEVICE;
369 370
}

371
void create_mailslot_device( struct directory *root, const struct unicode_str *name )
372 373 374
{
    struct mailslot_device *dev;

375
    if ((dev = create_named_object_dir( root, name, 0, &mailslot_device_ops )) &&
376 377
        get_error() != STATUS_OBJECT_NAME_EXISTS)
    {
378
        dev->mailslots = NULL;
379
        if (!(dev->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, &dev->obj, 0 )) ||
380
            !(dev->mailslots = create_namespace( 7 )))
381 382 383 384 385
        {
            release_object( dev );
            dev = NULL;
        }
    }
386
    if (dev) make_object_static( &dev->obj );
387 388 389 390
}

static struct mailslot *create_mailslot( struct directory *root,
                                         const struct unicode_str *name, unsigned int attr,
391
                                         int max_msgsize, timeout_t read_timeout )
Mike McCormack's avatar
Mike McCormack committed
392
{
393 394 395
    struct object *obj;
    struct unicode_str new_name;
    struct mailslot_device *dev;
Mike McCormack's avatar
Mike McCormack committed
396 397 398
    struct mailslot *mailslot;
    int fds[2];

399 400
    if (!name || !name->len) return alloc_object( &mailslot_ops );

401 402 403 404 405
    if (!(obj = find_object_dir( root, name, attr, &new_name )))
    {
        set_error( STATUS_OBJECT_NAME_INVALID );
        return NULL;
    }
406 407

    if (!new_name.len)
Mike McCormack's avatar
Mike McCormack committed
408
    {
409 410 411 412 413 414 415 416
        if (attr & OBJ_OPENIF && obj->ops == &mailslot_ops)
            /* it already exists - there can only be one mailslot to read from */
            set_error( STATUS_OBJECT_NAME_EXISTS );
        else if (attr & OBJ_OPENIF)
            set_error( STATUS_OBJECT_TYPE_MISMATCH );
        else
            set_error( STATUS_OBJECT_NAME_COLLISION );
        release_object( obj );
Mike McCormack's avatar
Mike McCormack committed
417 418 419
        return NULL;
    }

420
    if (obj->ops != &mailslot_device_ops)
Mike McCormack's avatar
Mike McCormack committed
421
    {
422
        set_error( STATUS_OBJECT_NAME_INVALID );
423
        release_object( obj );
Mike McCormack's avatar
Mike McCormack committed
424 425 426
        return NULL;
    }

427 428 429 430 431 432
    dev = (struct mailslot_device *)obj;
    mailslot = create_object( dev->mailslots, &mailslot_ops, &new_name, NULL );
    release_object( dev );

    if (!mailslot) return NULL;

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

439
    if (!socketpair( PF_UNIX, SOCK_DGRAM, 0, fds ))
Mike McCormack's avatar
Mike McCormack committed
440 441 442
    {
        fcntl( fds[0], F_SETFL, O_NONBLOCK );
        fcntl( fds[1], F_SETFL, O_NONBLOCK );
443
        shutdown( fds[0], SHUT_RD );
444
        mailslot->write_fd = fds[0];
445 446
        mailslot->fd = create_anonymous_fd( &mailslot_fd_ops, fds[1], &mailslot->obj,
                                            FILE_SYNCHRONOUS_IO_NONALERT );
447
        if (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 directory *root = NULL;
Mike McCormack's avatar
Mike McCormack committed
501 502

    reply->handle = 0;
503
    get_req_unicode_str( &name );
504 505 506 507 508
    if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
        return;

    if ((mailslot = create_mailslot( root, &name, req->attributes, req->max_msgsize,
                                     req->read_timeout )))
Mike McCormack's avatar
Mike McCormack committed
509
    {
510
        reply->handle = alloc_handle( current->process, mailslot, req->access, req->attributes );
Mike McCormack's avatar
Mike McCormack committed
511 512
        release_object( mailslot );
    }
513 514

    if (root) release_object( root );
Mike McCormack's avatar
Mike McCormack committed
515 516 517 518 519 520 521 522
}


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

523
    if (mailslot)
Mike McCormack's avatar
Mike McCormack committed
524
    {
525
        if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
Mike McCormack's avatar
Mike McCormack committed
526 527 528 529 530 531
            mailslot->read_timeout = req->read_timeout;
        reply->max_msgsize = mailslot->max_msgsize;
        reply->read_timeout = mailslot->read_timeout;
        release_object( mailslot );
    }
}