Merge tag 'pull-qapi-2024-03-26' of https://repo.or.cz/qemu/armbru into staging
[qemu/armbru.git] / backends / rng.c
blob9bbd0c77b6959f2fc03c4fc77908a8237de09b5a
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.h"
15 #include "qapi/error.h"
16 #include "qemu/module.h"
17 #include "qom/object_interfaces.h"
19 void rng_backend_request_entropy(RngBackend *s, size_t size,
20 EntropyReceiveFunc *receive_entropy,
21 void *opaque)
23 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
24 RngRequest *req;
26 if (k->request_entropy) {
27 req = g_malloc(sizeof(*req));
29 req->offset = 0;
30 req->size = size;
31 req->receive_entropy = receive_entropy;
32 req->opaque = opaque;
33 req->data = g_malloc(req->size);
35 k->request_entropy(s, req);
37 QSIMPLEQ_INSERT_TAIL(&s->requests, req, next);
41 static bool rng_backend_prop_get_opened(Object *obj, Error **errp)
43 RngBackend *s = RNG_BACKEND(obj);
45 return s->opened;
48 static void rng_backend_complete(UserCreatable *uc, Error **errp)
50 RngBackend *s = RNG_BACKEND(uc);
51 RngBackendClass *k = RNG_BACKEND_GET_CLASS(s);
52 Error *local_err = NULL;
54 if (k->opened) {
55 k->opened(s, &local_err);
56 if (local_err) {
57 error_propagate(errp, local_err);
58 return;
62 s->opened = true;
65 static void rng_backend_free_request(RngRequest *req)
67 g_free(req->data);
68 g_free(req);
71 static void rng_backend_free_requests(RngBackend *s)
73 RngRequest *req, *next;
75 QSIMPLEQ_FOREACH_SAFE(req, &s->requests, next, next) {
76 rng_backend_free_request(req);
79 QSIMPLEQ_INIT(&s->requests);
82 void rng_backend_finalize_request(RngBackend *s, RngRequest *req)
84 QSIMPLEQ_REMOVE(&s->requests, req, RngRequest, next);
85 rng_backend_free_request(req);
88 static void rng_backend_init(Object *obj)
90 RngBackend *s = RNG_BACKEND(obj);
92 QSIMPLEQ_INIT(&s->requests);
95 static void rng_backend_finalize(Object *obj)
97 RngBackend *s = RNG_BACKEND(obj);
99 rng_backend_free_requests(s);
102 static void rng_backend_class_init(ObjectClass *oc, void *data)
104 UserCreatableClass *ucc = USER_CREATABLE_CLASS(oc);
106 ucc->complete = rng_backend_complete;
108 object_class_property_add_bool(oc, "opened",
109 rng_backend_prop_get_opened,
110 NULL);
113 static const TypeInfo rng_backend_info = {
114 .name = TYPE_RNG_BACKEND,
115 .parent = TYPE_OBJECT,
116 .instance_size = sizeof(RngBackend),
117 .instance_init = rng_backend_init,
118 .instance_finalize = rng_backend_finalize,
119 .class_size = sizeof(RngBackendClass),
120 .class_init = rng_backend_class_init,
121 .abstract = true,
122 .interfaces = (InterfaceInfo[]) {
123 { TYPE_USER_CREATABLE },
128 static void register_types(void)
130 type_register_static(&rng_backend_info);
133 type_init(register_types);