]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/utmp-wtmp.c
tree-wide: drop license boilerplate
[thirdparty/systemd.git] / src / shared / utmp-wtmp.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <poll.h>
11 #include <stddef.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <sys/time.h>
16 #include <sys/utsname.h>
17 #include <unistd.h>
18 #include <utmpx.h>
19
20 #include "alloc-util.h"
21 #include "fd-util.h"
22 #include "hostname-util.h"
23 #include "macro.h"
24 #include "path-util.h"
25 #include "string-util.h"
26 #include "terminal-util.h"
27 #include "time-util.h"
28 #include "user-util.h"
29 #include "util.h"
30 #include "utmp-wtmp.h"
31
32 int utmp_get_runlevel(int *runlevel, int *previous) {
33 struct utmpx *found, lookup = { .ut_type = RUN_LVL };
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
46 e = getenv("RUNLEVEL");
47 if (e && e[0] > 0) {
48 *runlevel = e[0];
49
50 if (previous) {
51 /* $PREVLEVEL seems to be an Upstart thing */
52
53 e = getenv("PREVLEVEL");
54 if (e && e[0] > 0)
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
68 found = getutxid(&lookup);
69 if (!found)
70 r = -errno;
71 else {
72 int a, b;
73
74 a = found->ut_pid & 0xFF;
75 b = (found->ut_pid >> 8) & 0xFF;
76
77 *runlevel = a;
78 if (previous)
79 *previous = b;
80
81 r = 0;
82 }
83
84 endutxent();
85
86 return r;
87 }
88
89 static void init_timestamp(struct utmpx *store, usec_t t) {
90 assert(store);
91
92 if (t <= 0)
93 t = now(CLOCK_REALTIME);
94
95 store->ut_tv.tv_sec = t / USEC_PER_SEC;
96 store->ut_tv.tv_usec = t % USEC_PER_SEC;
97 }
98
99 static void init_entry(struct utmpx *store, usec_t t) {
100 struct utsname uts = {};
101
102 assert(store);
103
104 init_timestamp(store, t);
105
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
113 static 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
137 static int write_entry_wtmp(const struct utmpx *store) {
138 assert(store);
139
140 /* wtmp is a simple append-only file where each entry is
141 simply appended to the end; i.e. basically a log. */
142
143 errno = 0;
144 updwtmpx(_PATH_WTMPX, store);
145 return -errno;
146 }
147
148 static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) {
149 int r, s;
150
151 r = write_entry_utmp(store_utmp);
152 s = write_entry_wtmp(store_wtmp);
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
165 static int write_entry_both(const struct utmpx *store) {
166 return write_utmp_wtmp(store, store);
167 }
168
169 int utmp_put_shutdown(void) {
170 struct utmpx store = {};
171
172 init_entry(&store, 0);
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
180 int utmp_put_reboot(usec_t t) {
181 struct utmpx store = {};
182
183 init_entry(&store, t);
184
185 store.ut_type = BOOT_TIME;
186 strncpy(store.ut_user, "reboot", sizeof(store.ut_user));
187
188 return write_entry_both(&store);
189 }
190
191 _pure_ static const char *sanitize_id(const char *id) {
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
203 int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) {
204 struct utmpx store = {
205 .ut_type = INIT_PROCESS,
206 .ut_pid = pid,
207 .ut_session = sid,
208 };
209 int r;
210
211 assert(id);
212
213 init_timestamp(&store, 0);
214
215 /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
216 strncpy(store.ut_id, sanitize_id(id), sizeof(store.ut_id));
217
218 if (line)
219 strncpy(store.ut_line, basename(line), sizeof(store.ut_line));
220
221 r = write_entry_both(&store);
222 if (r < 0)
223 return r;
224
225 if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) {
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;
241 }
242
243 int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
244 struct utmpx lookup = {
245 .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
246 }, store, store_wtmp, *found;
247
248 assert(id);
249
250 setutxent();
251
252 /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
253 strncpy(lookup.ut_id, sanitize_id(id), sizeof(lookup.ut_id));
254
255 found = getutxid(&lookup);
256 if (!found)
257 return 0;
258
259 if (found->ut_pid != pid)
260 return 0;
261
262 memcpy(&store, found, sizeof(store));
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
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);
276 }
277
278
279 int utmp_put_runlevel(int runlevel, int previous) {
280 struct utmpx store = {};
281 int r;
282
283 assert(runlevel > 0);
284
285 if (previous <= 0) {
286 /* Find the old runlevel automatically */
287
288 r = utmp_get_runlevel(&previous, NULL);
289 if (r < 0) {
290 if (r != -ESRCH)
291 return r;
292
293 previous = 0;
294 }
295 }
296
297 if (previous == runlevel)
298 return 0;
299
300 init_entry(&store, 0);
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 }
308
309 #define TIMEOUT_MSEC 50
310
311 static int write_to_terminal(const char *tty, const char *message) {
312 _cleanup_close_ int fd = -1;
313 const char *p;
314 size_t left;
315 usec_t end;
316
317 assert(tty);
318 assert(message);
319
320 fd = open(tty, O_WRONLY|O_NONBLOCK|O_NOCTTY|O_CLOEXEC);
321 if (fd < 0 || !isatty(fd))
322 return -errno;
323
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;
331 struct pollfd pollfd = {
332 .fd = fd,
333 .events = POLLOUT,
334 };
335 usec_t t;
336 int k;
337
338 t = now(CLOCK_MONOTONIC);
339
340 if (t >= end)
341 return -ETIME;
342
343 k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC);
344 if (k < 0)
345 return -errno;
346
347 if (k == 0)
348 return -ETIME;
349
350 n = write(fd, p, left);
351 if (n < 0) {
352 if (errno == EAGAIN)
353 continue;
354
355 return -errno;
356 }
357
358 assert((size_t) n <= left);
359
360 p += n;
361 left -= n;
362 }
363
364 return 0;
365 }
366
367 int 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;
375 char date[FORMAT_TIMESTAMP_MAX];
376 struct utmpx *u;
377 int r;
378
379 hn = gethostname_malloc();
380 if (!hn)
381 return -ENOMEM;
382 if (!username) {
383 un = getlogname_malloc();
384 if (!un)
385 return -ENOMEM;
386 }
387
388 if (!origin_tty) {
389 getttyname_harder(STDIN_FILENO, &stdin_tty);
390 origin_tty = stdin_tty;
391 }
392
393 if (asprintf(&text,
394 "\a\r\n"
395 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
396 "%s\r\n\r\n",
397 un ?: username, hn,
398 origin_tty ? " on " : "", strempty(origin_tty),
399 format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
400 message) < 0)
401 return -ENOMEM;
402
403 setutxent();
404
405 r = 0;
406
407 while ((u = getutxent())) {
408 _cleanup_free_ char *buf = NULL;
409 const char *path;
410 int q;
411
412 if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0)
413 continue;
414
415 /* this access is fine, because STRLEN("/dev/") << 32 (UT_LINESIZE) */
416 if (path_startswith(u->ut_line, "/dev/"))
417 path = u->ut_line;
418 else {
419 if (asprintf(&buf, "/dev/%.*s", (int) sizeof(u->ut_line), u->ut_line) < 0)
420 return -ENOMEM;
421
422 path = buf;
423 }
424
425 if (!match_tty || match_tty(path, userdata)) {
426 q = write_to_terminal(path, text);
427 if (q < 0)
428 r = q;
429 }
430 }
431
432 return r;
433 }