chiark / gitweb /
88446fcc7c0b10631873c3513efcae26fb7524d3
[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 "sd-bus.h"
23
24 int main(int argc, char *argv[]) {
25         _cleanup_bus_unref_ sd_bus *bus = NULL;
26         _cleanup_strv_free_ char **l = NULL;
27         char **i;
28         int r;
29
30         r = bus_open_system(&bus);
31         if (r < 0) {
32                 log_error("Failed to connect to bus: %s", strerror(-r));
33                 goto fail;
34         }
35
36         r = sd_bus_list_names(bus, &l);
37         if (r < 0) {
38                 log_error("Failed to list names: %s", strerror(-r));
39                 goto fail;
40         }
41
42         STRV_FOREACH(i, l) {
43                 _cleanup_free_ char *owner = NULL;
44                 pid_t pid = 0;
45                 uid_t uid;
46                 bool uid_valid;
47
48                 r = sd_bus_get_owner(bus, *i, &owner);
49                 if (r == -ENXIO)
50                         continue;
51
52                 r = sd_get_owner_pid(bus, *i, &pid);
53                 if (r == -ENXIO)
54                         continue;
55
56                 r = sd_get_owner_uid(bus, *i, &pid);
57                 if (r == -ENXIO)
58                         continue;
59                 uid_valid = r >= 0;
60
61                 printf("%s (%s) %llu %llu\n", *i, owner, (unsigned long long) pid, (unsigned long long) uid);
62         }
63
64         r = 0;
65
66 fail:
67         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
68 }