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.h"
15 #include "qapi/qmp/qerror.h"
16 #include "qom/object_interfaces.h"
18 void rng_backend_request_entropy(RngBackend
*s
, size_t size
,
19 EntropyReceiveFunc
*receive_entropy
,
22 RngBackendClass
*k
= RNG_BACKEND_GET_CLASS(s
);
24 if (k
->request_entropy
) {
25 k
->request_entropy(s
, size
, receive_entropy
, opaque
);
29 static bool rng_backend_prop_get_opened(Object
*obj
, Error
**errp
)
31 RngBackend
*s
= RNG_BACKEND(obj
);
36 static void rng_backend_complete(UserCreatable
*uc
, Error
**errp
)
38 object_property_set_bool(OBJECT(uc
), true, "opened", errp
);
41 static void rng_backend_prop_set_opened(Object
*obj
, bool value
, Error
**errp
)
43 RngBackend
*s
= RNG_BACKEND(obj
);
44 RngBackendClass
*k
= RNG_BACKEND_GET_CLASS(s
);
45 Error
*local_err
= NULL
;
47 if (value
== s
->opened
) {
51 if (!value
&& s
->opened
) {
52 error_setg(errp
, QERR_PERMISSION_DENIED
);
57 k
->opened(s
, &local_err
);
59 error_propagate(errp
, local_err
);
67 static void rng_backend_free_request(RngRequest
*req
)
73 static void rng_backend_free_requests(RngBackend
*s
)
77 for (i
= s
->requests
; i
; i
= i
->next
) {
78 rng_backend_free_request(i
->data
);
81 g_slist_free(s
->requests
);
85 void rng_backend_finalize_request(RngBackend
*s
, RngRequest
*req
)
87 s
->requests
= g_slist_remove(s
->requests
, req
);
88 rng_backend_free_request(req
);
91 static void rng_backend_init(Object
*obj
)
93 object_property_add_bool(obj
, "opened",
94 rng_backend_prop_get_opened
,
95 rng_backend_prop_set_opened
,
99 static void rng_backend_finalize(Object
*obj
)
101 RngBackend
*s
= RNG_BACKEND(obj
);
103 rng_backend_free_requests(s
);
106 static void rng_backend_class_init(ObjectClass
*oc
, void *data
)
108 UserCreatableClass
*ucc
= USER_CREATABLE_CLASS(oc
);
110 ucc
->complete
= rng_backend_complete
;
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
,
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
);