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