1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <unistd.h>
7
8 #if HAVE_AUDIT
9 #include <libaudit.h>
10 #endif
11
12 #include "sd-bus.h"
13
14 #include "alloc-util.h"
15 #include "bus-error.h"
16 #include "bus-util.h"
17 #include "format-util.h"
18 #include "log.h"
19 #include "macro.h"
20 #include "main-func.h"
21 #include "process-util.h"
22 #include "special.h"
23 #include "stdio-util.h"
24 #include "strv.h"
25 #include "unit-name.h"
26 #include "util.h"
27 #include "utmp-wtmp.h"
28
29 typedef struct Context {
30 sd_bus *bus;
31 #if HAVE_AUDIT
32 int audit_fd;
33 #endif
34 } Context;
35
context_clear(Context * c)36 static void context_clear(Context *c) {
37 assert(c);
38
39 c->bus = sd_bus_flush_close_unref(c->bus);
40 #if HAVE_AUDIT
41 if (c->audit_fd >= 0)
42 audit_close(c->audit_fd);
43 c->audit_fd = -1;
44 #endif
45 }
46
get_startup_monotonic_time(Context * c)47 static usec_t get_startup_monotonic_time(Context *c) {
48 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
49 usec_t t = 0;
50 int r;
51
52 assert(c);
53
54 r = sd_bus_get_property_trivial(
55 c->bus,
56 "org.freedesktop.systemd1",
57 "/org/freedesktop/systemd1",
58 "org.freedesktop.systemd1.Manager",
59 "UserspaceTimestampMonotonic",
60 &error,
61 't', &t);
62 if (r < 0) {
63 log_error_errno(r, "Failed to get timestamp: %s", bus_error_message(&error, r));
64 return 0;
65 }
66
67 return t;
68 }
69
get_current_runlevel(Context * c)70 static int get_current_runlevel(Context *c) {
71 static const struct {
72 const int runlevel;
73 const char *special;
74 } table[] = {
75 /* The first target of this list that is active or has
76 * a job scheduled wins. We prefer runlevels 5 and 3
77 * here over the others, since these are the main
78 * runlevels used on Fedora. It might make sense to
79 * change the order on some distributions. */
80 { '5', SPECIAL_GRAPHICAL_TARGET },
81 { '3', SPECIAL_MULTI_USER_TARGET },
82 { '1', SPECIAL_RESCUE_TARGET },
83 };
84
85 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
86 int r;
87
88 assert(c);
89
90 for (size_t i = 0; i < ELEMENTSOF(table); i++) {
91 _cleanup_free_ char *state = NULL, *path = NULL;
92
93 path = unit_dbus_path_from_name(table[i].special);
94 if (!path)
95 return log_oom();
96
97 r = sd_bus_get_property_string(
98 c->bus,
99 "org.freedesktop.systemd1",
100 path,
101 "org.freedesktop.systemd1.Unit",
102 "ActiveState",
103 &error,
104 &state);
105 if (r < 0)
106 return log_warning_errno(r, "Failed to get state: %s", bus_error_message(&error, r));
107
108 if (STR_IN_SET(state, "active", "reloading"))
109 return table[i].runlevel;
110 }
111
112 return 0;
113 }
114
on_reboot(Context * c)115 static int on_reboot(Context *c) {
116 int r = 0, q;
117 usec_t t;
118 usec_t boottime;
119
120 assert(c);
121
122 /* We finished start-up, so let's write the utmp
123 * record and send the audit msg */
124
125 #if HAVE_AUDIT
126 if (c->audit_fd >= 0)
127 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
128 errno != EPERM)
129 r = log_error_errno(errno, "Failed to send audit message: %m");
130 #endif
131
132 /* If this call fails it will return 0, which
133 * utmp_put_reboot() will then fix to the current time */
134 t = get_startup_monotonic_time(c);
135 boottime = map_clock_usec(t, CLOCK_MONOTONIC, CLOCK_REALTIME);
136 /* We query the recorded monotonic time here (instead of the system clock CLOCK_REALTIME),
137 * even though we actually want the system clock time. That's because there's a likely
138 * chance that the system clock wasn't set right during early boot. By manually converting
139 * the monotonic clock to the system clock here we can compensate
140 * for incorrectly set clocks during early boot. */
141
142 q = utmp_put_reboot(boottime);
143 if (q < 0)
144 r = log_error_errno(q, "Failed to write utmp record: %m");
145
146 return r;
147 }
148
on_shutdown(Context * c)149 static int on_shutdown(Context *c) {
150 int r = 0, q;
151
152 assert(c);
153
154 /* We started shut-down, so let's write the utmp
155 * record and send the audit msg */
156
157 #if HAVE_AUDIT
158 if (c->audit_fd >= 0)
159 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
160 errno != EPERM)
161 r = log_error_errno(errno, "Failed to send audit message: %m");
162 #endif
163
164 q = utmp_put_shutdown();
165 if (q < 0)
166 r = log_error_errno(q, "Failed to write utmp record: %m");
167
168 return r;
169 }
170
on_runlevel(Context * c)171 static int on_runlevel(Context *c) {
172 int r = 0, q, previous, runlevel;
173
174 assert(c);
175
176 /* We finished changing runlevel, so let's write the
177 * utmp record and send the audit msg */
178
179 /* First, get last runlevel */
180 q = utmp_get_runlevel(&previous, NULL);
181
182 if (q < 0) {
183 if (!IN_SET(q, -ESRCH, -ENOENT))
184 return log_error_errno(q, "Failed to get current runlevel: %m");
185
186 previous = 0;
187 }
188
189 /* Secondly, get new runlevel */
190 runlevel = get_current_runlevel(c);
191 if (runlevel < 0)
192 return runlevel;
193 if (runlevel == 0) {
194 log_warning("Failed to get new runlevel, utmp update skipped.");
195 return 0;
196 }
197
198 if (previous == runlevel)
199 return 0;
200
201 #if HAVE_AUDIT
202 if (c->audit_fd >= 0) {
203 char s[STRLEN("old-level=_ new-level=_") + 1];
204
205 xsprintf(s, "old-level=%c new-level=%c",
206 previous > 0 ? previous : 'N',
207 runlevel);
208
209 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s,
210 "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
211 r = log_error_errno(errno, "Failed to send audit message: %m");
212 }
213 #endif
214
215 q = utmp_put_runlevel(runlevel, previous);
216 if (q < 0 && !IN_SET(q, -ESRCH, -ENOENT))
217 return log_error_errno(q, "Failed to write utmp record: %m");
218
219 return r;
220 }
221
run(int argc,char * argv[])222 static int run(int argc, char *argv[]) {
223 _cleanup_(context_clear) Context c = {
224 #if HAVE_AUDIT
225 .audit_fd = -1,
226 #endif
227 };
228 int r;
229
230 if (argc != 2)
231 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
232 "This program requires one argument.");
233
234 log_setup();
235
236 umask(0022);
237
238 #if HAVE_AUDIT
239 /* If the kernel lacks netlink or audit support, don't worry about it. */
240 c.audit_fd = audit_open();
241 if (c.audit_fd < 0)
242 log_full_errno(IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT) ? LOG_DEBUG : LOG_ERR,
243 errno, "Failed to connect to audit log: %m");
244 #endif
245 r = bus_connect_system_systemd(&c.bus);
246 if (r < 0)
247 return log_error_errno(r, "Failed to get D-Bus connection: %m");
248
249 if (streq(argv[1], "reboot"))
250 return on_reboot(&c);
251 if (streq(argv[1], "shutdown"))
252 return on_shutdown(&c);
253 if (streq(argv[1], "runlevel"))
254 return on_runlevel(&c);
255 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command %s", argv[1]);
256 }
257
258 DEFINE_MAIN_FUNCTION(run);
259