File: mailslot.c

package info (click to toggle)
wine 10.0~repack-12
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 326,476 kB
  • sloc: ansic: 4,155,993; perl: 23,800; yacc: 22,031; javascript: 15,872; makefile: 12,346; pascal: 9,519; objc: 6,923; lex: 5,273; xml: 3,219; python: 2,688; cpp: 1,741; sh: 871; java: 750; asm: 299; cs: 62
file content (681 lines) | stat: -rw-r--r-- 23,838 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
 * 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
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 */

#include "config.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>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif

#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"

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

struct mailslot_message
{
    struct list entry;
    struct iosb *iosb;
};

struct mailslot
{
    struct object       obj;
    struct fd          *fd;
    unsigned int        max_msgsize;
    timeout_t           read_timeout;
    struct list         writers;
    struct list         messages;
    struct async_queue  read_q;
};

/* mailslot functions */
static void mailslot_dump( struct object*, int );
static struct fd *mailslot_get_fd( struct object * );
static unsigned int mailslot_map_access( struct object *obj, unsigned int access );
static int mailslot_link_name( struct object *obj, struct object_name *name, struct object *parent );
static struct object *mailslot_open_file( struct object *obj, unsigned int access,
                                          unsigned int sharing, unsigned int options );
static void mailslot_destroy( struct object * );

static const struct object_ops mailslot_ops =
{
    sizeof(struct mailslot),   /* size */
    &file_type,                /* type */
    mailslot_dump,             /* dump */
    add_queue,                 /* add_queue */
    remove_queue,              /* remove_queue */
    default_fd_signaled,       /* signaled */
    no_satisfied,              /* satisfied */
    no_signal,                 /* signal */
    mailslot_get_fd,           /* get_fd */
    mailslot_map_access,       /* map_access */
    default_get_sd,            /* get_sd */
    default_set_sd,            /* set_sd */
    default_get_full_name,     /* get_full_name */
    no_lookup_name,            /* lookup_name */
    mailslot_link_name,        /* link_name */
    default_unlink_name,       /* unlink_name */
    mailslot_open_file,        /* open_file */
    no_kernel_obj_list,        /* get_kernel_obj_list */
    no_close_handle,           /* close_handle */
    mailslot_destroy           /* destroy */
};

static enum server_fd_type mailslot_get_fd_type( struct fd *fd );
static void mailslot_read( struct fd *fd, struct async *async, file_pos_t pos );
static void mailslot_write( struct fd *fd, struct async *async, file_pos_t pos );
static void mailslot_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class );

static const struct fd_ops mailslot_fd_ops =
{
    default_fd_get_poll_events, /* get_poll_events */
    default_poll_event,         /* poll_event */
    mailslot_get_fd_type,       /* get_fd_type */
    mailslot_read,              /* read */
    mailslot_write,             /* write */
    no_fd_flush,                /* flush */
    mailslot_get_file_info,     /* get_file_info */
    no_fd_get_volume_info,      /* get_volume_info */
    default_fd_ioctl,           /* ioctl */
    default_fd_cancel_async,    /* cancel_async */
    no_fd_queue_async,          /* queue_async */
    default_fd_reselect_async   /* reselect_async */
};


struct mail_writer
{
    struct object         obj;
    struct fd            *fd;
    struct mailslot      *mailslot;
    struct list           entry;
    unsigned int          access;
    unsigned int          sharing;
};

static void mail_writer_dump( struct object *obj, int verbose );
static struct fd *mail_writer_get_fd( struct object *obj );
static unsigned int mail_writer_map_access( struct object *obj, unsigned int access );
static void mail_writer_destroy( struct object *obj);

static const struct object_ops mail_writer_ops =
{
    sizeof(struct mail_writer), /* size */
    &file_type,                 /* type */
    mail_writer_dump,           /* dump */
    no_add_queue,               /* add_queue */
    NULL,                       /* remove_queue */
    NULL,                       /* signaled */
    NULL,                       /* satisfied */
    no_signal,                  /* signal */
    mail_writer_get_fd,         /* get_fd */
    mail_writer_map_access,     /* map_access */
    default_get_sd,             /* get_sd */
    default_set_sd,             /* set_sd */
    no_get_full_name,           /* get_full_name */
    no_lookup_name,             /* lookup_name */
    no_link_name,               /* link_name */
    NULL,                       /* unlink_name */
    no_open_file,               /* open_file */
    no_kernel_obj_list,         /* get_kernel_obj_list */
    no_close_handle,            /* close_handle */
    mail_writer_destroy         /* destroy */
};

