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"
34 static bool arg_no_pager = false;
35 static char *arg_address = NULL;
36 static bool arg_user = false;
37 static bool arg_no_unique = false;
38 static char **arg_matches = NULL;
40 static void pager_open_if_enabled(void) {
42 /* Cache result before we open the pager */
49 static int list_bus_names(sd_bus *bus, char **argv) {
50 _cleanup_strv_free_ char **l = NULL;
57 r = sd_bus_list_names(bus, &l);
59 log_error("Failed to list names: %s", strerror(-r));
63 pager_open_if_enabled();
68 max_i = MAX(max_i, strlen(*i));
70 printf("%-*s %*s %-*s %-*s CONNECTION\n",
71 (int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER");
74 _cleanup_free_ char *owner = NULL;
78 if (arg_no_unique && (*i)[0] == ':')
81 printf("%-*s", (int) max_i, *i);
83 r = sd_bus_get_owner_pid(bus, *i, &pid);
85 _cleanup_free_ char *comm = NULL;
87 printf(" %10lu", (unsigned long) pid);
89 get_process_comm(pid, &comm);
90 printf(" %-15s", strna(comm));
94 r = sd_bus_get_owner_uid(bus, *i, &uid);
96 _cleanup_free_ char *u = NULL;
109 r = sd_bus_get_owner(bus, *i, &owner);
111 printf(" %s\n", owner);
119 static int monitor(sd_bus *bus, char *argv[]) {
123 STRV_FOREACH(i, argv+1) {
124 _cleanup_free_ char *m = NULL;
126 if (!service_name_is_valid(*i)) {
127 log_error("Invalid service name '%s'", *i);
131 m = strjoin("sender='", *i, "'", NULL);
135 r = sd_bus_add_match(bus, m, NULL, NULL);
137 log_error("Failed to add match: %s", strerror(-r));
142 STRV_FOREACH(i, arg_matches) {
143 r = sd_bus_add_match(bus, *i, NULL, NULL);
145 log_error("Failed to add match: %s", strerror(-r));
151 _cleanup_bus_message_unref_ sd_bus_message *m = NULL;
153 r = sd_bus_process(bus, &m);
155 log_error("Failed to process bus: %s", strerror(-r));
167 r = sd_bus_wait(bus, (uint64_t) -1);
169 log_error("Failed to wait for bus: %s", strerror(-r));
177 static int help(void) {
179 printf("%s [OPTIONS...] {COMMAND} ...\n\n"
180 "Introspect the bus.\n\n"
181 " -h --help Show this help\n"
182 " --version Show package version\n"
183 " --system Connect to system bus\n"
184 " --user Connect to user bus\n"
185 " --address=ADDRESS Connect to bus specified by address\n"
186 " --no-unique Only show well-known names\n"
187 " --match=MATCH Only show matching messages\n"
188 " --no-pager Do not pipe output into a pager\n\n"
190 " list List bus names\n"
191 " monitor [SERVICE...] Show bus traffic\n",
192 program_invocation_short_name);
197 static int parse_argv(int argc, char *argv[]) {
209 static const struct option options[] = {
210 { "help", no_argument, NULL, 'h' },
211 { "version", no_argument, NULL, ARG_VERSION },
212 { "no-pager", no_argument, NULL, ARG_NO_PAGER },
213 { "system", no_argument, NULL, ARG_SYSTEM },
214 { "user", no_argument, NULL, ARG_USER },
215 { "address", required_argument, NULL, ARG_ADDRESS },
216 { "no-unique", no_argument, NULL, ARG_NO_UNIQUE },
217 { "match", required_argument, NULL, ARG_MATCH },
218 { NULL, 0, NULL, 0 },
226 while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0) {
234 puts(PACKAGE_STRING);
235 puts(SYSTEMD_FEATURES);
251 arg_address = optarg;
255 arg_no_unique = true;
259 if (strv_extend(&arg_matches, optarg) < 0)
267 log_error("Unknown option code %c", c);
275 static int busctl_main(sd_bus *bus, int argc, char *argv[]) {
278 if (optind >= argc ||
279 streq(argv[optind], "list"))
280 return list_bus_names(bus, argv + optind);
282 if (streq(argv[optind], "monitor"))
283 return monitor(bus, argv + optind);
285 if (streq(argv[optind], "help"))
288 log_error("Unknown command '%s'", argv[optind]);
292 int main(int argc, char *argv[]) {
293 _cleanup_bus_unref_ sd_bus *bus = NULL;
296 log_parse_environment();
299 r = parse_argv(argc, argv);
304 r = sd_bus_new(&bus);
306 log_error("Failed to allocate bus: %s", strerror(-r));
310 r = sd_bus_set_address(bus, arg_address);
312 log_error("Failed to set address: %s", strerror(-r));
316 r = sd_bus_set_bus_client(bus, true);
318 log_error("Failed to set bus client: %s", strerror(-r));
322 r = sd_bus_start(bus);
324 r = sd_bus_open_user(&bus);
326 r = sd_bus_open_system(&bus);
329 log_error("Failed to connect to bus: %s", strerror(-r));
333 r = busctl_main(bus, argc, argv);
337 strv_free(arg_matches);
339 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;