Signed-off-by: Anthony Liguori <aligu...@us.ibm.com> --- backends/Makefile.objs | 2 +- backends/devrng.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 1 deletions(-) create mode 100644 backends/devrng.c
diff --git a/backends/Makefile.objs b/backends/Makefile.objs index 06e08c7..31589b6 100644 --- a/backends/Makefile.objs +++ b/backends/Makefile.objs @@ -1 +1 @@ -common-obj-y += rng.o +common-obj-y += rng.o devrng.o diff --git a/backends/devrng.c b/backends/devrng.c new file mode 100644 index 0000000..16cc67f --- /dev/null +++ b/backends/devrng.c @@ -0,0 +1,139 @@ +/* + * QEMU Random Number Generator Backend + * + * Copyright IBM, Corp. 2012 + * + * Authors: + * Anthony Liguori <aligu...@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. + */ + +#include "qemu/rng.h" +#include "qerror.h" + +#define TYPE_DEV_RNG "dev-rng" +#define DEV_RNG(obj) OBJECT_CHECK(DevRng, (obj), TYPE_DEV_RNG) + +typedef struct DevRng +{ + RngBackend parent; + + int fd; + char *filename; +} DevRng; + +/** + * A simple and incomplete backend to request entropy from /dev/urandom. + * + * This backend exposes an additional "filename" property that can be used to + * set the filename to use to open the backend. + */ +static void dev_rng_request_entropy(RngBackend *b, size_t size, + EntropyReceiveFunc *receive_entropy, + void *opaque) +{ + DevRng *s = DEV_RNG(b); + uint8_t data[size]; + ssize_t ret; + + g_assert(s->fd != -1); + + do { + ret = read(s->fd, data, size); + } while (ret == -1 && errno == EINTR); + + g_assert(ret != -1); + + /* FIXME: needs to be done via a qemu_set_fd_handler() callback */ + receive_entropy(opaque, data, ret); +} + +static void dev_rng_opened(RngBackend *b, Error **errp) +{ + DevRng *s = DEV_RNG(b); + + if (s->filename == NULL) { + error_set(errp, QERR_INVALID_PARAMETER_VALUE, + "filename", "a valid filename"); + } else { + s->fd = open(s->filename, O_RDONLY); + + if (s->fd == -1) { + error_set(errp, QERR_OPEN_FILE_FAILED, s->filename); + } + } +} + +static char *dev_rng_get_filename(Object *obj, Error **errp) +{ + DevRng *s = DEV_RNG(obj); + + if (s->filename) { + return g_strdup(s->filename); + } + + return NULL; +} + +static void dev_rng_set_filename(Object *obj, const char *filename, + Error **errp) +{ + RngBackend *b = RNG_BACKEND(obj); + DevRng *s = DEV_RNG(obj); + + if (b->opened) { + error_set(errp, QERR_PERMISSION_DENIED); + return; + } + + if (s->filename) { + g_free(s->filename); + } + + s->filename = g_strdup(filename); +} + +static void dev_rng_init(Object *obj) +{ + object_property_add_str(obj, "filename", + dev_rng_get_filename, + dev_rng_set_filename, + NULL); +} + +static void dev_rng_finalize(Object *obj) +{ + DevRng *s = DEV_RNG(obj); + + if (s->fd != -1) { + close(s->fd); + } + + g_free(s->filename); +} + +static void dev_rng_class_init(ObjectClass *klass, void *data) +{ + RngBackendClass *rbc = RNG_BACKEND_CLASS(klass); + + rbc->request_entropy = dev_rng_request_entropy; + rbc->opened = dev_rng_opened; +} + +static TypeInfo dev_rng_info = { + .name = TYPE_DEV_RNG, + .parent = TYPE_RNG_BACKEND, + .instance_size = sizeof(DevRng), + .class_init = dev_rng_class_init, + .instance_init = dev_rng_init, + .instance_finalize = dev_rng_finalize, +}; + +static void register_types(void) +{ + type_register_static(&dev_rng_info); +} + +type_init(register_types); -- 1.7.5.4