]> git.proxmox.com Git - systemd.git/blame - src/login/test-inhibit.c
Imported Upstream version 229
[systemd.git] / src / login / test-inhibit.c
CommitLineData
663996b3
MS
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
60f067b4 22#include "sd-bus.h"
db2df898 23
60f067b4 24#include "bus-util.h"
db2df898
MP
25#include "fd-util.h"
26#include "macro.h"
27#include "util.h"
663996b3 28
60f067b4 29static int inhibit(sd_bus *bus, const char *what) {
4c89c718
MP
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;
663996b3
MS
32 const char *who = "Test Tool", *reason = "Just because!", *mode = "block";
33 int fd;
60f067b4 34 int r;
663996b3 35
60f067b4 36 r = sd_bus_call_method(bus,
663996b3
MS
37 "org.freedesktop.login1",
38 "/org/freedesktop/login1",
39 "org.freedesktop.login1.Manager",
60f067b4
JS
40 "Inhibit",
41 &error,
42 &reply,
43 "ssss", what, who, reason, mode);
f47781d8 44 assert_se(r >= 0);
663996b3 45
60f067b4 46 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
f47781d8
MP
47 assert_se(r >= 0);
48 assert_se(fd >= 0);
663996b3 49
60f067b4 50 return dup(fd);
663996b3
MS
51}
52
60f067b4 53static void print_inhibitors(sd_bus *bus) {
4c89c718
MP
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;
60f067b4
JS
56 const char *what, *who, *why, *mode;
57 uint32_t uid, pid;
663996b3 58 unsigned n = 0;
60f067b4 59 int r;
663996b3 60
60f067b4 61 r = sd_bus_call_method(bus,
663996b3
MS
62 "org.freedesktop.login1",
63 "/org/freedesktop/login1",
64 "org.freedesktop.login1.Manager",
60f067b4
JS
65 "ListInhibitors",
66 &error,
67 &reply,
68 "");
f47781d8 69 assert_se(r >= 0);
663996b3 70
60f067b4 71 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
f47781d8 72 assert_se(r >= 0);
663996b3 73
60f067b4 74 while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
e735f4d4 75 printf("what=<%s> who=<%s> why=<%s> mode=<%s> uid=<%"PRIu32"> pid=<%"PRIu32">\n",
60f067b4 76 what, who, why, mode, uid, pid);
663996b3
MS
77
78 n++;
79 }
f47781d8 80 assert_se(r >= 0);
663996b3
MS
81
82 printf("%u inhibitors\n", n);
663996b3
MS
83}
84
85int main(int argc, char*argv[]) {
4c89c718 86 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
663996b3 87 int fd1, fd2;
60f067b4 88 int r;
663996b3 89
60f067b4 90 r = sd_bus_open_system(&bus);
f47781d8 91 assert_se(r >= 0);
663996b3
MS
92
93 print_inhibitors(bus);
94
95 fd1 = inhibit(bus, "sleep");
f47781d8 96 assert_se(fd1 >= 0);
663996b3
MS
97 print_inhibitors(bus);
98
99 fd2 = inhibit(bus, "idle:shutdown");
f47781d8 100 assert_se(fd2 >= 0);
663996b3
MS
101 print_inhibitors(bus);
102
60f067b4 103 safe_close(fd1);
663996b3
MS
104 sleep(1);
105 print_inhibitors(bus);
106
60f067b4 107 safe_close(fd2);
663996b3
MS
108 sleep(1);
109 print_inhibitors(bus);
110
111 return 0;
112}