chiark / gitweb /
bus: implicitly set no_reply flag on outgoing messages if the serial number is not...
[elogind.git] / src / libsystemd-bus / busctl.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include "strv.h"
23 #include "util.h"
24 #include "log.h"
25
26 #include "sd-bus.h"
27 #include "bus-message.h"
28 #include "bus-internal.h"
29
30 int main(int argc, char *argv[]) {
31         _cleanup_bus_unref_ sd_bus *bus = NULL;
32         _cleanup_strv_free_ char **l = NULL;
33         char **i;
34         int r;
35         size_t max_i = 0;
36
37         r = sd_bus_open_user(&bus);
38         if (r < 0) {
39                 log_error("Failed to connect to bus: %s", strerror(-r));
40                 goto fail;
41         }
42
43         r = sd_bus_list_names(bus, &l);
44         if (r < 0) {
45                 log_error("Failed to list names: %s", strerror(-r));
46                 goto fail;
47         }
48
49         strv_sort(l);
50
51         STRV_FOREACH(i, l)
52                 max_i = MAX(max_i, strlen(*i));
53
54         printf("%-*s %*s %-*s %-*s CONNECTION\n",
55                (int) max_i, "NAME", 10, "PID", 15, "PROCESS", 16, "USER");
56
57         STRV_FOREACH(i, l) {
58                 _cleanup_free_ char *owner = NULL;
59                 pid_t pid;
60                 uid_t uid;
61
62                 /* if ((*i)[0] == ':') */
63                 /*         continue; */
64
65                 printf("%-*s", (int) max_i, *i);
66
67                 r = sd_bus_get_owner_pid(bus, *i, &pid);
68                 if (r >= 0) {
69                         _cleanup_free_ char *comm = NULL;
70
71                         printf(" %10lu", (unsigned long) pid);
72
73                         get_process_comm(pid, &comm);
74                         printf(" %-15s", strna(comm));
75                 } else
76                         printf("          - -              ");
77
78                 r = sd_bus_get_owner_uid(bus, *i, &uid);
79                 if (r >= 0) {
80                         _cleanup_free_ char *u = NULL;
81
82                         u = uid_to_name(uid);
83                         if (!u) {
84                                 log_oom();
85                                 goto fail;
86                         }
87
88                         if (strlen(u) > 16)
89                                 u[16] = 0;
90
91                         printf(" %-16s", u);
92                 } else
93                         printf(" -               ");
94
95                 r = sd_bus_get_owner(bus, *i, &owner);
96                 if (r >= 0)
97                         printf(" %s\n", owner);
98                 else
99                         printf(" -\n");
100         }
101
102         r = 0;
103
104 fail:
105         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
106 }