1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 #include "bus-message.h"
32 #include "bus-internal.h"
35 static bool arg_no_pager = false;
36 static char *arg_address = NULL;
37 static bool arg_no_unique = false;
38 static char **arg_matches = NULL;
39 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
40 static char *arg_host = NULL;
41 static bool arg_user = false;
43 static void pager_open_if_enabled(void) {
45 /* Cache result before we open the pager */
52 static int list_bus_names(sd_bus *bus, char **argv) {
53 _cleanup_strv_free_ char **l = NULL;
60 r = sd_bus_list_names(bus, &l);
62 log_error("Failed to list names: %s", strerror(-r));
66 pager_open_if_enabled();
71 max_i = MAX(max_i, strlen(*i));
73 printf("%-*s %*s %-*s %-*s CONNECTION\n",
74 (int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER");
77 _cleanup_free_ char *owner = NULL;
81 if (arg_no_unique && (*i)[0] == ':')
84 printf("%-*s", (int) max_i, *i);
86 r = sd_bus_get_owner_pid(bus, *i, &pid);
88 _cleanup_free_ char *comm = NULL;
90 printf(" %10lu", (unsigned long) pid);
92 get_process_comm(pid, &comm);
93 printf(" %-15s", strna(comm));
97 r = sd_bus_get_owner_uid(bus, *i, &uid);
99 _cleanup_free_ char *u = NULL;
101 u = uid_to_name(uid);
112 r = sd_bus_get_owner(bus, *i, &owner);
114 printf(" %s\n", owner);
122 static int monitor(sd_bus *bus, char *argv[]) {
126 STRV_FOREACH(i, argv+1) {
127 _cleanup_free_ char *m = NULL;
129 if (!service_name_is_valid(*i)) {
130 log_error("Invalid service name '%s'", *i);
134 m = strjoin("sender='", *i, "'", NULL);
138 r = sd_bus_add_match(bus, m, NULL, NULL);
140 log_error("Failed to add match: %s", strerror(-r));
145 STRV_FOREACH(i, arg_matches) {
146 r = sd_bus_add_match(bus, *i, NULL, NULL);
148 log_error("Failed to add match: %s", strerror(-r));
154 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
156 r = sd_bus_process(bus, &m);
158 log_error("Failed to process bus: %s", strerror(-r));
163 bus_message_dump(m, stdout, true);
170 r = sd_bus_wait(bus, (uint64_t) -1);
172 log_error("Failed to wait for bus: %s", strerror(-r));
180 static int help(void) {
182 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
183 "Introspect the bus.\n\n"
184 " -h --help Show this help\n"
185 " --version Show package version\n"
186 " --system Connect to system bus\n"
187 " --user Connect to user bus\n"
188 " -H --host=[USER@]HOST Operate on remote host\n"
189 " -M --machine=CONTAINER Operate on local container\n"
190 " --address=ADDRESS Connect to bus specified by address\n"
191 " --no-unique Only show well-known names\n"
192 " --match=MATCH Only show matching messages\n"
193 " --no-pager Do not pipe output into a pager\n\n"
195 " list List bus names\n"
196 " monitor [SERVICE...] Show bus traffic\n",
197 program_invocation_short_name);
202 static int parse_argv(int argc, char *argv[]) {
214 static const struct option options[] = {
215 { "help", no_argument, NULL, 'h' },
216 { "version", no_argument, NULL, ARG_VERSION },
217 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
218 { "system", no_argument, NULL, ARG_SYSTEM },
219 { "user", no_argument, NULL, ARG_USER },
220 { "address", required_argument, NULL, ARG_ADDRESS },
221 { "no-unique", no_argument, NULL, ARG_NO_UNIQUE },
222 { "match", required_argument, NULL, ARG_MATCH },
223 { NULL, 0, NULL, 0 },
231 while ((c = getopt_long(argc, argv, "hH:M:", options, NULL)) >= 0) {
239 puts(PACKAGE_STRING);
240 puts(SYSTEMD_FEATURES);
256 arg_address = optarg;
260 arg_no_unique = true;
264 if (strv_extend(&arg_matches, optarg) < 0)
269 arg_transport = BUS_TRANSPORT_REMOTE;
274 arg_transport = BUS_TRANSPORT_CONTAINER;
282 log_error("Unknown option code %c", c);
290 static int busctl_main(sd_bus *bus, int argc, char *argv[]) {
293 if (optind >= argc ||
294 streq(argv[optind], "list"))
295 return list_bus_names(bus, argv + optind);
297 if (streq(argv[optind], "monitor"))
298 return monitor(bus, argv + optind);
300 if (streq(argv[optind], "help"))
303 log_error("Unknown command '%s'", argv[optind]);
307 int main(int argc, char *argv[]) {
308 _cleanup_bus_unref_ sd_bus *bus = NULL;
311 log_parse_environment();
314 r = parse_argv(argc, argv);
319 r = sd_bus_new(&bus);
321 log_error("Failed to allocate bus: %s", strerror(-r));
325 r = sd_bus_set_address(bus, arg_address);
327 log_error("Failed to set address: %s", strerror(-r));
331 r = sd_bus_set_bus_client(bus, true);
333 log_error("Failed to set bus client: %s", strerror(-r));
337 r = sd_bus_start(bus);
339 r = bus_open_transport(arg_transport, arg_host, arg_user, &bus);
342 log_error("Failed to connect to bus: %s", strerror(-r));
346 r = busctl_main(bus, argc, argv);
351 strv_free(arg_matches);
353 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;