chiark / gitweb /
logind: add shutdown/suspend/idle inhibition framework
[elogind.git] / src / login / test-inhibit.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <unistd.h>
23
24 #include <dbus/dbus.h>
25
26 #include "macro.h"
27 #include "util.h"
28 #include "dbus-common.h"
29
30 static int inhibit(DBusConnection *bus, const char *what) {
31         DBusMessage *m, *reply;
32         DBusError error;
33         const char *who = "Test Tool", *reason = "Just because!";
34         int fd;
35
36         dbus_error_init(&error);
37
38         m = dbus_message_new_method_call(
39                         "org.freedesktop.login1",
40                         "/org/freedesktop/login1",
41                         "org.freedesktop.login1.Manager",
42                         "Inhibit");
43         assert(m);
44
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_INVALID));
50
51         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
52         assert(reply);
53
54         assert(dbus_message_get_args(reply, &error,
55                                      DBUS_TYPE_UNIX_FD, &fd,
56                                      DBUS_TYPE_INVALID));
57
58         dbus_message_unref(m);
59         dbus_message_unref(reply);
60
61         return fd;
62 }
63
64 static void print_inhibitors(DBusConnection *bus) {
65         DBusMessage *m, *reply;
66         DBusError error;
67         unsigned n = 0;
68         DBusMessageIter iter, sub, sub2;
69
70         dbus_error_init(&error);
71
72         m = dbus_message_new_method_call(
73                         "org.freedesktop.login1",
74                         "/org/freedesktop/login1",
75                         "org.freedesktop.login1.Manager",
76                         "ListInhibitors");
77         assert(m);
78
79         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
80         assert(reply);
81
82         assert(dbus_message_iter_init(reply, &iter));
83         dbus_message_iter_recurse(&iter, &sub);
84
85         while (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_INVALID) {
86                 const char *what, *who, *reason;
87                 dbus_uint32_t uid, pid;
88
89                 dbus_message_iter_recurse(&sub, &sub2);
90
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);
96
97                 printf("what=<%s> who=<%s> reason=<%s> uid=<%lu> pid=<%lu>\n",
98                        what, who, reason, (unsigned long) uid, (unsigned long) pid);
99
100                 dbus_message_iter_next(&sub);
101
102                 n++;
103         }
104
105         printf("%u inhibitors\n", n);
106
107         dbus_message_unref(m);
108         dbus_message_unref(reply);
109 }
110
111 int main(int argc, char*argv[]) {
112         DBusConnection *bus;
113         int fd1, fd2;
114
115         bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, NULL);
116         assert(bus);
117
118         print_inhibitors(bus);
119
120         fd1 = inhibit(bus, "suspend");
121         assert(fd1 >= 0);
122         print_inhibitors(bus);
123
124         fd2 = inhibit(bus, "idle:shutdown");
125         assert(fd2 >= 0);
126         print_inhibitors(bus);
127
128         close_nointr_nofail(fd1);
129         sleep(1);
130         print_inhibitors(bus);
131
132         close_nointr_nofail(fd2);
133         sleep(1);
134         print_inhibitors(bus);
135
136         return 0;
137 }