device.c 18.4 KB
Newer Older
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
/*
 * Server-side device support
 *
 * Copyright (C) 2007 Alexandre Julliard
 *
 * 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 <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

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

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

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
struct ioctl_call
{
    struct object          obj;           /* object header */
    struct list            dev_entry;     /* entry in device queue */
    struct list            mgr_entry;     /* entry in manager queue */
    struct device         *device;        /* device containing this ioctl */
    struct thread         *thread;        /* thread that queued the ioctl */
    void                  *user_arg;      /* user arg used to identify the request */
    struct async          *async;         /* pending async op */
    ioctl_code_t           code;          /* ioctl code */
    unsigned int           status;        /* resulting status (or STATUS_PENDING) */
    data_size_t            in_size;       /* size of input data */
    void                  *in_data;       /* input data */
    data_size_t            out_size;      /* size of output data */
    void                  *out_data;      /* output data */
};

static void ioctl_call_dump( struct object *obj, int verbose );
static int ioctl_call_signaled( struct object *obj, struct thread *thread );
static void ioctl_call_destroy( struct object *obj );

static const struct object_ops ioctl_call_ops =
{
    sizeof(struct ioctl_call),        /* size */
    ioctl_call_dump,                  /* dump */
61
    no_get_type,                      /* get_type */
62 63 64 65 66 67 68
    add_queue,                        /* add_queue */
    remove_queue,                     /* remove_queue */
    ioctl_call_signaled,              /* signaled */
    no_satisfied,                     /* satisfied */
    no_signal,                        /* signal */
    no_get_fd,                        /* get_fd */
    no_map_access,                    /* map_access */
69 70
    default_get_sd,                   /* get_sd */
    default_set_sd,                   /* set_sd */
71 72 73 74 75 76 77
    no_lookup_name,                   /* lookup_name */
    no_open_file,                     /* open_file */
    no_close_handle,                  /* close_handle */
    ioctl_call_destroy                /* destroy */
};


78 79 80 81
struct device_manager
{
    struct object          obj;           /* object header */
    struct list            devices;       /* list of devices */
82
    struct list            requests;      /* list of pending ioctls across all devices */
83 84 85
};

static void device_manager_dump( struct object *obj, int verbose );
86
static int device_manager_signaled( struct object *obj, struct thread *thread );
87 88 89 90 91 92
static void device_manager_destroy( struct object *obj );

static const struct object_ops device_manager_ops =
{
    sizeof(struct device_manager),    /* size */
    device_manager_dump,              /* dump */
93
    no_get_type,                      /* get_type */
94 95 96
    add_queue,                        /* add_queue */
    remove_queue,                     /* remove_queue */
    device_manager_signaled,          /* signaled */
97 98 99 100
    no_satisfied,                     /* satisfied */
    no_signal,                        /* signal */
    no_get_fd,                        /* get_fd */
    no_map_access,                    /* map_access */
101 102
    default_get_sd,                   /* get_sd */
    default_set_sd,                   /* set_sd */
103 104 105 106 107 108 109 110 111 112 113 114 115 116
    no_lookup_name,                   /* lookup_name */
    no_open_file,                     /* open_file */
    no_close_handle,                  /* close_handle */
    device_manager_destroy            /* destroy */
};


struct device
{
    struct object          obj;           /* object header */
    struct device_manager *manager;       /* manager for this device (or NULL if deleted) */
    struct fd             *fd;            /* file descriptor for ioctl */
    void                  *user_ptr;      /* opaque ptr for client side */
    struct list            entry;         /* entry in device manager list */
117
    struct list            requests;      /* list of pending ioctl requests */
118 119 120
};

static void device_dump( struct object *obj, int verbose );
121
static struct object_type *device_get_type( struct object *obj );
122 123 124 125 126
static struct fd *device_get_fd( struct object *obj );
static void device_destroy( struct object *obj );
static struct object *device_open_file( struct object *obj, unsigned int access,
                                        unsigned int sharing, unsigned int options );
static enum server_fd_type device_get_fd_type( struct fd *fd );
127 128
static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
                                  const void *data, data_size_t size );
129 130 131 132 133

