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