]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/utmp-wtmp.c
Merge pull request #16344 from keszybz/update-utmp-erofs
[thirdparty/systemd.git] / src / shared / utmp-wtmp.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <poll.h>
6 #include <stddef.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/time.h>
10 #include <sys/utsname.h>
11 #include <unistd.h>
12 #include <utmpx.h>
13
14 #include "alloc-util.h"
15 #include "fd-util.h"
16 #include "hostname-util.h"
17 #include "io-util.h"
18 #include "macro.h"
19 #include "memory-util.h"
20 #include "path-util.h"
21 #include "string-util.h"
22 #include "terminal-util.h"
23 #include "time-util.h"
24 #include "user-util.h"
25 #include "utmp-wtmp.h"
26
27 int utmp_get_runlevel(int *runlevel, int *previous) {
28 _cleanup_(utxent_cleanup) bool utmpx = false;
29 struct utmpx *found, lookup = { .ut_type = RUN_LVL };
30 const char *e;
31
32 assert(runlevel);
33
34 /* If these values are set in the environment this takes
35 * precedence. Presumably, sysvinit does this to work around a
36 * race condition that would otherwise exist where we'd always
37 * go to disk and hence might read runlevel data that might be
38 * very new and not apply to the current script being executed. */
39
40 e = getenv("RUNLEVEL");
41 if (e && e[0] > 0) {
42 *runlevel = e[0];
43
44 if (previous) {
45 /* $PREVLEVEL seems to be an Upstart thing */
46
47 e = getenv("PREVLEVEL");
48 if (e && e[0] > 0)
49 *previous = e[0];
50 else
51 *previous = 0;
52 }
53
54 return 0;
55 }
56
57 if (utmpxname(_PATH_UTMPX) < 0)
58 return -errno;
59
60 utmpx = utxent_start();
61
62 found = getutxid(&lookup);
63 if (!found)
64 return -errno;
65
66 *runlevel = found->ut_pid & 0xFF;
67 if (previous)
68 *previous = (found->ut_pid >> 8) & 0xFF;
69
70 return 0;
71 }
72
73 static void init_timestamp(struct utmpx *store, usec_t t) {
74 assert(store);
75
76 if (t <= 0)
77 t = now(CLOCK_REALTIME);
78
79 store->ut_tv.tv_sec = t / USEC_PER_SEC;
80 store->ut_tv.tv_usec = t % USEC_PER_SEC;
81 }
82
83 static void init_entry(struct utmpx *store, usec_t t) {
84 struct utsname uts = {};
85
86 assert(store);
87
88 init_timestamp(store, t);
89
90 if (uname(&uts) >= 0)
91 strncpy(store->ut_host, uts.release, sizeof(store->ut_host));
92
93 strncpy(store->ut_line, "~", sizeof(store->ut_line)); /* or ~~ ? */
94 strncpy(store->ut_id, "~~", sizeof(store->ut_id));
95 }
96
97 static int write_entry_utmp(const struct utmpx *store) {
98 _cleanup_(utxent_cleanup) bool utmpx = false;
99
100 assert(store);
101
102 /* utmp is similar to wtmp, but there is only one entry for
103 * each entry type resp. user; i.e. basically a key/value
104 * table. */
105
106 if (utmpxname(_PATH_UTMPX) < 0)
107 return -errno;
108
109 utmpx = utxent_start();
110
111 if (pututxline(store))
112 return 0;
113 if (errno == ENOENT) {
114 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
115 log_debug_errno(errno, "Not writing utmp: %m");
116 return 0;
117 }
118 return -errno;
119 }
120
121 static int write_entry_wtmp(const struct utmpx *store) {
122 assert(store);
123
124 /* wtmp is a simple append-only file where each entry is
125 * simply appended to the end; i.e. basically a log. */
126
127 errno = 0;
128 updwtmpx(_PATH_WTMPX, store);
129 if (errno == ENOENT) {
130 /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */
131 log_debug_errno(errno, "Not writing wtmp: %m");
132 return 0;
133 }
134 if (errno == EROFS) {
135 log_warning_errno(errno, "Failed to write wtmp record, ignoring: %m");
136 return 0;
137 }
138 return -errno;
139 }
140
141 static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) {
142 int r, s;
143
144 r = write_entry_utmp(store_utmp);
145 s = write_entry_wtmp(store_wtmp);
146 return r < 0 ? r : s;
147 }
148
149 static int write_entry_both(const struct utmpx *store) {
150 return write_utmp_wtmp(store, store);
151 }
152
153 int utmp_put_shutdown(void) {
154 struct utmpx store = {};
155
156 init_entry(&store, 0);
157
158 store.ut_type = RUN_LVL;
159 strncpy(store.ut_user, "shutdown", sizeof(store.ut_user));
160
161 return write_entry_both(&store);
162 }
163
164 int utmp_put_reboot(usec_t t) {
165 struct utmpx store = {};
166
167 init_entry(&store, t);
168
169 store.ut_type = BOOT_TIME;
170 strncpy(store.ut_user, "reboot", sizeof(store.ut_user));
171
172 return write_entry_both(&store);
173 }
174
175 static void copy_suffix(char *buf, size_t buf_size, const char *src) {
176 size_t l;
177
178 l = strlen(src);
179 if (l < buf_size)
180 strncpy(buf, src, buf_size);
181 else
182 memcpy(buf, src + l - buf_size, buf_size);
183 }
184
185 int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) {
186 struct utmpx store = {
187 .ut_type = INIT_PROCESS,
188 .ut_pid = pid,
189 .ut_session = sid,
190 };
191 int r;
192
193 assert(id);
194
195 init_timestamp(&store, 0);
196
197 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
198 copy_suffix(store.ut_id, sizeof(store.ut_id), id);
199
200 if (line)
201 strncpy_exact(store.ut_line, line, sizeof(store.ut_line));
202
203 r = write_entry_both(&store);
204 if (r < 0)
205 return r;
206
207 if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) {
208 store.ut_type = LOGIN_PROCESS;
209 r = write_entry_both(&store);
210 if (r < 0)
211 return r;
212 }
213
214 if (ut_type == USER_PROCESS) {
215 store.ut_type = USER_PROCESS;
216 strncpy(store.ut_user, user, sizeof(store.ut_user)-1);
217 r = write_entry_both(&store);
218 if (r < 0)
219 return r;
220 }
221
222 return 0;
223 }
224
225 int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
226 struct utmpx lookup = {
227 .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
228 }, store, store_wtmp, *found;
229
230 assert(id);
231
232 setutxent();
233
234 /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */
235 copy_suffix(store.ut_id, sizeof(store.ut_id), id);
236
237 found = getutxid(&lookup);
238 if (!found)
239 return 0;
240
241 if (found->ut_pid != pid)
242 return 0;
243
244 memcpy(&store, found, sizeof(store));
245 store.ut_type = DEAD_PROCESS;
246 store.ut_exit.e_termination = code;
247 store.ut_exit.e_exit = status;
248
249 zero(store.ut_user);
250 zero(store.ut_host);
251 zero(store.ut_tv);
252
253 memcpy(&store_wtmp, &store, sizeof(store_wtmp));
254 /* wtmp wants the current time */
255 init_timestamp(&store_wtmp, 0);
256
257 return write_utmp_wtmp(&store, &store_wtmp);
258 }
259
260 int utmp_put_runlevel(int runlevel, int previous) {
261 struct utmpx store = {};
262 int r;
263
264 assert(runlevel > 0);
265
266 if (previous <= 0) {
267 /* Find the old runlevel automatically */
268
269 r = utmp_get_runlevel(&previous, NULL);
270 if (r < 0) {
271 if (r != -ESRCH)
272 return r;
273
274 previous = 0;
275 }
276 }
277
278 if (previous == runlevel)
279 return 0;
280
281 init_entry(&store, 0);
282
283 store.ut_type = RUN_LVL;
284 store.ut_pid = (runlevel & 0xFF) | ((previous & 0xFF) << 8);
285 strncpy(store.ut_user, "runlevel", sizeof(store.ut_user));
286
287 return write_entry_both(&store);
288 }
289
290 #define TIMEOUT_USEC (50 * USEC_PER_MSEC)
291
292 static int write_to_terminal(const char *tty, const char *message) {
293 _cleanup_close_ int fd = -1;
294 const char *p;
295 size_t left;
296 usec_t end;
297
298 assert(tty);
299 assert(message);
300
301 fd = open(tty, O_WRONLY|O_NONBLOCK|O_NOCTTY|O_CLOEXEC);
302 if (fd < 0 || !isatty(fd))
303 return -errno;
304
305 p = message;
306 left = strlen(message);
307
308 end = now(CLOCK_MONOTONIC) + TIMEOUT_USEC;
309
310 while (left > 0) {
311 ssize_t n;
312 usec_t t;
313 int k;
314
315 t = now(CLOCK_MONOTONIC);
316
317 if (t >= end)
318 return -ETIME;
319
320 k = fd_wait_for_event(fd, POLLOUT, end - t);
321 if (k < 0)
322 return k;
323 if (k == 0)
324 return -ETIME;
325
326 n = write(fd, p, left);
327 if (n < 0) {
328 if (errno == EAGAIN)
329 continue;
330
331 return -errno;
332 }
333
334 assert((size_t) n <= left);
335
336 p += n;
337 left -= n;
338 }
339
340 return 0;
341 }
342
343 int utmp_wall(
344 const char *message,
345 const char *username,
346 const char *origin_tty,
347 bool (*match_tty)(const char *tty, void *userdata),
348 void *userdata) {
349
350 _cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *stdin_tty = NULL;
351 char date[FORMAT_TIMESTAMP_MAX];
352 struct utmpx *u;
353 int r;
354
355 hn = gethostname_malloc();
356 if (!hn)
357 return -ENOMEM;
358 if (!username) {
359 un = getlogname_malloc();
360 if (!un)
361 return -ENOMEM;
362 }
363
364 if (!origin_tty) {
365 getttyname_harder(STDIN_FILENO, &stdin_tty);
366 origin_tty = stdin_tty;
367 }
368
369 if (asprintf(&text,
370 "\a\r\n"
371 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
372 "%s\r\n\r\n",
373 un ?: username, hn,
374 origin_tty ? " on " : "", strempty(origin_tty),
375 format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
376 message) < 0)
377 return -ENOMEM;
378
379 setutxent();
380
381 r = 0;
382
383 while ((u = getutxent())) {
384 _cleanup_free_ char *buf = NULL;
385 const char *path;
386 int q;
387
388 if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0)
389 continue;
390
391 /* this access is fine, because STRLEN("/dev/") << 32 (UT_LINESIZE) */
392 if (path_startswith(u->ut_line, "/dev/"))
393 path = u->ut_line;
394 else {
395 if (asprintf(&buf, "/dev/%.*s", (int) sizeof(u->ut_line), u->ut_line) < 0)
396 return -ENOMEM;
397
398 path = buf;
399 }
400
401 if (!match_tty || match_tty(path, userdata)) {
402 q = write_to_terminal(path, text);
403 if (q < 0)
404 r = q;
405 }
406 }
407
408 return r;
409 }