bus-dump.c revision ca7b42c81652c342288a3e0a10abd885c66ac5b2
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "util.h"
#include "capability.h"
#include "strv.h"
#include "audit.h"
#include "bus-message.h"
#include "bus-internal.h"
#include "bus-type.h"
#include "bus-dump.h"
char *p;
if (!p)
return NULL;
p[0] = p[1] = ' ';
p[2 + level] = 0;
return p;
}
unsigned level = 1;
int r;
assert(m);
if (!f)
f = stdout;
if (with_header) {
fprintf(f,
"%s%s%sType=%s%s%s Endian=%c Flags=%u Version=%u Priority=%lli",
m->header->type != SD_BUS_MESSAGE_SIGNAL ? ansi_highlight() : "", draw_special_char(DRAW_TRIANGULAR_BULLET), ansi_highlight_off(),
(long long) m->priority);
/* Display synthetic message serial number in a more readable
* format than (uint32_t) -1 */
if (BUS_MESSAGE_COOKIE(m) == 0xFFFFFFFFULL)
fprintf(f, " Cookie=-1");
else
if (m->reply_cookie != 0)
fputs("\n", f);
if (m->sender)
if (m->destination)
if (m->path)
if (m->interface)
if (m->member)
fputs("\n", f);
if (sd_bus_error_is_set(&m->error))
fprintf(f,
" ErrorName=%s%s%s"
" ErrorMessage=%s\"%s\"%s\n",
if (m->monotonic != 0)
if (m->realtime != 0)
if (m->seqnum != 0)
fputs("\n", f);
bus_creds_dump(&m->creds, f);
}
r = sd_bus_message_rewind(m, true);
if (r < 0) {
return r;
}
for (;;) {
char type;
union {
double d64;
const char *string;
int i;
} basic;
if (r < 0) {
return r;
}
if (r == 0) {
if (level <= 1)
break;
r = sd_bus_message_exit_container(m);
if (r < 0) {
return r;
}
level--;
if (!prefix)
return log_oom();
continue;
}
if (!prefix)
return log_oom();
if (bus_type_is_container(type) > 0) {
if (r < 0) {
return r;
}
if (type == SD_BUS_TYPE_ARRAY)
else if (type == SD_BUS_TYPE_VARIANT)
else if (type == SD_BUS_TYPE_STRUCT)
else if (type == SD_BUS_TYPE_DICT_ENTRY)
level ++;
continue;
}
if (r < 0) {
return r;
}
assert(r > 0);
switch (type) {
case SD_BUS_TYPE_BYTE:
break;
case SD_BUS_TYPE_BOOLEAN:
fprintf(f, "%sBOOLEAN %s%s%s;\n", prefix, ansi_highlight(), true_false(basic.i), ansi_highlight_off());
break;
case SD_BUS_TYPE_INT16:
break;
case SD_BUS_TYPE_UINT16:
break;
case SD_BUS_TYPE_INT32:
break;
case SD_BUS_TYPE_UINT32:
break;
case SD_BUS_TYPE_INT64:
fprintf(f, "%sINT64 %s%lli%s;\n", prefix, ansi_highlight(), (long long) basic.s64, ansi_highlight_off());
break;
case SD_BUS_TYPE_UINT64:
fprintf(f, "%sUINT64 %s%llu%s;\n", prefix, ansi_highlight(), (unsigned long long) basic.u64, ansi_highlight_off());
break;
case SD_BUS_TYPE_DOUBLE:
break;
case SD_BUS_TYPE_STRING:
break;
case SD_BUS_TYPE_OBJECT_PATH:
fprintf(f, "%sOBJECT_PATH \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_highlight_off());
break;
case SD_BUS_TYPE_SIGNATURE:
fprintf(f, "%sSIGNATURE \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_highlight_off());
break;
case SD_BUS_TYPE_UNIX_FD:
break;
default:
assert_not_reached("Unknown basic type.");
}
}
fprintf(f, " };\n\n");
return 0;
}
static void dump_capabilities(
sd_bus_creds *c,
FILE *f,
const char *name,
unsigned long i, last_cap;
unsigned n = 0;
int r;
assert(c);
assert(f);
i = 0;
r = has(c, i);
if (r < 0)
return;
last_cap = cap_last_cap();
for (;;) {
if (r > 0) {
_cleanup_cap_free_charp_ char *t;
if (n > 0)
fputc(' ', f);
if (n % 4 == 3)
fputs("\n ", f);
t = cap_to_name(i);
fprintf(f, "%s", t);
n++;
}
i++;
if (i > last_cap)
break;
r = has(c, i);
}
fputs("\n", f);
}
bool audit_sessionid_is_set = false, audit_loginuid_is_set = false;
int r;
assert(c);
if (!f)
f = stdout;
if (c->mask & SD_BUS_CREDS_PID)
if (c->mask & SD_BUS_CREDS_PID_STARTTIME)
if (c->mask & SD_BUS_CREDS_TID)
if (c->mask & SD_BUS_CREDS_UID)
r = sd_bus_creds_get_owner_uid(c, &owner);
if (r >= 0)
if (c->mask & SD_BUS_CREDS_GID)
if ((c->mask & (SD_BUS_CREDS_PID|SD_BUS_CREDS_PID_STARTTIME|SD_BUS_CREDS_TID|SD_BUS_CREDS_UID|SD_BUS_CREDS_GID)) || r >= 0)
fputs("\n", f);
if (c->mask & SD_BUS_CREDS_EXE)
if (c->mask & SD_BUS_CREDS_COMM)
if (c->mask & SD_BUS_CREDS_TID_COMM)
fputs("\n", f);
if (c->mask & SD_BUS_CREDS_SELINUX_CONTEXT)
if (c->mask & SD_BUS_CREDS_CONNECTION_NAME)
fputs("\n", f);
if (sd_bus_creds_get_cmdline(c, &cmdline) >= 0) {
char **i;
fputs(" CommandLine={", f);
STRV_FOREACH(i, cmdline) {
if (i != cmdline)
fputc(' ', f);
fputs(*i, f);
}
fputs("}\n", f);
}
if (c->mask & SD_BUS_CREDS_CGROUP)
sd_bus_creds_get_unit(c, &u);
if (u)
fprintf(f, " Unit=%s", u);
sd_bus_creds_get_user_unit(c, &uu);
if (uu)
sd_bus_creds_get_slice(c, &sl);
if (sl)
sd_bus_creds_get_session(c, &s);
if (s)
fprintf(f, " Session=%s", s);
fputs("\n", f);
if (sd_bus_creds_get_audit_login_uid(c, &audit_loginuid) >= 0) {
audit_loginuid_is_set = true;
}
if (sd_bus_creds_get_audit_session_id(c, &audit_sessionid) >= 0) {
audit_sessionid_is_set = true;
}
fputs("\n", f);
if (c->mask & SD_BUS_CREDS_UNIQUE_NAME)
if (sd_bus_creds_get_well_known_names(c, &well_known) >= 0) {
char **i;
fputs(" WellKnownNames={", f);
STRV_FOREACH(i, well_known) {
if (i != well_known)
fputc(' ', f);
fputs(*i, f);
}
fputc('}', f);
}
fputc('\n', f);
return 0;
}