chiark / gitweb /
ecba4c8fe4cb44f12989653565e3946c74fc2c36
[elogind.git] / src / update-utmp / update-utmp.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <assert.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27
28 #ifdef HAVE_AUDIT
29 #include <libaudit.h>
30 #endif
31
32 #include "sd-bus.h"
33
34 #include "log.h"
35 #include "macro.h"
36 #include "util.h"
37 #include "special.h"
38 #include "utmp-wtmp.h"
39 #include "bus-util.h"
40 #include "bus-error.h"
41
42 typedef struct Context {
43         sd_bus *bus;
44 #ifdef HAVE_AUDIT
45         int audit_fd;
46 #endif
47 } Context;
48
49 static usec_t get_startup_time(Context *c) {
50         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
51         usec_t t = 0;
52         int r;
53
54         assert(c);
55
56         r = sd_bus_get_property_trivial(
57                         c->bus,
58                         "org.freedesktop.systemd1",
59                         "/org/freedesktop/systemd1",
60                         "org.freedesktop.systemd1.Manager",
61                         "UserspaceTimestamp",
62                         &error,
63                         't', &t);
64         if (r < 0) {
65                 log_error("Failed to get timestamp: %s", bus_error_message(&error, -r));
66                 return 0;
67         }
68
69         return t;
70 }
71
72 static int get_current_runlevel(Context *c) {
73         static const struct {
74                 const int runlevel;
75                 const char *special;
76         } table[] = {
77                 /* The first target of this list that is active or has
78                  * a job scheduled wins. We prefer runlevels 5 and 3
79                  * here over the others, since these are the main
80                  * runlevels used on Fedora. It might make sense to
81                  * change the order on some distributions. */
82                 { '5', SPECIAL_RUNLEVEL5_TARGET },
83                 { '3', SPECIAL_RUNLEVEL3_TARGET },
84                 { '4', SPECIAL_RUNLEVEL4_TARGET },
85                 { '2', SPECIAL_RUNLEVEL2_TARGET },
86                 { '1', SPECIAL_RESCUE_TARGET },
87         };
88
89         _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
90         int r;
91         unsigned i;
92
93         assert(c);
94
95         for (i = 0; i < ELEMENTSOF(table); i++) {
96                 _cleanup_bus_message_unref_ sd_bus_message *reply = NULL;
97                 _cleanup_free_ char *state = NULL;
98                 const char *path = NULL;
99
100                 r = sd_bus_call_method(
101                                 c->bus,
102                                 "org.freedesktop.systemd1",
103                                 "/org/freedesktop/systemd1",
104                                 "org.freedesktop.systemd1.Manager",
105                                 "LoadUnit",
106                                 &error,
107                                 &reply,
108                                 "s", table[i].special);
109                 if (r < 0) {
110                         log_warning("Failed to get runlevel: %s", bus_error_message(&error, -r));
111                         continue;
112                 }
113
114                 r = sd_bus_message_read(reply, "o", &path);
115                 if (r < 0)
116                         return bus_log_parse_error(r);
117
118                 r = sd_bus_get_property_string(
119                                 c->bus,
120                                 "org.freedesktop.systemd1",
121                                 path,
122                                 "org.freedesktop.systemd1.Unit",
123                                 "ActiveState",
124                                 &error,
125                                 &state);
126                 if (r < 0) {
127                         log_warning("Failed to get state: %s", bus_error_message(&error, -r));
128                         return r;
129                 }
130
131                 if (streq(state, "active") || streq(state, "reloading"))
132                         return table[i].runlevel;
133         }
134
135         return 0;
136 }
137
138 static int on_reboot(Context *c) {
139         int r = 0, q;
140         usec_t t;
141
142         assert(c);
143
144         /* We finished start-up, so let's write the utmp
145          * record and send the audit msg */
146
147 #ifdef HAVE_AUDIT
148         if (c->audit_fd >= 0)
149                 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "init", NULL, NULL, NULL, 1) < 0 &&
150                     errno != EPERM) {
151                         log_error("Failed to send audit message: %m");
152                         r = -errno;
153                 }
154 #endif
155
156         /* If this call fails it will return 0, which
157          * utmp_put_reboot() will then fix to the current time */
158         t = get_startup_time(c);
159
160         q = utmp_put_reboot(t);
161         if (q < 0) {
162                 log_error("Failed to write utmp record: %s", strerror(-q));
163                 r = q;
164         }
165
166         return r;
167 }
168
169 static int on_shutdown(Context *c) {
170         int r = 0, q;
171
172         assert(c);
173
174         /* We started shut-down, so let's write the utmp
175          * record and send the audit msg */
176
177 #ifdef HAVE_AUDIT
178         if (c->audit_fd >= 0)
179                 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "init", NULL, NULL, NULL, 1) < 0 &&
180                     errno != EPERM) {
181                         log_error("Failed to send audit message: %m");
182                         r = -errno;
183                 }
184 #endif
185
186         q = utmp_put_shutdown();
187         if (q < 0) {
188                 log_error("Failed to write utmp record: %s", strerror(-q));
189                 r = q;
190         }
191
192         return r;
193 }
194
195 static int on_runlevel(Context *c) {
196         int r = 0, q, previous, runlevel;
197
198         assert(c);
199
200         /* We finished changing runlevel, so let's write the
201          * utmp record and send the audit msg */
202
203         /* First, get last runlevel */
204         q = utmp_get_runlevel(&previous, NULL);
205         if (q < 0) {
206
207                 if (q != -ESRCH && q != -ENOENT) {
208                         log_error("Failed to get current runlevel: %s", strerror(-q));
209                         return q;
210                 }
211
212                 /* Hmm, we didn't find any runlevel, that means we
213                  * have been rebooted */
214                 r = on_reboot(c);
215                 previous = 0;
216         }
217
218         /* Secondly, get new runlevel */
219         runlevel = get_current_runlevel(c);
220         if (runlevel < 0)
221                 return runlevel;
222
223         if (previous == runlevel)
224                 return 0;
225
226 #ifdef HAVE_AUDIT
227         if (c->audit_fd >= 0) {
228                 _cleanup_free_ char *s = NULL;
229
230                 if (asprintf(&s, "old-level=%c new-level=%c",
231                              previous > 0 ? previous : 'N',
232                              runlevel > 0 ? runlevel : 'N') < 0)
233                         return log_oom();
234
235                 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0 &&
236                     errno != EPERM) {
237                         log_error("Failed to send audit message: %m");
238                         r = -errno;
239                 }
240         }
241 #endif
242
243         q = utmp_put_runlevel(runlevel, previous);
244         if (q < 0 && q != -ESRCH && q != -ENOENT) {
245                 log_error("Failed to write utmp record: %s", strerror(-q));
246                 r = q;
247         }
248
249         return r;
250 }
251
252 int main(int argc, char *argv[]) {
253         Context c = {
254 #ifdef HAVE_AUDIT
255                 .audit_fd = -1
256 #endif
257         };
258         int r;
259
260         if (getppid() != 1) {
261                 log_error("This program should be invoked by init only.");
262                 return EXIT_FAILURE;
263         }
264
265         if (argc != 2) {
266                 log_error("This program requires one argument.");
267                 return EXIT_FAILURE;
268         }
269
270         log_set_target(LOG_TARGET_AUTO);
271         log_parse_environment();
272         log_open();
273
274         umask(0022);
275
276 #ifdef HAVE_AUDIT
277         /* If the kernel lacks netlink or audit support,
278          * don't worry about it. */
279         c.audit_fd = audit_open();
280         if (c.audit_fd < 0 && errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
281                 log_error("Failed to connect to audit log: %m");
282 #endif
283         r = bus_open_system_systemd(&c.bus);
284         if (r < 0) {
285                 log_error("Failed to get D-Bus connection: %s", strerror(-r));
286                 r = -EIO;
287                 goto finish;
288         }
289
290         log_debug("systemd-update-utmp running as pid %lu", (unsigned long) getpid());
291
292         if (streq(argv[1], "reboot"))
293                 r = on_reboot(&c);
294         else if (streq(argv[1], "shutdown"))
295                 r = on_shutdown(&c);
296         else if (streq(argv[1], "runlevel"))
297                 r = on_runlevel(&c);
298         else {
299                 log_error("Unknown command %s", argv[1]);
300                 r = -EINVAL;
301         }
302
303         log_debug("systemd-update-utmp stopped as pid %lu", (unsigned long) getpid());
304
305 finish:
306 #ifdef HAVE_AUDIT
307         if (c.audit_fd >= 0)
308                 audit_close(c.audit_fd);
309 #endif
310
311         if (c.bus)
312                 sd_bus_unref(c.bus);
313
314         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
315 }