static const struct object_ops device_ops =
{
    sizeof(struct device),            /* size */
    device_dump,                      /* dump */
134
    device_get_type,                  /* get_type */
135 136 137 138 139 140
    no_add_queue,                     /* add_queue */
    NULL,                             /* remove_queue */
    NULL,                             /* signaled */
    no_satisfied,                     /* satisfied */
    no_signal,                        /* signal */
    device_get_fd,                    /* get_fd */
141
    default_fd_map_access,            /* map_access */
142 143
    default_get_sd,                   /* get_sd */
    default_set_sd,                   /* set_sd */
144 145 146 147 148 149 150 151 152 153 154 155
    no_lookup_name,                   /* lookup_name */
    device_open_file,                 /* open_file */
    no_close_handle,                  /* close_handle */
    device_destroy                    /* destroy */
};

static const struct fd_ops device_fd_ops =
{
    default_fd_get_poll_events,       /* get_poll_events */
    default_poll_event,               /* poll_event */
    no_flush,                         /* flush */
    device_get_fd_type,               /* get_fd_type */
156
    device_ioctl,                     /* ioctl */
157 158 159 160 161 162
    default_fd_queue_async,           /* queue_async */
    default_fd_reselect_async,        /* reselect_async */
    default_fd_cancel_async           /* cancel_async */
};


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
static void ioctl_call_dump( struct object *obj, int verbose )
{
    struct ioctl_call *ioctl = (struct ioctl_call *)obj;
    fprintf( stderr, "Ioctl call code=%08x device=%p\n", ioctl->code, ioctl->device );
}

static int ioctl_call_signaled( struct object *obj, struct thread *thread )
{
    struct ioctl_call *ioctl = (struct ioctl_call *)obj;

    return !ioctl->device;  /* device is cleared once the ioctl has completed */
}

static void ioctl_call_destroy( struct object *obj )
{
    struct ioctl_call *ioctl = (struct ioctl_call *)obj;

    free( ioctl->in_data );
    free( ioctl->out_data );
    if (ioctl->async)
    {
        async_terminate( ioctl->async, STATUS_CANCELLED );
        release_object( ioctl->async );
    }
    if (ioctl->device) release_object( ioctl->device );
    release_object( ioctl->thread );
}

static struct ioctl_call *create_ioctl( struct device *device, ioctl_code_t code,
                                        const void *in_data, data_size_t in_size,
                                        data_size_t out_size )
{
    struct ioctl_call *ioctl;

    if ((ioctl = alloc_object( &ioctl_call_ops )))
    {
        ioctl->device   = (struct device *)grab_object( device );
        ioctl->code     = code;
        ioctl->async    = NULL;
        ioctl->status   = STATUS_PENDING;
        ioctl->in_size  = in_size;
        ioctl->in_data  = NULL;
        ioctl->out_size = out_size;
        ioctl->out_data = NULL;

        if (ioctl->in_size && !(ioctl->in_data = memdup( in_data, in_size )))
        {
            release_object( ioctl );
            ioctl = NULL;
        }
    }
    return ioctl;
}

static void set_ioctl_result( struct ioctl_call *ioctl, unsigned int status,
                              const void *out_data, data_size_t out_size )
{
    struct device *device = ioctl->device;

    if (!device) return;  /* already finished */

    /* FIXME: handle the STATUS_PENDING case */
    ioctl->status = status;
    ioctl->out_size = min( ioctl->out_size, out_size );
    if (ioctl->out_size && !(ioctl->out_data = memdup( out_data, ioctl->out_size )))
        ioctl->out_size = 0;
    release_object( device );
    ioctl->device = NULL;
    if (ioctl->async)
    {
233 234
        if (ioctl->out_size) status = STATUS_ALERTED;
        async_terminate( ioctl->async, status );
235 236 237 238
        release_object( ioctl->async );
        ioctl->async = NULL;
    }
    wake_up( &ioctl->obj, 0 );
239 240 241 242 243 244 245 246

    if (status != STATUS_ALERTED)
    {
        /* remove it from the device queue */
        /* (for STATUS_ALERTED this will be done in get_ioctl_result) */
        list_remove( &ioctl->dev_entry );
        release_object( ioctl );  /* no longer on the device queue */
    }
247 248 249
}


250 251 252 253 254 255 256 257 258
static void device_dump( struct object *obj, int verbose )
{
    struct device *device = (struct device *)obj;

    fprintf( stderr, "Device " );
    dump_object_name( &device->obj );
    fputc( '\n', stderr );
}

