chiark / gitweb /
tree-wide: use IN_SET macro (#6977)
[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 "format-util.h"
34 //#include "log.h"
35 //#include "macro.h"
36 #include "process-util.h"
37 //#include "special.h"
38 #include "strv.h"
39 //#include "unit-name.h"
40 //#include "util.h"
41 #include "utmp-wtmp.h"
42
43 /// Additional includes needed by elogind
44 #include "string-util.h"
45 #include "time-util.h"
46 #include "update-utmp.h"
47 typedef struct Context {
48         sd_bus *bus;
49 #ifdef HAVE_AUDIT
50         int audit_fd;
51 #endif
52 } Context;
53
54 #if 0 /// UNNEEDED by elogind
55 static usec_t get_startup_time(Context *c) {
56         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
57         usec_t t = 0;
58         int r;
59
60         assert(c);
61
62         r = sd_bus_get_property_trivial(
63                         c->bus,
64                         "org.freedesktop.systemd1",
65                         "/org/freedesktop/systemd1",
66                         "org.freedesktop.systemd1.Manager",
67                         "UserspaceTimestamp",
68                         &error,
69                         't', &t);
70         if (r < 0) {
71                 log_error_errno(r, "Failed to get timestamp: %s", bus_error_message(&error, r));
72                 return 0;
73         }
74
75         return t;
76 }
77
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 (STR_IN_SET(state, "active", "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 0 /// systemd hasn't started the system, so elogind always uses NOW()
143         /* If this call fails it will return 0, which
144          * utmp_put_reboot() will then fix to the current time */
145         t = get_startup_time(c);
146 #else
147         t = now(CLOCK_REALTIME);
148 #endif // 0
149
150         q = utmp_put_reboot(t);
151         if (q < 0) {
152                 log_error_errno(q, "Failed to write utmp record: %m");
153                 r = q;
154         }
155
156         return r;
157 }
158
159 static int on_shutdown(Context *c) {
160         int r = 0, q;
161
162         assert(c);
163
164         /* We started shut-down, so let's write the utmp
165          * record and send the audit msg */
166
167 #ifdef HAVE_AUDIT
168         if (c->audit_fd >= 0)
169                 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_SHUTDOWN, "", "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 &&
170                     errno != EPERM) {
171                         r = log_error_errno(errno, "Failed to send audit message: %m");
172                 }
173 #endif
174
175         q = utmp_put_shutdown();
176         if (q < 0) {
177                 log_error_errno(q, "Failed to write utmp record: %m");
178                 r = q;
179         }
180
181         return r;
182 }
183
184 #if 0 /// UNNEEDED by elogind
185 static int on_runlevel(Context *c) {
186         int r = 0, q, previous, runlevel;
187
188         assert(c);
189
190         /* We finished changing runlevel, so let's write the
191          * utmp record and send the audit msg */
192
193         /* First, get last runlevel */
194         q = utmp_get_runlevel(&previous, NULL);
195
196         if (q < 0) {
197                 if (!IN_SET(q, -ESRCH, -ENOENT))
198                         return log_error_errno(q, "Failed to get current runlevel: %m");
199
200                 previous = 0;
201         }
202
203         /* Secondly, get new runlevel */
204         runlevel = get_current_runlevel(c);
205
206         if (runlevel < 0)
207                 return runlevel;
208
209         if (previous == runlevel)
210                 return 0;
211
212 #ifdef HAVE_AUDIT
213         if (c->audit_fd >= 0) {
214                 _cleanup_free_ char *s = NULL;
215
216                 if (asprintf(&s, "old-level=%c new-level=%c",
217                              previous > 0 ? previous : 'N',
218                              runlevel > 0 ? runlevel : 'N') < 0)
219                         return log_oom();
220
221                 if (audit_log_user_comm_message(c->audit_fd, AUDIT_SYSTEM_RUNLEVEL, s, "systemd-update-utmp", NULL, NULL, NULL, 1) < 0 && errno != EPERM)
222                         r = log_error_errno(errno, "Failed to send audit message: %m");
223         }
224 #endif
225
226         q = utmp_put_runlevel(runlevel, previous);
227         if (q < 0 && !IN_SET(q, -ESRCH, -ENOENT)) {
228                 log_error_errno(q, "Failed to write utmp record: %m");
229                 r = q;
230         }
231
232         return r;
233 }
234 #endif // 0
235
236 #if 0 /// elogind needs this to be a callable function
237 int main(int argc, char *argv[]) {
238 #else
239 void update_utmp(int argc, char* argv[], sd_bus *bus) {
240 #endif // 0
241         Context c = {
242 #ifdef HAVE_AUDIT
243                 .audit_fd = -1
244 #endif
245         };
246 #if 0 /// UNNEEDED by elogind
247         int r;
248
249         if (getppid() != 1) {
250                 log_error("This program should be invoked by init only.");
251                 return EXIT_FAILURE;
252         }
253
254         if (argc != 2) {
255                 log_error("This program requires one argument.");
256                 return EXIT_FAILURE;
257         }
258
259         log_set_target(LOG_TARGET_AUTO);
260         log_parse_environment();
261         log_open();
262
263         umask(0022);
264 #else
265         assert(2 == argc);
266         assert(argv[1]);
267         assert(bus);
268 #endif // 0
269
270 #ifdef HAVE_AUDIT
271         /* If the kernel lacks netlink or audit support,
272          * don't worry about it. */
273         c.audit_fd = audit_open();
274         if (c.audit_fd < 0 && !IN_SET(errno, EAFNOSUPPORT, EPROTONOSUPPORT))
275                 log_error_errno(errno, "Failed to connect to audit log: %m");
276 #endif
277 #if 0 /// UNNEEDED by elogind
278         r = bus_connect_system_systemd(&c.bus);
279         if (r < 0) {
280                 log_error_errno(r, "Failed to get D-Bus connection: %m");
281                 r = -EIO;
282                 goto finish;
283         }
284
285         log_debug("systemd-update-utmp running as pid "PID_FMT, getpid_cached());
286
287         if (streq(argv[1], "reboot"))
288                 r = on_reboot(&c);
289         else if (streq(argv[1], "shutdown"))
290                 r = on_shutdown(&c);
291         else if (streq(argv[1], "runlevel"))
292                 r = on_runlevel(&c);
293         else {
294                 log_error("Unknown command %s", argv[1]);
295                 r = -EINVAL;
296         }
297
298         log_debug("systemd-update-utmp stopped as pid "PID_FMT, getpid_cached());
299
300 finish:
301 #else
302         c.bus = bus;
303         if (streq(argv[1], "reboot"))
304                 (void)on_reboot(&c);
305         else if (streq(argv[1], "shutdown"))
306                 (void)on_shutdown(&c);
307 #endif // 0
308 #ifdef HAVE_AUDIT
309         if (c.audit_fd >= 0)
310                 audit_close(c.audit_fd);
311 #endif
312
313         sd_bus_flush_close_unref(c.bus);
314 #if 0 /// UNNEEDED by elogind
315         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
316 #endif // 0
317 }