busctl.c revision a4297f08ed774ed6d980b6cadaf844bfb2c7a403
/*-*- 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 "sd-bus.h"
#include "bus-message.h"
#include "bus-internal.h"
#include "bus-util.h"
#include "bus-dump.h"
static bool arg_no_pager = false;
static char *arg_address = NULL;
static bool arg_no_unique = false;
static bool arg_no_machine = false;
static char **arg_matches = NULL;
static bool arg_user = false;
static void pager_open_if_enabled(void) {
/* Cache result before we open the pager */
if (arg_no_pager)
return;
pager_open(false);
}
_cleanup_strv_free_ char **l = NULL;
char **i;
int r;
r = sd_bus_list_names(bus, &l);
if (r < 0) {
return r;
}
strv_sort(l);
STRV_FOREACH(i, l)
printf("%-*s %*s %-*s %-*s %-*s",
if (!arg_no_machine)
puts(" MACHINE");
else
putchar('\n');
STRV_FOREACH(i, l) {
if (arg_no_unique && (*i)[0] == ':')
continue;
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 (owner)
else
} else
printf(" - - - - ");
if (arg_no_machine)
putchar('\n');
else {
if (r >= 0) {
char m[SD_ID128_STRING_MAX];
} else
puts(" -");
}
}
return 0;
}
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) {
bus_message_dump(m, stdout, true);
continue;
}
if (r > 0)
continue;
if (r < 0) {
return r;
}
}
return -EINVAL;
}
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"
" --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"
" --no-unique Only show well-known names\n"
" --no-machine Don't show machine ID column in list\n\n"
" --match=MATCH Only show matching messages\n"
"Commands:\n"
" list List bus names\n"
" monitor [SERVICE...] Show bus traffic\n",
return 0;
}
enum {
ARG_VERSION = 0x100,
};
{},
};
int c;
switch (c) {
case 'h':
return help();
case ARG_VERSION:
return 0;
case ARG_NO_PAGER:
arg_no_pager = true;
break;
case ARG_USER:
arg_user = true;
break;
case ARG_SYSTEM:
arg_user = false;
break;
case ARG_ADDRESS:
break;
case ARG_NO_UNIQUE:
arg_no_unique = true;
break;
case ARG_NO_MACHINE:
arg_no_machine = true;
break;
case ARG_MATCH:
return log_oom();
break;
case 'H':
break;
case 'M':
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;
if (arg_address) {
r = sd_bus_new(&bus);
if (r < 0) {
goto finish;
}
if (r < 0) {
goto finish;
}
r = sd_bus_set_bus_client(bus, true);
if (r < 0) {
goto finish;
}
r = sd_bus_start(bus);
} else
if (r < 0) {
goto finish;
}
pager_close();
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}