busctl.c revision a1ad376761af16da46c9ad90fd8df41c8c5c0976
/*-*- 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 <getopt.h>
#include "strv.h"
#include "util.h"
#include "log.h"
#include "build.h"
#include "pager.h"
#include "xml.h"
#include "path-util.h"
#include "sd-bus.h"
#include "bus-message.h"
#include "bus-internal.h"
#include "bus-util.h"
#include "bus-dump.h"
#include "bus-signature.h"
#include "busctl-introspect.h"
static bool arg_no_pager = false;
static bool arg_legend = true;
static char *arg_address = NULL;
static bool arg_unique = false;
static bool arg_acquired = false;
static bool arg_activatable = false;
static bool arg_show_machine = false;
static char **arg_matches = NULL;
static bool arg_user = false;
static bool arg_list = false;
static bool arg_quiet = false;
static void pager_open_if_enabled(void) {
/* Cache result before we open the pager */
if (arg_no_pager)
return;
pager_open(false);
}
char **i;
int r;
unsigned n = 0;
void *v;
char *k;
r = sd_bus_list_names(bus, (arg_acquired || arg_unique) ? &acquired : NULL, arg_activatable ? &activatable : NULL);
if (r < 0) {
return r;
}
if (!names)
return log_oom();
STRV_FOREACH(i, acquired) {
if (r < 0) {
return r;
}
}
STRV_FOREACH(i, activatable) {
if (r < 0 && r != -EEXIST) {
return r;
}
}
merged[n++] = k;
if (arg_legend) {
printf("%-*s %*s %-*s %-*s %-*s %-*s %-*s %-*s",
(int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER", 13, "CONNECTION", 25, "UNIT", 10, "SESSION", 19, "DESCRIPTION");
if (arg_show_machine)
puts(" MACHINE");
else
putchar('\n');
}
STRV_FOREACH(i, merged) {
/* Activatable */
printf(" - - - (activatable) - - ");
if (arg_show_machine)
puts(" -");
else
putchar('\n');
continue;
}
if (!arg_unique && (*i)[0] == ':')
continue;
if (!arg_acquired && (*i)[0] != ':')
continue;
r = sd_bus_get_name_creds(bus, *i,
if (r >= 0) {
if (r >= 0) {
} else
if (r >= 0) {
_cleanup_free_ char *u = NULL;
u = uid_to_name(uid);
if (!u)
return log_oom();
if (strlen(u) > 16)
u[16] = 0;
printf(" %-16s", u);
} else
if (r >= 0)
else
if (r >= 0) {
_cleanup_free_ char *e;
if (!e)
return log_oom();
printf(" %-25s", e);
} else
if (r >= 0)
else
if (r >= 0)
else
} else
printf(" - - - - - - - ");
if (arg_show_machine) {
if (r >= 0) {
char m[SD_ID128_STRING_MAX];
} else
puts(" -");
} else
putchar('\n');
}
return 0;
}
char **n;
/* We assume the list is sorted. Let's first skip over the
* entry we are looking at. */
for (;;) {
if (!*l)
return;
break;
l++;
}
for (;;) {
bool has_more = false;
if (!*l || !path_startswith(*l, path))
break;
n = l + 1;
for (;;) {
if (!*n || !path_startswith(*n, path))
break;
if (!path_startswith(*n, *l)) {
has_more = true;
break;
}
n++;
}
l = n;
}
}
static void print_tree(const char *prefix, char **l) {
if (arg_list) {
char **i;
STRV_FOREACH(i, l)
return;
}
if (strv_isempty(l)) {
printf("No objects discovered.\n");
return;
}
printf("Only root object discovered.\n");
return;
}
}
int r;
if (r < 0)
return log_oom();
return 0;
}
const XMLIntrospectOps ops = {
};
const char *xml;
int r;
r = sd_bus_call_method(bus, service, path, "org.freedesktop.DBus.Introspectable", "Introspect", &error, &reply, "");
if (r < 0) {
log_error("Failed to introspect object %s of service %s: %s", path, service, bus_error_message(&error, r));
return r;
}
if (r < 0)
return bus_log_parse_error(r);
/* fputs(xml, stdout); */
}
_cleanup_free_ char **l = NULL;
char *m;
int r;
if (!paths)
return log_oom();
if (!done)
return log_oom();
if (!failed)
return log_oom();
m = strdup("/");
if (!m)
return log_oom();
if (r < 0) {
free(m);
return log_oom();
}
for (;;) {
_cleanup_free_ char *p = NULL;
int q;
p = set_steal_first(paths);
if (!p)
break;
if (set_contains(done, p) ||
set_contains(failed, p))
continue;
if (q < 0) {
if (r >= 0)
r = q;
} else
if (q < 0)
return log_oom();
assert(q != 0);
p = NULL;
}
l = set_get_strv(done);
if (!l)
return log_oom();
strv_sort(l);
print_tree(prefix, l);
return r;
}
char **i;
int r = 0;
if (!arg_unique && !arg_acquired)
arg_acquired = true;
bool not_first = false;
if (r < 0) {
return r;
}
STRV_FOREACH(i, names) {
int q;
if (!arg_unique && (*i)[0] == ':')
continue;
if (!arg_acquired && (*i)[0] == ':')
continue;
if (not_first)
printf("\n");
if (q < 0 && r >= 0)
r = q;
not_first = true;
}
} else {
int q;
if (i > argv+1)
printf("\n");
if (argv[2])
if (q < 0 && r >= 0)
r = q;
}
}
return r;
}
return bus_message_dump(m, f, BUS_MESSAGE_DUMP_WITH_HEADER);
}
return bus_message_pcap_frame(m, arg_snaplen, f);
}
bool added_something = false;
char **i;
int r;
_cleanup_free_ char *m = NULL;
if (!service_name_is_valid(*i)) {
log_error("Invalid service name '%s'", *i);
return -EINVAL;
}
if (!m)
return log_oom();
if (r < 0) {
return r;
}
added_something = true;
}
STRV_FOREACH(i, arg_matches) {
if (r < 0) {
return r;
}
added_something = true;
}
if (!added_something) {
if (r < 0) {
return r;
}
}
for (;;) {
r = sd_bus_process(bus, &m);
if (r < 0) {
return r;
}
if (m) {
continue;
}
if (r > 0)
continue;
if (r < 0) {
return r;
}
}
}
int r;
log_error("Refusing to write message data to console, please redirect output to a file.");
return -EINVAL;
}
if (r < 0)
return r;
log_error("Couldn't write capture file.");
return -EIO;
}
return r;
}
int r;
log_error("Expects one argument.");
return -EINVAL;
}
if (r < 0)
else
if (r < 0) {
return r;
}
return 0;
}
char **p;
int r;
assert(m);
assert(x);
p = *x;
for (;;) {
const char *v;
char t;
t = *signature;
v = *p;
if (t == 0)
break;
if (!v) {
log_error("Too few parameters for signature.");
return -EINVAL;
}
signature++;
p++;
switch (t) {
case SD_BUS_TYPE_BOOLEAN:
r = parse_boolean(v);
if (r < 0) {
log_error("Failed to parse as boolean: %s", v);
return r;
}
r = sd_bus_message_append_basic(m, t, &r);
break;
case SD_BUS_TYPE_BYTE: {
uint8_t z;
r = safe_atou8(v, &z);
if (r < 0) {
log_error("Failed to parse as byte (unsigned 8bit integer): %s", v);
return r;
}
r = sd_bus_message_append_basic(m, t, &z);
break;
}
case SD_BUS_TYPE_INT16: {
int16_t z;
r = safe_atoi16(v, &z);
if (r < 0) {
log_error("Failed to parse as signed 16bit integer: %s", v);
return r;
}
r = sd_bus_message_append_basic(m, t, &z);
break;
}
case SD_BUS_TYPE_UINT16: {
uint16_t z;
r = safe_atou16(v, &z);
if (r < 0) {
log_error("Failed to parse as unsigned 16bit integer: %s", v);
return r;
}
r = sd_bus_message_append_basic(m, t, &z);
break;
}
case SD_BUS_TYPE_INT32: {
int32_t z;
r = safe_atoi32(v, &z);
if (r < 0) {
log_error("Failed to parse as signed 32bit integer: %s", v);
return r;
}
r = sd_bus_message_append_basic(m, t, &z);
break;
}
case SD_BUS_TYPE_UINT32: {
uint32_t z;
r = safe_atou32(v, &z);
if (r < 0) {
log_error("Failed to parse as unsigned 32bit integer: %s", v);
return r;
}
r = sd_bus_message_append_basic(m, t, &z);
break;
}
case SD_BUS_TYPE_INT64: {
int64_t z;
r = safe_atoi64(v, &z);
if (r < 0) {
log_error("Failed to parse as signed 64bit integer: %s", v);
return r;
}
r = sd_bus_message_append_basic(m, t, &z);
break;
}
case SD_BUS_TYPE_UINT64: {
uint64_t z;
r = safe_atou64(v, &z);
if (r < 0) {
log_error("Failed to parse as unsigned 64bit integer: %s", v);
return r;
}
r = sd_bus_message_append_basic(m, t, &z);
break;
}
case SD_BUS_TYPE_DOUBLE: {
double z;
r = safe_atod(v, &z);
if (r < 0) {
log_error("Failed to parse as double precision floating point: %s", v);
return r;
}
r = sd_bus_message_append_basic(m, t, &z);
break;
}
case SD_BUS_TYPE_STRING:
case SD_BUS_TYPE_OBJECT_PATH:
case SD_BUS_TYPE_SIGNATURE:
r = sd_bus_message_append_basic(m, t, v);
break;
case SD_BUS_TYPE_ARRAY: {
uint32_t n;
size_t k;
r = safe_atou32(v, &n);
if (r < 0) {
log_error("Failed to parse number of array entries: %s", v);
return r;
}
r = signature_element_length(signature, &k);
if (r < 0) {
log_error("Invalid array signature.");
return r;
}
{
unsigned i;
char s[k + 1];
s[k] = 0;
r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, s);
if (r < 0)
return bus_log_create_error(r);
for (i = 0; i < n; i++) {
r = message_append_cmdline(m, s, &p);
if (r < 0)
return r;
}
}
signature += k;
r = sd_bus_message_close_container(m);
break;
}
case SD_BUS_TYPE_VARIANT:
r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, v);
if (r < 0)
return bus_log_create_error(r);
r = message_append_cmdline(m, v, &p);
if (r < 0)
return r;
r = sd_bus_message_close_container(m);
break;
case SD_BUS_TYPE_STRUCT_BEGIN:
case SD_BUS_TYPE_DICT_ENTRY_BEGIN: {
size_t k;
signature--;
p--;
r = signature_element_length(signature, &k);
if (r < 0) {
return r;
}
{
char s[k-1];
s[k - 2] = 0;
r = sd_bus_message_open_container(m, t == SD_BUS_TYPE_STRUCT_BEGIN ? SD_BUS_TYPE_STRUCT : SD_BUS_TYPE_DICT_ENTRY, s);
if (r < 0)
return bus_log_create_error(r);
r = message_append_cmdline(m, s, &p);
if (r < 0)
return r;
}
signature += k;
r = sd_bus_message_close_container(m);
break;
}
case SD_BUS_TYPE_UNIX_FD:
log_error("UNIX file descriptor not supported as type.");
return -EINVAL;
default:
log_error("Unknown signature type %c.", t);
return -EINVAL;
}
if (r < 0)
return bus_log_create_error(r);
}
*x = p;
return 0;
}
int r;
log_error("Expects at least four arguments.");
return -EINVAL;
}
if (r < 0) {
return r;
}
char **p;
p = argv+6;
if (r < 0)
return r;
if (*p) {
log_error("Too many parameters for signature.");
return -EINVAL;
}
}
if (r < 0) {
return r;
}
r = sd_bus_message_is_empty(reply);
if (r < 0)
return bus_log_parse_error(r);
if (r == 0 && !arg_quiet) {
}
return 0;
}
unsigned n;
int r;
n = strv_length(argv);
if (n < 3) {
log_error("Expects at least three arguments.");
return -EINVAL;
}
if (n < 5) {
bool not_first = false;
r = sd_bus_call_method(bus, argv[1], argv[2], "org.freedesktop.DBus.Properties", "GetAll", &error, &reply, "s", strempty(argv[3]));
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
for (;;) {
const char *name;
if (r < 0)
return bus_log_parse_error(r);
if (r == 0)
break;
if (r < 0)
return bus_log_parse_error(r);
if (not_first)
printf("\n");
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
if (r < 0)
return bus_log_parse_error(r);
not_first = true;
}
if (r < 0)
return bus_log_parse_error(r);
} else {
char **i;
r = sd_bus_call_method(bus, argv[1], argv[2], "org.freedesktop.DBus.Properties", "Get", &error, &reply, "ss", argv[3], *i);
if (r < 0) {
return r;
}
if (r < 0)
return bus_log_parse_error(r);
if (i > argv + 4)
printf("\n");
if (argv[5])
printf("Property %s:\n", *i);
if (r < 0)
return bus_log_parse_error(r);
}
}
return 0;
}
static int help(void) {
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
"Introspect the bus.\n\n"
" -h --help Show this help\n"
" --version Show package version\n"
" --no-pager Do not pipe output into a pager\n"
" --no-legend Do not show the headers and footers\n"
" --system Connect to system bus\n"
" --user Connect to user bus\n"
" -H --host=[USER@]HOST Operate on remote host\n"
" -M --machine=CONTAINER Operate on local container\n"
" --address=ADDRESS Connect to bus specified by address\n"
" --show-machine Show machine ID column in list\n"
" --unique Only show unique names\n"
" --acquired Only show acquired names\n"
" --activatable Only show activatable names\n"
" --match=MATCH Only show matching messages\n"
" --list Don't show tree, but simple object path list\n"
" --quiet Don't show method call reply\n\n"
"Commands:\n"
" list List bus names\n"
" tree [SERVICE...] Show object tree of service\n"
" monitor [SERVICE...] Show bus traffic\n"
" capture [SERVICE...] Capture bus traffic as pcap\n"
" status SERVICE Show service name status\n"
" call SERVICE PATH INTERFACE METHOD [SIGNATURE [ARGUMENTS...]]\n"
" Call a method\n"
" get-property SERVICE PATH [INTERFACE [PROPERTY...]]\n"
" Get property value\n"
" help Show this help\n"
return 0;
}
enum {
ARG_VERSION = 0x100,
};
{},
};
int c, r;
switch (c) {
case 'h':
return help();
case ARG_VERSION:
return 0;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case ARG_NO_LEGEND:
arg_legend = false;
break;
case ARG_USER:
arg_user = true;
break;
case ARG_SYSTEM:
arg_user = false;
break;
case ARG_ADDRESS:
break;
case ARG_SHOW_MACHINE:
arg_show_machine = true;
break;
case ARG_UNIQUE:
arg_unique = true;
break;
case ARG_ACQUIRED:
arg_acquired = true;
break;
case ARG_ACTIVATABLE:
arg_activatable = true;
break;
case ARG_MATCH:
return log_oom();
break;
case ARG_SIZE: {
off_t o;
r = parse_size(optarg, 0, &o);
if (r < 0) {
return r;
}
log_error("Size out of range.");
return -E2BIG;
}
arg_snaplen = (size_t) o;
break;
}
case ARG_LIST:
arg_list = true;
break;
case 'H':
break;
case 'M':
break;
case 'q':
arg_quiet = true;
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
return 1;
}
return help();
return -EINVAL;
}
int r;
log_open();
if (r <= 0)
goto finish;
r = sd_bus_new(&bus);
if (r < 0) {
goto finish;
}
r = sd_bus_set_monitor(bus, true);
if (r < 0) {
goto finish;
}
if (r < 0) {
goto finish;
}
r = sd_bus_negotiate_timestamp(bus, true);
if (r < 0) {
goto finish;
}
r = sd_bus_negotiate_fds(bus, true);
if (r < 0) {
goto finish;
}
}
if (arg_address)
else {
switch (arg_transport) {
case BUS_TRANSPORT_LOCAL:
if (arg_user)
r = bus_set_address_user(bus);
else
r = bus_set_address_system(bus);
break;
case BUS_TRANSPORT_REMOTE:
break;
case BUS_TRANSPORT_CONTAINER:
break;
default:
assert_not_reached("Hmm, unknown transport type.");
}
}
if (r < 0) {
goto finish;
}
r = sd_bus_set_bus_client(bus, true);
if (r < 0) {
goto finish;
}
r = sd_bus_start(bus);
if (r < 0) {
goto finish;
}
pager_close();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}