busctl.c revision 8fe12d88a8b4d5beb64e5d4efd205043a01b0c13
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 <getopt.h>
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "strv.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "util.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "log.h"
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner#include "build.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "pager.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering#include "sd-bus.h"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen#include "bus-message.h"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen#include "bus-internal.h"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen#include "bus-util.h"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic bool arg_no_pager = false;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic char *arg_address = NULL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic bool arg_no_unique = false;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic char **arg_matches = NULL;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sieversstatic BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic char *arg_host = NULL;
c978343015c787713651dff571acb5207367f5f2Lennart Poetteringstatic bool arg_user = false;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersenstatic void pager_open_if_enabled(void) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace /* Cache result before we open the pager */
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (arg_no_pager)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering pager_open(false);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
1b12a7b5896f94bdf33b3a6661ebabd761ea6adcHarald Hoyerstatic int list_bus_names(sd_bus *bus, char **argv) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering _cleanup_strv_free_ char **l = NULL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering char **i;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering size_t max_i = 0;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering assert(bus);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_bus_list_names(bus, &l);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error("Failed to list names: %s", strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering }
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering pager_open_if_enabled();
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers strv_sort(l);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers STRV_FOREACH(i, l)
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers max_i = MAX(max_i, strlen(*i));
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers printf("%-*s %*s %-*s %-*s CONNECTION\n",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering (int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER");
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers STRV_FOREACH(i, l) {
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers _cleanup_free_ char *owner = NULL;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers pid_t pid;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers uid_t uid;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (arg_no_unique && (*i)[0] == ':')
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers continue;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers printf("%-*s", (int) max_i, *i);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers r = sd_bus_get_owner_pid(bus, *i, &pid);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (r >= 0) {
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers _cleanup_free_ char *comm = NULL;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers printf(" %10lu", (unsigned long) pid);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering get_process_comm(pid, &comm);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers printf(" %-15s", strna(comm));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering } else
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers printf(" - - ");
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_bus_get_owner_uid(bus, *i, &uid);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (r >= 0) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers _cleanup_free_ char *u = NULL;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers u = uid_to_name(uid);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (!u)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return log_oom();
599659860c770058f2eb04d578c521c16e0b1853Lennart Poettering
599659860c770058f2eb04d578c521c16e0b1853Lennart Poettering if (strlen(u) > 16)
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers u[16] = 0;
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers printf(" %-16s", u);
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers } else
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers printf(" - ");
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers r = sd_bus_get_owner(bus, *i, &owner);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r >= 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering printf(" %s\n", owner);
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering else
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers printf(" -\n");
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers }
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering}
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sieversstatic int monitor(sd_bus *bus, char *argv[]) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering char **i;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers int r;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering STRV_FOREACH(i, argv+1) {
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers _cleanup_free_ char *m = NULL;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers if (!service_name_is_valid(*i)) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers log_error("Invalid service name '%s'", *i);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers return -EINVAL;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers }
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering m = strjoin("sender='", *i, "'", NULL);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (!m)
bd5ce8e9fc10a593822344c098ccbe8c47fe34e9Lennart Poettering return log_oom();
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
bd5ce8e9fc10a593822344c098ccbe8c47fe34e9Lennart Poettering r = sd_bus_add_match(bus, m, NULL, NULL);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error("Failed to add match: %s", strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return r;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers }
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers }
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers STRV_FOREACH(i, arg_matches) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_bus_add_match(bus, *i, NULL, NULL);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (r < 0) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers log_error("Failed to add match: %s", strerror(-r));
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers return r;
e2fd5e5ba281a22886fa3797dc6265cca670448bKay Sievers }
e2fd5e5ba281a22886fa3797dc6265cca670448bKay Sievers }
e2fd5e5ba281a22886fa3797dc6265cca670448bKay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers for (;;) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers r = sd_bus_process(bus, &m);
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering if (r < 0) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers log_error("Failed to process bus: %s", strerror(-r));
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers return r;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers }
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (m) {
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering bus_message_dump(m, stdout, true);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers continue;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers }
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (r > 0)
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers continue;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering r = sd_bus_wait(bus, (uint64_t) -1);
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (r < 0) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers log_error("Failed to wait for bus: %s", strerror(-r));
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers return r;
2af32104c47dadf426f2e7697cd7382520476fc5Lennart Poettering }
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers }
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers return -EINVAL;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers}
3062c15117ab6eac5e8b3a3ceb5351ec22ea4481Lennart Poettering
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sieversstatic int help(void) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers printf("%s [OPTIONS...] {COMMAND} ...\n\n"
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers "Introspect the bus.\n\n"
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers " -h --help Show this help\n"
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers " --version Show package version\n"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering " --no-pager Do not pipe output into a pager\n"
e9dd9f9547350c7dc0473583b5c2228dc8f0ab76Jason St. John " --system Connect to system bus\n"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering " --user Connect to user bus\n"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering " -H --host=[USER@]HOST Operate on remote host\n"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering " -M --machine=CONTAINER Operate on local container\n"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering " --address=ADDRESS Connect to bus specified by address\n"
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering " --no-unique Only show well-known names\n"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen " --match=MATCH Only show matching messages\n\n"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen "Commands:\n"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen " list List bus names\n"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen " monitor [SERVICE...] Show bus traffic\n",
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering program_invocation_short_name);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return 0;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen}
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersenstatic int parse_argv(int argc, char *argv[]) {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen enum {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen ARG_VERSION = 0x100,
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen ARG_NO_PAGER,
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen ARG_SYSTEM,
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen ARG_USER,
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen ARG_ADDRESS,
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen ARG_MATCH,
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering ARG_NO_UNIQUE
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering };
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen static const struct option options[] = {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen { "help", no_argument, NULL, 'h' },
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen { "version", no_argument, NULL, ARG_VERSION },
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering { "no-pager", no_argument, NULL, ARG_NO_PAGER },
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering { "system", no_argument, NULL, ARG_SYSTEM },
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering { "user", no_argument, NULL, ARG_USER },
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering { "address", required_argument, NULL, ARG_ADDRESS },
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering { "no-unique", no_argument, NULL, ARG_NO_UNIQUE },
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers { "match", required_argument, NULL, ARG_MATCH },
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers { "host", required_argument, NULL, 'H' },
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers { "machine", required_argument, NULL, 'M' },
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers {},
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers };
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers int c;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers assert(argc >= 0);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers assert(argv);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0) {
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers switch (c) {
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers case 'h':
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers return help();
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers case ARG_VERSION:
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers puts(PACKAGE_STRING);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers puts(SYSTEMD_FEATURES);
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers return 0;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers case ARG_NO_PAGER:
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers arg_no_pager = true;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers break;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers case ARG_USER:
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers arg_user = true;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen break;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen case ARG_SYSTEM:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering arg_user = false;
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek break;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen case ARG_ADDRESS:
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering arg_address = optarg;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen break;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen case ARG_NO_UNIQUE:
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen arg_no_unique = true;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen break;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen case ARG_MATCH:
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (strv_extend(&arg_matches, optarg) < 0)
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen return log_oom();
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen break;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering case 'H':
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering arg_transport = BUS_TRANSPORT_REMOTE;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen arg_host = optarg;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen break;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering case 'M':
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers arg_transport = BUS_TRANSPORT_CONTAINER;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers arg_host = optarg;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers break;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers case '?':
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers return -EINVAL;
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers default:
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers assert_not_reached("Unhandled option");
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers }
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers }
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers return 1;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen}
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busctl_main(sd_bus *bus, int argc, char *argv[]) {
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers assert(bus);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (optind >= argc ||
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering streq(argv[optind], "list"))
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers return list_bus_names(bus, argv + optind);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (streq(argv[optind], "monitor"))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return monitor(bus, argv + optind);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (streq(argv[optind], "help"))
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering return help();
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen log_error("Unknown command '%s'", argv[optind]);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen return -EINVAL;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen}
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringint main(int argc, char *argv[]) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering _cleanup_bus_unref_ sd_bus *bus = NULL;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering int r;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_parse_environment();
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_open();
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = parse_argv(argc, argv);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r <= 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering goto finish;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (arg_address) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = sd_bus_new(&bus);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (r < 0) {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen log_error("Failed to allocate bus: %s", strerror(-r));
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen goto finish;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen }
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen r = sd_bus_set_address(bus, arg_address);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (r < 0) {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen log_error("Failed to set address: %s", strerror(-r));
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen goto finish;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen }
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen r = sd_bus_set_bus_client(bus, true);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error("Failed to set bus client: %s", strerror(-r));
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen goto finish;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen }
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen r = sd_bus_start(bus);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering } else
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = bus_open_transport(arg_transport, arg_host, arg_user, &bus);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering if (r < 0) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error("Failed to connect to bus: %s", strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering goto finish;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen }
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen r = busctl_main(bus, argc, argv);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersenfinish:
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen pager_close();
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen strv_free(arg_matches);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen}
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen