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