2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include <sys/utsname.h>
32 #include "alloc-util.h"
34 #include "hostname-util.h"
36 #include "path-util.h"
37 #include "string-util.h"
38 #include "terminal-util.h"
39 #include "time-util.h"
40 #include "user-util.h"
42 #include "utmp-wtmp.h"
44 int utmp_get_runlevel(int *runlevel, int *previous) {
45 struct utmpx *found, lookup = { .ut_type = RUN_LVL };
51 /* If these values are set in the environment this takes
52 * precedence. Presumably, sysvinit does this to work around a
53 * race condition that would otherwise exist where we'd always
54 * go to disk and hence might read runlevel data that might be
55 * very new and does not apply to the current script being
58 e = getenv("RUNLEVEL");
63 /* $PREVLEVEL seems to be an Upstart thing */
65 e = getenv("PREVLEVEL");
75 if (utmpxname(_PATH_UTMPX) < 0)
80 found = getutxid(&lookup);
86 a = found->ut_pid & 0xFF;
87 b = (found->ut_pid >> 8) & 0xFF;
101 static void init_timestamp(struct utmpx *store, usec_t t) {
105 t = now(CLOCK_REALTIME);
107 store->ut_tv.tv_sec = t / USEC_PER_SEC;
108 store->ut_tv.tv_usec = t % USEC_PER_SEC;
111 static void init_entry(struct utmpx *store, usec_t t) {
112 struct utsname uts = {};
116 init_timestamp(store, t);
118 if (uname(&uts) >= 0)
119 strncpy(store->ut_host, uts.release, sizeof(store->ut_host));
121 strncpy(store->ut_line, "~", sizeof(store->ut_line)); /* or ~~ ? */
122 strncpy(store->ut_id, "~~", sizeof(store->ut_id));
125 static int write_entry_utmp(const struct utmpx *store) {
130 /* utmp is similar to wtmp, but there is only one entry for
131 * each entry type resp. user; i.e. basically a key/value
134 if (utmpxname(_PATH_UTMPX) < 0)
139 if (!pututxline(store))
149 static int write_entry_wtmp(const struct utmpx *store) {
152 /* wtmp is a simple append-only file where each entry is
153 simply appended to the end; i.e. basically a log. */
156 updwtmpx(_PATH_WTMPX, store);
160 static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) {
163 r = write_entry_utmp(store_utmp);
164 s = write_entry_wtmp(store_wtmp);
169 /* If utmp/wtmp have been disabled, that's a good thing, hence
170 * ignore the errors */
177 static int write_entry_both(const struct utmpx *store) {
178 return write_utmp_wtmp(store, store);
181 int utmp_put_shutdown(void) {
182 struct utmpx store = {};
184 init_entry(&store, 0);
186 store.ut_type = RUN_LVL;
187 strncpy(store.ut_user, "shutdown", sizeof(store.ut_user));
189 return write_entry_both(&store);
192 int utmp_put_reboot(usec_t t) {
193 struct utmpx store = {};
195 init_entry(&store, t);
197 store.ut_type = BOOT_TIME;
198 strncpy(store.ut_user, "reboot", sizeof(store.ut_user));
200 return write_entry_both(&store);
203 _pure_ static const char *sanitize_id(const char *id) {
209 if (l <= sizeof(((struct utmpx*) NULL)->ut_id))
212 return id + l - sizeof(((struct utmpx*) NULL)->ut_id);
215 int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) {
216 struct utmpx store = {
217 .ut_type = INIT_PROCESS,
225 init_timestamp(&store, 0);
227 /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
228 strncpy(store.ut_id, sanitize_id(id), sizeof(store.ut_id));
231 strncpy(store.ut_line, basename(line), sizeof(store.ut_line));
233 r = write_entry_both(&store);
237 if (ut_type == LOGIN_PROCESS || ut_type == USER_PROCESS) {
238 store.ut_type = LOGIN_PROCESS;
239 r = write_entry_both(&store);
244 if (ut_type == USER_PROCESS) {
245 store.ut_type = USER_PROCESS;
246 strncpy(store.ut_user, user, sizeof(store.ut_user)-1);
247 r = write_entry_both(&store);
255 int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
256 struct utmpx lookup = {
257 .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
258 }, store, store_wtmp, *found;
264 /* ut_id needs only be nul-terminated if it is shorter than sizeof(ut_id) */
265 strncpy(lookup.ut_id, sanitize_id(id), sizeof(lookup.ut_id));
267 found = getutxid(&lookup);
271 if (found->ut_pid != pid)
274 memcpy(&store, found, sizeof(store));
275 store.ut_type = DEAD_PROCESS;
276 store.ut_exit.e_termination = code;
277 store.ut_exit.e_exit = status;
283 memcpy(&store_wtmp, &store, sizeof(store_wtmp));
284 /* wtmp wants the current time */
285 init_timestamp(&store_wtmp, 0);
287 return write_utmp_wtmp(&store, &store_wtmp);
291 int utmp_put_runlevel(int runlevel, int previous) {
292 struct utmpx store = {};
295 assert(runlevel > 0);
298 /* Find the old runlevel automatically */
300 r = utmp_get_runlevel(&previous, NULL);
309 if (previous == runlevel)
312 init_entry(&store, 0);
314 store.ut_type = RUN_LVL;
315 store.ut_pid = (runlevel & 0xFF) | ((previous & 0xFF) << 8);
316 strncpy(store.ut_user, "runlevel", sizeof(store.ut_user));
318 return write_entry_both(&store);
321 #define TIMEOUT_MSEC 50
323 static int write_to_terminal(const char *tty, const char *message) {
324 _cleanup_close_ int fd = -1;
332 fd = open(tty, O_WRONLY|O_NDELAY|O_NOCTTY|O_CLOEXEC);
333 if (fd < 0 || !isatty(fd))
337 left = strlen(message);
339 end = now(CLOCK_MONOTONIC) + TIMEOUT_MSEC*USEC_PER_MSEC;
343 struct pollfd pollfd = {
350 t = now(CLOCK_MONOTONIC);
355 k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC);
362 n = write(fd, p, left);
370 assert((size_t) n <= left);
381 const char *username,
382 const char *origin_tty,
383 bool (*match_tty)(const char *tty, void *userdata),
386 _cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *stdin_tty = NULL;
387 char date[FORMAT_TIMESTAMP_MAX];
391 hn = gethostname_malloc();
395 un = getlogname_malloc();
401 getttyname_harder(STDIN_FILENO, &stdin_tty);
402 origin_tty = stdin_tty;
407 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
410 origin_tty ? " on " : "", strempty(origin_tty),
411 format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
419 while ((u = getutxent())) {
420 _cleanup_free_ char *buf = NULL;
424 if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0)
427 /* this access is fine, because strlen("/dev/") << 32 (UT_LINESIZE) */
428 if (path_startswith(u->ut_line, "/dev/"))
431 if (asprintf(&buf, "/dev/%.*s", (int) sizeof(u->ut_line), u->ut_line) < 0)
437 if (!match_tty || match_tty(path, userdata)) {
438 q = write_to_terminal(path, text);