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