31 #ifndef OPENSCAP_OVAL_PROBES_SYSTEMDSHARED_H_ 32 #define OPENSCAP_OVAL_PROBES_SYSTEMDSHARED_H_ 38 #include <dbus/dbus.h> 40 #include "oscap_helpers.h" 48 dbus_uint32_t first32;
49 dbus_uint32_t second32;
54 unsigned char bytes[8];
60 #ifdef DBUS_HAVE_INT64 71 static char *get_path_by_unit(DBusConnection *conn,
const char *unit)
73 DBusMessage *msg = NULL;
74 DBusPendingCall *pending = NULL;
78 msg = dbus_message_new_method_call(
79 "org.freedesktop.systemd1",
80 "/org/freedesktop/systemd1",
81 "org.freedesktop.systemd1.Manager",
87 dD(
"Failed to create dbus_message via dbus_message_new_method_call!");
93 dbus_message_iter_init_append(msg, &args);
94 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &unit)) {
95 dD(
"Failed to append unit '%s' string parameter to dbus message!", unit);
99 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
100 dD(
"Failed to send message via dbus!");
103 if (pending == NULL) {
104 dD(
"Invalid dbus pending call!");
108 dbus_connection_flush(conn);
109 dbus_message_unref(msg); msg = NULL;
111 dbus_pending_call_block(pending);
112 msg = dbus_pending_call_steal_reply(pending);
114 dD(
"Failed to steal dbus pending call reply.");
117 dbus_pending_call_unref(pending); pending = NULL;
119 if (!dbus_message_iter_init(msg, &args)) {
120 dD(
"Failed to initialize iterator over received dbus message.");
124 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
125 dD(
"Expected string argument in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
129 dbus_message_iter_get_basic(&args, &path);
130 ret = oscap_strdup(path.
str);
131 dbus_message_unref(msg); msg = NULL;
135 dbus_pending_call_unref(pending);
138 dbus_message_unref(msg);
143 static int get_all_systemd_units(DBusConnection* conn,
int(*callback)(
const char *,
void *),
void *cbarg)
145 DBusMessage *msg = NULL;
146 DBusPendingCall *pending = NULL;
149 msg = dbus_message_new_method_call(
150 "org.freedesktop.systemd1",
151 "/org/freedesktop/systemd1",
152 "org.freedesktop.systemd1.Manager",
156 dD(
"Failed to create dbus_message via dbus_message_new_method_call!");
160 DBusMessageIter args, unit_iter;
163 dbus_message_iter_init_append(msg, &args);
165 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
166 dD(
"Failed to send message via dbus!");
169 if (pending == NULL) {
170 dD(
"Invalid dbus pending call!");
174 dbus_connection_flush(conn);
175 dbus_message_unref(msg); msg = NULL;
177 dbus_pending_call_block(pending);
178 msg = dbus_pending_call_steal_reply(pending);
180 dD(
"Failed to steal dbus pending call reply.");
183 dbus_pending_call_unref(pending); pending = NULL;
185 if (!dbus_message_iter_init(msg, &args)) {
186 dD(
"Failed to initialize iterator over received dbus message.");
190 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_ARRAY) {
191 dD(
"Expected array of structs in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
195 dbus_message_iter_recurse(&args, &unit_iter);
197 if (dbus_message_iter_get_arg_type(&unit_iter) != DBUS_TYPE_STRUCT) {
198 dD(
"Expected unit struct as elements in returned array. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&unit_iter)));
202 DBusMessageIter unit_name;
203 dbus_message_iter_recurse(&unit_iter, &unit_name);
205 if (dbus_message_iter_get_arg_type(&unit_name) != DBUS_TYPE_STRING) {
206 dD(
"Expected string as the first element in the unit struct. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&unit_name)));
211 dbus_message_iter_get_basic(&unit_name, &value);
212 char *unit_name_s = oscap_strdup(value.
str);
213 int cbret = callback(unit_name_s, cbarg);
219 while (dbus_message_iter_next(&unit_iter));
221 dbus_message_unref(msg); msg = NULL;
227 dbus_pending_call_unref(pending);
230 dbus_message_unref(msg);
235 static char *dbus_value_to_string(DBusMessageIter *iter)
237 const int arg_type = dbus_message_iter_get_arg_type(iter);
238 if (dbus_type_is_basic(arg_type)) {
240 dbus_message_iter_get_basic(iter, &value);
245 return oscap_sprintf(
"%c", value.
byt);
247 case DBUS_TYPE_BOOLEAN:
248 return oscap_strdup(value.
bool_val ?
"true" :
"false");
250 case DBUS_TYPE_INT16:
251 return oscap_sprintf(
"%i", value.
i16);
253 case DBUS_TYPE_UINT16:
254 return oscap_sprintf(
"%u", value.
u16);
256 case DBUS_TYPE_INT32:
257 return oscap_sprintf(
"%i", value.
i32);
259 case DBUS_TYPE_UINT32:
260 return oscap_sprintf(
"%u", value.
u32);
262 #ifdef DBUS_HAVE_INT64 263 case DBUS_TYPE_INT64:
264 return oscap_sprintf(
"%lli", value.
i32);
266 case DBUS_TYPE_UINT64:
267 return oscap_sprintf(
"%llu", value.
u32);
270 case DBUS_TYPE_DOUBLE:
271 return oscap_sprintf(
"%g", value.
dbl);
273 case DBUS_TYPE_STRING:
274 case DBUS_TYPE_OBJECT_PATH:
275 case DBUS_TYPE_SIGNATURE:
276 return oscap_strdup(value.
str);
288 dD(
"Encountered unknown dbus basic type!");
289 return oscap_strdup(
"error, unknown basic type!");
292 else if (arg_type == DBUS_TYPE_ARRAY) {
293 DBusMessageIter array;
294 dbus_message_iter_recurse(iter, &array);
298 char *element = dbus_value_to_string(&array);
305 ret = oscap_sprintf(
"%s", element);
307 ret = oscap_sprintf(
"%s, %s", old_ret, element);
312 while (dbus_message_iter_next(&array));
325 static DBusConnection *connect_dbus()
327 DBusConnection *conn = NULL;
330 dbus_error_init(&err);
332 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
333 if (dbus_error_is_set(&err)) {
334 dD(
"Failed to get DBUS_BUS_SYSTEM connection - %s", err.message);
338 dD(
"DBusConnection == NULL!");
342 dbus_bus_register(conn, &err);
343 if (dbus_error_is_set(&err)) {
344 dD(
"Failed to register on dbus - %s", err.message);
349 dbus_error_free(&err);
354 static void disconnect_dbus(DBusConnection *conn)
Definition: systemdshared.h:46
dbus_int32_t i32
as int32
Definition: systemdshared.h:57
unsigned char byt
as byte
Definition: systemdshared.h:66
dbus_uint32_t u32
as int32
Definition: systemdshared.h:58
int fd
as Unix file descriptor
Definition: systemdshared.h:68
_DBus8ByteStruct eight
as 8-byte struct
Definition: systemdshared.h:64
char * str
as char* (string, object path or signature)
Definition: systemdshared.h:67
Definition: systemdshared.h:52
dbus_int16_t i16
as int16
Definition: systemdshared.h:55
oscap debug helpers private header
dbus_bool_t bool_val
as boolean
Definition: systemdshared.h:59
dbus_uint16_t u16
as int16
Definition: systemdshared.h:56
double dbl
as double
Definition: systemdshared.h:65