bus-dump.c revision 0258159049d5b381c6097533a69a1b29c3ba7ce2
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering/***
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering This file is part of systemd.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Copyright 2013 Lennart Poettering
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering systemd is free software; you can redistribute it and/or modify it
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering under the terms of the GNU Lesser General Public License as published by
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering (at your option) any later version.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering systemd is distributed in the hope that it will be useful, but
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Lesser General Public License for more details.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering You should have received a copy of the GNU Lesser General Public License
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering***/
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "util.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "capability.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "strv.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "audit.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "macro.h"
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "bus-message.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "bus-internal.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "bus-type.h"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen#include "bus-dump.h"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersenstatic char *indent(unsigned level, unsigned flags) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering char *p;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unsigned n, i = 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n = 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY && level > 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering level -= 1;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (flags & BUS_MESSAGE_DUMP_WITH_HEADER)
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering n += 2;
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering p = new(char, n + level*8 + 1);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!p)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return NULL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (flags & BUS_MESSAGE_DUMP_WITH_HEADER) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering p[i++] = ' ';
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering p[i++] = ' ';
1b12a7b5896f94bdf33b3a6661ebabd761ea6adcHarald Hoyer }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering memset(p + i, ' ', level*8);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering p[i + level*8] = 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return p;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringint bus_message_dump(sd_bus_message *m, FILE *f, unsigned flags) {
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering unsigned level = 1;
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering int r;
46e65dcc3a522b5e992e165b5e61d14254026859Lennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(m);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!f)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering f = stdout;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers if (flags & BUS_MESSAGE_DUMP_WITH_HEADER) {
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers fprintf(f,
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers "%s%s%s Type=%s%s%s Endian=%c Flags=%u Version=%u Priority=%lli",
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers m->header->type == SD_BUS_MESSAGE_METHOD_ERROR ? ansi_highlight_red() :
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers m->header->type == SD_BUS_MESSAGE_METHOD_RETURN ? ansi_highlight_green() :
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers m->header->type != SD_BUS_MESSAGE_SIGNAL ? ansi_highlight() : "", draw_special_char(DRAW_TRIANGULAR_BULLET), ansi_highlight_off(),
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers ansi_highlight(), bus_message_type_to_string(m->header->type), ansi_highlight_off(),
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers m->header->endian,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering m->header->flags,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering m->header->version,
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers (long long) m->priority);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers /* Display synthetic message serial number in a more readable
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers * format than (uint32_t) -1 */
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (BUS_MESSAGE_COOKIE(m) == 0xFFFFFFFFULL)
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers fprintf(f, " Cookie=-1");
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers else
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers fprintf(f, " Cookie=%" PRIu64, BUS_MESSAGE_COOKIE(m));
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (m->reply_cookie != 0)
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers fprintf(f, " ReplyCookie=%" PRIu64, m->reply_cookie);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers fputs("\n", f);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (m->sender)
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers fprintf(f, " Sender=%s%s%s", ansi_highlight(), m->sender, ansi_highlight_off());
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers if (m->destination)
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers fprintf(f, " Destination=%s%s%s", ansi_highlight(), m->destination, ansi_highlight_off());
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (m->path)
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers fprintf(f, " Path=%s%s%s", ansi_highlight(), m->path, ansi_highlight_off());
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (m->interface)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, " Interface=%s%s%s", ansi_highlight(), m->interface, ansi_highlight_off());
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden if (m->member)
480a61ae742378a6a9a0eb84cf29c6c1e4ea22ffLennart Poettering fprintf(f, " Member=%s%s%s", ansi_highlight(), m->member, ansi_highlight_off());
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
480a61ae742378a6a9a0eb84cf29c6c1e4ea22ffLennart Poettering if (m->sender || m->destination || m->path || m->interface || m->member)
480a61ae742378a6a9a0eb84cf29c6c1e4ea22ffLennart Poettering fputs("\n", f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (sd_bus_error_is_set(&m->error))
599659860c770058f2eb04d578c521c16e0b1853Lennart Poettering fprintf(f,
599659860c770058f2eb04d578c521c16e0b1853Lennart Poettering " ErrorName=%s%s%s"
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers " ErrorMessage=%s\"%s\"%s\n",
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers ansi_highlight_red(), strna(m->error.name), ansi_highlight_off(),
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John ansi_highlight_red(), strna(m->error.message), ansi_highlight_off());
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (m->monotonic != 0)
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers fprintf(f, " Monotonic="USEC_FMT, m->monotonic);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden if (m->realtime != 0)
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden fprintf(f, " Realtime="USEC_FMT, m->realtime);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden if (m->seqnum != 0)
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden fprintf(f, " SequenceNumber=%"PRIu64, m->seqnum);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden if (m->monotonic != 0 || m->realtime != 0 || m->seqnum != 0)
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden fputs("\n", f);
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering bus_creds_dump(&m->creds, f, true);
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden }
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden r = sd_bus_message_rewind(m, !(flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY));
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden if (r < 0) {
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden log_error("Failed to rewind: %s", strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden }
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden if (!(flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY))
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden fprintf(f, "%sMESSAGE \"%s\" {\n", indent(0, flags), strempty(m->root_container.signature));
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden for (;;) {
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden _cleanup_free_ char *prefix = NULL;
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden const char *contents = NULL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering char type;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers union {
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers uint8_t u8;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uint16_t u16;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers int16_t s16;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers uint32_t u32;
7f35b7bc4a241e9aa3b1512fd345cbf5b2e5a782Kay Sievers int32_t s32;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers uint64_t u64;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers int64_t s64;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers double d64;
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden const char *string;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int i;
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen } basic;
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen r = sd_bus_message_peek_type(m, &type, &contents);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen if (r < 0) {
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen log_error("Failed to peek type: %s", strerror(-r));
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen return r;
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r == 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (level <= 1)
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen break;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers r = sd_bus_message_exit_container(m);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers if (r < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error("Failed to exit container: %s", strerror(-r));
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen return r;
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen }
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen level--;
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen prefix = indent(level, flags);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen if (!prefix)
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen return log_oom();
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen fprintf(f, "%s};\n", prefix);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen continue;
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen }
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen prefix = indent(level, flags);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen if (!prefix)
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen return log_oom();
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen if (bus_type_is_container(type) > 0) {
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen r = sd_bus_message_enter_container(m, type, contents);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen if (r < 0) {
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen log_error("Failed to enter container: %s", strerror(-r));
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen return r;
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen }
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen if (type == SD_BUS_TYPE_ARRAY)
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen fprintf(f, "%sARRAY \"%s\" {\n", prefix, contents);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen else if (type == SD_BUS_TYPE_VARIANT)
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen fprintf(f, "%sVARIANT \"%s\" {\n", prefix, contents);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen else if (type == SD_BUS_TYPE_STRUCT)
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen fprintf(f, "%sSTRUCT \"%s\" {\n", prefix, contents);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen else if (type == SD_BUS_TYPE_DICT_ENTRY)
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen fprintf(f, "%sDICT_ENTRY \"%s\" {\n", prefix, contents);
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen level ++;
2667cc25896a15f82f9f1583e80d416beb1316e1Thomas Hindoe Paaboel Andersen
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden continue;
9ff09bcb86fb125768667aca9bc0b10b1745370aShawn Landden }
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers r = sd_bus_message_read_basic(m, type, &basic);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0) {
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John log_error("Failed to get basic: %s", strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John }
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(r > 0);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen switch (type) {
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers case SD_BUS_TYPE_BYTE:
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers fprintf(f, "%sBYTE %s%u%s;\n", prefix, ansi_highlight(), basic.u8, ansi_highlight_off());
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers break;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers case SD_BUS_TYPE_BOOLEAN:
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers fprintf(f, "%sBOOLEAN %s%s%s;\n", prefix, ansi_highlight(), true_false(basic.i), ansi_highlight_off());
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers break;
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers case SD_BUS_TYPE_INT16:
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers fprintf(f, "%sINT16 %s%i%s;\n", prefix, ansi_highlight(), basic.s16, ansi_highlight_off());
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers break;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen case SD_BUS_TYPE_UINT16:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sUINT16 %s%u%s;\n", prefix, ansi_highlight(), basic.u16, ansi_highlight_off());
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers break;
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers case SD_BUS_TYPE_INT32:
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers fprintf(f, "%sINT32 %s%i%s;\n", prefix, ansi_highlight(), basic.s32, ansi_highlight_off());
9f6eb1cd58f2ddf2eb6ba0e4de056e13d938af75Kay Sievers break;
adacb9575a09981fcf11279f2f661e3fc21e58ffLennart Poettering
adacb9575a09981fcf11279f2f661e3fc21e58ffLennart Poettering case SD_BUS_TYPE_UINT32:
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers fprintf(f, "%sUINT32 %s%u%s;\n", prefix, ansi_highlight(), basic.u32, ansi_highlight_off());
adacb9575a09981fcf11279f2f661e3fc21e58ffLennart Poettering break;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering case SD_BUS_TYPE_INT64:
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers fprintf(f, "%sINT64 %s%"PRIi64"%s;\n", prefix, ansi_highlight(), basic.s64, ansi_highlight_off());
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers break;
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers case SD_BUS_TYPE_UINT64:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sUINT64 %s%"PRIu64"%s;\n", prefix, ansi_highlight(), basic.u64, ansi_highlight_off());
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering break;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen case SD_BUS_TYPE_DOUBLE:
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen fprintf(f, "%sDOUBLE %s%g%s;\n", prefix, ansi_highlight(), basic.d64, ansi_highlight_off());
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering break;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering case SD_BUS_TYPE_STRING:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sSTRING \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_highlight_off());
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering break;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering case SD_BUS_TYPE_OBJECT_PATH:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sOBJECT_PATH \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_highlight_off());
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering break;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering case SD_BUS_TYPE_SIGNATURE:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sSIGNATURE \"%s%s%s\";\n", prefix, ansi_highlight(), basic.string, ansi_highlight_off());
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering break;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen case SD_BUS_TYPE_UNIX_FD:
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen fprintf(f, "%sUNIX_FD %s%i%s;\n", prefix, ansi_highlight(), basic.i, ansi_highlight_off());
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen break;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen default:
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen assert_not_reached("Unknown basic type.");
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen }
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen }
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (!(flags & BUS_MESSAGE_DUMP_SUBTREE_ONLY))
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen fprintf(f, "%s};\n\n", indent(0, flags));
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersenstatic void dump_capabilities(
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen sd_bus_creds *c,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering FILE *f,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering const char *name,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering bool terse,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int (*has)(sd_bus_creds *c, int capability)) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unsigned long i, last_cap;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen unsigned n = 0;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen int r;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen assert(c);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen assert(f);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen assert(name);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen assert(has);
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen i = 0;
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John r = has(c, i);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (r < 0)
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen return;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%s%s=%s", terse ? " " : "", name, terse ? "" : ansi_highlight());
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen last_cap = cap_last_cap();
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering for (;;) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r > 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering _cleanup_cap_free_charp_ char *t;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n > 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fputc(' ', f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (n % 4 == 3)
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering fprintf(f, terse ? "\n " : "\n ");
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering t = cap_to_name(i);
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering fprintf(f, "%s", t);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering n++;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen i++;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (i > last_cap)
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen break;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen r = has(c, i);
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering }
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen fputs("\n", f);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (!terse)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fputs(ansi_highlight_off(), f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersenint bus_creds_dump(sd_bus_creds *c, FILE *f, bool terse) {
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering bool audit_sessionid_is_set = false, audit_loginuid_is_set = false;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering const char *u = NULL, *uu = NULL, *s = NULL, *sl = NULL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uid_t owner, audit_loginuid;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uint32_t audit_sessionid;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering char **cmdline = NULL, **well_known = NULL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering const char *prefix, *color, *suffix;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering assert(c);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering if (!f)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering f = stdout;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (terse) {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen prefix = " ";
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen suffix = "";
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen color = "";
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen } else {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen const char *off;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
e5609878d8802e2469c433be418bcbcf55fbe63bLennart Poettering prefix = "";
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen color = ansi_highlight();
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen off = ansi_highlight_off();
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen suffix = strappenda(off, "\n");
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (c->mask & SD_BUS_CREDS_PID)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sPID=%s"PID_FMT"%s", prefix, color, c->pid, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_PID_STARTTIME)
f75cb30bf97f623417cc7ee4b1bcc5c36cdbeb20Dave Reisner fprintf(f, "%sPIDStartTime=%s"USEC_FMT"%s", prefix, color, c->pid_starttime, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_TID)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sTID=%s"PID_FMT"%s", prefix, color, c->tid, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (terse && ((c->mask & (SD_BUS_CREDS_PID|SD_BUS_CREDS_PID_STARTTIME|SD_BUS_CREDS_TID))))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fputs("\n", f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John if (c->mask & SD_BUS_CREDS_UID)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sUID=%s"UID_FMT"%s", prefix, color, c->uid, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_EUID)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sEUID=%s"UID_FMT"%s", prefix, color, c->euid, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_SUID)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sSUID=%s"UID_FMT"%s", prefix, color, c->suid, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_FSUID)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sFSUID=%s"UID_FMT"%s", prefix, color, c->fsuid, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_bus_creds_get_owner_uid(c, &owner);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r >= 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sOwnerUID=%s"UID_FMT"%s", prefix, color, owner, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_GID)
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John fprintf(f, "%sGID=%s"GID_FMT"%s", prefix, color, c->gid, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_EGID)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sEGID=%s"GID_FMT"%s", prefix, color, c->egid, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_SGID)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sSGID=%s"GID_FMT"%s", prefix, color, c->sgid, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_FSGID)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sFSGID=%s"GID_FMT"%s", prefix, color, c->fsgid, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_SUPPLEMENTARY_GIDS) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unsigned i;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sSupplementaryGIDs=%s", prefix, color);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering for (i = 0; i < c->n_supplementary_gids; i++)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%s" GID_FMT, i > 0 ? " " : "", c->supplementary_gids[i]);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%s", suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (terse && ((c->mask & (SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_SUID|SD_BUS_CREDS_FSUID|
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering SD_BUS_CREDS_GID|SD_BUS_CREDS_EGID|SD_BUS_CREDS_SGID|SD_BUS_CREDS_FSGID|
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering SD_BUS_CREDS_SUPPLEMENTARY_GIDS)) || r >= 0))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fputs("\n", f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_COMM)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sComm=%s%s%s", prefix, color, c->comm, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_TID_COMM)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sTIDComm=%s%s%s", prefix, color, c->tid_comm, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_EXE)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sExe=%s%s%s", prefix, color, c->exe, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (terse && (c->mask & (SD_BUS_CREDS_EXE|SD_BUS_CREDS_COMM|SD_BUS_CREDS_TID_COMM)))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fputs("\n", f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (sd_bus_creds_get_cmdline(c, &cmdline) >= 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering char **i;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sCommandLine=%s", prefix, color);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering STRV_FOREACH(i, cmdline) {
857a493d55f94731394e4d9f61ffce661858e9a0Lennart Poettering if (i != cmdline)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fputc(' ', f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fputs(*i, f);
857a493d55f94731394e4d9f61ffce661858e9a0Lennart Poettering }
7c2d80944afb4196f2eff614e8da1450dffcbeaaThomas Hindoe Paaboel Andersen
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%s", suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_SELINUX_CONTEXT)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sLabel=%s%s%s", prefix, color, c->label, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (c->mask & SD_BUS_CREDS_DESCRIPTION)
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poettering fprintf(f, "%sDescription=%s%s%s", prefix, color, c->description, suffix);
7591abd48079edc1f2adbd922e4b83eb73abeabeLennart Poettering
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John if (terse && (c->mask & (SD_BUS_CREDS_SELINUX_CONTEXT|SD_BUS_CREDS_DESCRIPTION)))
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek fputs("\n", f);
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek if (c->mask & SD_BUS_CREDS_CGROUP)
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek fprintf(f, "%sCGroup=%s%s%s", prefix, color, c->cgroup, suffix);
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek sd_bus_creds_get_unit(c, &u);
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek if (u)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sUnit=%s%s%s", prefix, color, u, suffix);
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek sd_bus_creds_get_user_unit(c, &uu);
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek if (uu)
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John fprintf(f, "%sUserUnit=%s%s%s", prefix, color, uu, suffix);
07a062a79374406e8f6b5a1e2f80c80baf031567Jason St. John sd_bus_creds_get_slice(c, &sl);
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek if (sl)
4f8f66cb4236783cd3cbee97fefc9aaa8469ac08Zbigniew Jędrzejewski-Szmek fprintf(f, "%sSlice=%s%s%s", prefix, color, sl, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering sd_bus_creds_get_session(c, &s);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (s)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sSession=%s%s%s", prefix, color, s, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (terse && ((c->mask & SD_BUS_CREDS_CGROUP) || u || uu || sl || s))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fputs("\n", f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (sd_bus_creds_get_audit_login_uid(c, &audit_loginuid) >= 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering audit_loginuid_is_set = true;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sAuditLoginUID=%s"UID_FMT"%s", prefix, color, audit_loginuid, suffix);
c978343015c787713651dff571acb5207367f5f2Lennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (sd_bus_creds_get_audit_session_id(c, &audit_sessionid) >= 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering audit_sessionid_is_set = true;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sAuditSessionID=%s%"PRIu32"%s", prefix, color, audit_sessionid, suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
c978343015c787713651dff571acb5207367f5f2Lennart Poettering
c978343015c787713651dff571acb5207367f5f2Lennart Poettering if (terse && (audit_loginuid_is_set || audit_sessionid_is_set))
c978343015c787713651dff571acb5207367f5f2Lennart Poettering fputs("\n", f);
c978343015c787713651dff571acb5207367f5f2Lennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (c->mask & SD_BUS_CREDS_UNIQUE_NAME)
c978343015c787713651dff571acb5207367f5f2Lennart Poettering fprintf(f, "%sUniqueName=%s%s%s", prefix, color, c->unique_name, suffix);
c978343015c787713651dff571acb5207367f5f2Lennart Poettering
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering if (sd_bus_creds_get_well_known_names(c, &well_known) >= 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering char **i;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%sWellKnownNames=%s", prefix, color);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering STRV_FOREACH(i, well_known) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (i != well_known)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fputc(' ', f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen fputs(*i, f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fprintf(f, "%s", suffix);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (terse && (c->mask & SD_BUS_CREDS_UNIQUE_NAME || well_known))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fputc('\n', f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering dump_capabilities(c, f, "EffectiveCapabilities", terse, sd_bus_creds_has_effective_cap);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering dump_capabilities(c, f, "PermittedCapabilities", terse, sd_bus_creds_has_permitted_cap);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering dump_capabilities(c, f, "InheritableCapabilities", terse, sd_bus_creds_has_inheritable_cap);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen dump_capabilities(c, f, "BoundingCapabilities", terse, sd_bus_creds_has_bounding_cap);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen/*
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen * For details about the file format, see:
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen *
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering * http://wiki.wireshark.org/Development/LibpcapFileFormat
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering */
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssentypedef struct _packed_ pcap_hdr_s {
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen uint32_t magic_number; /* magic number */
546158bc6f46f8004cc11e81d19d223e0da56730Jan Janssen uint16_t version_major; /* major version number */
c978343015c787713651dff571acb5207367f5f2Lennart Poettering uint16_t version_minor; /* minor version number */
c978343015c787713651dff571acb5207367f5f2Lennart Poettering int32_t thiszone; /* GMT to local correction */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uint32_t sigfigs; /* accuracy of timestamps */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uint32_t snaplen; /* max length of captured packets, in octets */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uint32_t network; /* data link type */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering} pcap_hdr_t ;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringtypedef struct _packed_ pcaprec_hdr_s {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uint32_t ts_sec; /* timestamp seconds */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uint32_t ts_usec; /* timestamp microseconds */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uint32_t incl_len; /* number of octets of packet saved in file */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering uint32_t orig_len; /* actual length of packet */
eb9da376d76b48585b3b63b4f91903b54f7abd36Lennart Poettering} pcaprec_hdr_t;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringint bus_pcap_header(size_t snaplen, FILE *f) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering pcap_hdr_t hdr = {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .magic_number = 0xa1b2c3d4U,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .version_major = 2,
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen .version_minor = 4,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .thiszone = 0, /* UTC */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .sigfigs = 0,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering .network = 231, /* D-Bus */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering };
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!f)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering f = stdout;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(snaplen > 0);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen assert((size_t) (uint32_t) snaplen == snaplen);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering hdr.snaplen = (uint32_t) snaplen;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fwrite(&hdr, 1, sizeof(hdr), f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fflush(f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringint bus_message_pcap_frame(sd_bus_message *m, size_t snaplen, FILE *f) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering struct bus_body_part *part;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering pcaprec_hdr_t hdr = {};
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering struct timeval tv;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering unsigned i;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering size_t w;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!f)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering f = stdout;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(m);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(snaplen > 0);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert((size_t) (uint32_t) snaplen == snaplen);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (m->realtime != 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering timeval_store(&tv, m->realtime);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering else
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert_se(gettimeofday(&tv, NULL) >= 0);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering hdr.ts_sec = tv.tv_sec;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering hdr.ts_usec = tv.tv_usec;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering hdr.orig_len = BUS_MESSAGE_SIZE(m);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering hdr.incl_len = MIN(hdr.orig_len, snaplen);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* write the pcap header */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fwrite(&hdr, 1, sizeof(hdr), f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* write the dbus header */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering w = MIN(BUS_MESSAGE_BODY_BEGIN(m), snaplen);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fwrite(m->header, 1, w, f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering snaplen -= w;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering /* write the dbus body */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering MESSAGE_FOREACH_PART(part, i, m) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (snaplen <= 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering break;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering w = MIN(part->size, snaplen);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fwrite(part->data, 1, w, f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering snaplen -= w;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering fflush(f);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering