busctl.c revision 601185b43da638b1c74153deae01dbd518680889
/*-*- 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 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 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, "CONNECTION-NAME");
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_owner(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;
}
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;
}
}
}
int r;
log_error("Expects one argument.");
return -EINVAL;
}
if (r < 0)
else
if (r < 0) {
return 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\n"
"Commands:\n"
" list List bus names\n"
" monitor [SERVICE...] Show bus traffic\n"
" status NAME Show name status\n"
" help Show this help\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_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 '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;
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;
}