1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
16 #include <sys/utsname.h>
20 #include "alloc-util.h"
22 #include "hostname-util.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"
30 #include "utmp-wtmp.h"
32 int utmp_get_runlevel(int *runlevel, int *previous) {
33 struct utmpx *found, lookup = { .ut_type = RUN_LVL };
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
46 e = getenv("RUNLEVEL");
51 /* $PREVLEVEL seems to be an Upstart thing */
53 e = getenv("PREVLEVEL");
63 if (utmpxname(_PATH_UTMPX) < 0)
68 found = getutxid(&lookup);
74 a = found->ut_pid & 0xFF;
75 b = (found->ut_pid >> 8) & 0xFF;
89 static void init_timestamp(struct utmpx *store, usec_t t) {
93 t = now(CLOCK_REALTIME);
95 store->ut_tv.tv_sec = t / USEC_PER_SEC;
96 store->ut_tv.tv_usec = t % USEC_PER_SEC;
99 static void init_entry(struct utmpx *store, usec_t t) {
100 struct utsname uts = {};
104 init_timestamp(store, t);
106 if (uname(&uts) >= 0)
107 strncpy(store->ut_host, uts.release, sizeof(store->ut_host));
109 strncpy(store->ut_line, "~", sizeof(store->ut_line)); /* or ~~ ? */
110 strncpy(store->ut_id, "~~", sizeof(store->ut_id));
113 static int write_entry_utmp(const struct utmpx *store) {
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
122 if (utmpxname(_PATH_UTMPX) < 0)
127 if (!pututxline(store))
137 static int write_entry_wtmp(const struct utmpx *store) {
140 /* wtmp is a simple append-only file where each entry is
141 simply appended to the end; i.e. basically a log. */
144 updwtmpx(_PATH_WTMPX, store);
148 static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) {
151 r = write_entry_utmp(store_utmp);
152 s = write_entry_wtmp(store_wtmp);
157 /* If utmp/wtmp have been disabled, that's a good thing, hence
158 * ignore the errors */
165 static int write_entry_both(const struct utmpx *store) {
166 return write_utmp_wtmp(store, store);
169 int utmp_put_shutdown(void) {
170 struct utmpx store = {};
172 init_entry(&store, 0);
174 store.ut_type = RUN_LVL;
175 strncpy(store.ut_user, "shutdown", sizeof(store.ut_user));
177 return write_entry_both(&store);
180 int utmp_put_reboot(usec_t t) {
181 struct utmpx store = {};
183 init_entry(&store, t);
185 store.ut_type = BOOT_TIME;
186 strncpy(store.ut_user, "reboot", sizeof(store.ut_user));
188 return write_entry_both(&store);
191 _pure_ static const char *sanitize_id(const char *id) {
197 if (l <= sizeof(((struct utmpx*) NULL)->ut_id))
200 return id + l - sizeof(((struct utmpx*) NULL)->ut_id);
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,
213 init_timestamp(&store, 0);
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));
219 strncpy(store.ut_line, basename(line), sizeof(store.ut_line));
221 r = write_entry_both(&store);
225 if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) {
226 store.ut_type = LOGIN_PROCESS;
227 r = write_entry_both(&store);
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);
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;
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));
255 found = getutxid(&lookup);
259 if (found->ut_pid != pid)
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;
271 memcpy(&store_wtmp, &store, sizeof(store_wtmp));
272 /* wtmp wants the current time */
273 init_timestamp(&store_wtmp, 0);
275 return write_utmp_wtmp(&store, &store_wtmp);
278 int utmp_put_runlevel(int runlevel, int previous) {
279 struct utmpx store = {};
282 assert(runlevel > 0);
285 /* Find the old runlevel automatically */
287 r = utmp_get_runlevel(&previous, NULL);
296 if (previous == runlevel)
299 init_entry(&store, 0);
301 store.ut_type = RUN_LVL;
302 store.ut_pid = (runlevel & 0xFF) | ((previous & 0xFF) << 8);
303 strncpy(store.ut_user, "runlevel", sizeof(store.ut_user));
305 return write_entry_both(&store);
308 #define TIMEOUT_MSEC 50
310 static int write_to_terminal(const char *tty, const char *message) {
311 _cleanup_close_ int fd = -1;
319 fd = open(tty, O_WRONLY|O_NONBLOCK|O_NOCTTY|O_CLOEXEC);
320 if (fd < 0 || !isatty(fd))
324 left = strlen(message);
326 end = now(CLOCK_MONOTONIC) + TIMEOUT_MSEC*USEC_PER_MSEC;
330 struct pollfd pollfd = {
337 t = now(CLOCK_MONOTONIC);
342 k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC);
349 n = write(fd, p, left);
357 assert((size_t) n <= left);
368 const char *username,
369 const char *origin_tty,
370 bool (*match_tty)(const char *tty, void *userdata),
373 _cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *stdin_tty = NULL;
374 char date[FORMAT_TIMESTAMP_MAX];
378 hn = gethostname_malloc();
382 un = getlogname_malloc();
388 getttyname_harder(STDIN_FILENO, &stdin_tty);
389 origin_tty = stdin_tty;
394 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
397 origin_tty ? " on " : "", strempty(origin_tty),
398 format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
406 while ((u = getutxent())) {
407 _cleanup_free_ char *buf = NULL;
411 if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0)
414 /* this access is fine, because STRLEN("/dev/") << 32 (UT_LINESIZE) */
415 if (path_startswith(u->ut_line, "/dev/"))
418 if (asprintf(&buf, "/dev/%.*s", (int) sizeof(u->ut_line), u->ut_line) < 0)
424 if (!match_tty || match_tty(path, userdata)) {
425 q = write_to_terminal(path, text);