chiark / gitweb /
e64a819aa4fed54af1675a218974a9d82b616860
[elogind.git] / src / update-utmp.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU 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 <libaudit.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <dbus/dbus.h>
30
31 #include "log.h"
32 #include "macro.h"
33 #include "util.h"
34 #include "special.h"
35 #include "utmp-wtmp.h"
36 #include "dbus-common.h"
37
38 typedef struct Context {
39         DBusConnection *bus;
40 #ifdef HAVE_AUDIT
41         int audit_fd;
42 #endif
43 } Context;
44
45 static usec_t get_startup_time(Context *c) {
46         const char
47                 *interface = "org.freedesktop.systemd1.Manager",
48                 *property = "StartupTimestamp";
49
50         DBusError error;
51         usec_t t = 0;
52         DBusMessage *m = NULL, *reply = NULL;
53         DBusMessageIter iter, sub;
54
55         dbus_error_init(&error);
56
57         assert(c);
58
59         if (!(m = dbus_message_new_method_call(
60                               "org.freedesktop.systemd1",
61                               "/org/freedesktop/systemd1",
62                               "org.freedesktop.DBus.Properties",
63                               "Get"))) {
64                 log_error("Could not allocate message.");
65                 goto finish;
66         }
67
68         if (!dbus_message_append_args(m,
69                                       DBUS_TYPE_STRING, &interface,
70                                       DBUS_TYPE_STRING, &property,
71                                       DBUS_TYPE_INVALID)) {
72                 log_error("Could not append arguments to message.");
73                 goto finish;
74         }
75
76         if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
77                 log_error("Failed to send command: %s", error.message);
78                 goto finish;
79         }
80
81         if (!dbus_message_iter_init(reply, &iter) ||
82             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
83                 log_error("Failed to parse reply.");
84                 goto finish;
85         }
86
87         dbus_message_iter_recurse(&iter, &sub);
88
89         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_UINT64)  {
90                 log_error("Failed to parse reply.");
91                 goto finish;
92         }
93
94         dbus_message_iter_get_basic(&sub, &t);
95
96 finish:
97         if (m)
98                 dbus_message_unref(m);
99
100         if (reply)
101                 dbus_message_unref(reply);
102
103         dbus_error_free(&error);
104
105         return t;
106 }
107
108 static int get_current_runlevel(Context *c) {
109         static const struct {
110                 const int runlevel;
111                 const char *special;
112         } table[] = {
113                 /* The first target of this list that is active or has
114                  * a job scheduled wins */
115                 { '0', SPECIAL_POWEROFF_TARGET },
116                 { '6', SPECIAL_REBOOT_TARGET },
117                 { '5', SPECIAL_RUNLEVEL5_TARGET },
118                 { '4', SPECIAL_RUNLEVEL4_TARGET },
119                 { '3', SPECIAL_RUNLEVEL3_TARGET },
120                 { '2', SPECIAL_RUNLEVEL2_TARGET },
121                 { '1', SPECIAL_RESCUE_TARGET },
122         };
123         const char
124                 *interface = "org.freedesktop.systemd1.Unit",
125                 *property = "ActiveState";
126
127         DBusMessage *m = NULL, *reply = NULL;
128         int r = 0;
129         unsigned i;
130         DBusError error;
131
132         assert(c);
133
134         dbus_error_init(&error);
135
136         for (i = 0; i < ELEMENTSOF(table); i++) {
137                 const char *path = NULL, *state;
138                 DBusMessageIter iter, sub;
139
140                 if (!(m = dbus_message_new_method_call(
141                                       "org.freedesktop.systemd1",
142                                       "/org/freedesktop/systemd1",
143                                       "org.freedesktop.systemd1.Manager",
144                                       "GetUnit"))) {
145                         log_error("Could not allocate message.");
146                         r = -ENOMEM;
147                         goto finish;
148                 }
149
150                 if (!dbus_message_append_args(m,
151                                               DBUS_TYPE_STRING, &table[i].special,
152                                               DBUS_TYPE_INVALID)) {
153                         log_error("Could not append arguments to message.");
154                         r = -ENOMEM;
155                         goto finish;
156                 }
157
158                 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
159                         dbus_error_free(&error);
160                         continue;
161                 }
162
163                 if (!dbus_message_get_args(reply, &error,
164                                            DBUS_TYPE_OBJECT_PATH, &path,
165                                            DBUS_TYPE_INVALID)) {
166                         log_error("Failed to parse reply: %s", error.message);
167                         r = -EIO;
168                         goto finish;
169                 }
170
171                 dbus_message_unref(m);
172                 if (!(m = dbus_message_new_method_call(
173                                       "org.freedesktop.systemd1",
174                                       path,
175                                       "org.freedesktop.DBus.Properties",
176                                       "Get"))) {
177                         log_error("Could not allocate message.");
178                         r = -ENOMEM;
179                         goto finish;
180                 }
181
182                 if (!dbus_message_append_args(m,
183                                               DBUS_TYPE_STRING, &interface,
184                                               DBUS_TYPE_STRING, &property,
185                                               DBUS_TYPE_INVALID)) {
186                         log_error("Could not append arguments to message.");
187                         r = -ENOMEM;
188                         goto finish;
189                 }
190
191                 dbus_message_unref(reply);
192                 if (!(reply = dbus_connection_send_with_reply_and_block(c->bus, m, -1, &error))) {
193                         log_error("Failed to send command: %s", error.message);
194                         r = -EIO;
195                         goto finish;
196                 }
197
198                 if (!dbus_message_iter_init(reply, &iter) ||
199                     dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)  {
200                         log_error("Failed to parse reply.");
201                         r = -EIO;
202                         goto finish;
203                 }
204
205                 dbus_message_iter_recurse(&iter, &sub);
206
207                 if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)  {
208                         log_error("Failed to parse reply.");
209                         r = -EIO;
210                         goto finish;
211                 }
212
213                 dbus_message_iter_get_basic(&sub, &state);
214
215                 if (streq(state, "active") || streq(state, "reloading"))
216                         r = table[i].runlevel;
217
218                 dbus_message_unref(m);
219                 dbus_message_unref(reply);
220                 m = reply = NULL;
221
222                 if (r)
223                         break;
224         }
225
226 finish:
227         if (m)
228                 dbus_message_unref(m);
229
230         if (reply)
231                 dbus_message_unref(reply);
232
233         dbus_error_free(&error);
234
235         return r;
236 }
237
238 static int on_reboot(Context *c) {
239         int r = 0, q;
240         usec_t t;
241
242         assert(c);
243
244         /* We finished start-up, so let's write the utmp
245          * record and send the audit msg */
246
247 #ifdef HAVE_AUDIT
248         if (c->audit_fd >= 0)
249                 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_BOOT, "", NULL, NULL, NULL, 1) < 0) {
250                         log_error("Failed to send audit message: %m");
251                         r = -errno;
252                 }
253 #endif
254
255         /* If this call fails it will return 0, which
256          * utmp_put_reboot() will then fix to the current time */
257         t = get_startup_time(c);
258
259         if ((q = utmp_put_reboot(t)) < 0) {
260                 log_error("Failed to write utmp record: %s", strerror(-q));
261                 r = q;
262         }
263
264         return r;
265 }
266
267 static int on_shutdown(Context *c) {
268         int r = 0, q;
269
270         assert(c);
271
272         /* We started shut-down, so let's write the utmp
273          * record and send the audit msg */
274
275 #ifdef HAVE_AUDIT
276         if (c->audit_fd >= 0)
277                 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", NULL, NULL, NULL, 1) < 0) {
278                         log_error("Failed to send audit message: %m");
279                         r = -errno;
280                 }
281 #endif
282
283         if ((q = utmp_put_shutdown(0)) < 0) {
284                 log_error("Failed to write utmp record: %s", strerror(-q));
285                 r = q;
286         }
287
288         return r;
289 }
290
291 static int on_runlevel(Context *c) {
292         int r = 0, q, previous, runlevel;
293
294         assert(c);
295
296         /* We finished changing runlevel, so let's write the
297          * utmp record and send the audit msg */
298
299         /* First, get last runlevel */
300         if ((q = utmp_get_runlevel(&previous, NULL)) < 0) {
301
302                 if (q != -ESRCH && q != -ENOENT) {
303                         log_error("Failed to get current runlevel: %s", strerror(-q));
304                         return q;
305                 }
306
307                 /* Hmm, we didn't find any runlevel, that means we
308                  * have been rebooted */
309                 r = on_reboot(c);
310                 previous = 0;
311         }
312
313         /* Second get new runlevel */
314         if ((runlevel = get_current_runlevel(c)) < 0)
315                 return runlevel;
316
317         if (previous == runlevel)
318                 return 0;
319
320 #ifdef HAVE_AUDIT
321         if (c->audit_fd >= 0) {
322                 char *s = NULL;
323
324                 if (asprintf(&s, "old-level=%c new-level=%c", previous, runlevel) < 0)
325                         return -ENOMEM;
326
327                 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0) {
328                         log_error("Failed to send audit message: %m");
329                         r = -errno;
330                 }
331
332                 free(s);
333         }
334 #endif
335
336         if ((q = utmp_put_runlevel(0, runlevel, previous)) < 0) {
337                 log_error("Failed to write utmp record: %s", strerror(-q));
338                 r = q;
339         }
340
341         return r;
342 }
343
344 int main(int argc, char *argv[]) {
345         int r;
346         DBusError error;
347         Context c;
348
349         dbus_error_init(&error);
350
351         zero(c);
352 #ifdef HAVE_AUDIT
353         c.audit_fd = -1;
354 #endif
355
356         /* if (getppid() != 1) { */
357         /*         log_error("This program should be invoked by init only."); */
358         /*         return 1; */
359         /* } */
360
361         if (argc != 2) {
362                 log_error("This program requires one argument.");
363                 return 1;
364         }
365
366         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
367         log_parse_environment();
368
369 #ifdef HAVE_AUDIT
370         if ((c.audit_fd = audit_open()) < 0)
371                 log_error("Failed to connect to audit log: %m");
372 #endif
373
374         if (bus_connect(DBUS_BUS_SYSTEM, &c.bus, NULL, &error) < 0) {
375                 log_error("Failed to get D-Bus connection: %s", error.message);
376                 r = -EIO;
377                 goto finish;
378         }
379
380         log_info("systemd-update-utmp running as pid %lu", (unsigned long) getpid());
381
382         if (streq(argv[1], "reboot"))
383                 r = on_reboot(&c);
384         else if (streq(argv[1], "shutdown"))
385                 r = on_shutdown(&c);
386         else if (streq(argv[1], "runlevel"))
387                 r = on_runlevel(&c);
388         else {
389                 log_error("Unknown command %s", argv[1]);
390                 r = -EINVAL;
391         }
392
393         log_info("systemd-update-utmp stopped as pid %lu", (unsigned long) getpid());
394 finish:
395
396 #ifdef HAVE_AUDIT
397         if (c.audit_fd >= 0)
398                 audit_close(c.audit_fd);
399 #endif
400
401         if (c.bus) {
402                dbus_connection_close(c.bus);
403                dbus_connection_unref(c.bus);
404         }
405
406         dbus_error_free(&error);
407         dbus_shutdown();
408
409         return r < 0 ? 1 : 0;
410 }