chiark / gitweb /
debfc273a2246e797a02575bd52408930896fa40
[elogind.git] / src / login / test-inhibit.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2012 Lennart Poettering
6 ***/
7
8 #include <unistd.h>
9
10 #include "sd-bus.h"
11
12 #include "bus-util.h"
13 #include "fd-util.h"
14 #include "macro.h"
15 #include "util.h"
16
17 static int inhibit(sd_bus *bus, const char *what) {
18         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
19         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
20         const char *who = "Test Tool", *reason = "Just because!", *mode = "block";
21         int fd;
22         int r;
23
24         r = sd_bus_call_method(bus,
25                         "org.freedesktop.login1",
26                         "/org/freedesktop/login1",
27                         "org.freedesktop.login1.Manager",
28                         "Inhibit",
29                         &error,
30                         &reply,
31                         "ssss", what, who, reason, mode);
32         assert_se(r >= 0);
33
34         r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
35         assert_se(r >= 0);
36         assert_se(fd >= 0);
37
38         return dup(fd);
39 }
40
41 static void print_inhibitors(sd_bus *bus) {
42         _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
43         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
44         const char *what, *who, *why, *mode;
45         uint32_t uid, pid;
46         unsigned n = 0;
47         int r;
48
49         r = sd_bus_call_method(bus,
50                         "org.freedesktop.login1",
51                         "/org/freedesktop/login1",
52                         "org.freedesktop.login1.Manager",
53                         "ListInhibitors",
54                         &error,
55                         &reply,
56                         "");
57         assert_se(r >= 0);
58
59         r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "(ssssuu)");
60         assert_se(r >= 0);
61
62         while ((r = sd_bus_message_read(reply, "(ssssuu)", &what, &who, &why, &mode, &uid, &pid)) > 0) {
63                 printf("what=<%s> who=<%s> why=<%s> mode=<%s> uid=<%"PRIu32"> pid=<%"PRIu32">\n",
64                        what, who, why, mode, uid, pid);
65
66                 n++;
67         }
68         assert_se(r >= 0);
69
70         printf("%u inhibitors\n", n);
71 }
72
73 int main(int argc, char*argv[]) {
74         _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
75         int fd1, fd2;
76         int r;
77
78         r = sd_bus_open_system(&bus);
79         assert_se(r >= 0);
80
81         print_inhibitors(bus);
82
83         fd1 = inhibit(bus, "sleep");
84         assert_se(fd1 >= 0);
85         print_inhibitors(bus);
86
87         fd2 = inhibit(bus, "idle:shutdown");
88         assert_se(fd2 >= 0);
89         print_inhibitors(bus);
90
91         safe_close(fd1);
92         sleep(1);
93         print_inhibitors(bus);
94
95         safe_close(fd2);
96         sleep(1);
97         print_inhibitors(bus);
98
99         return 0;
100 }