31 #ifndef OPENSCAP_OVAL_PROBES_SYSTEMDSHARED_H_
32 #define OPENSCAP_OVAL_PROBES_SYSTEMDSHARED_H_
38 #include <dbus/dbus.h>
47 dbus_uint32_t first32;
48 dbus_uint32_t second32;
53 unsigned char bytes[8];
59 #ifdef DBUS_HAVE_INT64
70 static char *get_path_by_unit(DBusConnection *conn,
const char *unit)
72 DBusMessage *msg = NULL;
73 DBusPendingCall *pending = NULL;
77 msg = dbus_message_new_method_call(
78 "org.freedesktop.systemd1",
79 "/org/freedesktop/systemd1",
80 "org.freedesktop.systemd1.Manager",
86 dI(
"Failed to create dbus_message via dbus_message_new_method_call!");
92 dbus_message_iter_init_append(msg, &args);
93 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &unit)) {
94 dI(
"Failed to append unit '%s' string parameter to dbus message!", unit);
98 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
99 dI(
"Failed to send message via dbus!");
102 if (pending == NULL) {
103 dI(
"Invalid dbus pending call!");
107 dbus_connection_flush(conn);
108 dbus_message_unref(msg); msg = NULL;
110 dbus_pending_call_block(pending);
111 msg = dbus_pending_call_steal_reply(pending);
113 dI(
"Failed to steal dbus pending call reply.");
116 dbus_pending_call_unref(pending); pending = NULL;
118 if (!dbus_message_iter_init(msg, &args)) {
119 dI(
"Failed to initialize iterator over received dbus message.");
123 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
124 dI(
"Expected string argument in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
128 dbus_message_iter_get_basic(&args, &path);
129 ret = oscap_strdup(path.
str);
130 dbus_message_unref(msg); msg = NULL;
134 dbus_pending_call_unref(pending);
137 dbus_message_unref(msg);
142 static int get_all_systemd_units(DBusConnection* conn,
int(*callback)(
const char *,
void *),
void *cbarg)
144 DBusMessage *msg = NULL;
145 DBusPendingCall *pending = NULL;
148 msg = dbus_message_new_method_call(
149 "org.freedesktop.systemd1",
150 "/org/freedesktop/systemd1",
151 "org.freedesktop.systemd1.Manager",
155 dI(
"Failed to create dbus_message via dbus_message_new_method_call!");
159 DBusMessageIter args, unit_iter;
162 dbus_message_iter_init_append(msg, &args);
164 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
165 dI(
"Failed to send message via dbus!");
168 if (pending == NULL) {
169 dI(
"Invalid dbus pending call!");
173 dbus_connection_flush(conn);
174 dbus_message_unref(msg); msg = NULL;
176 dbus_pending_call_block(pending);
177 msg = dbus_pending_call_steal_reply(pending);
179 dI(
"Failed to steal dbus pending call reply.");
182 dbus_pending_call_unref(pending); pending = NULL;
184 if (!dbus_message_iter_init(msg, &args)) {
185 dI(
"Failed to initialize iterator over received dbus message.");
189 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_ARRAY) {
190 dI(
"Expected array of structs in reply. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
194 dbus_message_iter_recurse(&args, &unit_iter);
196 if (dbus_message_iter_get_arg_type(&unit_iter) != DBUS_TYPE_STRUCT) {
197 dI(
"Expected unit struct as elements in returned array. Instead received: %s.", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&unit_iter)));
201 DBusMessageIter unit_name;
202 dbus_message_iter_recurse(&unit_iter, &unit_name);
204 if (dbus_message_iter_get_arg_type(&unit_name) != DBUS_TYPE_STRING) {
205 dI(
"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)));
210 dbus_message_iter_get_basic(&unit_name, &value);
211 char *unit_name_s = oscap_strdup(value.
str);
212 int cbret = callback(unit_name_s, cbarg);
218 while (dbus_message_iter_next(&unit_iter));
220 dbus_message_unref(msg); msg = NULL;
226 dbus_pending_call_unref(pending);
229 dbus_message_unref(msg);
234 static char *dbus_value_to_string(DBusMessageIter *iter)
236 const int arg_type = dbus_message_iter_get_arg_type(iter);
237 if (dbus_type_is_basic(arg_type)) {
239 dbus_message_iter_get_basic(iter, &value);
244 return oscap_sprintf(
"%c", value.
byt);
246 case DBUS_TYPE_BOOLEAN:
247 return oscap_strdup(value.
bool_val ?
"true" :
"false");
249 case DBUS_TYPE_INT16:
250 return oscap_sprintf(
"%i", value.
i16);
252 case DBUS_TYPE_UINT16:
253 return oscap_sprintf(
"%u", value.
u16);
255 case DBUS_TYPE_INT32:
256 return oscap_sprintf(
"%i", value.
i32);
258 case DBUS_TYPE_UINT32:
259 return oscap_sprintf(
"%u", value.
u32);
261 #ifdef DBUS_HAVE_INT64
262 case DBUS_TYPE_INT64:
263 return oscap_sprintf(
"%lli", value.
i32);
265 case DBUS_TYPE_UINT64:
266 return oscap_sprintf(
"%llu", value.
u32);
269 case DBUS_TYPE_DOUBLE:
270 return oscap_sprintf(
"%g", value.
dbl);
272 case DBUS_TYPE_STRING:
273 case DBUS_TYPE_OBJECT_PATH:
274 case DBUS_TYPE_SIGNATURE:
275 return oscap_strdup(value.
str);
287 dI(
"Encountered unknown dbus basic type!");
288 return oscap_strdup(
"error, unknown basic type!");
291 else if (arg_type == DBUS_TYPE_ARRAY) {
292 DBusMessageIter array;
293 dbus_message_iter_recurse(iter, &array);
297 char *element = dbus_value_to_string(&array);
304 ret = oscap_sprintf(
"%s", element);
306 ret = oscap_sprintf(
"%s, %s", old_ret, element);
311 while (dbus_message_iter_next(&array));
324 static DBusConnection *connect_dbus()
326 DBusConnection *conn = NULL;
329 dbus_error_init(&err);
331 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
332 if (dbus_error_is_set(&err)) {
333 dI(
"Failed to get DBUS_BUS_SYSTEM connection - %s", err.message);
337 dI(
"DBusConnection == NULL!");
341 dbus_bus_register(conn, &err);
342 if (dbus_error_is_set(&err)) {
343 dI(
"Failed to register on dbus - %s", err.message);
348 dbus_error_free(&err);
353 static void disconnect_dbus(DBusConnection *conn)
Definition: systemdshared.h:45
dbus_int32_t i32
as int32
Definition: systemdshared.h:56
unsigned char byt
as byte
Definition: systemdshared.h:65
dbus_uint32_t u32
as int32
Definition: systemdshared.h:57
int fd
as Unix file descriptor
Definition: systemdshared.h:67
_DBus8ByteStruct eight
as 8-byte struct
Definition: systemdshared.h:63
char * str
as char* (string, object path or signature)
Definition: systemdshared.h:66
Definition: systemdshared.h:51
dbus_int16_t i16
as int16
Definition: systemdshared.h:54
oscap debug helpers private header
dbus_bool_t bool_val
as boolean
Definition: systemdshared.h:58
dbus_uint16_t u16
as int16
Definition: systemdshared.h:55
double dbl
as double
Definition: systemdshared.h:64