1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
14 #include <sys/utsname.h>
18 #include "alloc-util.h"
20 #include "hostname-util.h"
22 #include "path-util.h"
23 #include "string-util.h"
24 #include "terminal-util.h"
25 #include "time-util.h"
26 #include "user-util.h"
28 #include "utmp-wtmp.h"
30 int utmp_get_runlevel(int *runlevel, int *previous) {
31 struct utmpx *found, lookup = { .ut_type = RUN_LVL };
37 /* If these values are set in the environment this takes
38 * precedence. Presumably, sysvinit does this to work around a
39 * race condition that would otherwise exist where we'd always
40 * go to disk and hence might read runlevel data that might be
41 * very new and does not apply to the current script being
44 e = getenv("RUNLEVEL");
49 /* $PREVLEVEL seems to be an Upstart thing */
51 e = getenv("PREVLEVEL");
61 if (utmpxname(_PATH_UTMPX) < 0)
66 found = getutxid(&lookup);
72 a = found->ut_pid & 0xFF;
73 b = (found->ut_pid >> 8) & 0xFF;
87 static void init_timestamp(struct utmpx *store, usec_t t) {
91 t = now(CLOCK_REALTIME);
93 store->ut_tv.tv_sec = t / USEC_PER_SEC;
94 store->ut_tv.tv_usec = t % USEC_PER_SEC;
97 static void init_entry(struct utmpx *store, usec_t t) {
98 struct utsname uts = {};
102 init_timestamp(store, t);
104 if (uname(&uts) >= 0)
105 strncpy(store->ut_host, uts.release, sizeof(store->ut_host));
107 strncpy(store->ut_line, "~", sizeof(store->ut_line)); /* or ~~ ? */
108 strncpy(store->ut_id, "~~", sizeof(store->ut_id));
111 static int write_entry_utmp(const struct utmpx *store) {
116 /* utmp is similar to wtmp, but there is only one entry for
117 * each entry type resp. user; i.e. basically a key/value
120 if (utmpxname(_PATH_UTMPX) < 0)
125 if (!pututxline(store))
135 static int write_entry_wtmp(const struct utmpx *store) {
138 /* wtmp is a simple append-only file where each entry is
139 simply appended to the end; i.e. basically a log. */
142 updwtmpx(_PATH_WTMPX, store);
146 static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) {
149 r = write_entry_utmp(store_utmp);
150 s = write_entry_wtmp(store_wtmp);
155 /* If utmp/wtmp have been disabled, that's a good thing, hence
156 * ignore the errors */
163 static int write_entry_both(const struct utmpx *store) {
164 return write_utmp_wtmp(store, store);
167 int utmp_put_shutdown(void) {
168 struct utmpx store = {};
170 init_entry(&store, 0);
172 store.ut_type = RUN_LVL;
173 strncpy(store.ut_user, "shutdown", sizeof(store.ut_user));
175 return write_entry_both(&store);
178 int utmp_put_reboot(usec_t t) {
179 struct utmpx store = {};
181 init_entry(&store, t);
183 store.ut_type = BOOT_TIME;
184 strncpy(store.ut_user, "reboot", sizeof(store.ut_user));
186 return write_entry_both(&store);
189 _pure_ static const char *sanitize_id(const char *id) {
195 if (l <= sizeof(((struct utmpx*) NULL)->ut_id))
198 return id + l - sizeof(((struct utmpx*) NULL)->ut_id);
201 int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) {
202 struct utmpx store = {
203 .ut_type = INIT_PROCESS,
211 init_timestamp(&store, 0);
213 /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
214 strncpy(store.ut_id, sanitize_id(id), sizeof(store.ut_id));
217 strncpy(store.ut_line, basename(line), sizeof(store.ut_line));
219 r = write_entry_both(&store);
223 if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) {
224 store.ut_type = LOGIN_PROCESS;
225 r = write_entry_both(&store);
230 if (ut_type == USER_PROCESS) {
231 store.ut_type = USER_PROCESS;
232 strncpy(store.ut_user, user, sizeof(store.ut_user)-1);
233 r = write_entry_both(&store);
241 int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
242 struct utmpx lookup = {
243 .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
244 }, store, store_wtmp, *found;
250 /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
251 strncpy(lookup.ut_id, sanitize_id(id), sizeof(lookup.ut_id));
253 found = getutxid(&lookup);
257 if (found->ut_pid != pid)
260 memcpy(&store, found, sizeof(store));
261 store.ut_type = DEAD_PROCESS;
262 store.ut_exit.e_termination = code;
263 store.ut_exit.e_exit = status;
269 memcpy(&store_wtmp, &store, sizeof(store_wtmp));
270 /* wtmp wants the current time */
271 init_timestamp(&store_wtmp, 0);
273 return write_utmp_wtmp(&store, &store_wtmp);
276 int utmp_put_runlevel(int runlevel, int previous) {
277 struct utmpx store = {};
280 assert(runlevel > 0);
283 /* Find the old runlevel automatically */
285 r = utmp_get_runlevel(&previous, NULL);
294 if (previous == runlevel)
297 init_entry(&store, 0);
299 store.ut_type = RUN_LVL;
300 store.ut_pid = (runlevel & 0xFF) | ((previous & 0xFF) << 8);
301 strncpy(store.ut_user, "runlevel", sizeof(store.ut_user));
303 return write_entry_both(&store);
306 #define TIMEOUT_MSEC 50
308 static int write_to_terminal(const char *tty, const char *message) {
309 _cleanup_close_ int fd = -1;
317 fd = open(tty, O_WRONLY|O_NONBLOCK|O_NOCTTY|O_CLOEXEC);
318 if (fd < 0 || !isatty(fd))
322 left = strlen(message);
324 end = now(CLOCK_MONOTONIC) + TIMEOUT_MSEC*USEC_PER_MSEC;
328 struct pollfd pollfd = {
335 t = now(CLOCK_MONOTONIC);
340 k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC);
347 n = write(fd, p, left);
355 assert((size_t) n <= left);
366 const char *username,
367 const char *origin_tty,
368 bool (*match_tty)(const char *tty, void *userdata),
371 _cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *stdin_tty = NULL;
372 char date[FORMAT_TIMESTAMP_MAX];
376 hn = gethostname_malloc();
380 un = getlogname_malloc();
386 getttyname_harder(STDIN_FILENO, &stdin_tty);
387 origin_tty = stdin_tty;
392 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
395 origin_tty ? " on " : "", strempty(origin_tty),
396 format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
404 while ((u = getutxent())) {
405 _cleanup_free_ char *buf = NULL;
409 if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0)
412 /* this access is fine, because STRLEN("/dev/") << 32 (UT_LINESIZE) */
413 if (path_startswith(u->ut_line, "/dev/"))
416 if (asprintf(&buf, "/dev/%.*s", (int) sizeof(u->ut_line), u->ut_line) < 0)
422 if (!match_tty || match_tty(path, userdata)) {
423 q = write_to_terminal(path, text);