]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/update-utmp/update-utmp.c
remove unused includes
[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 "log.h"
33 #include "macro.h"
34 #include "util.h"
35 #include "special.h"
36 #include "utmp-wtmp.h"
37 #include "bus-util.h"
38 #include "bus-error.h"
39 #include "unit-name.h"
40
41 typedef struct Context {
42 sd_bus *bus;
43 #ifdef HAVE_AUDIT
44 int audit_fd;
45 #endif
46 } Context;
47
48 static usec_t get_startup_time(Context *c) {
49 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
50 usec_t t = 0;
51 int r;
52
53 assert(c);
54
55 r = sd_bus_get_property_trivial(
56 c->bus,
57 "org.freedesktop.systemd1",
58 "/org/freedesktop/systemd1",
59 "org.freedesktop.systemd1.Manager",
60 "UserspaceTimestamp",
61 &error,
62 't', &t);
63 if (r < 0) {
64 log_error("Failed to get timestamp: %s", bus_error_message(&error, -r));
65 return 0;
66 }
67
68 return t;
69 }
70
71 static 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
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. */
81 { '5', SPECIAL_GRAPHICAL_TARGET },
82 { '3', SPECIAL_MULTI_USER_TARGET },
83 { '1', SPECIAL_RESCUE_TARGET },
84 };
85
86 _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
87 int r;
88 unsigned i;
89
90 assert(c);
91
92 for (i = 0; i < ELEMENTSOF(table); i++) {
93 _cleanup_free_ char *state = NULL, *path = NULL;
94
95 path = unit_dbus_path_from_name(table[i].special);
96 if (!path)
97 return log_oom();
98
99 r = sd_bus_get_property_string(
100 c->bus,
101 "org.freedesktop.systemd1",
102 path,
103 "org.freedesktop.systemd1.Unit",
104 "ActiveState",
105 &error,
106 &state);
107 if (r < 0) {
108 log_warning("Failed to get state: %s", bus_error_message(&error, -r));
109 return r;
110 }
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 log_error_errno(errno, "Failed to send audit message: %m");
133 r = -errno;
134 }
135 #endif
136
137 /* If this call fails it will return 0, which
138 * utmp_put_reboot() will then fix to the current time */
139 t = get_startup_time(c);
140
141 q = utmp_put_reboot(t);
142 if (q < 0) {
143 log_error_errno(q, "Failed to write utmp record: %m");
144 r = q;
145 }
146
147 return r;
148 }
149
150 static int on_shutdown(Context *c) {
151 int r = 0, q;
152
153 assert(c);
154
155 /* We started shut-down, so let's write the utmp
156 * record and send the audit msg */
157
158 #ifdef HAVE_AUDIT
159 if (c->audit_fd >= 0)
160 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
161 errno != EPERM) {
162 log_error_errno(errno, "Failed to send audit message: %m");
163 r = -errno;
164 }
165 #endif
166
167 q = utmp_put_shutdown();
168 if (q < 0) {
169 log_error_errno(q, "Failed to write utmp record: %m");
170 r = q;
171 }
172
173 return r;
174 }
175
176 static int on_runlevel(Context *c) {
177 int r = 0, q, previous, runlevel;
178
179 assert(c);
180
181 /* We finished changing runlevel, so let's write the
182 * utmp record and send the audit msg */
183
184 /* First, get last runlevel */
185 q = utmp_get_runlevel(&previous, NULL);
186
187 if (q < 0) {
188 if (q != -ESRCH && q != -ENOENT)
189 return log_error_errno(q, "Failed to get current runlevel: %m");
190
191 previous = 0;
192 }
193
194 /* Secondly, get new runlevel */
195 runlevel = get_current_runlevel(c);
196
197 if (runlevel < 0)
198 return runlevel;
199
200 if (previous == runlevel)
201 return 0;
202
203 #ifdef HAVE_AUDIT
204 if (c->audit_fd >= 0) {
205 _cleanup_free_ char *s = NULL;
206
207 if (asprintf(&s, "old-level=%c new-level=%c",
208 previous > 0 ? previous : 'N',
209 runlevel > 0 ? runlevel : 'N') < 0)
210 return log_oom();
211
212 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
213 errno != EPERM) {
214 log_error_errno(errno, "Failed to send audit message: %m");
215 r = -errno;
216 }
217 }
218 #endif
219
220 q = utmp_put_runlevel(runlevel, previous);
221 if (q < 0 && q != -ESRCH && q != -ENOENT) {
222 log_error_errno(q, "Failed to write utmp record: %m");
223 r = q;
224 }
225
226 return r;
227 }
228
229 int main(int argc, char *argv[]) {
230 Context c = {
231 #ifdef HAVE_AUDIT
232 .audit_fd = -1
233 #endif
234 };
235 int r;
236
237 if (getppid() != 1) {
238 log_error("This program should be invoked by init only.");
239 return EXIT_FAILURE;
240 }
241
242 if (argc != 2) {
243 log_error("This program requires one argument.");
244 return EXIT_FAILURE;
245 }
246
247 log_set_target(LOG_TARGET_AUTO);
248 log_parse_environment();
249 log_open();
250
251 umask(0022);
252
253 #ifdef HAVE_AUDIT
254 /* If the kernel lacks netlink or audit support,
255 * don't worry about it. */
256 c.audit_fd = audit_open();
257 if (c.audit_fd < 0 && errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
258 log_error_errno(errno, "Failed to connect to audit log: %m");
259 #endif
260 r = bus_open_system_systemd(&c.bus);
261 if (r < 0) {
262 log_error_errno(r, "Failed to get D-Bus connection: %m");
263 r = -EIO;
264 goto finish;
265 }
266
267 log_debug("systemd-update-utmp running as pid "PID_FMT, getpid());
268
269 if (streq(argv[1], "reboot"))
270 r = on_reboot(&c);
271 else if (streq(argv[1], "shutdown"))
272 r = on_shutdown(&c);
273 else if (streq(argv[1], "runlevel"))
274 r = on_runlevel(&c);
275 else {
276 log_error("Unknown command %s", argv[1]);
277 r = -EINVAL;
278 }
279
280 log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid());
281
282 finish:
283 #ifdef HAVE_AUDIT
284 if (c.audit_fd >= 0)
285 audit_close(c.audit_fd);
286 #endif
287
288 if (c.bus)
289 sd_bus_unref(c.bus);
290
291 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
292 }