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