hw/pxa2xx_timer: Explicitly mark fallthroughs
[qemu/pbrook.git] / backends / rng-random.c
blob0d1108811d41e02ce20049a86c6722eab657b899
1 /*
2 * QEMU Random Number Generator Backend
4 * Copyright IBM, Corp. 2012
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10 * See the COPYING file in the top-level directory.
13 #include "qemu/rng-random.h"
14 #include "qemu/rng.h"
15 #include "qapi/qmp/qerror.h"
16 #include "qemu/main-loop.h"
18 struct RndRandom
20 RngBackend parent;
22 int fd;
23 char *filename;
25 EntropyReceiveFunc *receive_func;
26 void *opaque;
27 size_t size;
30 /**
31 * A simple and incomplete backend to request entropy from /dev/random.
33 * This backend exposes an additional "filename" property that can be used to
34 * set the filename to use to open the backend.
37 static void entropy_available(void *opaque)
39 RndRandom *s = RNG_RANDOM(opaque);
40 uint8_t buffer[s->size];
41 ssize_t len;
43 len = read(s->fd, buffer, s->size);
44 g_assert(len != -1);
46 s->receive_func(s->opaque, buffer, len);
47 s->receive_func = NULL;
49 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
52 static void rng_random_request_entropy(RngBackend *b, size_t size,
53 EntropyReceiveFunc *receive_entropy,
54 void *opaque)
56 RndRandom *s = RNG_RANDOM(b);
58 if (s->receive_func) {
59 s->receive_func(s->opaque, NULL, 0);
62 s->receive_func = receive_entropy;
63 s->opaque = opaque;
64 s->size = size;
66 qemu_set_fd_handler(s->fd, entropy_available, NULL, s);
69 static void rng_random_opened(RngBackend *b, Error **errp)
71 RndRandom *s = RNG_RANDOM(b);
73 if (s->filename == NULL) {
74 error_set(errp, QERR_INVALID_PARAMETER_VALUE,
75 "filename", "a valid filename");
76 } else {
77 s->fd = open(s->filename, O_RDONLY | O_NONBLOCK);
79 if (s->fd == -1) {
80 error_set(errp, QERR_OPEN_FILE_FAILED, s->filename);
85 static char *rng_random_get_filename(Object *obj, Error **errp)
87 RndRandom *s = RNG_RANDOM(obj);
89 if (s->filename) {
90 return g_strdup(s->filename);
93 return NULL;
96 static void rng_random_set_filename(Object *obj, const char *filename,
97 Error **errp)
99 RngBackend *b = RNG_BACKEND(obj);
100 RndRandom *s = RNG_RANDOM(obj);
102 if (b->opened) {
103 error_set(errp, QERR_PERMISSION_DENIED);
104 return;
107 if (s->filename) {
108 g_free(s->filename);
111 s->filename = g_strdup(filename);
114 static void rng_random_init(Object *obj)
116 RndRandom *s = RNG_RANDOM(obj);
118 object_property_add_str(obj, "filename",
119 rng_random_get_filename,
120 rng_random_set_filename,
121 NULL);
123 s->filename = g_strdup("/dev/random");
126 static void rng_random_finalize(Object *obj)
128 RndRandom *s = RNG_RANDOM(obj);
130 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
132 if (s->fd != -1) {
133 close(s->fd);
136 g_free(s->filename);
139 static void rng_random_class_init(ObjectClass *klass, void *data)
141 RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
143 rbc->request_entropy = rng_random_request_entropy;
144 rbc->opened = rng_random_opened;
147 static const TypeInfo rng_random_info = {
148 .name = TYPE_RNG_RANDOM,
149 .parent = TYPE_RNG_BACKEND,
150 .instance_size = sizeof(RndRandom),
151 .class_init = rng_random_class_init,
152 .instance_init = rng_random_init,
153 .instance_finalize = rng_random_finalize,
156 static void register_types(void)
158 type_register_static(&rng_random_info);
161 type_init(register_types);