1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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 
inhibit(sd_bus * bus,const char * what)13 static int inhibit(sd_bus *bus, const char *what) {
14         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
15         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
16         const char *who = "Test Tool", *reason = "Just because!", *mode = "block";
17         int fd;
18         int r;
19 
20         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         assert_se(r >= 0);
29 
30         r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
31         assert_se(r >= 0);
32         assert_se(fd >= 0);
33 
34         return fcntl(fd, F_DUPFD_CLOEXEC, 3);
35 }
36 
print_inhibitors(sd_bus * bus)37 static void print_inhibitors(sd_bus *bus) {
38         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
39         _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         unsigned n = 0;
43         int r;
44 
45         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         assert_se(r >= 0);
54 
55         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
56         assert_se(r >= 0);
57 
58         while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
59                 printf("what=<%s> who=<%s> why=<%s> mode=<%s> uid=<%"PRIu32"> pid=<%"PRIu32">\n",
60                        what, who, why, mode, uid, pid);
61 
62                 n++;
63         }
64         assert_se(r >= 0);
65 
66         printf("%u inhibitors\n", n);
67 }
68 
main(int argc,char * argv[])69 int main(int argc, char *argv[]) {
70         _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
71         int fd1, fd2;
72         int r;
73 
74         r = sd_bus_open_system(&bus);
75         assert_se(r >= 0);
76 
77         print_inhibitors(bus);
78 
79         fd1 = inhibit(bus, "sleep");
80         assert_se(fd1 >= 0);
81         print_inhibitors(bus);
82 
83         fd2 = inhibit(bus, "idle:shutdown");
84         assert_se(fd2 >= 0);
85         print_inhibitors(bus);
86 
87         safe_close(fd1);
88         sleep(1);
89         print_inhibitors(bus);
90 
91         safe_close(fd2);
92         sleep(1);
93         print_inhibitors(bus);
94 
95         return 0;
96 }
97