2013-03-19 20:03:16 +01:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2013 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
|
|
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/>.
|
|
|
|
***/
|
|
|
|
|
2013-04-24 17:56:28 +02:00
|
|
|
#include <getopt.h>
|
|
|
|
|
2013-03-20 22:56:25 +01:00
|
|
|
#include "strv.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "log.h"
|
2013-04-24 17:56:28 +02:00
|
|
|
#include "build.h"
|
|
|
|
#include "pager.h"
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-03-19 20:03:16 +01:00
|
|
|
#include "sd-bus.h"
|
2013-03-20 22:56:25 +01:00
|
|
|
#include "bus-message.h"
|
|
|
|
#include "bus-internal.h"
|
2013-10-16 06:10:04 +02:00
|
|
|
#include "bus-util.h"
|
2013-11-12 00:12:08 +01:00
|
|
|
#include "bus-dump.h"
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-04-24 17:56:28 +02:00
|
|
|
static bool arg_no_pager = false;
|
2014-02-19 17:35:35 +01:00
|
|
|
static bool arg_legend = true;
|
2013-04-24 17:56:28 +02:00
|
|
|
static char *arg_address = NULL;
|
2013-12-24 18:15:38 +01:00
|
|
|
static bool arg_unique = false;
|
|
|
|
static bool arg_acquired = false;
|
|
|
|
static bool arg_activatable = false;
|
|
|
|
static bool arg_show_machine = false;
|
2013-04-24 17:56:28 +02:00
|
|
|
static char **arg_matches = NULL;
|
2013-11-06 17:33:24 +01:00
|
|
|
static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
|
|
|
|
static char *arg_host = NULL;
|
|
|
|
static bool arg_user = false;
|
2013-04-24 17:56:28 +02:00
|
|
|
|
|
|
|
static void pager_open_if_enabled(void) {
|
|
|
|
|
|
|
|
/* Cache result before we open the pager */
|
|
|
|
if (arg_no_pager)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pager_open(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int list_bus_names(sd_bus *bus, char **argv) {
|
2013-12-03 18:42:51 +01:00
|
|
|
_cleanup_strv_free_ char **acquired = NULL, **activatable = NULL;
|
2013-12-18 02:49:03 +01:00
|
|
|
_cleanup_free_ char **merged = NULL;
|
|
|
|
_cleanup_hashmap_free_ Hashmap *names = NULL;
|
2013-03-19 20:03:16 +01:00
|
|
|
char **i;
|
|
|
|
int r;
|
2013-03-20 22:56:25 +01:00
|
|
|
size_t max_i = 0;
|
2013-12-18 02:49:03 +01:00
|
|
|
unsigned n = 0;
|
|
|
|
void *v;
|
|
|
|
char *k;
|
|
|
|
Iterator iterator;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-04-24 17:56:28 +02:00
|
|
|
assert(bus);
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-12-24 18:15:38 +01:00
|
|
|
r = sd_bus_list_names(bus, (arg_acquired || arg_unique) ? &acquired : NULL, arg_activatable ? &activatable : NULL);
|
2013-03-19 20:03:16 +01:00
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to list names: %s", strerror(-r));
|
2013-04-24 17:56:28 +02:00
|
|
|
return r;
|
2013-03-19 20:03:16 +01:00
|
|
|
}
|
|
|
|
|
2013-04-24 17:56:28 +02:00
|
|
|
pager_open_if_enabled();
|
|
|
|
|
2013-12-18 02:49:03 +01:00
|
|
|
names = hashmap_new(string_hash_func, string_compare_func);
|
|
|
|
if (!names)
|
|
|
|
return log_oom();
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-12-18 02:49:03 +01:00
|
|
|
STRV_FOREACH(i, acquired) {
|
2013-12-03 18:42:51 +01:00
|
|
|
max_i = MAX(max_i, strlen(*i));
|
|
|
|
|
2013-12-18 02:49:03 +01:00
|
|
|
r = hashmap_put(names, *i, INT_TO_PTR(1));
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to add to hashmap: %s", strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
STRV_FOREACH(i, activatable) {
|
2013-03-20 22:56:25 +01:00
|
|
|
max_i = MAX(max_i, strlen(*i));
|
|
|
|
|
2013-12-18 02:49:03 +01:00
|
|
|
r = hashmap_put(names, *i, INT_TO_PTR(2));
|
|
|
|
if (r < 0 && r != -EEXIST) {
|
|
|
|
log_error("Failed to add to hashmap: %s", strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
merged = new(char*, hashmap_size(names) + 1);
|
|
|
|
HASHMAP_FOREACH_KEY(v, k, names, iterator)
|
|
|
|
merged[n++] = k;
|
|
|
|
|
|
|
|
merged[n] = NULL;
|
|
|
|
strv_sort(merged);
|
|
|
|
|
2014-02-19 17:35:35 +01:00
|
|
|
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");
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2014-02-19 17:35:35 +01:00
|
|
|
if (arg_show_machine)
|
|
|
|
puts(" MACHINE");
|
|
|
|
else
|
|
|
|
putchar('\n');
|
|
|
|
}
|
2013-11-28 20:41:55 +01:00
|
|
|
|
2013-12-18 02:49:03 +01:00
|
|
|
STRV_FOREACH(i, merged) {
|
|
|
|
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
|
|
|
|
sd_id128_t mid;
|
2013-12-03 18:42:51 +01:00
|
|
|
|
2013-12-18 02:49:03 +01:00
|
|
|
if (hashmap_get(names, *i) == INT_TO_PTR(2)) {
|
|
|
|
/* Activatable */
|
2013-12-03 18:42:51 +01:00
|
|
|
|
2013-12-18 02:49:03 +01:00
|
|
|
printf("%-*s", (int) max_i, *i);
|
2013-12-24 18:15:38 +01:00
|
|
|
printf(" - - - (activatable) - - ");
|
|
|
|
if (arg_show_machine)
|
2013-12-18 02:49:03 +01:00
|
|
|
puts(" -");
|
2013-12-24 18:15:38 +01:00
|
|
|
else
|
|
|
|
putchar('\n');
|
2013-12-03 18:42:51 +01:00
|
|
|
continue;
|
|
|
|
|
2013-12-18 02:49:03 +01:00
|
|
|
}
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-12-24 18:15:38 +01:00
|
|
|
if (!arg_unique && (*i)[0] == ':')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!arg_acquired && (*i)[0] != ':')
|
2013-04-24 17:56:28 +02:00
|
|
|
continue;
|
2013-03-20 22:56:25 +01:00
|
|
|
|
|
|
|
printf("%-*s", (int) max_i, *i);
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2014-01-22 18:43:03 +01:00
|
|
|
r = sd_bus_get_owner(bus, *i,
|
|
|
|
SD_BUS_CREDS_UID|SD_BUS_CREDS_PID|SD_BUS_CREDS_COMM|
|
|
|
|
SD_BUS_CREDS_UNIQUE_NAME|SD_BUS_CREDS_UNIT|SD_BUS_CREDS_SESSION|
|
|
|
|
SD_BUS_CREDS_CONNECTION_NAME, &creds);
|
2013-03-20 22:56:25 +01:00
|
|
|
if (r >= 0) {
|
2014-01-22 18:43:03 +01:00
|
|
|
const char *unique, *session, *unit, *cn;
|
2013-11-28 17:50:02 +01:00
|
|
|
pid_t pid;
|
|
|
|
uid_t uid;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
r = sd_bus_creds_get_pid(creds, &pid);
|
|
|
|
if (r >= 0) {
|
|
|
|
const char *comm = NULL;
|
2013-03-19 20:03:16 +01:00
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
sd_bus_creds_get_comm(creds, &comm);
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
printf(" %10lu %-15s", (unsigned long) pid, strna(comm));
|
|
|
|
} else
|
2013-11-28 20:41:55 +01:00
|
|
|
fputs(" - - ", stdout);
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
r = sd_bus_creds_get_uid(creds, &uid);
|
|
|
|
if (r >= 0) {
|
|
|
|
_cleanup_free_ char *u = NULL;
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
u = uid_to_name(uid);
|
|
|
|
if (!u)
|
|
|
|
return log_oom();
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-11-28 17:50:02 +01:00
|
|
|
if (strlen(u) > 16)
|
|
|
|
u[16] = 0;
|
|
|
|
|
|
|
|
printf(" %-16s", u);
|
|
|
|
} else
|
2013-11-28 20:41:55 +01:00
|
|
|
fputs(" - ", stdout);
|
2013-03-20 22:56:25 +01:00
|
|
|
|
2013-11-30 04:14:10 +01:00
|
|
|
r = sd_bus_creds_get_unique_name(creds, &unique);
|
|
|
|
if (r >= 0)
|
2013-12-24 18:15:38 +01:00
|
|
|
printf(" %-13s", unique);
|
|
|
|
else
|
|
|
|
fputs(" - ", stdout);
|
|
|
|
|
|
|
|
r = sd_bus_creds_get_unit(creds, &unit);
|
|
|
|
if (r >= 0) {
|
|
|
|
_cleanup_free_ char *e;
|
|
|
|
|
|
|
|
e = ellipsize(unit, 25, 100);
|
|
|
|
if (!e)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
printf(" %-25s", e);
|
|
|
|
} else
|
|
|
|
fputs(" - ", stdout);
|
|
|
|
|
|
|
|
r = sd_bus_creds_get_session(creds, &session);
|
|
|
|
if (r >= 0)
|
|
|
|
printf(" %-10s", session);
|
2013-11-28 20:41:55 +01:00
|
|
|
else
|
2013-12-24 18:15:38 +01:00
|
|
|
fputs(" - ", stdout);
|
2013-11-11 23:45:36 +01:00
|
|
|
|
2014-01-22 18:43:03 +01:00
|
|
|
r = sd_bus_creds_get_connection_name(creds, &cn);
|
|
|
|
if (r >= 0)
|
|
|
|
printf(" %-19s", cn);
|
|
|
|
else
|
|
|
|
fputs(" - ", stdout);
|
|
|
|
|
2013-11-11 23:45:36 +01:00
|
|
|
} else
|
2014-01-22 18:43:03 +01:00
|
|
|
printf(" - - - - - - - ");
|
2013-11-28 20:41:55 +01:00
|
|
|
|
2013-12-24 18:15:38 +01:00
|
|
|
if (arg_show_machine) {
|
2013-11-28 20:41:55 +01:00
|
|
|
r = sd_bus_get_owner_machine_id(bus, *i, &mid);
|
|
|
|
if (r >= 0) {
|
|
|
|
char m[SD_ID128_STRING_MAX];
|
|
|
|
printf(" %s\n", sd_id128_to_string(mid, m));
|
|
|
|
} else
|
|
|
|
puts(" -");
|
2013-12-24 18:15:38 +01:00
|
|
|
} else
|
|
|
|
putchar('\n');
|
2013-03-19 20:03:16 +01:00
|
|
|
}
|
|
|
|
|
2013-04-24 17:56:28 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int monitor(sd_bus *bus, char *argv[]) {
|
2013-11-19 21:37:32 +01:00
|
|
|
bool added_something = false;
|
2013-04-24 17:56:28 +02:00
|
|
|
char **i;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
STRV_FOREACH(i, argv+1) {
|
|
|
|
_cleanup_free_ char *m = NULL;
|
|
|
|
|
|
|
|
if (!service_name_is_valid(*i)) {
|
|
|
|
log_error("Invalid service name '%s'", *i);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
m = strjoin("sender='", *i, "'", NULL);
|
|
|
|
if (!m)
|
|
|
|
return log_oom();
|
|
|
|
|
|
|
|
r = sd_bus_add_match(bus, m, NULL, NULL);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to add match: %s", strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
2013-11-19 21:37:32 +01:00
|
|
|
|
|
|
|
added_something = true;
|
2013-04-24 17:56:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
STRV_FOREACH(i, arg_matches) {
|
|
|
|
r = sd_bus_add_match(bus, *i, NULL, NULL);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to add match: %s", strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
2013-11-19 21:37:32 +01:00
|
|
|
|
|
|
|
added_something = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!added_something) {
|
|
|
|
r = sd_bus_add_match(bus, "", NULL, NULL);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to add match: %s", strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
2013-04-24 17:56:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
_cleanup_bus_message_unref_ sd_bus_message *m = NULL;
|
|
|
|
|
|
|
|
r = sd_bus_process(bus, &m);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to process bus: %s", strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m) {
|
2013-10-29 19:30:55 +01:00
|
|
|
bus_message_dump(m, stdout, true);
|
2013-04-24 17:56:28 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r > 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
r = sd_bus_wait(bus, (uint64_t) -1);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to wait for bus: %s", strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
2013-12-02 18:40:19 +01:00
|
|
|
}
|
2013-04-24 17:56:28 +02:00
|
|
|
|
2013-12-02 18:40:19 +01:00
|
|
|
static int status(sd_bus *bus, char *argv[]) {
|
|
|
|
_cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL;
|
|
|
|
pid_t pid;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(bus);
|
|
|
|
|
|
|
|
if (strv_length(argv) != 2) {
|
|
|
|
log_error("Expects one argument.");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = parse_pid(argv[1], &pid);
|
|
|
|
if (r < 0)
|
|
|
|
r = sd_bus_get_owner(bus, argv[1], _SD_BUS_CREDS_ALL, &creds);
|
|
|
|
else
|
2014-02-19 23:54:58 +01:00
|
|
|
r = sd_bus_creds_new_from_pid(&creds, pid, _SD_BUS_CREDS_ALL);
|
2013-12-02 18:40:19 +01:00
|
|
|
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to get credentials: %s", strerror(-r));
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
bus_creds_dump(creds, NULL);
|
|
|
|
return 0;
|
2013-04-24 17:56:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int help(void) {
|
|
|
|
|
|
|
|
printf("%s [OPTIONS...] {COMMAND} ...\n\n"
|
|
|
|
"Introspect the bus.\n\n"
|
2013-11-06 17:33:24 +01:00
|
|
|
" -h --help Show this help\n"
|
|
|
|
" --version Show package version\n"
|
2013-11-07 21:31:13 +01:00
|
|
|
" --no-pager Do not pipe output into a pager\n"
|
2014-02-19 17:35:35 +01:00
|
|
|
" --no-legend Do not show the headers and footers\n"
|
2013-11-06 17:33:24 +01:00
|
|
|
" --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"
|
2013-12-24 18:15:38 +01:00
|
|
|
" --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"
|
2013-11-29 20:08:54 +01:00
|
|
|
" --match=MATCH Only show matching messages\n\n"
|
2013-04-24 17:56:28 +02:00
|
|
|
"Commands:\n"
|
2013-11-06 17:33:24 +01:00
|
|
|
" list List bus names\n"
|
2013-12-06 13:56:23 +01:00
|
|
|
" monitor [SERVICE...] Show bus traffic\n"
|
2013-12-24 18:15:38 +01:00
|
|
|
" status NAME Show name status\n"
|
2013-12-06 03:33:08 +01:00
|
|
|
" help Show this help\n",
|
2013-04-24 17:56:28 +02:00
|
|
|
program_invocation_short_name);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int parse_argv(int argc, char *argv[]) {
|
|
|
|
|
|
|
|
enum {
|
|
|
|
ARG_VERSION = 0x100,
|
|
|
|
ARG_NO_PAGER,
|
2014-02-19 17:35:35 +01:00
|
|
|
ARG_NO_LEGEND,
|
2013-04-24 17:56:28 +02:00
|
|
|
ARG_SYSTEM,
|
|
|
|
ARG_USER,
|
|
|
|
ARG_ADDRESS,
|
|
|
|
ARG_MATCH,
|
2013-12-24 18:15:38 +01:00
|
|
|
ARG_SHOW_MACHINE,
|
|
|
|
ARG_UNIQUE,
|
|
|
|
ARG_ACQUIRED,
|
|
|
|
ARG_ACTIVATABLE
|
2013-04-24 17:56:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct option options[] = {
|
2013-12-24 18:15:38 +01:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, ARG_VERSION },
|
|
|
|
{ "no-pager", no_argument, NULL, ARG_NO_PAGER },
|
2014-02-19 17:35:35 +01:00
|
|
|
{ "no-legend", no_argument, NULL, ARG_NO_LEGEND },
|
2013-12-24 18:15:38 +01:00
|
|
|
{ "system", no_argument, NULL, ARG_SYSTEM },
|
|
|
|
{ "user", no_argument, NULL, ARG_USER },
|
|
|
|
{ "address", required_argument, NULL, ARG_ADDRESS },
|
|
|
|
{ "show-machine", no_argument, NULL, ARG_SHOW_MACHINE },
|
|
|
|
{ "unique", no_argument, NULL, ARG_UNIQUE },
|
|
|
|
{ "acquired", no_argument, NULL, ARG_ACQUIRED },
|
|
|
|
{ "activatable", no_argument, NULL, ARG_ACTIVATABLE },
|
|
|
|
{ "match", required_argument, NULL, ARG_MATCH },
|
|
|
|
{ "host", required_argument, NULL, 'H' },
|
|
|
|
{ "machine", required_argument, NULL, 'M' },
|
2013-11-06 18:28:39 +01:00
|
|
|
{},
|
2013-04-24 17:56:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
int c;
|
|
|
|
|
|
|
|
assert(argc >= 0);
|
|
|
|
assert(argv);
|
|
|
|
|
2013-11-06 17:33:24 +01:00
|
|
|
while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0) {
|
2013-04-24 17:56:28 +02:00
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
return help();
|
|
|
|
|
|
|
|
case ARG_VERSION:
|
|
|
|
puts(PACKAGE_STRING);
|
|
|
|
puts(SYSTEMD_FEATURES);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case ARG_NO_PAGER:
|
|
|
|
arg_no_pager = true;
|
|
|
|
break;
|
|
|
|
|
2014-02-19 17:35:35 +01:00
|
|
|
case ARG_NO_LEGEND:
|
|
|
|
arg_legend = false;
|
|
|
|
break;
|
|
|
|
|
2013-04-24 17:56:28 +02:00
|
|
|
case ARG_USER:
|
|
|
|
arg_user = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_SYSTEM:
|
|
|
|
arg_user = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_ADDRESS:
|
|
|
|
arg_address = optarg;
|
|
|
|
break;
|
|
|
|
|
2013-12-24 18:15:38 +01:00
|
|
|
case ARG_SHOW_MACHINE:
|
|
|
|
arg_show_machine = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_UNIQUE:
|
|
|
|
arg_unique = true;
|
2013-04-24 17:56:28 +02:00
|
|
|
break;
|
|
|
|
|
2013-12-24 18:15:38 +01:00
|
|
|
case ARG_ACQUIRED:
|
|
|
|
arg_acquired = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ARG_ACTIVATABLE:
|
|
|
|
arg_activatable = true;
|
2013-11-28 20:41:55 +01:00
|
|
|
break;
|
|
|
|
|
2013-04-24 17:56:28 +02:00
|
|
|
case ARG_MATCH:
|
|
|
|
if (strv_extend(&arg_matches, optarg) < 0)
|
|
|
|
return log_oom();
|
|
|
|
break;
|
|
|
|
|
2013-11-06 17:33:24 +01:00
|
|
|
case 'H':
|
|
|
|
arg_transport = BUS_TRANSPORT_REMOTE;
|
|
|
|
arg_host = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
arg_transport = BUS_TRANSPORT_CONTAINER;
|
|
|
|
arg_host = optarg;
|
|
|
|
break;
|
|
|
|
|
2013-04-24 17:56:28 +02:00
|
|
|
case '?':
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
default:
|
2013-11-06 18:28:39 +01:00
|
|
|
assert_not_reached("Unhandled option");
|
2013-04-24 17:56:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 18:15:38 +01:00
|
|
|
if (!arg_unique && !arg_acquired && !arg_activatable)
|
|
|
|
arg_unique = arg_acquired = arg_activatable = true;
|
|
|
|
|
2013-04-24 17:56:28 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int busctl_main(sd_bus *bus, int argc, char *argv[]) {
|
|
|
|
assert(bus);
|
|
|
|
|
|
|
|
if (optind >= argc ||
|
|
|
|
streq(argv[optind], "list"))
|
|
|
|
return list_bus_names(bus, argv + optind);
|
|
|
|
|
|
|
|
if (streq(argv[optind], "monitor"))
|
|
|
|
return monitor(bus, argv + optind);
|
|
|
|
|
2013-12-02 18:40:19 +01:00
|
|
|
if (streq(argv[optind], "status"))
|
|
|
|
return status(bus, argv + optind);
|
|
|
|
|
2013-04-24 17:56:28 +02:00
|
|
|
if (streq(argv[optind], "help"))
|
|
|
|
return help();
|
|
|
|
|
|
|
|
log_error("Unknown command '%s'", argv[optind]);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
_cleanup_bus_unref_ sd_bus *bus = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
log_parse_environment();
|
|
|
|
log_open();
|
|
|
|
|
|
|
|
r = parse_argv(argc, argv);
|
|
|
|
if (r <= 0)
|
|
|
|
goto finish;
|
|
|
|
|
2014-03-19 04:17:00 +01:00
|
|
|
r = sd_bus_new(&bus);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to allocate bus: %s", strerror(-r));
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (streq_ptr(argv[optind], "monitor")) {
|
|
|
|
|
|
|
|
r = sd_bus_set_monitor(bus, true);
|
2013-04-24 17:56:28 +02:00
|
|
|
if (r < 0) {
|
2014-03-19 04:17:00 +01:00
|
|
|
log_error("Failed to set monitor mode: %s", strerror(-r));
|
2013-04-24 17:56:28 +02:00
|
|
|
goto finish;
|
|
|
|
}
|
2014-03-19 04:17:00 +01:00
|
|
|
}
|
2013-04-24 17:56:28 +02:00
|
|
|
|
2014-03-19 04:17:00 +01:00
|
|
|
if (arg_address)
|
2013-04-24 17:56:28 +02:00
|
|
|
r = sd_bus_set_address(bus, arg_address);
|
2014-03-19 04:17:00 +01:00
|
|
|
else {
|
|
|
|
switch (arg_transport) {
|
2013-04-24 17:56:28 +02:00
|
|
|
|
2014-03-19 04:17:00 +01:00
|
|
|
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:
|
|
|
|
r = bus_set_address_system_remote(bus, arg_host);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case BUS_TRANSPORT_CONTAINER:
|
|
|
|
r = bus_set_address_system_container(bus, arg_host);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert_not_reached("Hmm, unknown transport type.");
|
2013-04-24 17:56:28 +02:00
|
|
|
}
|
2014-03-19 04:17:00 +01:00
|
|
|
}
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to set address: %s", strerror(-r));
|
|
|
|
goto finish;
|
|
|
|
}
|
2013-04-24 17:56:28 +02:00
|
|
|
|
2014-03-19 04:17:00 +01:00
|
|
|
r = sd_bus_set_bus_client(bus, true);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to set bus client: %s", strerror(-r));
|
|
|
|
goto finish;
|
|
|
|
}
|
2013-04-24 17:56:28 +02:00
|
|
|
|
2014-03-19 04:17:00 +01:00
|
|
|
r = sd_bus_start(bus);
|
2013-04-24 17:56:28 +02:00
|
|
|
if (r < 0) {
|
|
|
|
log_error("Failed to connect to bus: %s", strerror(-r));
|
|
|
|
goto finish;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = busctl_main(bus, argc, argv);
|
|
|
|
|
|
|
|
finish:
|
|
|
|
pager_close();
|
2013-11-06 17:33:24 +01:00
|
|
|
|
2013-04-24 17:56:28 +02:00
|
|
|
strv_free(arg_matches);
|
2013-03-19 20:03:16 +01:00
|
|
|
|
|
|
|
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
|
|
}
|