Branch data Line data Source code
1 : : /* SPDX-License-Identifier: LGPL-2.1+ */
2 : :
3 : : #include <unistd.h>
4 : : #include <fcntl.h>
5 : :
6 : : #include "sd-bus.h"
7 : :
8 : : #include "bus-util.h"
9 : : #include "fd-util.h"
10 : : #include "macro.h"
11 : : #include "util.h"
12 : :
13 : 0 : static int inhibit(sd_bus *bus, const char *what) {
14 : 0 : _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
15 : 0 : _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
16 : 0 : const char *who = "Test Tool", *reason = "Just because!", *mode = "block";
17 : : int fd;
18 : : int r;
19 : :
20 : 0 : r = sd_bus_call_method(bus,
21 : : "org.freedesktop.login1",
22 : : "/org/freedesktop/login1",
23 : : "org.freedesktop.login1.Manager",
24 : : "Inhibit",
25 : : &error,
26 : : &reply,
27 : : "ssss", what, who, reason, mode);
28 [ # # ]: 0 : assert_se(r >= 0);
29 : :
30 : 0 : r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
31 [ # # ]: 0 : assert_se(r >= 0);
32 [ # # ]: 0 : assert_se(fd >= 0);
33 : :
34 : 0 : return fcntl(fd, F_DUPFD_CLOEXEC, 3);
35 : : }
36 : :
37 : 0 : static void print_inhibitors(sd_bus *bus) {
38 : 0 : _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
39 : 0 : _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
40 : : const char *what, *who, *why, *mode;
41 : : uint32_t uid, pid;
42 : 0 : unsigned n = 0;
43 : : int r;
44 : :
45 : 0 : r = sd_bus_call_method(bus,
46 : : "org.freedesktop.login1",
47 : : "/org/freedesktop/login1",
48 : : "org.freedesktop.login1.Manager",
49 : : "ListInhibitors",
50 : : &error,
51 : : &reply,
52 : : "");
53 [ # # ]: 0 : assert_se(r >= 0);
54 : :
55 : 0 : r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
56 [ # # ]: 0 : assert_se(r >= 0);
57 : :
58 [ # # ]: 0 : while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
59 : 0 : printf("what=<%s> who=<%s> why=<%s> mode=<%s> uid=<%"PRIu32"> pid=<%"PRIu32">\n",
60 : : what, who, why, mode, uid, pid);
61 : :
62 : 0 : n++;
63 : : }
64 [ # # ]: 0 : assert_se(r >= 0);
65 : :
66 : 0 : printf("%u inhibitors\n", n);
67 : 0 : }
68 : :
69 : 0 : int main(int argc, char *argv[]) {
70 : 0 : _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
71 : : int fd1, fd2;
72 : : int r;
73 : :
74 : 0 : r = sd_bus_open_system(&bus);
75 [ # # ]: 0 : assert_se(r >= 0);
76 : :
77 : 0 : print_inhibitors(bus);
78 : :
79 : 0 : fd1 = inhibit(bus, "sleep");
80 [ # # ]: 0 : assert_se(fd1 >= 0);
81 : 0 : print_inhibitors(bus);
82 : :
83 : 0 : fd2 = inhibit(bus, "idle:shutdown");
84 [ # # ]: 0 : assert_se(fd2 >= 0);
85 : 0 : print_inhibitors(bus);
86 : :
87 : 0 : safe_close(fd1);
88 : 0 : sleep(1);
89 : 0 : print_inhibitors(bus);
90 : :
91 : 0 : safe_close(fd2);
92 : 0 : sleep(1);
93 : 0 : print_inhibitors(bus);
94 : :
95 : 0 : return 0;
96 : : }
|