chiark / gitweb /
efi: set a nice description string in the ESP units
[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!", *mode = "block";
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_STRING, &mode,
50                                            DBUS_TYPE_INVALID));
51
52         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
53         assert(reply);
54
55         assert(dbus_message_get_args(reply, &error,
56                                      DBUS_TYPE_UNIX_FD, &fd,
57                                      DBUS_TYPE_INVALID));
58
59         dbus_message_unref(m);
60         dbus_message_unref(reply);
61
62         return fd;
63 }
64
65 static void print_inhibitors(DBusConnection *bus) {
66         DBusMessage *m, *reply;
67         DBusError error;
68         unsigned n = 0;
69         DBusMessageIter iter, sub, sub2;
70
71         dbus_error_init(&error);
72
73         m = dbus_message_new_method_call(
74                         "org.freedesktop.login1",
75                         "/org/freedesktop/login1",
76                         "org.freedesktop.login1.Manager",
77                         "ListInhibitors");
78         assert(m);
79
80         reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error);
81         assert(reply);
82
83         assert(dbus_message_iter_init(reply, &iter));
84         dbus_message_iter_recurse(&iter, &sub);
85
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;
89
90                 dbus_message_iter_recurse(&sub, &sub2);
91
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);
98
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);
101
102                 dbus_message_iter_next(&sub);
103
104                 n++;
105         }
106
107         printf("%u inhibitors\n", n);
108
109         dbus_message_unref(m);
110         dbus_message_unref(reply);
111 }
112
113 int main(int argc, char*argv[]) {
114         DBusConnection *bus;
115         int fd1, fd2;
116
117         bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, NULL);
118         assert(bus);
119
120         print_inhibitors(bus);
121
122         fd1 = inhibit(bus, "sleep");
123         assert(fd1 >= 0);
124         print_inhibitors(bus);
125
126         fd2 = inhibit(bus, "idle:shutdown");
127         assert(fd2 >= 0);
128         print_inhibitors(bus);
129
130         close_nointr_nofail(fd1);
131         sleep(1);
132         print_inhibitors(bus);
133
134         close_nointr_nofail(fd2);
135         sleep(1);
136         print_inhibitors(bus);
137
138         return 0;
139 }