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!";
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,
51 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
54 assert(dbus_message_get_args(reply, &error,
55 DBUS_TYPE_UNIX_FD, &fd,
58 dbus_message_unref(m);
59 dbus_message_unref(reply);
64 static void print_inhibitors(DBusConnection *bus) {
65 DBusMessage *m, *reply;
68 DBusMessageIter iter, sub, sub2;
70 dbus_error_init(&error);
72 m = dbus_message_new_method_call(
73 "org.freedesktop.login1",
74 "/org/freedesktop/login1",
75 "org.freedesktop.login1.Manager",
79 reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
82 assert(dbus_message_iter_init(reply, &iter));
83 dbus_message_iter_recurse(&iter, &sub);
85 while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
86 const char *what, *who, *reason;
87 dbus_uint32_t uid, pid;
89 dbus_message_iter_recurse(&sub, &sub2);
91 assert_se(bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &what, true) >= 0);
92 assert_se(bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &who, true) >= 0);
93 assert_se(bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_STRING, &reason, true) >= 0);
94 assert_se(bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &uid, true) >= 0);
95 assert_se(bus_iter_get_basic_and_next(&sub2, DBUS_TYPE_UINT32, &pid, false) >= 0);
97 printf("what=<%s> who=<%s> reason=<%s> uid=<%lu> pid=<%lu>\n",
98 what, who, reason, (unsigned long) uid, (unsigned long) pid);
100 dbus_message_iter_next(&sub);
105 printf("%u inhibitors\n", n);
107 dbus_message_unref(m);
108 dbus_message_unref(reply);
111 int main(int argc, char*argv[]) {
115 bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, NULL);
118 print_inhibitors(bus);
120 fd1 = inhibit(bus, "suspend");
122 print_inhibitors(bus);
124 fd2 = inhibit(bus, "idle:shutdown");
126 print_inhibitors(bus);
128 close_nointr_nofail(fd1);
130 print_inhibitors(bus);
132 close_nointr_nofail(fd2);
134 print_inhibitors(bus);