]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/test-inhibit.c
Merge pull request #7045 from poettering/namespace-casing
[thirdparty/systemd.git] / src / login / test-inhibit.c
CommitLineData
f8e2fb7b
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2012 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
20#include <unistd.h>
21
997d4ae7 22#include "sd-bus.h"
07630cea 23
997d4ae7 24#include "bus-util.h"
3ffd4af2 25#include "fd-util.h"
07630cea
LP
26#include "macro.h"
27#include "util.h"
f8e2fb7b 28
997d4ae7 29static int inhibit(sd_bus *bus, const char *what) {
4afd3348
LP
30 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
31 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
dd5f0a96 32 const char *who = "Test Tool", *reason = "Just because!", *mode = "block";
f8e2fb7b 33 int fd;
997d4ae7 34 int r;
f8e2fb7b 35
997d4ae7 36 r = sd_bus_call_method(bus,
f8e2fb7b
LP
37 "org.freedesktop.login1",
38 "/org/freedesktop/login1",
39 "org.freedesktop.login1.Manager",
997d4ae7
KS
40 "Inhibit",
41 &error,
42 &reply,
43 "ssss", what, who, reason, mode);
787784c4 44 assert_se(r >= 0);
f8e2fb7b 45
997d4ae7 46 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
787784c4
RC
47 assert_se(r >= 0);
48 assert_se(fd >= 0);
f8e2fb7b 49
997d4ae7 50 return dup(fd);
f8e2fb7b
LP
51}
52
997d4ae7 53static void print_inhibitors(sd_bus *bus) {
4afd3348
LP
54 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
55 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
997d4ae7
KS
56 const char *what, *who, *why, *mode;
57 uint32_t uid, pid;
f8e2fb7b 58 unsigned n = 0;
997d4ae7 59 int r;
f8e2fb7b 60
997d4ae7 61 r = sd_bus_call_method(bus,
f8e2fb7b
LP
62 "org.freedesktop.login1",
63 "/org/freedesktop/login1",
64 "org.freedesktop.login1.Manager",
997d4ae7
KS
65 "ListInhibitors",
66 &error,
67 &reply,
68 "");
787784c4 69 assert_se(r >= 0);
f8e2fb7b 70
997d4ae7 71 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
787784c4 72 assert_se(r >= 0);
f8e2fb7b 73
997d4ae7 74 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
1fa2f38f 75 printf("what=<%s> who=<%s> why=<%s> mode=<%s> uid=<%"PRIu32"> pid=<%"PRIu32">\n",
de0671ee 76 what, who, why, mode, uid, pid);
f8e2fb7b 77
f8e2fb7b
LP
78 n++;
79 }
787784c4 80 assert_se(r >= 0);
f8e2fb7b
LP
81
82 printf("%u inhibitors\n", n);
f8e2fb7b
LP
83}
84
85int main(int argc, char*argv[]) {
4afd3348 86 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
f8e2fb7b 87 int fd1, fd2;
997d4ae7 88 int r;
f8e2fb7b 89
997d4ae7 90 r = sd_bus_open_system(&bus);
787784c4 91 assert_se(r >= 0);
f8e2fb7b
LP
92
93 print_inhibitors(bus);
94
4943c1c9 95 fd1 = inhibit(bus, "sleep");
787784c4 96 assert_se(fd1 >= 0);
f8e2fb7b
LP
97 print_inhibitors(bus);
98
99 fd2 = inhibit(bus, "idle:shutdown");
787784c4 100 assert_se(fd2 >= 0);
f8e2fb7b
LP
101 print_inhibitors(bus);
102
03e334a1 103 safe_close(fd1);
f8e2fb7b
LP
104 sleep(1);
105 print_inhibitors(bus);
106
03e334a1 107 safe_close(fd2);
f8e2fb7b
LP
108 sleep(1);
109 print_inhibitors(bus);
110
111 return 0;
112}