]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/update-utmp/update-utmp.c
Merge pull request #2495 from heftig/master
[thirdparty/systemd.git] / src / update-utmp / update-utmp.c
CommitLineData
4927fcae
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
4927fcae
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
4927fcae 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
4927fcae
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
4927fcae
LP
20#include <errno.h>
21#include <string.h>
4927fcae
LP
22#include <unistd.h>
23
830f6caa
LP
24#ifdef HAVE_AUDIT
25#include <libaudit.h>
26#endif
27
89456fce
TG
28#include "sd-bus.h"
29
b5efdb8a
LP
30#include "alloc-util.h"
31#include "bus-error.h"
32#include "bus-util.h"
33#include "formats-util.h"
4927fcae
LP
34#include "log.h"
35#include "macro.h"
4927fcae 36#include "special.h"
e7fb33ff 37#include "unit-name.h"
b5efdb8a
LP
38#include "util.h"
39#include "utmp-wtmp.h"
4927fcae
LP
40
41typedef struct Context {
89456fce 42 sd_bus *bus;
4927fcae
LP
43#ifdef HAVE_AUDIT
44 int audit_fd;
45#endif
46} Context;
47
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
4927fcae 110 if (streq(state, "active") || streq(state, "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
126#ifdef HAVE_AUDIT
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
155#ifdef HAVE_AUDIT
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) {
f647962d
MS
184 if (q != -ESRCH && q != -ENOENT)
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 191 runlevel = get_current_runlevel(c);
e7fb33ff 192
1cea22a5 193 if (runlevel < 0)
4927fcae
LP
194 return runlevel;
195
196 if (previous == runlevel)
197 return 0;
198
199#ifdef HAVE_AUDIT
200 if (c->audit_fd >= 0) {
1cea22a5 201 _cleanup_free_ char *s = NULL;
4927fcae 202
cd6d0a45
LP
203 if (asprintf(&s, "old-level=%c new-level=%c",
204 previous > 0 ? previous : 'N',
205 runlevel > 0 ? runlevel : 'N') < 0)
1cea22a5 206 return log_oom();
4927fcae 207
ece174c5 208 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
f0960da0 209 r = log_error_errno(errno, "Failed to send audit message: %m");
4927fcae
LP
210 }
211#endif
212
1cea22a5
LP
213 q = utmp_put_runlevel(runlevel, previous);
214 if (q < 0 && q != -ESRCH && q != -ENOENT) {
da927ba9 215 log_error_errno(q, "Failed to write utmp record: %m");
1cea22a5 216 r = q;
4927fcae
LP
217 }
218
219 return r;
220}
221
222int main(int argc, char *argv[]) {
1cea22a5 223 Context c = {
4927fcae 224#ifdef HAVE_AUDIT
1cea22a5 225 .audit_fd = -1
4927fcae 226#endif
1cea22a5
LP
227 };
228 int r;
4927fcae 229
cd6d0a45
LP
230 if (getppid() != 1) {
231 log_error("This program should be invoked by init only.");
22f4096c 232 return EXIT_FAILURE;
cd6d0a45 233 }
4927fcae
LP
234
235 if (argc != 2) {
236 log_error("This program requires one argument.");
22f4096c 237 return EXIT_FAILURE;
4927fcae
LP
238 }
239
4cfa2c99 240 log_set_target(LOG_TARGET_AUTO);
4927fcae 241 log_parse_environment();
2396fb04 242 log_open();
4927fcae 243
4c12626c
LP
244 umask(0022);
245
4927fcae 246#ifdef HAVE_AUDIT
1cea22a5
LP
247 /* If the kernel lacks netlink or audit support,
248 * don't worry about it. */
249 c.audit_fd = audit_open();
250 if (c.audit_fd < 0 && errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
56f64d95 251 log_error_errno(errno, "Failed to connect to audit log: %m");
4927fcae 252#endif
266f3e26 253 r = bus_connect_system_systemd(&c.bus);
89456fce 254 if (r < 0) {
da927ba9 255 log_error_errno(r, "Failed to get D-Bus connection: %m");
4927fcae
LP
256 r = -EIO;
257 goto finish;
258 }
259
de0671ee 260 log_debug("systemd-update-utmp running as pid "PID_FMT, getpid());
4927fcae
LP
261
262 if (streq(argv[1], "reboot"))
263 r = on_reboot(&c);
264 else if (streq(argv[1], "shutdown"))
265 r = on_shutdown(&c);
266 else if (streq(argv[1], "runlevel"))
267 r = on_runlevel(&c);
268 else {
269 log_error("Unknown command %s", argv[1]);
270 r = -EINVAL;
271 }
272
de0671ee 273 log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid());
4927fcae 274
cd6d0a45 275finish:
4927fcae
LP
276#ifdef HAVE_AUDIT
277 if (c.audit_fd >= 0)
278 audit_close(c.audit_fd);
279#endif
280
f0792aae 281 sd_bus_flush_close_unref(c.bus);
22f4096c 282 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
4927fcae 283}