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