chiark / gitweb /
175cc5d7cb7d3212b306ba4c661616353f8a20af
[elogind.git] / src / update-utmp / update-utmp.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3   This file is part of systemd.
4
5   Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <string.h>
10 #include <unistd.h>
11
12 #if HAVE_AUDIT
13 #include <libaudit.h>
14 #endif
15
16 #include "sd-bus.h"
17
18 //#include "alloc-util.h"
19 #include "bus-error.h"
20 //#include "bus-util.h"
21 //#include "format-util.h"
22 //#include "log.h"
23 //#include "macro.h"
24 #include "process-util.h"
25 //#include "special.h"
26 #include "strv.h"
27 //#include "unit-name.h"
28 //#include "util.h"
29 #include "utmp-wtmp.h"
30
31 /// Additional includes needed by elogind
32 #include "string-util.h"
33 #include "time-util.h"
34 #include "update-utmp.h"
35 typedef struct Context {
36         sd_bus *bus;
37 #if HAVE_AUDIT
38         int audit_fd;
39 #endif
40 } Context;
41
42 #if 0 /// UNNEEDED by elogind
43 static usec_t get_startup_time(Context *c) {
44         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
45         usec_t t = 0;
46         int r;
47
48         assert(c);
49
50         r = sd_bus_get_property_trivial(
51                         c->bus,
52                         "org.freedesktop.systemd1",
53                         "/org/freedesktop/systemd1",
54                         "org.freedesktop.systemd1.Manager",
55                         "UserspaceTimestamp",
56                         &error,
57                         't', &t);
58         if (r < 0) {
59                 log_error_errno(r, "Failed to get timestamp: %s", bus_error_message(&error, r));
60                 return 0;
61         }
62
63         return t;
64 }
65
66 static int get_current_runlevel(Context *c) {
67         static const struct {
68                 const int runlevel;
69                 const char *special;
70         } table[] = {
71                 /* The first target of this list that is active or has
72                  * a job scheduled wins. We prefer runlevels 5 and 3
73                  * here over the others, since these are the main
74                  * runlevels used on Fedora. It might make sense to
75                  * change the order on some distributions. */
76                 { '5', SPECIAL_GRAPHICAL_TARGET  },
77                 { '3', SPECIAL_MULTI_USER_TARGET },
78                 { '1', SPECIAL_RESCUE_TARGET     },
79         };
80
81         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
82         int r;
83         unsigned i;
84
85         assert(c);
86
87         for (i = 0; i < ELEMENTSOF(table); i++) {
88                 _cleanup_free_ char *state = NULL, *path = NULL;
89
90                 path = unit_dbus_path_from_name(table[i].special);
91                 if (!path)
92                         return log_oom();
93
94                 r = sd_bus_get_property_string(
95                                 c->bus,
96                                 "org.freedesktop.systemd1",
97                                 path,
98                                 "org.freedesktop.systemd1.Unit",
99                                 "ActiveState",
100                                 &error,
101                                 &state);
102                 if (r < 0)
103                         return log_warning_errno(r, "Failed to get state: %s", bus_error_message(&error, r));
104
105                 if (STR_IN_SET(state, "active", "reloading"))
106                         return table[i].runlevel;
107         }
108
109         return 0;
110 }
111 #endif // 0
112
113 static int on_reboot(Context *c) {
114         int r = 0, q;
115         usec_t t;
116
117         assert(c);
118
119         /* We finished start-up, so let's write the utmp
120          * record and send the audit msg */
121
122 #if HAVE_AUDIT
123         if (c->audit_fd >= 0)
124                 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
125                     errno != EPERM) {
126                         r = log_error_errno(errno, "Failed to send audit message: %m");
127                 }
128 #endif
129
130 #if 0 /// systemd hasn't started the system, so elogind always uses NOW()
131         /* If this call fails it will return 0, which
132          * utmp_put_reboot() will then fix to the current time */
133         t = get_startup_time(c);
134 #else
135         t = now(CLOCK_REALTIME);
136 #endif // 0
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 #if 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 #if 0 /// UNNEEDED by elogind
173 static int on_runlevel(Context *c) {
174         int r = 0, q, previous, runlevel;
175
176         assert(c);
177
178         /* We finished changing runlevel, so let's write the
179          * utmp record and send the audit msg */
180
181         /* First, get last runlevel */
182         q = utmp_get_runlevel(&previous, NULL);
183
184         if (q < 0) {
185                 if (!IN_SET(q, -ESRCH, -ENOENT))
186                         return log_error_errno(q, "Failed to get current runlevel: %m");
187
188                 previous = 0;
189         }
190
191         /* Secondly, get new runlevel */
192         runlevel = get_current_runlevel(c);
193
194         if (runlevel < 0)
195                 return runlevel;
196
197         if (previous == runlevel)
198                 return 0;
199
200 #if HAVE_AUDIT
201         if (c->audit_fd >= 0) {
202                 _cleanup_free_ char *s = NULL;
203
204                 if (asprintf(&s, "old-level=%c new-level=%c",
205                              previous > 0 ? previous : 'N',
206                              runlevel > 0 ? runlevel : 'N') < 0)
207                         return log_oom();
208
209                 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
210                         r = log_error_errno(errno, "Failed to send audit message: %m");
211         }
212 #endif
213
214         q = utmp_put_runlevel(runlevel, previous);
215         if (q < 0 && !IN_SET(q, -ESRCH, -ENOENT)) {
216                 log_error_errno(q, "Failed to write utmp record: %m");
217                 r = q;
218         }
219
220         return r;
221 }
222 #endif // 0
223
224 #if 0 /// elogind needs this to be a callable function
225 int main(int argc, char *argv[]) {
226 #else
227 void update_utmp(int argc, char* argv[]) {
228 #endif // 0
229         Context c = {
230 #if HAVE_AUDIT
231                 .audit_fd = -1
232 #endif
233         };
234 #if 0 /// UNNEEDED by elogind
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 #else
253         assert(2 == argc);
254         assert(argv[1]);
255 #endif // 0
256
257 #if HAVE_AUDIT
258         /* If the kernel lacks netlink or audit support,
259          * don't worry about it. */
260         c.audit_fd = audit_open();
261         if (c.audit_fd < 0 && !IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT))
262                 log_error_errno(errno, "Failed to connect to audit log: %m");
263 #endif
264 #if 0 /// UNNEEDED by elogind
265         r = bus_connect_system_systemd(&c.bus);
266         if (r < 0) {
267                 log_error_errno(r, "Failed to get D-Bus connection: %m");
268                 r = -EIO;
269                 goto finish;
270         }
271
272         log_debug("systemd-update-utmp running as pid "PID_FMT, getpid_cached());
273
274         if (streq(argv[1], "reboot"))
275                 r = on_reboot(&c);
276         else if (streq(argv[1], "shutdown"))
277                 r = on_shutdown(&c);
278         else if (streq(argv[1], "runlevel"))
279                 r = on_runlevel(&c);
280         else {
281                 log_error("Unknown command %s", argv[1]);
282                 r = -EINVAL;
283         }
284
285         log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid_cached());
286
287 finish:
288 #else
289         if (streq(argv[1], "reboot"))
290                 (void)on_reboot(&c);
291         else if (streq(argv[1], "shutdown"))
292                 (void)on_shutdown(&c);
293 #endif // 0
294 #if HAVE_AUDIT
295         if (c.audit_fd >= 0)
296                 audit_close(c.audit_fd);
297 #endif
298
299 #if 0 /// UNNEEDED by elogind
300         sd_bus_flush_close_unref(c.bus);
301         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
302 #endif // 0
303 }