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