259 260 261 262 263 264 265
static struct object_type *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 );
}

266 267 268 269 270 271 272 273 274 275
static struct fd *device_get_fd( struct object *obj )
{
    struct device *device = (struct device *)obj;

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

static void device_destroy( struct object *obj )
{
    struct device *device = (struct device *)obj;
276
    struct ioctl_call *ioctl, *next;
277

278 279 280 281 282
    LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &device->requests, struct ioctl_call, dev_entry )
    {
        list_remove( &ioctl->dev_entry );
        release_object( ioctl );  /* no longer on the device queue */
    }
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
    if (device->fd) release_object( device->fd );
    if (device->manager) list_remove( &device->entry );
}

static struct object *device_open_file( struct object *obj, unsigned int access,
                                        unsigned int sharing, unsigned int options )
{
    return grab_object( obj );
}

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

298 299 300 301 302 303 304 305 306 307 308 309
static struct ioctl_call *find_ioctl_call( struct device *device, struct thread *thread,
                                           void *user_arg )
{
    struct ioctl_call *ioctl;

    LIST_FOR_EACH_ENTRY( ioctl, &device->requests, struct ioctl_call, dev_entry )
        if (ioctl->thread == thread && ioctl->user_arg == user_arg) return ioctl;

    set_error( STATUS_INVALID_PARAMETER );
    return NULL;
}

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
static obj_handle_t device_ioctl( struct fd *fd, ioctl_code_t code, const async_data_t *async_data,
                                  const void *data, data_size_t size )
{
    struct device *device = get_fd_user( fd );
    struct ioctl_call *ioctl;
    obj_handle_t handle;

    if (!device->manager)  /* it has been deleted */
    {
        set_error( STATUS_FILE_DELETED );
        return 0;
    }

    if (!(ioctl = create_ioctl( device, code, data, size, get_reply_max_size() )))
        return 0;

    ioctl->thread   = (struct thread *)grab_object( current );
    ioctl->user_arg = async_data->arg;

    if (!(handle = alloc_handle( current->process, ioctl, SYNCHRONIZE, 0 )))
    {
        release_object( ioctl );
        return 0;
    }

    if (!(ioctl->async = fd_queue_async( device->fd, async_data, ASYNC_TYPE_WAIT, 0 )))
    {
        close_handle( current->process, handle );
        release_object( ioctl );
        return 0;
    }
    list_add_tail( &device->requests, &ioctl->dev_entry );
    list_add_tail( &device->manager->requests, &ioctl->mgr_entry );
    if (list_head( &device->manager->requests ) == &ioctl->mgr_entry)  /* first one */
        wake_up( &device->manager->obj, 0 );
    /* don't release ioctl since it is now queued in the device */
    set_error( STATUS_PENDING );
    return handle;
}

350 351 352 353 354 355 356 357 358 359 360 361
static struct device *create_device( struct directory *root, const struct unicode_str *name,
                                     struct device_manager *manager, unsigned int attr )
{
    struct device *device;

    if ((device = create_named_object_dir( root, name, attr, &device_ops )))
    {
        if (get_error() != STATUS_OBJECT_NAME_EXISTS)
        {
            /* initialize it if it didn't already exist */
            device->manager = manager;
            list_add_tail( &manager->devices, &device->entry );
362
            list_init( &device->requests );
363 364 365 366 367 368 369 370 371 372 373 374
            if (!(device->fd = alloc_pseudo_fd( &device_fd_ops, &device->obj, 0 )))
            {
                release_object( device );
                device = NULL;
            }
        }
    }
    return device;
}

static void delete_device( struct device *device )
{
375
    struct ioctl_call *ioctl, *next;
376

377 378
    if (!device->manager) return;  /* already deleted */

379
    /* terminate all pending requests */
380
    LIST_FOR_EACH_ENTRY_SAFE( ioctl, next, &device->requests, struct ioctl_call, dev_entry )
381 382 383 384
    {
        list_remove( &ioctl->mgr_entry );
        set_ioctl_result( ioctl, STATUS_FILE_DELETED, NULL, 0 );
    }
385 386 387 388 389 390 391 392 393 394 395
    unlink_named_object( &device->obj );
    list_remove( &device->entry );
    device->manager = NULL;
}


