1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
31 static int inhibit(sd_bus *bus, const char *what) {
32 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
33 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
34 const char *who = "Test Tool", *reason = "Just because!", *mode = "block";
38 r = sd_bus_call_method(bus,
39 "org.freedesktop.login1",
40 "/org/freedesktop/login1",
41 "org.freedesktop.login1.Manager",
45 "ssss", what, who, reason, mode);
48 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
55 static void print_inhibitors(sd_bus *bus) {
56 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
57 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
58 const char *what, *who, *why, *mode;
63 r = sd_bus_call_method(bus,
64 "org.freedesktop.login1",
65 "/org/freedesktop/login1",
66 "org.freedesktop.login1.Manager",
73 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
76 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
77 printf("what=<%s> who=<%s> why=<%s> mode=<%s> uid=<%"PRIu32"> pid=<%"PRIu32">\n",
78 what, who, why, mode, uid, pid);
84 printf("%u inhibitors\n", n);
87 int main(int argc, char*argv[]) {
88 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
92 r = sd_bus_open_system(&bus);
95 print_inhibitors(bus);
97 fd1 = inhibit(bus, "sleep");
99 print_inhibitors(bus);
101 fd2 = inhibit(bus, "idle:shutdown");
103 print_inhibitors(bus);
107 print_inhibitors(bus);
111 print_inhibitors(bus);