1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include <sys/utsname.h>
15 #include "alloc-util.h"
17 #include "hostname-util.h"
19 #include "path-util.h"
20 #include "string-util.h"
21 #include "terminal-util.h"
22 #include "time-util.h"
23 #include "user-util.h"
25 #include "utmp-wtmp.h"
27 int utmp_get_runlevel(int *runlevel, int *previous) {
28 struct utmpx *found, lookup = { .ut_type = RUN_LVL };
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 does not apply to the current script being
41 e = getenv("RUNLEVEL");
46 /* $PREVLEVEL seems to be an Upstart thing */
48 e = getenv("PREVLEVEL");
58 if (utmpxname(_PATH_UTMPX) < 0)
63 found = getutxid(&lookup);
69 a = found->ut_pid & 0xFF;
70 b = (found->ut_pid >> 8) & 0xFF;
84 static void init_timestamp(struct utmpx *store, usec_t t) {
88 t = now(CLOCK_REALTIME);
90 store->ut_tv.tv_sec = t / USEC_PER_SEC;
91 store->ut_tv.tv_usec = t % USEC_PER_SEC;
94 static void init_entry(struct utmpx *store, usec_t t) {
95 struct utsname uts = {};
99 init_timestamp(store, t);
101 if (uname(&uts) >= 0)
102 strncpy(store->ut_host, uts.release, sizeof(store->ut_host));
104 strncpy(store->ut_line, "~", sizeof(store->ut_line)); /* or ~~ ? */
105 strncpy(store->ut_id, "~~", sizeof(store->ut_id));
108 static int write_entry_utmp(const struct utmpx *store) {
113 /* utmp is similar to wtmp, but there is only one entry for
114 * each entry type resp. user; i.e. basically a key/value
117 if (utmpxname(_PATH_UTMPX) < 0)
122 if (!pututxline(store))
132 static int write_entry_wtmp(const struct utmpx *store) {
135 /* wtmp is a simple append-only file where each entry is
136 simply appended to the end; i.e. basically a log. */
139 updwtmpx(_PATH_WTMPX, store);
143 static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) {
146 r = write_entry_utmp(store_utmp);
147 s = write_entry_wtmp(store_wtmp);
152 /* If utmp/wtmp have been disabled, that's a good thing, hence
153 * ignore the errors */
160 static int write_entry_both(const struct utmpx *store) {
161 return write_utmp_wtmp(store, store);
164 int utmp_put_shutdown(void) {
165 struct utmpx store = {};
167 init_entry(&store, 0);
169 store.ut_type = RUN_LVL;
170 strncpy(store.ut_user, "shutdown", sizeof(store.ut_user));
172 return write_entry_both(&store);
175 int utmp_put_reboot(usec_t t) {
176 struct utmpx store = {};
178 init_entry(&store, t);
180 store.ut_type = BOOT_TIME;
181 strncpy(store.ut_user, "reboot", sizeof(store.ut_user));
183 return write_entry_both(&store);
186 _pure_ static const char *sanitize_id(const char *id) {
192 if (l <= sizeof(((struct utmpx*) NULL)->ut_id))
195 return id + l - sizeof(((struct utmpx*) NULL)->ut_id);
198 int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) {
199 struct utmpx store = {
200 .ut_type = INIT_PROCESS,
208 init_timestamp(&store, 0);
210 /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
211 strncpy(store.ut_id, sanitize_id(id), sizeof(store.ut_id));
214 strncpy(store.ut_line, basename(line), sizeof(store.ut_line));
216 r = write_entry_both(&store);
220 if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) {
221 store.ut_type = LOGIN_PROCESS;
222 r = write_entry_both(&store);
227 if (ut_type == USER_PROCESS) {
228 store.ut_type = USER_PROCESS;
229 strncpy(store.ut_user, user, sizeof(store.ut_user)-1);
230 r = write_entry_both(&store);
238 int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
239 struct utmpx lookup = {
240 .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
241 }, store, store_wtmp, *found;
247 /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
248 strncpy(lookup.ut_id, sanitize_id(id), sizeof(lookup.ut_id));
250 found = getutxid(&lookup);
254 if (found->ut_pid != pid)
257 memcpy(&store, found, sizeof(store));
258 store.ut_type = DEAD_PROCESS;
259 store.ut_exit.e_termination = code;
260 store.ut_exit.e_exit = status;
266 memcpy(&store_wtmp, &store, sizeof(store_wtmp));
267 /* wtmp wants the current time */
268 init_timestamp(&store_wtmp, 0);
270 return write_utmp_wtmp(&store, &store_wtmp);
273 int utmp_put_runlevel(int runlevel, int previous) {
274 struct utmpx store = {};
277 assert(runlevel > 0);
280 /* Find the old runlevel automatically */
282 r = utmp_get_runlevel(&previous, NULL);
291 if (previous == runlevel)
294 init_entry(&store, 0);
296 store.ut_type = RUN_LVL;
297 store.ut_pid = (runlevel & 0xFF) | ((previous & 0xFF) << 8);
298 strncpy(store.ut_user, "runlevel", sizeof(store.ut_user));
300 return write_entry_both(&store);
303 #define TIMEOUT_MSEC 50
305 static int write_to_terminal(const char *tty, const char *message) {
306 _cleanup_close_ int fd = -1;
314 fd = open(tty, O_WRONLY|O_NONBLOCK|O_NOCTTY|O_CLOEXEC);
315 if (fd < 0 || !isatty(fd))
319 left = strlen(message);
321 end = now(CLOCK_MONOTONIC) + TIMEOUT_MSEC*USEC_PER_MSEC;
325 struct pollfd pollfd = {
332 t = now(CLOCK_MONOTONIC);
337 k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC);
344 n = write(fd, p, left);
352 assert((size_t) n <= left);
363 const char *username,
364 const char *origin_tty,
365 bool (*match_tty)(const char *tty, void *userdata),
368 _cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *stdin_tty = NULL;
369 char date[FORMAT_TIMESTAMP_MAX];
373 hn = gethostname_malloc();
377 un = getlogname_malloc();
383 getttyname_harder(STDIN_FILENO, &stdin_tty);
384 origin_tty = stdin_tty;
389 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
392 origin_tty ? " on " : "", strempty(origin_tty),
393 format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
401 while ((u = getutxent())) {
402 _cleanup_free_ char *buf = NULL;
406 if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0)
409 /* this access is fine, because STRLEN("/dev/") << 32 (UT_LINESIZE) */
410 if (path_startswith(u->ut_line, "/dev/"))
413 if (asprintf(&buf, "/dev/%.*s", (int) sizeof(u->ut_line), u->ut_line) < 0)
419 if (!match_tty || match_tty(path, userdata)) {
420 q = write_to_terminal(path, text);