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