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/>.
24 #include <dbus/dbus.h>
28 #include "dbus-common.h"
30 static int inhibit(DBusConnection *bus, const char *what) {
31 DBusMessage *m, *reply;
33 const char *who = "Test Tool", *reason = "Just because!", *mode = "block";
36 dbus_error_init(&error);
38 m = dbus_message_new_method_call(
39 "org.freedesktop.login1",
40 "/org/freedesktop/login1",
41 "org.freedesktop.login1.Manager",
45 assert_se(dbus_message_append_args(m,
46 DBUS_TYPE_STRING, &what,
47 DBUS_TYPE_STRING, &who,
48 DBUS_TYPE_STRING, &reason,
49 DBUS_TYPE_STRING, &mode,
52 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
55 assert(dbus_message_get_args(reply, &error,
56 DBUS_TYPE_UNIX_FD, &fd,
59 dbus_message_unref(m);
60 dbus_message_unref(reply);
65 static void print_inhibitors(DBusConnection *bus) {
66 DBusMessage *m, *reply;
69 DBusMessageIter iter, sub, sub2;
71 dbus_error_init(&error);
73 m = dbus_message_new_method_call(
74 "org.freedesktop.login1",
75 "/org/freedesktop/login1",
76 "org.freedesktop.login1.Manager",
80 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
83 assert(dbus_message_iter_init(reply, &iter));
84 dbus_message_iter_recurse(&iter, &sub);
86 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
87 const char *what, *who, *why, *mode;
88 dbus_uint32_t uid, pid;
90 dbus_message_iter_recurse(&sub, &sub2);
92 assert_se(bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &what, true) >= 0);
93 assert_se(bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &who, true) >= 0);
94 assert_se(bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &why, true) >= 0);
95 assert_se(bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &mode, true) >= 0);
96 assert_se(bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) >= 0);
97 assert_se(bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, false) >= 0);
99 printf("what=<%s> who=<%s> why=<%s> mode=<%s> uid=<%lu> pid=<%lu>\n",
100 what, who, why, mode, (unsigned long) uid, (unsigned long) pid);
102 dbus_message_iter_next(&sub);
107 printf("%u inhibitors\n", n);
109 dbus_message_unref(m);
110 dbus_message_unref(reply);
113 int main(int argc, char*argv[]) {
117 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, NULL);
120 print_inhibitors(bus);
122 fd1 = inhibit(bus, "sleep");
124 print_inhibitors(bus);
126 fd2 = inhibit(bus, "idle:shutdown");
128 print_inhibitors(bus);
130 close_nointr_nofail(fd1);
132 print_inhibitors(bus);
134 close_nointr_nofail(fd2);
136 print_inhibitors(bus);