1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
17 static int inhibit(sd_bus *bus, const char *what) {
18 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
19 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
20 const char *who = "Test Tool", *reason = "Just because!", *mode = "block";
24 r = sd_bus_call_method(bus,
25 "org.freedesktop.login1",
26 "/org/freedesktop/login1",
27 "org.freedesktop.login1.Manager",
31 "ssss", what, who, reason, mode);
34 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
41 static void print_inhibitors(sd_bus *bus) {
42 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
43 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
44 const char *what, *who, *why, *mode;
49 r = sd_bus_call_method(bus,
50 "org.freedesktop.login1",
51 "/org/freedesktop/login1",
52 "org.freedesktop.login1.Manager",
59 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
62 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
63 printf("what=<%s> who=<%s> why=<%s> mode=<%s> uid=<%"PRIu32"> pid=<%"PRIu32">\n",
64 what, who, why, mode, uid, pid);
70 printf("%u inhibitors\n", n);
73 int main(int argc, char*argv[]) {
74 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
78 r = sd_bus_open_system(&bus);
81 print_inhibitors(bus);
83 fd1 = inhibit(bus, "sleep");
85 print_inhibitors(bus);
87 fd2 = inhibit(bus, "idle:shutdown");
89 print_inhibitors(bus);
93 print_inhibitors(bus);
97 print_inhibitors(bus);