busctl.c revision 8fe12d88a8b4d5beb64e5d4efd205043a01b0c13
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering This file is part of systemd.
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering Copyright 2013 Lennart Poettering
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 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 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 Poetteringstatic bool arg_no_pager = false;
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic bool arg_no_unique = false;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sieversstatic BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
c978343015c787713651dff571acb5207367f5f2Lennart Poetteringstatic bool arg_user = false;
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersenstatic void pager_open_if_enabled(void) {
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace /* Cache result before we open the pager */
1b12a7b5896f94bdf33b3a6661ebabd761ea6adcHarald Hoyerstatic int list_bus_names(sd_bus *bus, char **argv) {
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error("Failed to list names: %s", strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering (int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER");
2311eb2ff0c3ff80ec3645b02c97170c9a565454Kay Sievers if (r >= 0) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (r >= 0) {
bd5ce8e9fc10a593822344c098ccbe8c47fe34e9Lennart Poettering r = sd_bus_add_match(bus, m, NULL, NULL);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error("Failed to add match: %s", strerror(-r));
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));
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers log_error("Failed to process bus: %s", strerror(-r));
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers if (r < 0) {
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sievers log_error("Failed to wait for bus: %s", strerror(-r));
f18ca9dcdeda247e208f7143e834fd2fb2070d80Kay Sieversstatic int help(void) {
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 " list List bus names\n"
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen " monitor [SERVICE...] Show bus traffic\n",
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersenstatic int parse_argv(int argc, char *argv[]) {
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 { "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 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0) {
2f6a59070559786428d9eaf199ae3d61772b2225Kay Sievers switch (c) {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (strv_extend(&arg_matches, optarg) < 0)
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poetteringstatic int busctl_main(sd_bus *bus, int argc, char *argv[]) {
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen if (streq(argv[optind], "monitor"))
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen log_error("Unknown command '%s'", argv[optind]);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen log_error("Failed to allocate bus: %s", strerror(-r));
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen r = sd_bus_set_address(bus, arg_address);
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen log_error("Failed to set address: %s", strerror(-r));
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen r = sd_bus_set_bus_client(bus, true);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error("Failed to set bus client: %s", strerror(-r));
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering r = bus_open_transport(arg_transport, arg_host, arg_user, &bus);
6d0274f11547a0f11200bb82bf598a5a253e12cfLennart Poettering log_error("Failed to connect to bus: %s", strerror(-r));
a281d9c7851b16c4c9195d042901540ee9ced799Thomas Hindoe Paaboel Andersen r = busctl_main(bus, argc, argv);