chiark / gitweb /
bus: use new property retrieval calls everywhere
[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         if ((q = utmp_put_reboot(t)) < 0) {
161                 log_error("Failed to write utmp record: %s", strerror(-q));
162                 r = q;
163         }
164
165         return r;
166 }
167
168 static int on_shutdown(Context *c) {
169         int r = 0, q;
170
171         assert(c);
172
173         /* We started shut-down, so let's write the utmp
174          * record and send the audit msg */
175
176 #ifdef HAVE_AUDIT
177         if (c->audit_fd >= 0)
178                 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "init", NULL, NULL, NULL, 1) < 0 &&
179                     errno != EPERM) {
180                         log_error("Failed to send audit message: %m");
181                         r = -errno;
182                 }
183 #endif
184
185         if ((q = utmp_put_shutdown()) < 0) {
186                 log_error("Failed to write utmp record: %s", strerror(-q));
187                 r = q;
188         }
189
190         return r;
191 }
192
193 static int on_runlevel(Context *c) {
194         int r = 0, q, previous, runlevel;
195
196         assert(c);
197
198         /* We finished changing runlevel, so let's write the
199          * utmp record and send the audit msg */
200
201         /* First, get last runlevel */
202         if ((q = utmp_get_runlevel(&previous, NULL)) < 0) {
203
204                 if (q != -ESRCH && q != -ENOENT) {
205                         log_error("Failed to get current runlevel: %s", strerror(-q));
206                         return q;
207                 }
208
209                 /* Hmm, we didn't find any runlevel, that means we
210                  * have been rebooted */
211                 r = on_reboot(c);
212                 previous = 0;
213         }
214
215         /* Secondly, get new runlevel */
216         if ((runlevel = get_current_runlevel(c)) < 0)
217                 return runlevel;
218
219         if (previous == runlevel)
220                 return 0;
221
222 #ifdef HAVE_AUDIT
223         if (c->audit_fd >= 0) {
224                 char *s = NULL;
225
226                 if (asprintf(&s, "old-level=%c new-level=%c",
227                              previous > 0 ? previous : 'N',
228                              runlevel > 0 ? runlevel : 'N') < 0)
229                         return -ENOMEM;
230
231                 if (audit_log_user_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, NULL, NULL, NULL, 1) < 0 &&
232                     errno != EPERM) {
233                         log_error("Failed to send audit message: %m");
234                         r = -errno;
235                 }
236
237                 free(s);
238         }
239 #endif
240
241         if ((q = utmp_put_runlevel(runlevel, previous)) < 0) {
242                 if (q != -ESRCH && q != -ENOENT) {
243                         log_error("Failed to write utmp record: %s", strerror(-q));
244                         r = q;
245                 }
246         }
247
248         return r;
249 }
250
251 int main(int argc, char *argv[]) {
252         int r;
253         Context c = {};
254
255 #ifdef HAVE_AUDIT
256         c.audit_fd = -1;
257 #endif
258
259         if (getppid() != 1) {
260                 log_error("This program should be invoked by init only.");
261                 return EXIT_FAILURE;
262         }
263
264         if (argc != 2) {
265                 log_error("This program requires one argument.");
266                 return EXIT_FAILURE;
267         }
268
269         log_set_target(LOG_TARGET_AUTO);
270         log_parse_environment();
271         log_open();
272
273         umask(0022);
274
275 #ifdef HAVE_AUDIT
276         if ((c.audit_fd = audit_open()) < 0 &&
277             /* If the kernel lacks netlink or audit support,
278              * don't worry about it. */
279             errno != EAFNOSUPPORT && errno != EPROTONOSUPPORT)
280                 log_error("Failed to connect to audit log: %m");
281 #endif
282         r = bus_open_system_systemd(&c.bus);
283         if (r < 0) {
284                 log_error("Failed to get D-Bus connection: %s", strerror(-r));
285                 r = -EIO;
286                 goto finish;
287         }
288
289         log_debug("systemd-update-utmp running as pid %lu", (unsigned long) getpid());
290
291         if (streq(argv[1], "reboot"))
292                 r = on_reboot(&c);
293         else if (streq(argv[1], "shutdown"))
294                 r = on_shutdown(&c);
295         else if (streq(argv[1], "runlevel"))
296                 r = on_runlevel(&c);
297         else {
298                 log_error("Unknown command %s", argv[1]);
299                 r = -EINVAL;
300         }
301
302         log_debug("systemd-update-utmp stopped as pid %lu", (unsigned long) getpid());
303
304 finish:
305 #ifdef HAVE_AUDIT
306         if (c.audit_fd >= 0)
307                 audit_close(c.audit_fd);
308 #endif
309
310         if (c.bus)
311                 sd_bus_unref(c.bus);
312
313         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
314 }