]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/update-utmp/update-utmp.c
pid1: use initrd.target in the initramfs by default
[thirdparty/systemd.git] / src / update-utmp / update-utmp.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
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
47 static usec_t get_startup_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 "UserspaceTimestamp",
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
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 unsigned i;
88
89 assert(c);
90
91 for (i = 0; i < ELEMENTSOF(table); i++) {
92 _cleanup_free_ char *state = NULL, *path = NULL;
93
94 path = unit_dbus_path_from_name(table[i].special);
95 if (!path)
96 return log_oom();
97
98 r = sd_bus_get_property_string(
99 c->bus,
100 "org.freedesktop.systemd1",
101 path,
102 "org.freedesktop.systemd1.Unit",
103 "ActiveState",
104 &error,
105 &state);
106 if (r < 0)
107 return log_warning_errno(r, "Failed to get state: %s", bus_error_message(&error, r));
108
109 if (STR_IN_SET(state, "active", "reloading"))
110 return table[i].runlevel;
111 }
112
113 return 0;
114 }
115
116 static int on_reboot(Context *c) {
117 int r = 0, q;
118 usec_t t;
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 }
131 #endif
132
133 /* If this call fails it will return 0, which
134 * utmp_put_reboot() will then fix to the current time */
135 t = get_startup_time(c);
136
137 q = utmp_put_reboot(t);
138 if (q < 0) {
139 log_error_errno(q, "Failed to write utmp record: %m");
140 r = q;
141 }
142
143 return r;
144 }
145
146 static int on_shutdown(Context *c) {
147 int r = 0, q;
148
149 assert(c);
150
151 /* We started shut-down, so let's write the utmp
152 * record and send the audit msg */
153
154 #if HAVE_AUDIT
155 if (c->audit_fd >= 0)
156 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
157 errno != EPERM) {
158 r = log_error_errno(errno, "Failed to send audit message: %m");
159 }
160 #endif
161
162 q = utmp_put_shutdown();
163 if (q < 0) {
164 log_error_errno(q, "Failed to write utmp record: %m");
165 r = q;
166 }
167
168 return r;
169 }
170
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 return log_warning("Failed to get new runlevel, utmp update skipped.");
195
196 if (previous == runlevel)
197 return 0;
198
199 #if HAVE_AUDIT
200 if (c->audit_fd >= 0) {
201 char s[STRLEN("old-level=_ new-level=_") + 1];
202
203 xsprintf(s, "old-level=%c new-level=%c",
204 previous > 0 ? previous : 'N',
205 runlevel);
206
207 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s,
208 "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
209 r = log_error_errno(errno, "Failed to send audit message: %m");
210 }
211 #endif
212
213 q = utmp_put_runlevel(runlevel, previous);
214 if (q < 0 && !IN_SET(q, -ESRCH, -ENOENT))
215 return log_error_errno(q, "Failed to write utmp record: %m");
216
217 return r;
218 }
219
220 static int run(int argc, char *argv[]) {
221 _cleanup_(context_clear) Context c = {
222 #if HAVE_AUDIT
223 .audit_fd = -1
224 #endif
225 };
226 int r;
227
228 if (getppid() != 1)
229 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
230 "This program should be invoked by init only.");
231 if (argc != 2)
232 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
233 "This program requires one argument.");
234
235 log_setup_service();
236
237 umask(0022);
238
239 #if HAVE_AUDIT
240 /* If the kernel lacks netlink or audit support, don't worry about it. */
241 c.audit_fd = audit_open();
242 if (c.audit_fd < 0)
243 log_full_errno(IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT) ? LOG_DEBUG : LOG_ERR,
244 errno, "Failed to connect to audit log: %m");
245 #endif
246 r = bus_connect_system_systemd(&c.bus);
247 if (r < 0)
248 return log_error_errno(r, "Failed to get D-Bus connection: %m");
249
250 if (streq(argv[1], "reboot"))
251 return on_reboot(&c);
252 if (streq(argv[1], "shutdown"))
253 return on_shutdown(&c);
254 if (streq(argv[1], "runlevel"))
255 return on_runlevel(&c);
256 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command %s", argv[1]);
257 }
258
259 DEFINE_MAIN_FUNCTION(run);