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