2 * QEMU Random Number Generator Backend
4 * Copyright IBM, Corp. 2012
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/qmp/qerror.h"
17 #include "qemu/main-loop.h"
28 * A simple and incomplete backend to request entropy from /dev/random.
30 * This backend exposes an additional "filename" property that can be used to
31 * set the filename to use to open the backend.
34 static void entropy_available(void *opaque
)
36 RndRandom
*s
= RNG_RANDOM(opaque
);
38 while (!QSIMPLEQ_EMPTY(&s
->parent
.requests
)) {
39 RngRequest
*req
= QSIMPLEQ_FIRST(&s
->parent
.requests
);
42 len
= read(s
->fd
, req
->data
, req
->size
);
43 if (len
< 0 && errno
== EAGAIN
) {
48 req
->receive_entropy(req
->opaque
, req
->data
, len
);
50 rng_backend_finalize_request(&s
->parent
, req
);
53 /* We've drained all requests, the fd handler can be reset. */
54 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
57 static void rng_random_request_entropy(RngBackend
*b
, RngRequest
*req
)
59 RndRandom
*s
= RNG_RANDOM(b
);
61 if (QSIMPLEQ_EMPTY(&s
->parent
.requests
)) {
62 /* If there are no pending requests yet, we need to
63 * install our fd handler. */
64 qemu_set_fd_handler(s
->fd
, entropy_available
, NULL
, s
);
68 static void rng_random_opened(RngBackend
*b
, Error
**errp
)
70 RndRandom
*s
= RNG_RANDOM(b
);
72 if (s
->filename
== NULL
) {
73 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
74 "filename", "a valid filename");
76 s
->fd
= qemu_open(s
->filename
, O_RDONLY
| O_NONBLOCK
);
78 error_setg_file_open(errp
, errno
, s
->filename
);
83 static char *rng_random_get_filename(Object
*obj
, Error
**errp
)
85 RndRandom
*s
= RNG_RANDOM(obj
);
87 return g_strdup(s
->filename
);
90 static void rng_random_set_filename(Object
*obj
, const char *filename
,
93 RngBackend
*b
= RNG_BACKEND(obj
);
94 RndRandom
*s
= RNG_RANDOM(obj
);
97 error_setg(errp
, QERR_PERMISSION_DENIED
);
102 s
->filename
= g_strdup(filename
);
105 static void rng_random_init(Object
*obj
)
107 RndRandom
*s
= RNG_RANDOM(obj
);
109 object_property_add_str(obj
, "filename",
110 rng_random_get_filename
,
111 rng_random_set_filename
,
114 s
->filename
= g_strdup("/dev/random");
118 static void rng_random_finalize(Object
*obj
)
120 RndRandom
*s
= RNG_RANDOM(obj
);
123 qemu_set_fd_handler(s
->fd
, NULL
, NULL
, NULL
);
130 static void rng_random_class_init(ObjectClass
*klass
, void *data
)
132 RngBackendClass
*rbc
= RNG_BACKEND_CLASS(klass
);
134 rbc
->request_entropy
= rng_random_request_entropy
;
135 rbc
->opened
= rng_random_opened
;
138 static const TypeInfo rng_random_info
= {
139 .name
= TYPE_RNG_RANDOM
,
140 .parent
= TYPE_RNG_BACKEND
,
141 .instance_size
= sizeof(RndRandom
),
142 .class_init
= rng_random_class_init
,
143 .instance_init
= rng_random_init
,
144 .instance_finalize
= rng_random_finalize
,
147 static void register_types(void)
149 type_register_static(&rng_random_info
);
152 type_init(register_types
);