rng-random.c 3.64 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * QEMU Random Number Generator Backend
 *
 * Copyright IBM, Corp. 2012
 *
 * Authors:
 *  Anthony Liguori   <aliguori@us.ibm.com>
 *
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
 * See the COPYING file in the top-level directory.
 */

13
#include "qemu/osdep.h"
14 15
#include "sysemu/rng-random.h"
#include "sysemu/rng.h"
16
#include "qapi/error.h"
17
#include "qapi/qmp/qerror.h"
18
#include "qemu/main-loop.h"
19

20
struct RngRandom
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
{
    RngBackend parent;

    int fd;
    char *filename;
};

/**
 * A simple and incomplete backend to request entropy from /dev/random.
 *
 * This backend exposes an additional "filename" property that can be used to
 * set the filename to use to open the backend.
 */

static void entropy_available(void *opaque)
{
37
    RngRandom *s = RNG_RANDOM(opaque);
38

39 40
    while (!QSIMPLEQ_EMPTY(&s->parent.requests)) {
        RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests);
41
        ssize_t len;
42

43 44 45 46 47
        len = read(s->fd, req->data, req->size);
        if (len < 0 && errno == EAGAIN) {
            return;
        }
        g_assert(len != -1);
48

49 50 51 52 53 54
        req->receive_entropy(req->opaque, req->data, len);

        rng_backend_finalize_request(&s->parent, req);
    }

    /* We've drained all requests, the fd handler can be reset. */
55 56 57
    qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
}

58
static void rng_random_request_entropy(RngBackend *b, RngRequest *req)
59
{
60
    RngRandom *s = RNG_RANDOM(b);
61

62
    if (QSIMPLEQ_EMPTY(&s->parent.requests)) {
63 64 65
        /* If there are no pending requests yet, we need to
         * install our fd handler. */
        qemu_set_fd_handler(s->fd, entropy_available, NULL, s);
66 67 68 69 70
    }
}

static void rng_random_opened(RngBackend *b, Error **errp)
{
71
    RngRandom *s = RNG_RANDOM(b);
72 73

    if (s->filename == NULL) {
74 75
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
                   "filename", "a valid filename");
76
    } else {
77
        s->fd = qemu_open(s->filename, O_RDONLY | O_NONBLOCK);
78
        if (s->fd == -1) {
79
            error_setg_file_open(errp, errno, s->filename);
80 81 82 83 84 85
        }
    }
}

static char *rng_random_get_filename(Object *obj, Error **errp)
{
86
    RngRandom *s = RNG_RANDOM(obj);
87

88
    return g_strdup(s->filename);
89 90 91 92 93 94
}

static void rng_random_set_filename(Object *obj, const char *filename,
                                 Error **errp)
{
    RngBackend *b = RNG_BACKEND(obj);
95
    RngRandom *s = RNG_RANDOM(obj);
96 97

    if (b->opened) {
98
        error_setg(errp, QERR_PERMISSION_DENIED);
99 100 101
        return;
    }

102
    g_free(s->filename);
103 104 105 106 107
    s->filename = g_strdup(filename);
}

static void rng_random_init(Object *obj)
{
108
    RngRandom *s = RNG_RANDOM(obj);
109 110 111 112 113 114 115

    object_property_add_str(obj, "filename",
                            rng_random_get_filename,
                            rng_random_set_filename,
                            NULL);

    s->filename = g_strdup("/dev/random");
116
    s->fd = -1;
117 118 119 120
}

static void rng_random_finalize(Object *obj)
{
121
    RngRandom *s = RNG_RANDOM(obj);
122 123

    if (s->fd != -1) {
124
        qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
125
        qemu_close(s->fd);
126 127 128 129 130 131 132 133 134 135 136 137 138
    }

    g_free(s->filename);
}

static void rng_random_class_init(ObjectClass *klass, void *data)
{
    RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);

    rbc->request_entropy = rng_random_request_entropy;
    rbc->opened = rng_random_opened;
}

139
static const TypeInfo rng_random_info = {
140 141
    .name = TYPE_RNG_RANDOM,
    .parent = TYPE_RNG_BACKEND,
142
    .instance_size = sizeof(RngRandom),
143 144 145 146 147 148 149 150 151 152 153
    .class_init = rng_random_class_init,
    .instance_init = rng_random_init,
    .instance_finalize = rng_random_finalize,
};

static void register_types(void)
{
    type_register_static(&rng_random_info);
}

type_init(register_types);