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