chiark / gitweb /
busctl: improve output of service creds
authorLennart Poettering <lennart@poettering.net>
Wed, 19 Nov 2014 22:59:26 +0000 (23:59 +0100)
committerLennart Poettering <lennart@poettering.net>
Wed, 19 Nov 2014 23:01:05 +0000 (00:01 +0100)
src/libsystemd/sd-bus/bus-dump.c
src/libsystemd/sd-bus/bus-dump.h
src/libsystemd/sd-bus/busctl.c
src/libsystemd/sd-bus/test-bus-creds.c

index 28fcdda..8814070 100644 (file)
@@ -121,7 +121,7 @@ int bus_message_dump(sd_bus_message *m, FILE *f, unsigned flags) {
                 if (m->monotonic != 0 || m->realtime != 0 || m->seqnum != 0)
                         fputs("\n", f);
 
-                bus_creds_dump(&m->creds, f);
+                bus_creds_dump(&m->creds, f, true);
         }
 
         r = sd_bus_message_rewind(m, !(flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY));
@@ -278,6 +278,7 @@ static void dump_capabilities(
                 sd_bus_creds *c,
                 FILE *f,
                 const char *name,
+                bool terse,
                 int (*has)(sd_bus_creds *c, int capability)) {
 
         unsigned long i, last_cap;
@@ -294,7 +295,7 @@ static void dump_capabilities(
         if (r < 0)
                 return;
 
-        fprintf(f, "  %s=", name);
+        fprintf(f, "%s%s=%s", terse ? "  " : "", name, terse ? "" : ansi_highlight());
         last_cap = cap_last_cap();
 
         for (;;) {
@@ -304,7 +305,7 @@ static void dump_capabilities(
                         if (n > 0)
                                 fputc(' ', f);
                         if (n % 4 == 3)
-                                fputs("\n          ", f);
+                                fprintf(f, terse ? "\n          " : "\n        ");
 
                         t = cap_to_name(i);
                         fprintf(f, "%s", t);
@@ -320,14 +321,18 @@ static void dump_capabilities(
         }
 
         fputs("\n", f);
+
+        if (!terse)
+                fputs(ansi_highlight_off(), f);
 }
 
-int bus_creds_dump(sd_bus_creds *c, FILE *f) {
+int bus_creds_dump(sd_bus_creds *c, FILE *f, bool terse) {
         bool audit_sessionid_is_set = false, audit_loginuid_is_set = false;
         const char *u = NULL, *uu = NULL, *s = NULL, *sl = NULL;
         uid_t owner, audit_loginuid;
         uint32_t audit_sessionid;
         char **cmdline = NULL, **well_known = NULL;
+        const char *prefix, *color, *suffix;
         int r;
 
         assert(c);
@@ -335,45 +340,59 @@ int bus_creds_dump(sd_bus_creds *c, FILE *f) {
         if (!f)
                 f = stdout;
 
+        if (terse) {
+                prefix = "  ";
+                suffix = "";
+                color = "";
+        } else {
+                const char *off;
+
+                prefix = "";
+                color = ansi_highlight();
+
+                off = ansi_highlight_off();
+                suffix = strappenda(off, "\n");
+        }
+
         if (c->mask & SD_BUS_CREDS_PID)
-                fprintf(f, "  PID="PID_FMT, c->pid);
+                fprintf(f, "%sPID=%s"PID_FMT"%s", prefix, color, c->pid, suffix);
         if (c->mask & SD_BUS_CREDS_PID_STARTTIME)
-                fprintf(f, "  PIDStartTime="USEC_FMT, c->pid_starttime);
+                fprintf(f, "%sPIDStartTime=%s"USEC_FMT"%s", prefix, color, c->pid_starttime, suffix);
         if (c->mask & SD_BUS_CREDS_TID)
-                fprintf(f, "  TID="PID_FMT, c->tid);
+                fprintf(f, "%sTID=%s"PID_FMT"%s", prefix, color, c->tid, suffix);
         if (c->mask & SD_BUS_CREDS_UID)
-                fprintf(f, "  UID="UID_FMT, c->uid);
+                fprintf(f, "%sUID=%s"UID_FMT"%s", prefix, color, c->uid, suffix);
         r = sd_bus_creds_get_owner_uid(c, &owner);
         if (r >= 0)
-                fprintf(f, "  OwnerUID="UID_FMT, owner);
+                fprintf(f, "%sOwnerUID=%s"UID_FMT"%s", prefix, color, owner, suffix);
         if (c->mask & SD_BUS_CREDS_GID)
-                fprintf(f, "  GID="GID_FMT, c->gid);
+                fprintf(f, "%sGID=%s"GID_FMT"%s", prefix, color, c->gid, suffix);
 
-        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)
+        if (terse && ((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)
-                fprintf(f, "  Exe=%s", c->exe);
+                fprintf(f, "%sExe=%s%s%s", prefix, color, c->exe, suffix);
         if (c->mask & SD_BUS_CREDS_COMM)
-                fprintf(f, "  Comm=%s", c->comm);
+                fprintf(f, "%sComm=%s%s%s", prefix, color, c->comm, suffix);
         if (c->mask & SD_BUS_CREDS_TID_COMM)
-                fprintf(f, "  TIDComm=%s", c->tid_comm);
+                fprintf(f, "%sTIDComm=%s%s%s", prefix, color, c->tid_comm, suffix);
 
-        if (c->mask & (SD_BUS_CREDS_EXE|SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM))
+        if (terse && (c->mask & (SD_BUS_CREDS_EXE|SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM)))
                 fputs("\n", f);
 
         if (c->mask & SD_BUS_CREDS_SELINUX_CONTEXT)
-                fprintf(f, "  Label=%s", c->label);
+                fprintf(f, "%sLabel=%s%s%s", prefix, color, c->label, suffix);
         if (c->mask & SD_BUS_CREDS_DESCRIPTION)
-                fprintf(f, "  Description=%s", c->description);
+                fprintf(f, "%sDescription=%s%s%s", prefix, color, c->description, suffix);
 
-        if (c->mask & (SD_BUS_CREDS_SELINUX_CONTEXT|SD_BUS_CREDS_DESCRIPTION))
+        if (terse && (c->mask & (SD_BUS_CREDS_SELINUX_CONTEXT|SD_BUS_CREDS_DESCRIPTION)))
                 fputs("\n", f);
 
         if (sd_bus_creds_get_cmdline(c, &cmdline) >= 0) {
                 char **i;
 
-                fputs("  CommandLine={", f);
+                fprintf(f, "%sCommandLine=%s", prefix, color);
                 STRV_FOREACH(i, cmdline) {
                         if (i != cmdline)
                                 fputc(' ', f);
@@ -381,46 +400,46 @@ int bus_creds_dump(sd_bus_creds *c, FILE *f) {
                         fputs(*i, f);
                 }
 
-                fputs("}\n", f);
+                fprintf(f, "%s", suffix);
         }
 
         if (c->mask & SD_BUS_CREDS_CGROUP)
-                fprintf(f, "  CGroup=%s", c->cgroup);
+                fprintf(f, "%sCGroup=%s%s%s", prefix, color, c->cgroup, suffix);
         sd_bus_creds_get_unit(c, &u);
         if (u)
-                fprintf(f, "  Unit=%s", u);
+                fprintf(f, "%sUnit=%s%s%s", prefix, color, u, suffix);
         sd_bus_creds_get_user_unit(c, &uu);
         if (uu)
-                fprintf(f, "  UserUnit=%s", uu);
+                fprintf(f, "%sUserUnit=%s%s%s", prefix, color, uu, suffix);
         sd_bus_creds_get_slice(c, &sl);
         if (sl)
-                fprintf(f, "  Slice=%s", sl);
+                fprintf(f, "%sSlice=%s%s%s", prefix, color, sl, suffix);
         sd_bus_creds_get_session(c, &s);
         if (s)
-                fprintf(f, "  Session=%s", s);
+                fprintf(f, "%sSession=%s%s%s", prefix, color, s, suffix);
 
-        if ((c->mask & SD_BUS_CREDS_CGROUP) || u || uu || sl || s)
+        if (terse && ((c->mask & SD_BUS_CREDS_CGROUP) || u || uu || sl || s))
                 fputs("\n", f);
 
         if (sd_bus_creds_get_audit_login_uid(c, &audit_loginuid) >= 0) {
                 audit_loginuid_is_set = true;
-                fprintf(f, "  AuditLoginUID="UID_FMT, audit_loginuid);
+                fprintf(f, "%sAuditLoginUID=%s"UID_FMT"%s", prefix, color, audit_loginuid, suffix);
         }
         if (sd_bus_creds_get_audit_session_id(c, &audit_sessionid) >= 0) {
                 audit_sessionid_is_set = true;
-                fprintf(f, "  AuditSessionID=%"PRIu32, audit_sessionid);
+                fprintf(f, "%sAuditSessionID=%s%"PRIu32"%s", prefix, color, audit_sessionid, suffix);
         }
 
-        if (audit_loginuid_is_set || audit_sessionid_is_set)
+        if (terse && (audit_loginuid_is_set || audit_sessionid_is_set))
                 fputs("\n", f);
 
         if (c->mask & SD_BUS_CREDS_UNIQUE_NAME)
-                fprintf(f, "  UniqueName=%s", c->unique_name);
+                fprintf(f, "%sUniqueName=%s%s%s", prefix, color, c->unique_name, suffix);
 
         if (sd_bus_creds_get_well_known_names(c, &well_known) >= 0) {
                 char **i;
 
-                fputs("  WellKnownNames={", f);
+                fprintf(f, "%sWellKnownNames=%s", prefix, color);
                 STRV_FOREACH(i, well_known) {
                         if (i != well_known)
                                 fputc(' ', f);
@@ -428,16 +447,16 @@ int bus_creds_dump(sd_bus_creds *c, FILE *f) {
                         fputs(*i, f);
                 }
 
-                fputc('}', f);
+                fprintf(f, "%s", suffix);
         }
 
-        if (c->mask & SD_BUS_CREDS_UNIQUE_NAME || well_known)
+        if (terse && (c->mask & SD_BUS_CREDS_UNIQUE_NAME || well_known))
                 fputc('\n', f);
 
-        dump_capabilities(c, f, "EffectiveCapabilities", sd_bus_creds_has_effective_cap);
-        dump_capabilities(c, f, "PermittedCapabilities", sd_bus_creds_has_permitted_cap);
-        dump_capabilities(c, f, "InheritableCapabilities", sd_bus_creds_has_inheritable_cap);
-        dump_capabilities(c, f, "BoundingCapabilities", sd_bus_creds_has_bounding_cap);
+        dump_capabilities(c, f, "EffectiveCapabilities", terse, sd_bus_creds_has_effective_cap);
+        dump_capabilities(c, f, "PermittedCapabilities", terse, sd_bus_creds_has_permitted_cap);
+        dump_capabilities(c, f, "InheritableCapabilities", terse, sd_bus_creds_has_inheritable_cap);
+        dump_capabilities(c, f, "BoundingCapabilities", terse, sd_bus_creds_has_bounding_cap);
 
         return 0;
 }
index 360c844..d2522ed 100644 (file)
@@ -33,7 +33,7 @@ enum {
 
 int bus_message_dump(sd_bus_message *m, FILE *f, unsigned flags);
 
-int bus_creds_dump(sd_bus_creds *c, FILE *f);
+int bus_creds_dump(sd_bus_creds *c, FILE *f, bool terse);
 
 int bus_pcap_header(size_t snaplen, FILE *f);
 int bus_message_pcap_frame(sd_bus_message *m, size_t snaplen, FILE *f);
index bb89ab9..32ffbce 100644 (file)
@@ -938,7 +938,7 @@ static int status(sd_bus *bus, char *argv[]) {
                 return r;
         }
 
-        bus_creds_dump(creds, NULL);
+        bus_creds_dump(creds, NULL, false);
         return 0;
 }
 
index c4894e8..ff2602b 100644 (file)
@@ -31,7 +31,7 @@ int main(int argc, char *argv[]) {
         r = sd_bus_creds_new_from_pid(&creds, 0, _SD_BUS_CREDS_ALL);
         assert_se(r >= 0);
 
-        bus_creds_dump(creds, NULL);
+        bus_creds_dump(creds, NULL, true);
 
         creds = sd_bus_creds_unref(creds);
 
@@ -39,7 +39,7 @@ int main(int argc, char *argv[]) {
         if (r != -EACCES) {
                 assert_se(r >= 0);
                 putchar('\n');
-                bus_creds_dump(creds, NULL);
+                bus_creds_dump(creds, NULL, true);
         }
 
         return 0;