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