static enum server_fd_type mail_writer_get_fd_type( struct fd *fd );
static void mail_writer_read( struct fd *fd, struct async *async, file_pos_t pos );
static void mail_writer_write( struct fd *fd, struct async *async, file_pos_t pos );

static const struct fd_ops mail_writer_fd_ops =
{
    default_fd_get_poll_events,  /* get_poll_events */
    default_poll_event,          /* poll_event */
    mail_writer_get_fd_type,     /* get_fd_type */
    mail_writer_read,            /* read */
    mail_writer_write,           /* write */
    no_fd_flush,                 /* flush */
    default_fd_get_file_info,    /* get_file_info */
    no_fd_get_volume_info,       /* get_volume_info */
    default_fd_ioctl,            /* ioctl */
    default_fd_cancel_async,     /* cancel_async */
    default_fd_queue_async,      /* queue_async */
    default_fd_reselect_async    /* reselect_async */
};


struct mailslot_device
{
    struct object       obj;         /* object header */
    struct namespace   *mailslots;   /* mailslot namespace */
};

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

static void mailslot_device_dump( struct object *obj, int verbose );
static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
                                                   unsigned int attr, struct object *root );
static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
                                                 unsigned int sharing, unsigned int options );
static void mailslot_device_destroy( struct object *obj );

static const struct object_ops mailslot_device_ops =
{
    sizeof(struct mailslot_device), /* size */
    &device_type,                   /* type */
    mailslot_device_dump,           /* dump */
    no_add_queue,                   /* add_queue */
    NULL,                           /* remove_queue */
    NULL,                           /* signaled */
    no_satisfied,                   /* satisfied */
    no_signal,                      /* signal */
    no_get_fd,                      /* get_fd */
    default_map_access,             /* map_access */
    default_get_sd,                 /* get_sd */
    default_set_sd,                 /* set_sd */
    default_get_full_name,          /* get_full_name */
    mailslot_device_lookup_name,    /* lookup_name */
    directory_link_name,            /* link_name */
    default_unlink_name,            /* unlink_name */
    mailslot_device_open_file,      /* open_file */
    no_kernel_obj_list,             /* get_kernel_obj_list */
    no_close_handle,                /* close_handle */
    mailslot_device_destroy         /* destroy */
};

static void mailslot_device_file_dump( struct object *obj, int verbose );
static struct fd *mailslot_device_file_get_fd( struct object *obj );
static WCHAR *mailslot_device_file_get_full_name( struct object *obj, data_size_t *len );
static void mailslot_device_file_destroy( struct object *obj );
static enum server_fd_type mailslot_device_file_get_fd_type( struct fd *fd );

static const struct object_ops mailslot_device_file_ops =
{
    sizeof(struct mailslot_device_file),    /* size */
    &file_type,                             /* type */
    mailslot_device_file_dump,              /* dump */
    add_queue,                              /* add_queue */
    remove_queue,                           /* remove_queue */
    default_fd_signaled,                    /* signaled */
    no_satisfied,                           /* satisfied */
    no_signal,                              /* signal */
    mailslot_device_file_get_fd,            /* get_fd */
    default_map_access,                     /* map_access */
    default_get_sd,                         /* get_sd */
    default_set_sd,                         /* set_sd */
    mailslot_device_file_get_full_name,     /* get_full_name */
    no_lookup_name,                         /* lookup_name */
    no_link_name,                           /* link_name */
    NULL,                                   /* unlink_name */
    no_open_file,                           /* open_file */
    no_kernel_obj_list,                     /* get_kernel_obj_list */
    no_close_handle,                        /* close_handle */
    mailslot_device_file_destroy            /* destroy */
};

static const struct fd_ops mailslot_device_fd_ops =
{
    default_fd_get_poll_events,         /* get_poll_events */
    default_poll_event,                 /* poll_event */
    mailslot_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 */
    default_fd_ioctl,                   /* ioctl */
    default_fd_cancel_async,            /* cancel_async */
    default_fd_queue_async,             /* queue_async */
    default_fd_reselect_async           /* reselect_async */
};

static struct mailslot_message *get_first_message( struct mailslot *mailslot )
{
    struct list *ptr = list_head( &mailslot->messages );
    return ptr ? LIST_ENTRY( ptr, struct mailslot_message, entry ) : NULL;
}

