33 #include <dbus/dbus.h>
42 dbus_uint32_t first32;
43 dbus_uint32_t second32;
48 unsigned char bytes[8];
54 #ifdef DBUS_HAVE_INT64
65 static char *get_path_by_unit(DBusConnection *conn,
const char *unit)
67 DBusMessage *msg = NULL;
68 DBusPendingCall *pending = NULL;
72 msg = dbus_message_new_method_call(
73 "org.freedesktop.systemd1",
74 "/org/freedesktop/systemd1",
75 "org.freedesktop.systemd1.Manager",
81 dI(
"Failed to create dbus_message via dbus_message_new_method_call!\n");
87 dbus_message_iter_init_append(msg, &args);
88 if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &unit)) {
89 dI(
"Failed to append unit '%s' string parameter to dbus message!\n", unit);
93 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
94 dI(
"Failed to send message via dbus!\n");
97 if (pending == NULL) {
98 dI(
"Invalid dbus pending call!\n");
102 dbus_connection_flush(conn);
103 dbus_message_unref(msg); msg = NULL;
105 dbus_pending_call_block(pending);
106 msg = dbus_pending_call_steal_reply(pending);
108 dI(
"Failed to steal dbus pending call reply.\n");
111 dbus_pending_call_unref(pending); pending = NULL;
113 if (!dbus_message_iter_init(msg, &args)) {
114 dI(
"Failed to initialize iterator over received dbus message.\n");
118 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
119 dI(
"Expected string argument in reply. Instead received: %s.\n", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
123 dbus_message_iter_get_basic(&args, &path);
124 ret = oscap_strdup(path.
str);
125 dbus_message_unref(msg); msg = NULL;
129 dbus_pending_call_unref(pending);
132 dbus_message_unref(msg);
137 static int get_all_systemd_units(DBusConnection* conn,
int(*callback)(
const char *,
void *),
void *cbarg)
139 DBusMessage *msg = NULL;
140 DBusPendingCall *pending = NULL;
143 msg = dbus_message_new_method_call(
144 "org.freedesktop.systemd1",
145 "/org/freedesktop/systemd1",
146 "org.freedesktop.systemd1.Manager",
150 dI(
"Failed to create dbus_message via dbus_message_new_method_call!\n");
154 DBusMessageIter args, unit_iter;
157 dbus_message_iter_init_append(msg, &args);
159 if (!dbus_connection_send_with_reply(conn, msg, &pending, -1)) {
160 dI(
"Failed to send message via dbus!\n");
163 if (pending == NULL) {
164 dI(
"Invalid dbus pending call!\n");
168 dbus_connection_flush(conn);
169 dbus_message_unref(msg); msg = NULL;
171 dbus_pending_call_block(pending);
172 msg = dbus_pending_call_steal_reply(pending);
174 dI(
"Failed to steal dbus pending call reply.\n");
177 dbus_pending_call_unref(pending); pending = NULL;
179 if (!dbus_message_iter_init(msg, &args)) {
180 dI(
"Failed to initialize iterator over received dbus message.\n");
184 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_ARRAY) {
185 dI(
"Expected array of structs in reply. Instead received: %s.\n", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&args)));
189 dbus_message_iter_recurse(&args, &unit_iter);
191 if (dbus_message_iter_get_arg_type(&unit_iter) != DBUS_TYPE_STRUCT) {
192 dI(
"Expected unit struct as elements in returned array. Instead received: %s.\n", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&unit_iter)));
196 DBusMessageIter unit_name;
197 dbus_message_iter_recurse(&unit_iter, &unit_name);
199 if (dbus_message_iter_get_arg_type(&unit_name) != DBUS_TYPE_STRING) {
200 dI(
"Expected string as the first element in the unit struct. Instead received: %s.\n", dbus_message_type_to_string(dbus_message_iter_get_arg_type(&unit_name)));
205 dbus_message_iter_get_basic(&unit_name, &value);
206 char *unit_name_s = oscap_strdup(value.
str);
207 int cbret = callback(unit_name_s, cbarg);
213 while (dbus_message_iter_next(&unit_iter));
215 dbus_message_unref(msg); msg = NULL;
221 dbus_pending_call_unref(pending);
224 dbus_message_unref(msg);
229 static char *dbus_value_to_string(DBusMessageIter *iter)
231 const int arg_type = dbus_message_iter_get_arg_type(iter);
232 if (dbus_type_is_basic(arg_type)) {
234 dbus_message_iter_get_basic(iter, &value);
239 return oscap_sprintf(
"%c", value.
byt);
241 case DBUS_TYPE_BOOLEAN:
242 return oscap_strdup(value.
bool_val ?
"true" :
"false");
244 case DBUS_TYPE_INT16:
245 return oscap_sprintf(
"%i", value.
i16);
247 case DBUS_TYPE_UINT16:
248 return oscap_sprintf(
"%u", value.
u16);
250 case DBUS_TYPE_INT32:
251 return oscap_sprintf(
"%i", value.
i32);
253 case DBUS_TYPE_UINT32:
254 return oscap_sprintf(
"%u", value.
u32);
256 #ifdef DBUS_HAVE_INT64
257 case DBUS_TYPE_INT64:
258 return oscap_sprintf(
"%lli", value.
i32);
260 case DBUS_TYPE_UINT64:
261 return oscap_sprintf(
"%llu", value.
u32);
264 case DBUS_TYPE_DOUBLE:
265 return oscap_sprintf(
"%g", value.
dbl);
267 case DBUS_TYPE_STRING:
268 case DBUS_TYPE_OBJECT_PATH:
269 case DBUS_TYPE_SIGNATURE:
270 return oscap_strdup(value.
str);
282 dI(
"Encountered unknown dbus basic type!\n");
283 return oscap_strdup(
"error, unknown basic type!");
286 else if (arg_type == DBUS_TYPE_ARRAY) {
287 DBusMessageIter array;
288 dbus_message_iter_recurse(iter, &array);
292 char *element = dbus_value_to_string(&array);
299 ret = oscap_sprintf(
"%s", element);
301 ret = oscap_sprintf(
"%s, %s", old_ret, element);
306 while (dbus_message_iter_next(&array));
319 static DBusConnection *connect_dbus()
321 DBusConnection *conn = NULL;
324 dbus_error_init(&err);
326 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err);
327 if (dbus_error_is_set(&err)) {
328 dI(
"Failed to get DBUS_BUS_SYSTEM connection - %s\n", err.message);
332 dI(
"DBusConnection == NULL!\n");
336 dbus_bus_register(conn, &err);
337 if (dbus_error_is_set(&err)) {
338 dI(
"Failed to register on dbus - %s\n", err.message);
343 dbus_error_free(&err);
348 static void disconnect_dbus(DBusConnection *conn)
Definition: systemdshared.h:40
dbus_int32_t i32
as int32
Definition: systemdshared.h:51
unsigned char byt
as byte
Definition: systemdshared.h:60
dbus_uint32_t u32
as int32
Definition: systemdshared.h:52
int fd
as Unix file descriptor
Definition: systemdshared.h:62
_DBus8ByteStruct eight
as 8-byte struct
Definition: systemdshared.h:58
char * str
as char* (string, object path or signature)
Definition: systemdshared.h:61
Definition: systemdshared.h:46
dbus_int16_t i16
as int16
Definition: systemdshared.h:49
oscap debug helpers private header
dbus_bool_t bool_val
as boolean
Definition: systemdshared.h:53
#define oscap_free(p)
free wrapper
Definition: alloc.h:152
dbus_uint16_t u16
as int16
Definition: systemdshared.h:50
double dbl
as double
Definition: systemdshared.h:59