]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/update-utmp/update-utmp.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / update-utmp / update-utmp.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <string.h>
5 #include <unistd.h>
6
7 #if HAVE_AUDIT
8 #include <libaudit.h>
9 #endif
10
11 #include "sd-bus.h"
12
13 #include "alloc-util.h"
14 #include "bus-error.h"
15 #include "bus-util.h"
16 #include "format-util.h"
17 #include "log.h"
18 #include "macro.h"
19 #include "process-util.h"
20 #include "special.h"
21 #include "strv.h"
22 #include "unit-name.h"
23 #include "util.h"
24 #include "utmp-wtmp.h"
25
26 typedef struct Context {
27 sd_bus *bus;
28 #if HAVE_AUDIT
29 int audit_fd;
30 #endif
31 } Context;
32
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
44 static usec_t get_startup_time(Context *c) {
45 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
46 usec_t t = 0;
47 int r;
48
49 assert(c);
50
51 r = sd_bus_get_property_trivial(
52 c->bus,
53 "org.freedesktop.systemd1",
54 "/org/freedesktop/systemd1",
55 "org.freedesktop.systemd1.Manager",
56 "UserspaceTimestamp",
57 &error,
58 't', &t);
59 if (r < 0) {
60 log_error_errno(r, "Failed to get timestamp: %s", bus_error_message(&error, r));
61 return 0;
62 }
63
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
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. */
77 { '5', SPECIAL_GRAPHICAL_TARGET },
78 { '3', SPECIAL_MULTI_USER_TARGET },
79 { '1', SPECIAL_RESCUE_TARGET },
80 };
81
82 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
83 int r;
84 unsigned i;
85
86 assert(c);
87
88 for (i = 0; i < ELEMENTSOF(table); i++) {
89 _cleanup_free_ char *state = NULL, *path = NULL;
90
91 path = unit_dbus_path_from_name(table[i].special);
92 if (!path)
93 return log_oom();
94
95 r = sd_bus_get_property_string(
96 c->bus,
97 "org.freedesktop.systemd1",
98 path,
99 "org.freedesktop.systemd1.Unit",
100 "ActiveState",
101 &error,
102 &state);
103 if (r < 0)
104 return log_warning_errno(r, "Failed to get state: %s", bus_error_message(&error, r));
105
106 if (STR_IN_SET(state, "active", "reloading"))
107 return table[i].runlevel;
108 }
109
110 return 0;
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
122 #if HAVE_AUDIT
123 if (c->audit_fd >= 0)
124 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
125 errno != EPERM) {
126 r = log_error_errno(errno, "Failed to send audit message: %m");
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
134 q = utmp_put_reboot(t);
135 if (q < 0) {
136 log_error_errno(q, "Failed to write utmp record: %m");
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
151 #if HAVE_AUDIT
152 if (c->audit_fd >= 0)
153 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
154 errno != EPERM) {
155 r = log_error_errno(errno, "Failed to send audit message: %m");
156 }
157 #endif
158
159 q = utmp_put_shutdown();
160 if (q < 0) {
161 log_error_errno(q, "Failed to write utmp record: %m");
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 */
177 q = utmp_get_runlevel(&previous, NULL);
178
179 if (q < 0) {
180 if (!IN_SET(q, -ESRCH, -ENOENT))
181 return log_error_errno(q, "Failed to get current runlevel: %m");
182
183 previous = 0;
184 }
185
186 /* Secondly, get new runlevel */
187 runlevel = get_current_runlevel(c);
188
189 if (runlevel < 0)
190 return runlevel;
191
192 if (previous == runlevel)
193 return 0;
194
195 #if HAVE_AUDIT
196 if (c->audit_fd >= 0) {
197 _cleanup_free_ char *s = NULL;
198
199 if (asprintf(&s, "old-level=%c new-level=%c",
200 previous > 0 ? previous : 'N',
201 runlevel > 0 ? runlevel : 'N') < 0)
202 return log_oom();
203
204 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
205 r = log_error_errno(errno, "Failed to send audit message: %m");
206 }
207 #endif
208
209 q = utmp_put_runlevel(runlevel, previous);
210 if (q < 0 && !IN_SET(q, -ESRCH, -ENOENT)) {
211 log_error_errno(q, "Failed to write utmp record: %m");
212 r = q;
213 }
214
215 return r;
216 }
217
218 int main(int argc, char *argv[]) {
219 _cleanup_(context_clear) Context c = {
220 #if HAVE_AUDIT
221 .audit_fd = -1
222 #endif
223 };
224 int r;
225
226 if (getppid() != 1) {
227 log_error("This program should be invoked by init only.");
228 return EXIT_FAILURE;
229 }
230
231 if (argc != 2) {
232 log_error("This program requires one argument.");
233 return EXIT_FAILURE;
234 }
235
236 log_setup_service();
237
238 umask(0022);
239
240 #if HAVE_AUDIT
241 /* If the kernel lacks netlink or audit support,
242 * don't worry about it. */
243 c.audit_fd = audit_open();
244 if (c.audit_fd < 0 && !IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT))
245 log_error_errno(errno, "Failed to connect to audit log: %m");
246 #endif
247 r = bus_connect_system_systemd(&c.bus);
248 if (r < 0) {
249 log_error_errno(r, "Failed to get D-Bus connection: %m");
250 return EXIT_FAILURE;
251 }
252
253 log_debug("systemd-update-utmp running as pid "PID_FMT, getpid_cached());
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]);
263 return EXIT_FAILURE;
264 }
265
266 log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid_cached());
267
268 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
269 }