static void mailslot_destroy( struct object *obj)
{
    struct mailslot *mailslot = (struct mailslot *) obj;
    struct mailslot_message *message;

    while ((message = get_first_message( mailslot )))
    {
        list_remove( &message->entry );
        release_object( message->iosb );
        free( message );
    }

    free_async_queue( &mailslot->read_q );

    assert( mailslot->fd );
    release_object( mailslot->fd );
}

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

    assert( obj->ops == &mailslot_ops );
    fprintf( stderr, "Mailslot max_msgsize=%d read_timeout=%s\n",
             mailslot->max_msgsize, get_timeout_str(mailslot->read_timeout) );
}

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

static void reselect_mailslot( struct mailslot *mailslot )
{
    struct mailslot_message *message;
    struct async *async;

    while ((message = get_first_message( mailslot )) && (async = find_pending_async( &mailslot->read_q )))
    {
        struct iosb *read_iosb = async_get_iosb( async );

        if (read_iosb->out_size < message->iosb->in_size)
        {
            async_request_complete( async, STATUS_BUFFER_TOO_SMALL, 0, 0, NULL );
        }
        else
        {
            async_request_complete( async, STATUS_SUCCESS, message->iosb->in_size,
                                    message->iosb->in_size, message->iosb->in_data );
            message->iosb->in_data = NULL;
            list_remove( &message->entry );
            release_object( message->iosb );
            free( message );
        }

        release_object( async );
        release_object( read_iosb );
    }
}

static void mailslot_read( struct fd *fd, struct async *async, file_pos_t pos )
{
    struct mailslot *mailslot = get_fd_user( fd );

    if (mailslot->read_timeout != (timeout_t)-1)
        async_set_timeout( async, mailslot->read_timeout ? mailslot->read_timeout : -1, STATUS_IO_TIMEOUT );
    queue_async( &mailslot->read_q, async );
    reselect_mailslot( mailslot );
    set_error( STATUS_PENDING );
}

static void mailslot_write( struct fd *fd, struct async *async, file_pos_t pos )
{
    set_error( STATUS_INVALID_PARAMETER );
}

static void mailslot_get_file_info( struct fd *fd, obj_handle_t handle, unsigned int info_class )
{
    struct mailslot *mailslot = get_fd_user( fd );

    switch (info_class)
    {
    case FileMailslotQueryInformation:
        {
            FILE_MAILSLOT_QUERY_INFORMATION *info;
            struct mailslot_message *message;

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

            if (!(info = set_reply_data_size( sizeof(*info) ))) return;
            info->MaximumMessageSize = mailslot->max_msgsize;
            info->MailslotQuota = 0;
            info->MessagesAvailable = list_count( &mailslot->messages );
            if ((message = get_first_message( mailslot )))
                info->NextMessageSize = message->iosb->in_size;
            else
                info->NextMessageSize = MAILSLOT_NO_MESSAGE;
            info->ReadTimeout.QuadPart = mailslot->read_timeout;
            break;
        }

    default:
        default_fd_get_file_info( fd, handle, info_class );
    }
}

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

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

static unsigned int mailslot_map_access( struct object *obj, unsigned int access )
{
    /* mailslots can only be read */
    return default_map_access( obj, access ) & FILE_GENERIC_READ;
}

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

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;

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

    if (!(writer = alloc_object( &mail_writer_ops )))
        return NULL;
    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 );

    if (!(writer->fd = alloc_pseudo_fd( &mail_writer_fd_ops, &writer->obj, options )))
    {
        release_object( writer );
        return NULL;
    }
    allow_fd_caching( writer->fd );
    return &writer->obj;
}

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

static struct object *mailslot_device_lookup_name( struct object *obj, struct unicode_str *name,
                                                   unsigned int attr, struct object *root )
{
    struct mailslot_device *device = (struct mailslot_device*)obj;
    struct object *found;

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

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

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

    return found;
}

static struct object *mailslot_device_open_file( struct object *obj, unsigned int access,
                                                 unsigned int sharing, unsigned int options )
{
    struct mailslot_device_file *file;

    if (!(file = alloc_object( &mailslot_device_file_ops ))) return NULL;
    file->device = (struct mailslot_device *)grab_object( obj );
    if (!(file->fd = alloc_pseudo_fd( &mailslot_device_fd_ops, obj, options )))
    {
        release_object( file );
        return NULL;
    }
    allow_fd_caching( file->fd );
    return &file->obj;
}

