char: move null chardev to its own file
[qemu/ar7.git] / backends / rng-random.c
blobe2a49b0571d79eab335d5a74841d92c50a727b6a
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/osdep.h"
14 #include "sysemu/rng-random.h"
15 #include "sysemu/rng.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/qerror.h"
18 #include "qemu/main-loop.h"
20 struct RngRandom
22 RngBackend parent;
24 int fd;
25 char *filename;
28 /**
29 * A simple and incomplete backend to request entropy from /dev/random.
31 * This backend exposes an additional "filename" property that can be used to
32 * set the filename to use to open the backend.
35 static void entropy_available(void *opaque)
37 RngRandom *s = RNG_RANDOM(opaque);
39 while (!QSIMPLEQ_EMPTY(&s->parent.requests)) {
40 RngRequest *req = QSIMPLEQ_FIRST(&s->parent.requests);
41 ssize_t len;
43 len = read(s->fd, req->data, req->size);
44 if (len < 0 && errno == EAGAIN) {
45 return;
47 g_assert(len != -1);
49 req->receive_entropy(req->opaque, req->data, len);
51 rng_backend_finalize_request(&s->parent, req);
54 /* We've drained all requests, the fd handler can be reset. */
55 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
58 static void rng_random_request_entropy(RngBackend *b, RngRequest *req)
60 RngRandom *s = RNG_RANDOM(b);
62 if (QSIMPLEQ_EMPTY(&s->parent.requests)) {
63 /* If there are no pending requests yet, we need to
64 * install our fd handler. */
65 qemu_set_fd_handler(s->fd, entropy_available, NULL, s);
69 static void rng_random_opened(RngBackend *b, Error **errp)
71 RngRandom *s = RNG_RANDOM(b);
73 if (s->filename == NULL) {
74 error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
75 "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);
84 static char *rng_random_get_filename(Object *obj, Error **errp)
86 RngRandom *s = RNG_RANDOM(obj);
88 return g_strdup(s->filename);
91 static void rng_random_set_filename(Object *obj, const char *filename,
92 Error **errp)
94 RngBackend *b = RNG_BACKEND(obj);
95 RngRandom *s = RNG_RANDOM(obj);
97 if (b->opened) {
98 error_setg(errp, QERR_PERMISSION_DENIED);
99 return;
102 g_free(s->filename);
103 s->filename = g_strdup(filename);
106 static void rng_random_init(Object *obj)
108 RngRandom *s = RNG_RANDOM(obj);
110 object_property_add_str(obj, "filename",
111 rng_random_get_filename,
112 rng_random_set_filename,
113 NULL);
115 s->filename = g_strdup("/dev/random");
116 s->fd = -1;
119 static void rng_random_finalize(Object *obj)
121 RngRandom *s = RNG_RANDOM(obj);
123 if (s->fd != -1) {
124 qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
125 qemu_close(s->fd);
128 g_free(s->filename);
131 static void rng_random_class_init(ObjectClass *klass, void *data)
133 RngBackendClass *rbc = RNG_BACKEND_CLASS(klass);
135 rbc->request_entropy = rng_random_request_entropy;
136 rbc->opened = rng_random_opened;
139 static const TypeInfo rng_random_info = {
140 .name = TYPE_RNG_RANDOM,
141 .parent = TYPE_RNG_BACKEND,
142 .instance_size = sizeof(RngRandom),
143 .class_init = rng_random_class_init,
144 .instance_init = rng_random_init,
145 .instance_finalize = rng_random_finalize,
148 static void register_types(void)
150 type_register_static(&rng_random_info);
153 type_init(register_types);