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