static void device_manager_dump( struct object *obj, int verbose )
{
    fprintf( stderr, "Device manager\n" );
}

396 397 398 399 400 401 402
static int device_manager_signaled( struct object *obj, struct thread *thread )
{
    struct device_manager *manager = (struct device_manager *)obj;

    return !list_empty( &manager->requests );
}

403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
static void device_manager_destroy( struct object *obj )
{
    struct device_manager *manager = (struct device_manager *)obj;
    struct list *ptr;

    while ((ptr = list_head( &manager->devices )))
    {
        struct device *device = LIST_ENTRY( ptr, struct device, entry );
        delete_device( device );
    }
}

static struct device_manager *create_device_manager(void)
{
    struct device_manager *manager;

    if ((manager = alloc_object( &device_manager_ops )))
    {
        list_init( &manager->devices );
422
        list_init( &manager->requests );
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
    }
    return manager;
}


/* create a device manager */
DECL_HANDLER(create_device_manager)
{
    struct device_manager *manager = create_device_manager();

    if (manager)
    {
        reply->handle = alloc_handle( current->process, manager, req->access, req->attributes );
        release_object( manager );
    }
}


/* create a device */
DECL_HANDLER(create_device)
{
    struct device *device;
    struct unicode_str name;
    struct device_manager *manager;
    struct directory *root = NULL;

    if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
                                                             0, &device_manager_ops )))
        return;

    get_req_unicode_str( &name );
    if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 )))
    {
        release_object( manager );
        return;
    }

    if ((device = create_device( root, &name, manager, req->attributes )))
    {
        device->user_ptr = req->user_ptr;
        reply->handle = alloc_handle( current->process, device, req->access, req->attributes );
        release_object( device );
    }

    if (root) release_object( root );
    release_object( manager );
}


/* delete a device */
DECL_HANDLER(delete_device)
{
    struct device *device;

    if ((device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
    {
        delete_device( device );
        release_object( device );
    }
}
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


/* retrieve the next pending device ioctl request */
DECL_HANDLER(get_next_device_request)
{
    struct ioctl_call *ioctl;
    struct device_manager *manager;
    struct list *ptr;

    if (!(manager = (struct device_manager *)get_handle_obj( current->process, req->manager,
                                                             0, &device_manager_ops )))
        return;

    if (req->prev)
    {
        if ((ioctl = (struct ioctl_call *)get_handle_obj( current->process, req->prev,
                                                          0, &ioctl_call_ops )))
        {
            set_ioctl_result( ioctl, req->status, get_req_data(), get_req_data_size() );
            close_handle( current->process, req->prev );  /* avoid an extra round-trip for close */
            release_object( ioctl );
        }
        clear_error();
    }

    if ((ptr = list_head( &manager->requests )))
    {
        ioctl = LIST_ENTRY( ptr, struct ioctl_call, mgr_entry );
        reply->code = ioctl->code;
        reply->user_ptr = ioctl->device->user_ptr;
        reply->in_size = ioctl->in_size;
        reply->out_size = ioctl->out_size;
        if (ioctl->in_size > get_reply_max_size()) set_error( STATUS_BUFFER_OVERFLOW );
        else if ((reply->next = alloc_handle( current->process, ioctl, 0, 0 )))
        {
            set_reply_data_ptr( ioctl->in_data, ioctl->in_size );
            ioctl->in_data = NULL;
            ioctl->in_size = 0;
            list_remove( &ioctl->mgr_entry );
            list_init( &ioctl->mgr_entry );
        }
    }
    else set_error( STATUS_PENDING );

    release_object( manager );
}
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


/* retrieve results of an async ioctl */
DECL_HANDLER(get_ioctl_result)
{
    struct device *device;
    struct ioctl_call *ioctl;

    if (!(device = (struct device *)get_handle_obj( current->process, req->handle, 0, &device_ops )))
        return;

    if ((ioctl = find_ioctl_call( device, current, req->user_arg )))
    {
        if (ioctl->out_data)
        {
            data_size_t size = min( ioctl->out_size, get_reply_max_size() );
            if (size)
            {
                set_reply_data_ptr( ioctl->out_data, size );
                ioctl->out_data = NULL;
            }
        }
        set_error( ioctl->status );
        list_remove( &ioctl->dev_entry );
        release_object( ioctl );  /* no longer on the device queue */
    }
    release_object( device );
}