2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
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.
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.
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/>.
30 //#include "alloc-util.h"
31 #include "bus-error.h"
32 //#include "bus-util.h"
33 //#include "formats-util.h"
36 //#include "special.h"
37 //#include "unit-name.h"
39 #include "utmp-wtmp.h"
41 /// Additional includes needed by elogind
42 #include "string-util.h"
43 #include "time-util.h"
44 #include "update-utmp.h"
45 typedef struct Context {
52 static usec_t get_startup_time(Context *c) {
53 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
59 r = sd_bus_get_property_trivial(
61 "org.freedesktop.systemd1",
62 "/org/freedesktop/systemd1",
63 "org.freedesktop.systemd1.Manager",
68 log_error_errno(r, "Failed to get timestamp: %s", bus_error_message(&error, r));
75 #if 0 /// UNNEEDED by elogind
76 static int get_current_runlevel(Context *c) {
81 /* The first target of this list that is active or has
82 * a job scheduled wins. We prefer runlevels 5 and 3
83 * here over the others, since these are the main
84 * runlevels used on Fedora. It might make sense to
85 * change the order on some distributions. */
86 { '5', SPECIAL_GRAPHICAL_TARGET },
87 { '3', SPECIAL_MULTI_USER_TARGET },
88 { '1', SPECIAL_RESCUE_TARGET },
91 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
97 for (i = 0; i < ELEMENTSOF(table); i++) {
98 _cleanup_free_ char *state = NULL, *path = NULL;
100 path = unit_dbus_path_from_name(table[i].special);
104 r = sd_bus_get_property_string(
106 "org.freedesktop.systemd1",
108 "org.freedesktop.systemd1.Unit",
113 return log_warning_errno(r, "Failed to get state: %s", bus_error_message(&error, r));
115 if (streq(state, "active") || streq(state, "reloading"))
116 return table[i].runlevel;
123 static int on_reboot(Context *c) {
129 /* We finished start-up, so let's write the utmp
130 * record and send the audit msg */
133 if (c->audit_fd >= 0)
134 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
136 r = log_error_errno(errno, "Failed to send audit message: %m");
140 /* If this call fails it will return 0, which
141 * utmp_put_reboot() will then fix to the current time */
142 t = get_startup_time(c);
144 q = utmp_put_reboot(t);
146 log_error_errno(q, "Failed to write utmp record: %m");
153 static int on_shutdown(Context *c) {
158 /* We started shut-down, so let's write the utmp
159 * record and send the audit msg */
162 if (c->audit_fd >= 0)
163 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
165 r = log_error_errno(errno, "Failed to send audit message: %m");
169 q = utmp_put_shutdown();
171 log_error_errno(q, "Failed to write utmp record: %m");
178 #if 0 /// UNNEEDED by elogind
179 static int on_runlevel(Context *c) {
180 int r = 0, q, previous, runlevel;
184 /* We finished changing runlevel, so let's write the
185 * utmp record and send the audit msg */
187 /* First, get last runlevel */
188 q = utmp_get_runlevel(&previous, NULL);
191 if (q != -ESRCH && q != -ENOENT)
192 return log_error_errno(q, "Failed to get current runlevel: %m");
197 /* Secondly, get new runlevel */
198 runlevel = get_current_runlevel(c);
203 if (previous == runlevel)
207 if (c->audit_fd >= 0) {
208 _cleanup_free_ char *s = NULL;
210 if (asprintf(&s, "old-level=%c new-level=%c",
211 previous > 0 ? previous : 'N',
212 runlevel > 0 ? runlevel : 'N') < 0)
215 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
216 r = log_error_errno(errno, "Failed to send audit message: %m");
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");
230 #if 0 /// elogind needs this to be a callable function
231 int main(int argc, char *argv[]) {
233 void update_utmp(int argc, char* argv[], sd_bus *bus) {
240 #if 0 /// UNNEEDED by elogind
243 if (getppid() != 1) {
244 log_error("This program should be invoked by init only.");
249 log_error("This program requires one argument.");
253 log_set_target(LOG_TARGET_AUTO);
254 log_parse_environment();
265 /* If the kernel lacks netlink or audit support,
266 * don't worry about it. */
267 c.audit_fd = audit_open();
268 if (c.audit_fd < 0 && errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
269 log_error_errno(errno, "Failed to connect to audit log: %m");
271 #if 0 /// UNNEEDED by elogind
272 r = bus_connect_system_systemd(&c.bus);
274 log_error_errno(r, "Failed to get D-Bus connection: %m");
279 log_debug("systemd-update-utmp running as pid "PID_FMT, getpid());
281 if (streq(argv[1], "reboot"))
283 else if (streq(argv[1], "shutdown"))
285 else if (streq(argv[1], "runlevel"))
288 log_error("Unknown command %s", argv[1]);
292 log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid());
297 if (streq(argv[1], "reboot"))
299 else if (streq(argv[1], "shutdown"))
300 (void)on_shutdown(&c);
304 audit_close(c.audit_fd);
307 sd_bus_flush_close_unref(c.bus);
308 #if 0 /// UNNEEDED by elogind
309 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;