]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
4927fcae | 2 | |
4927fcae LP |
3 | #include <errno.h> |
4 | #include <string.h> | |
4927fcae LP |
5 | #include <unistd.h> |
6 | ||
349cc4a5 | 7 | #if HAVE_AUDIT |
830f6caa LP |
8 | #include <libaudit.h> |
9 | #endif | |
10 | ||
89456fce TG |
11 | #include "sd-bus.h" |
12 | ||
b5efdb8a LP |
13 | #include "alloc-util.h" |
14 | #include "bus-error.h" | |
15 | #include "bus-util.h" | |
f97b34a6 | 16 | #include "format-util.h" |
4927fcae LP |
17 | #include "log.h" |
18 | #include "macro.h" | |
df0ff127 | 19 | #include "process-util.h" |
4927fcae | 20 | #include "special.h" |
dfc7f594 | 21 | #include "strv.h" |
e7fb33ff | 22 | #include "unit-name.h" |
b5efdb8a LP |
23 | #include "util.h" |
24 | #include "utmp-wtmp.h" | |
4927fcae LP |
25 | |
26 | typedef struct Context { | |
89456fce | 27 | sd_bus *bus; |
349cc4a5 | 28 | #if HAVE_AUDIT |
4927fcae LP |
29 | int audit_fd; |
30 | #endif | |
31 | } Context; | |
32 | ||
3664cbab YW |
33 | static void context_clear(Context *c) { |
34 | assert(c); | |
35 | ||
36 | c->bus = sd_bus_flush_close_unref(c->bus); | |
37 | #if HAVE_AUDIT | |
38 | if (c->audit_fd >= 0) | |
39 | audit_close(c->audit_fd); | |
40 | c->audit_fd = -1; | |
41 | #endif | |
42 | } | |
43 | ||
4927fcae | 44 | static usec_t get_startup_time(Context *c) { |
4afd3348 | 45 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
47c649b5 | 46 | usec_t t = 0; |
89456fce | 47 | int r; |
4927fcae | 48 | |
4927fcae LP |
49 | assert(c); |
50 | ||
47c649b5 | 51 | r = sd_bus_get_property_trivial( |
436ddded SP |
52 | c->bus, |
53 | "org.freedesktop.systemd1", | |
54 | "/org/freedesktop/systemd1", | |
89456fce | 55 | "org.freedesktop.systemd1.Manager", |
47c649b5 LP |
56 | "UserspaceTimestamp", |
57 | &error, | |
58 | 't', &t); | |
89456fce | 59 | if (r < 0) { |
f0960da0 | 60 | log_error_errno(r, "Failed to get timestamp: %s", bus_error_message(&error, r)); |
47c649b5 | 61 | return 0; |
4927fcae LP |
62 | } |
63 | ||
4927fcae LP |
64 | return t; |
65 | } | |
66 | ||
67 | static int get_current_runlevel(Context *c) { | |
68 | static const struct { | |
69 | const int runlevel; | |
70 | const char *special; | |
71 | } table[] = { | |
72 | /* The first target of this list that is active or has | |
ee2083d5 LP |
73 | * a job scheduled wins. We prefer runlevels 5 and 3 |
74 | * here over the others, since these are the main | |
75 | * runlevels used on Fedora. It might make sense to | |
76 | * change the order on some distributions. */ | |
d5d8429a LP |
77 | { '5', SPECIAL_GRAPHICAL_TARGET }, |
78 | { '3', SPECIAL_MULTI_USER_TARGET }, | |
79 | { '1', SPECIAL_RESCUE_TARGET }, | |
4927fcae | 80 | }; |
4927fcae | 81 | |
4afd3348 | 82 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
89456fce | 83 | int r; |
4927fcae | 84 | unsigned i; |
4927fcae LP |
85 | |
86 | assert(c); | |
87 | ||
4927fcae | 88 | for (i = 0; i < ELEMENTSOF(table); i++) { |
e7fb33ff | 89 | _cleanup_free_ char *state = NULL, *path = NULL; |
4927fcae | 90 | |
e7fb33ff LP |
91 | path = unit_dbus_path_from_name(table[i].special); |
92 | if (!path) | |
93 | return log_oom(); | |
89456fce | 94 | |
47c649b5 | 95 | r = sd_bus_get_property_string( |
436ddded SP |
96 | c->bus, |
97 | "org.freedesktop.systemd1", | |
98 | path, | |
47c649b5 LP |
99 | "org.freedesktop.systemd1.Unit", |
100 | "ActiveState", | |
89456fce | 101 | &error, |
47c649b5 | 102 | &state); |
f0960da0 LP |
103 | if (r < 0) |
104 | return log_warning_errno(r, "Failed to get state: %s", bus_error_message(&error, r)); | |
4927fcae | 105 | |
dfc7f594 | 106 | if (STR_IN_SET(state, "active", "reloading")) |
89456fce | 107 | return table[i].runlevel; |
4927fcae LP |
108 | } |
109 | ||
89456fce | 110 | return 0; |
4927fcae LP |
111 | } |
112 | ||
113 | static int on_reboot(Context *c) { | |
114 | int r = 0, q; | |
115 | usec_t t; | |
116 | ||
117 | assert(c); | |
118 | ||
119 | /* We finished start-up, so let's write the utmp | |
120 | * record and send the audit msg */ | |
121 | ||
349cc4a5 | 122 | #if HAVE_AUDIT |
4927fcae | 123 | if (c->audit_fd >= 0) |
0aa281df | 124 | if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && |
44785992 | 125 | errno != EPERM) { |
f0960da0 | 126 | r = log_error_errno(errno, "Failed to send audit message: %m"); |
4927fcae LP |
127 | } |
128 | #endif | |
129 | ||
130 | /* If this call fails it will return 0, which | |
131 | * utmp_put_reboot() will then fix to the current time */ | |
132 | t = get_startup_time(c); | |
133 | ||
1cea22a5 LP |
134 | q = utmp_put_reboot(t); |
135 | if (q < 0) { | |
da927ba9 | 136 | log_error_errno(q, "Failed to write utmp record: %m"); |
4927fcae LP |
137 | r = q; |
138 | } | |
139 | ||
140 | return r; | |
141 | } | |
142 | ||
143 | static int on_shutdown(Context *c) { | |
144 | int r = 0, q; | |
145 | ||
146 | assert(c); | |
147 | ||
148 | /* We started shut-down, so let's write the utmp | |
149 | * record and send the audit msg */ | |
150 | ||
349cc4a5 | 151 | #if HAVE_AUDIT |
4927fcae | 152 | if (c->audit_fd >= 0) |
0aa281df | 153 | if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && |
44785992 | 154 | errno != EPERM) { |
f0960da0 | 155 | r = log_error_errno(errno, "Failed to send audit message: %m"); |
4927fcae LP |
156 | } |
157 | #endif | |
158 | ||
1cea22a5 LP |
159 | q = utmp_put_shutdown(); |
160 | if (q < 0) { | |
da927ba9 | 161 | log_error_errno(q, "Failed to write utmp record: %m"); |
4927fcae LP |
162 | r = q; |
163 | } | |
164 | ||
165 | return r; | |
166 | } | |
167 | ||
168 | static int on_runlevel(Context *c) { | |
169 | int r = 0, q, previous, runlevel; | |
170 | ||
171 | assert(c); | |
172 | ||
173 | /* We finished changing runlevel, so let's write the | |
174 | * utmp record and send the audit msg */ | |
175 | ||
176 | /* First, get last runlevel */ | |
1cea22a5 | 177 | q = utmp_get_runlevel(&previous, NULL); |
4927fcae | 178 | |
e7fb33ff | 179 | if (q < 0) { |
4c701096 | 180 | if (!IN_SET(q, -ESRCH, -ENOENT)) |
f647962d | 181 | return log_error_errno(q, "Failed to get current runlevel: %m"); |
4927fcae | 182 | |
4927fcae LP |
183 | previous = 0; |
184 | } | |
185 | ||
830f6caa | 186 | /* Secondly, get new runlevel */ |
1cea22a5 | 187 | runlevel = get_current_runlevel(c); |
e7fb33ff | 188 | |
1cea22a5 | 189 | if (runlevel < 0) |
4927fcae LP |
190 | return runlevel; |
191 | ||
192 | if (previous == runlevel) | |
193 | return 0; | |
194 | ||
349cc4a5 | 195 | #if HAVE_AUDIT |
4927fcae | 196 | if (c->audit_fd >= 0) { |
1cea22a5 | 197 | _cleanup_free_ char *s = NULL; |
4927fcae | 198 | |
cd6d0a45 LP |
199 | if (asprintf(&s, "old-level=%c new-level=%c", |
200 | previous > 0 ? previous : 'N', | |
201 | runlevel > 0 ? runlevel : 'N') < 0) | |
1cea22a5 | 202 | return log_oom(); |
4927fcae | 203 | |
ece174c5 | 204 | if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM) |
f0960da0 | 205 | r = log_error_errno(errno, "Failed to send audit message: %m"); |
4927fcae LP |
206 | } |
207 | #endif | |
208 | ||
1cea22a5 | 209 | q = utmp_put_runlevel(runlevel, previous); |
4c701096 | 210 | if (q < 0 && !IN_SET(q, -ESRCH, -ENOENT)) { |
da927ba9 | 211 | log_error_errno(q, "Failed to write utmp record: %m"); |
1cea22a5 | 212 | r = q; |
4927fcae LP |
213 | } |
214 | ||
215 | return r; | |
216 | } | |
217 | ||
218 | int main(int argc, char *argv[]) { | |
3664cbab | 219 | _cleanup_(context_clear) Context c = { |
349cc4a5 | 220 | #if HAVE_AUDIT |
1cea22a5 | 221 | .audit_fd = -1 |
4927fcae | 222 | #endif |
1cea22a5 LP |
223 | }; |
224 | int r; | |
4927fcae | 225 | |
cd6d0a45 LP |
226 | if (getppid() != 1) { |
227 | log_error("This program should be invoked by init only."); | |
22f4096c | 228 | return EXIT_FAILURE; |
cd6d0a45 | 229 | } |
4927fcae LP |
230 | |
231 | if (argc != 2) { | |
232 | log_error("This program requires one argument."); | |
22f4096c | 233 | return EXIT_FAILURE; |
4927fcae LP |
234 | } |
235 | ||
6bf3c61c | 236 | log_setup_service(); |
4927fcae | 237 | |
4c12626c LP |
238 | umask(0022); |
239 | ||
349cc4a5 | 240 | #if HAVE_AUDIT |
1cea22a5 LP |
241 | /* If the kernel lacks netlink or audit support, |
242 | * don't worry about it. */ | |
243 | c.audit_fd = audit_open(); | |
4c701096 | 244 | if (c.audit_fd < 0 && !IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT)) |
56f64d95 | 245 | log_error_errno(errno, "Failed to connect to audit log: %m"); |
4927fcae | 246 | #endif |
266f3e26 | 247 | r = bus_connect_system_systemd(&c.bus); |
89456fce | 248 | if (r < 0) { |
da927ba9 | 249 | log_error_errno(r, "Failed to get D-Bus connection: %m"); |
3664cbab | 250 | return EXIT_FAILURE; |
4927fcae LP |
251 | } |
252 | ||
df0ff127 | 253 | log_debug("systemd-update-utmp running as pid "PID_FMT, getpid_cached()); |
4927fcae LP |
254 | |
255 | if (streq(argv[1], "reboot")) | |
256 | r = on_reboot(&c); | |
257 | else if (streq(argv[1], "shutdown")) | |
258 | r = on_shutdown(&c); | |
259 | else if (streq(argv[1], "runlevel")) | |
260 | r = on_runlevel(&c); | |
261 | else { | |
262 | log_error("Unknown command %s", argv[1]); | |
3664cbab | 263 | return EXIT_FAILURE; |
4927fcae LP |
264 | } |
265 | ||
df0ff127 | 266 | log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid_cached()); |
4927fcae | 267 | |
22f4096c | 268 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
4927fcae | 269 | } |