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/>.
28 #include "bus-error.h"
30 static int inhibit(sd_bus *bus, const char *what) {
31 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
32 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
33 const char *who = "Test Tool", *reason = "Just because!", *mode = "block";
37 r = sd_bus_call_method(bus,
38 "org.freedesktop.login1",
39 "/org/freedesktop/login1",
40 "org.freedesktop.login1.Manager",
44 "ssss", what, who, reason, mode);
47 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
54 static void print_inhibitors(sd_bus *bus) {
55 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
56 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
57 const char *what, *who, *why, *mode;
62 r = sd_bus_call_method(bus,
63 "org.freedesktop.login1",
64 "/org/freedesktop/login1",
65 "org.freedesktop.login1.Manager",
72 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
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=<"UID_FMT"> pid=<"PID_FMT">\n",
77 what, who, why, mode, uid, pid);
83 printf("%u inhibitors\n", n);
86 int main(int argc, char*argv[]) {
87 _cleanup_bus_unref_ sd_bus *bus = NULL;
91 r = sd_bus_open_system(&bus);
94 print_inhibitors(bus);
96 fd1 = inhibit(bus, "sleep");
98 print_inhibitors(bus);
100 fd2 = inhibit(bus, "idle:shutdown");
102 print_inhibitors(bus);
106 print_inhibitors(bus);
110 print_inhibitors(bus);