static void mailslot_device_destroy( struct object *obj )
{
    struct mailslot_device *device = (struct mailslot_device*)obj;
    assert( obj->ops == &mailslot_device_ops );
    free( device->mailslots );
}

struct object *create_mailslot_device( struct object *root, const struct unicode_str *name,
                                       unsigned int attr, const struct security_descriptor *sd )
{
    struct mailslot_device *dev;

    if ((dev = create_named_object( root, &mailslot_device_ops, name, attr, sd )) &&
        get_error() != STATUS_OBJECT_NAME_EXISTS)
    {
        dev->mailslots = NULL;
        if (!(dev->mailslots = create_namespace( 7 )))
        {
            release_object( dev );
            dev = NULL;
        }
    }
    return &dev->obj;
}

static void mailslot_device_file_dump( struct object *obj, int verbose )
{
    struct mailslot_device_file *file = (struct mailslot_device_file *)obj;

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

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

static WCHAR *mailslot_device_file_get_full_name( struct object *obj, data_size_t *len )
{
    struct mailslot_device_file *file = (struct mailslot_device_file *)obj;
    return file->device->obj.ops->get_full_name( &file->device->obj, len );
}

static void mailslot_device_file_destroy( struct object *obj )
{
    struct mailslot_device_file *file = (struct mailslot_device_file*)obj;
    assert( obj->ops == &mailslot_device_file_ops );
    if (file->fd) release_object( file->fd );
    release_object( file->device );
}

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

static struct mailslot *create_mailslot( struct object *root,
                                         const struct unicode_str *name, unsigned int attr,
                                         unsigned int options, int max_msgsize, timeout_t read_timeout,
                                         const struct security_descriptor *sd )
{
    struct mailslot *mailslot;

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

    mailslot->fd = NULL;
    mailslot->max_msgsize = max_msgsize;
    mailslot->read_timeout = read_timeout;
    list_init( &mailslot->writers );
    list_init( &mailslot->messages );
    init_async_queue( &mailslot->read_q );

    if ((mailslot->fd = alloc_pseudo_fd( &mailslot_fd_ops, &mailslot->obj, options )))
    {
        allow_fd_caching( mailslot->fd );
        return mailslot;
    }

    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;

    if (writer->fd) release_object( writer->fd );
    list_remove( &writer->entry );
    release_object( writer->mailslot );
}

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

static void mail_writer_read( struct fd *fd, struct async *async, file_pos_t pos )
{
    set_error( STATUS_INVALID_PARAMETER );
}

static void mail_writer_write( struct fd *fd, struct async *async, file_pos_t pos )
{
    struct mail_writer *writer = get_fd_user( fd );
    struct mailslot_message *message;
    data_size_t size;

    if (!(message = mem_alloc( sizeof(*message) ))) return;
    message->iosb = async_get_iosb( async );
    size = message->iosb->in_size;
    list_add_tail( &writer->mailslot->messages, &message->entry );
    reselect_mailslot( writer->mailslot );
    async_request_complete( async, STATUS_SUCCESS, size, 0, NULL );
}

static struct fd *mail_writer_get_fd( struct object *obj )
{
    struct mail_writer *writer = (struct mail_writer *) obj;
    return (struct fd *)grab_object( writer->fd );
}

static unsigned int mail_writer_map_access( struct object *obj, unsigned int access )
{
    /* mailslot writers can only get write access */
    return default_map_access( obj, access ) & FILE_GENERIC_WRITE;
}

static struct mailslot *get_mailslot_obj( struct process *process, obj_handle_t handle,
                                          unsigned int access )
{
    return (struct mailslot *)get_handle_obj( process, handle, access, &mailslot_ops );
}


/* create a mailslot */
DECL_HANDLER(create_mailslot)
{
    struct mailslot *mailslot;
    struct unicode_str name;
    struct object *root;
    const struct security_descriptor *sd;
    const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );

    if (!objattr) return;

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

    if ((mailslot = create_mailslot( root, &name, objattr->attributes, req->options, req->max_msgsize,
                                     req->read_timeout, sd )))
    {
        reply->handle = alloc_handle( current->process, mailslot, req->access, objattr->attributes );
        release_object( mailslot );
    }

    if (root) release_object( root );
}


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

    if (mailslot)
    {
        if (req->flags & MAILSLOT_SET_READ_TIMEOUT)
            mailslot->read_timeout = req->read_timeout;
        reply->max_msgsize = mailslot->max_msgsize;
        reply->read_timeout = mailslot->read_timeout;
        release_object( mailslot );
    }
}