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