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