chiark / gitweb /
basic: fix for IPv6 status (#4224)
[elogind.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 /// Additional includes needed by elogind
42 #include "string-util.h"
43 #include "time-util.h"
44 #include "update-utmp.h"
45 typedef struct Context {
46         sd_bus *bus;
47 #ifdef HAVE_AUDIT
48         int audit_fd;
49 #endif
50 } Context;
51
52 #if 0 /// UNNEEDED by elogind
53 static usec_t get_startup_time(Context *c) {
54         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
55         usec_t t = 0;
56         int r;
57
58         assert(c);
59
60         r = sd_bus_get_property_trivial(
61                         c->bus,
62                         "org.freedesktop.systemd1",
63                         "/org/freedesktop/systemd1",
64                         "org.freedesktop.systemd1.Manager",
65                         "UserspaceTimestamp",
66                         &error,
67                         't', &t);
68         if (r < 0) {
69                 log_error_errno(r, "Failed to get timestamp: %s", bus_error_message(&error, r));
70                 return 0;
71         }
72
73         return t;
74 }
75
76 static int get_current_runlevel(Context *c) {
77         static const struct {
78                 const int runlevel;
79                 const char *special;
80         } table[] = {
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     },
89         };
90
91         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
92         int r;
93         unsigned i;
94
95         assert(c);
96
97         for (i = 0; i < ELEMENTSOF(table); i++) {
98                 _cleanup_free_ char *state = NULL, *path = NULL;
99
100                 path = unit_dbus_path_from_name(table[i].special);
101                 if (!path)
102                         return log_oom();
103
104                 r = sd_bus_get_property_string(
105                                 c->bus,
106                                 "org.freedesktop.systemd1",
107                                 path,
108                                 "org.freedesktop.systemd1.Unit",
109                                 "ActiveState",
110                                 &error,
111                                 &state);
112                 if (r < 0)
113                         return log_warning_errno(r, "Failed to get state: %s", bus_error_message(&error, r));
114
115                 if (streq(state, "active") || streq(state, "reloading"))
116                         return table[i].runlevel;
117         }
118
119         return 0;
120 }
121 #endif // 0
122
123 static int on_reboot(Context *c) {
124         int r = 0, q;
125         usec_t t;
126
127         assert(c);
128
129         /* We finished start-up, so let's write the utmp
130          * record and send the audit msg */
131
132 #ifdef HAVE_AUDIT
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 &&
135                     errno != EPERM) {
136                         r = log_error_errno(errno, "Failed to send audit message: %m");
137                 }
138 #endif
139
140 #if 0 /// systemd hasn't started the system, so elogind always uses NOW()
141         /* If this call fails it will return 0, which
142          * utmp_put_reboot() will then fix to the current time */
143         t = get_startup_time(c);
144 #else
145         t = now(CLOCK_REALTIME);
146 #endif // 0
147
148         q = utmp_put_reboot(t);
149         if (q < 0) {
150                 log_error_errno(q, "Failed to write utmp record: %m");
151                 r = q;
152         }
153
154         return r;
155 }
156
157 static int on_shutdown(Context *c) {
158         int r = 0, q;
159
160         assert(c);
161
162         /* We started shut-down, so let's write the utmp
163          * record and send the audit msg */
164
165 #ifdef HAVE_AUDIT
166         if (c->audit_fd >= 0)
167                 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
168                     errno != EPERM) {
169                         r = log_error_errno(errno, "Failed to send audit message: %m");
170                 }
171 #endif
172
173         q = utmp_put_shutdown();
174         if (q < 0) {
175                 log_error_errno(q, "Failed to write utmp record: %m");
176                 r = q;
177         }
178
179         return r;
180 }
181
182 #if 0 /// UNNEEDED by elogind
183 static int on_runlevel(Context *c) {
184         int r = 0, q, previous, runlevel;
185
186         assert(c);
187
188         /* We finished changing runlevel, so let's write the
189          * utmp record and send the audit msg */
190
191         /* First, get last runlevel */
192         q = utmp_get_runlevel(&previous, NULL);
193
194         if (q < 0) {
195                 if (q != -ESRCH && q != -ENOENT)
196                         return log_error_errno(q, "Failed to get current runlevel: %m");
197
198                 previous = 0;
199         }
200
201         /* Secondly, get new runlevel */
202         runlevel = get_current_runlevel(c);
203
204         if (runlevel < 0)
205                 return runlevel;
206
207         if (previous == runlevel)
208                 return 0;
209
210 #ifdef HAVE_AUDIT
211         if (c->audit_fd >= 0) {
212                 _cleanup_free_ char *s = NULL;
213
214                 if (asprintf(&s, "old-level=%c new-level=%c",
215                              previous > 0 ? previous : 'N',
216                              runlevel > 0 ? runlevel : 'N') < 0)
217                         return log_oom();
218
219                 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
220                         r = log_error_errno(errno, "Failed to send audit message: %m");
221         }
222 #endif
223
224         q = utmp_put_runlevel(runlevel, previous);
225         if (q < 0 && q != -ESRCH && q != -ENOENT) {
226                 log_error_errno(q, "Failed to write utmp record: %m");
227                 r = q;
228         }
229
230         return r;
231 }
232 #endif // 0
233
234 #if 0 /// elogind needs this to be a callable function
235 int main(int argc, char *argv[]) {
236 #else
237 void update_utmp(int argc, char* argv[], sd_bus *bus) {
238 #endif // 0
239         Context c = {
240 #ifdef HAVE_AUDIT
241                 .audit_fd = -1
242 #endif
243         };
244 #if 0 /// UNNEEDED by elogind
245         int r;
246
247         if (getppid() != 1) {
248                 log_error("This program should be invoked by init only.");
249                 return EXIT_FAILURE;
250         }
251
252         if (argc != 2) {
253                 log_error("This program requires one argument.");
254                 return EXIT_FAILURE;
255         }
256
257         log_set_target(LOG_TARGET_AUTO);
258         log_parse_environment();
259         log_open();
260
261         umask(0022);
262 #else
263         assert(2 == argc);
264         assert(argv[1]);
265         assert(bus);
266 #endif // 0
267
268 #ifdef HAVE_AUDIT
269         /* If the kernel lacks netlink or audit support,
270          * don't worry about it. */
271         c.audit_fd = audit_open();
272         if (c.audit_fd < 0 && errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
273                 log_error_errno(errno, "Failed to connect to audit log: %m");
274 #endif
275 #if 0 /// UNNEEDED by elogind
276         r = bus_connect_system_systemd(&c.bus);
277         if (r < 0) {
278                 log_error_errno(r, "Failed to get D-Bus connection: %m");
279                 r = -EIO;
280                 goto finish;
281         }
282
283         log_debug("systemd-update-utmp running as pid "PID_FMT, getpid());
284
285         if (streq(argv[1], "reboot"))
286                 r = on_reboot(&c);
287         else if (streq(argv[1], "shutdown"))
288                 r = on_shutdown(&c);
289         else if (streq(argv[1], "runlevel"))
290                 r = on_runlevel(&c);
291         else {
292                 log_error("Unknown command %s", argv[1]);
293                 r = -EINVAL;
294         }
295
296         log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid());
297
298 finish:
299 #else
300         c.bus = bus;
301         if (streq(argv[1], "reboot"))
302                 (void)on_reboot(&c);
303         else if (streq(argv[1], "shutdown"))
304                 (void)on_shutdown(&c);
305 #endif // 0
306 #ifdef HAVE_AUDIT
307         if (c.audit_fd >= 0)
308                 audit_close(c.audit_fd);
309 #endif
310
311         sd_bus_flush_close_unref(c.bus);
312 #if 0 /// UNNEEDED by elogind
313         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
314 #endif // 0
315 }