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