]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/update-utmp/update-utmp.c
tree-wide: remove Emacs lines from all files
[thirdparty/systemd.git] / src / update-utmp / update-utmp.c
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
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
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
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #ifdef HAVE_AUDIT
25 #include <libaudit.h>
26 #endif
27
28 #include "sd-bus.h"
29
30 #include "alloc-util.h"
31 #include "bus-error.h"
32 #include "bus-util.h"
33 #include "formats-util.h"
34 #include "log.h"
35 #include "macro.h"
36 #include "special.h"
37 #include "unit-name.h"
38 #include "util.h"
39 #include "utmp-wtmp.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_(sd_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_errno(r, "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_(sd_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 return log_warning_errno(r, "Failed to get state: %s", bus_error_message(&error, r));
109
110 if (streq(state, "active") || streq(state, "reloading"))
111 return table[i].runlevel;
112 }
113
114 return 0;
115 }
116
117 static 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)
128 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
129 errno != EPERM) {
130 r = log_error_errno(errno, "Failed to send audit message: %m");
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
138 q = utmp_put_reboot(t);
139 if (q < 0) {
140 log_error_errno(q, "Failed to write utmp record: %m");
141 r = q;
142 }
143
144 return r;
145 }
146
147 static 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)
157 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
158 errno != EPERM) {
159 r = log_error_errno(errno, "Failed to send audit message: %m");
160 }
161 #endif
162
163 q = utmp_put_shutdown();
164 if (q < 0) {
165 log_error_errno(q, "Failed to write utmp record: %m");
166 r = q;
167 }
168
169 return r;
170 }
171
172 static 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 */
181 q = utmp_get_runlevel(&previous, NULL);
182
183 if (q < 0) {
184 if (q != -ESRCH && q != -ENOENT)
185 return log_error_errno(q, "Failed to get current runlevel: %m");
186
187 previous = 0;
188 }
189
190 /* Secondly, get new runlevel */
191 runlevel = get_current_runlevel(c);
192
193 if (runlevel < 0)
194 return runlevel;
195
196 if (previous == runlevel)
197 return 0;
198
199 #ifdef HAVE_AUDIT
200 if (c->audit_fd >= 0) {
201 _cleanup_free_ char *s = NULL;
202
203 if (asprintf(&s, "old-level=%c new-level=%c",
204 previous > 0 ? previous : 'N',
205 runlevel > 0 ? runlevel : 'N') < 0)
206 return log_oom();
207
208 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
209 r = log_error_errno(errno, "Failed to send audit message: %m");
210 }
211 #endif
212
213 q = utmp_put_runlevel(runlevel, previous);
214 if (q < 0 && q != -ESRCH && q != -ENOENT) {
215 log_error_errno(q, "Failed to write utmp record: %m");
216 r = q;
217 }
218
219 return r;
220 }
221
222 int main(int argc, char *argv[]) {
223 Context c = {
224 #ifdef HAVE_AUDIT
225 .audit_fd = -1
226 #endif
227 };
228 int r;
229
230 if (getppid() != 1) {
231 log_error("This program should be invoked by init only.");
232 return EXIT_FAILURE;
233 }
234
235 if (argc != 2) {
236 log_error("This program requires one argument.");
237 return EXIT_FAILURE;
238 }
239
240 log_set_target(LOG_TARGET_AUTO);
241 log_parse_environment();
242 log_open();
243
244 umask(0022);
245
246 #ifdef HAVE_AUDIT
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)
251 log_error_errno(errno, "Failed to connect to audit log: %m");
252 #endif
253 r = bus_connect_system_systemd(&c.bus);
254 if (r < 0) {
255 log_error_errno(r, "Failed to get D-Bus connection: %m");
256 r = -EIO;
257 goto finish;
258 }
259
260 log_debug("systemd-update-utmp running as pid "PID_FMT, getpid());
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
273 log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid());
274
275 finish:
276 #ifdef HAVE_AUDIT
277 if (c.audit_fd >= 0)
278 audit_close(c.audit_fd);
279 #endif
280
281 sd_bus_flush_close_unref(c.bus);
282 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
283 }