]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/login/test-inhibit.c
Merge pull request #8025 from sourcejedi/pid1_journal_or2
[thirdparty/systemd.git] / src / login / test-inhibit.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2012 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19 ***/
20
21 #include <unistd.h>
22
23 #include "sd-bus.h"
24
25 #include "bus-util.h"
26 #include "fd-util.h"
27 #include "macro.h"
28 #include "util.h"
29
30 static int inhibit(sd_bus *bus, const char *what) {
31 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
32 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
33 const char *who = "Test Tool", *reason = "Just because!", *mode = "block";
34 int fd;
35 int r;
36
37 r = sd_bus_call_method(bus,
38 "org.freedesktop.login1",
39 "/org/freedesktop/login1",
40 "org.freedesktop.login1.Manager",
41 "Inhibit",
42 &error,
43 &reply,
44 "ssss", what, who, reason, mode);
45 assert_se(r >= 0);
46
47 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
48 assert_se(r >= 0);
49 assert_se(fd >= 0);
50
51 return dup(fd);
52 }
53
54 static void print_inhibitors(sd_bus *bus) {
55 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
56 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
57 const char *what, *who, *why, *mode;
58 uint32_t uid, pid;
59 unsigned n = 0;
60 int r;
61
62 r = sd_bus_call_method(bus,
63 "org.freedesktop.login1",
64 "/org/freedesktop/login1",
65 "org.freedesktop.login1.Manager",
66 "ListInhibitors",
67 &error,
68 &reply,
69 "");
70 assert_se(r >= 0);
71
72 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
73 assert_se(r >= 0);
74
75 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
76 printf("what=<%s> who=<%s> why=<%s> mode=<%s> uid=<%"PRIu32"> pid=<%"PRIu32">\n",
77 what, who, why, mode, uid, pid);
78
79 n++;
80 }
81 assert_se(r >= 0);
82
83 printf("%u inhibitors\n", n);
84 }
85
86 int main(int argc, char*argv[]) {
87 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
88 int fd1, fd2;
89 int r;
90
91 r = sd_bus_open_system(&bus);
92 assert_se(r >= 0);
93
94 print_inhibitors(bus);
95
96 fd1 = inhibit(bus, "sleep");
97 assert_se(fd1 >= 0);
98 print_inhibitors(bus);
99
100 fd2 = inhibit(bus, "idle:shutdown");
101 assert_se(fd2 >= 0);
102 print_inhibitors(bus);
103
104 safe_close(fd1);
105 sleep(1);
106 print_inhibitors(bus);
107
108 safe_close(fd2);
109 sleep(1);
110 print_inhibitors(bus);
111
112 return 0;
113 }