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