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