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