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