1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/utsname.h>
32 #include "utmp-wtmp.h"
34 int utmp_get_runlevel(int *runlevel, int *previous) {
35 struct utmpx lookup, *found;
41 /* If these values are set in the environment this takes
42 * precedence. Presumably, sysvinit does this to work around a
43 * race condition that would otherwise exist where we'd always
44 * go to disk and hence might read runlevel data that might be
45 * very new and does not apply to the current script being
48 if ((e = getenv("RUNLEVEL")) && e[0] > 0) {
52 /* $PREVLEVEL seems to be an Upstart thing */
54 if ((e = getenv("PREVLEVEL")) && e[0] > 0)
63 if (utmpxname(_PATH_UTMPX) < 0)
69 lookup.ut_type = RUN_LVL;
71 if (!(found = getutxid(&lookup)))
76 a = found->ut_pid & 0xFF;
77 b = (found->ut_pid >> 8) & 0xFF;
95 static void init_timestamp(struct utmpx *store, usec_t t) {
101 t = now(CLOCK_REALTIME);
103 store->ut_tv.tv_sec = t / USEC_PER_SEC;
104 store->ut_tv.tv_usec = t % USEC_PER_SEC;
107 static void init_entry(struct utmpx *store, usec_t t) {
112 init_timestamp(store, t);
116 if (uname(&uts) >= 0)
117 strncpy(store->ut_host, uts.release, sizeof(store->ut_host));
119 strncpy(store->ut_line, "~", sizeof(store->ut_line)); /* or ~~ ? */
120 strncpy(store->ut_id, "~~", sizeof(store->ut_id));
123 static int write_entry_utmp(const struct utmpx *store) {
128 /* utmp is similar to wtmp, but there is only one entry for
129 * each entry type resp. user; i.e. basically a key/value
132 if (utmpxname(_PATH_UTMPX) < 0)
137 if (!pututxline(store))
147 static int write_entry_wtmp(const struct utmpx *store) {
150 /* wtmp is a simple append-only file where each entry is
151 simply appended to * the end; i.e. basically a log. */
154 updwtmpx(_PATH_WTMPX, store);
158 static int write_entry_both(const struct utmpx *store) {
161 r = write_entry_utmp(store);
162 s = write_entry_wtmp(store);
167 /* If utmp/wtmp have been disabled, that's a good thing, hence
168 * ignore the errors */
175 int utmp_put_shutdown(usec_t t) {
178 init_entry(&store, t);
180 store.ut_type = RUN_LVL;
181 strncpy(store.ut_user, "shutdown", sizeof(store.ut_user));
183 return write_entry_both(&store);
186 int utmp_put_reboot(usec_t t) {
189 init_entry(&store, t);
191 store.ut_type = BOOT_TIME;
192 strncpy(store.ut_user, "reboot", sizeof(store.ut_user));
194 return write_entry_both(&store);
197 static const char *sanitize_id(const char *id) {
203 if (l <= sizeof(((struct utmpx*) NULL)->ut_id))
206 return id + l - sizeof(((struct utmpx*) NULL)->ut_id);
209 int utmp_put_init_process(usec_t t, const char *id, pid_t pid, pid_t sid, const char *line) {
214 init_timestamp(&store, t);
216 store.ut_type = INIT_PROCESS;
218 store.ut_session = sid;
220 strncpy(store.ut_id, sanitize_id(id), sizeof(store.ut_id));
223 strncpy(store.ut_line, file_name_from_path(line), sizeof(store.ut_line));
225 return write_entry_both(&store);
228 int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) {
229 struct utmpx lookup, store, *found;
236 lookup.ut_type = INIT_PROCESS; /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */
237 strncpy(lookup.ut_id, sanitize_id(id), sizeof(lookup.ut_id));
239 if (!(found = getutxid(&lookup)))
242 if (found->ut_pid != pid)
247 memcpy(&store, &lookup, sizeof(store));
248 store.ut_type = DEAD_PROCESS;
249 store.ut_exit.e_termination = code;
250 store.ut_exit.e_exit = status;
256 return write_entry_both(&store);
260 int utmp_put_runlevel(usec_t t, int runlevel, int previous) {
264 assert(runlevel > 0);
267 /* Find the old runlevel automatically */
269 if ((r = utmp_get_runlevel(&previous, NULL)) < 0) {
277 if (previous == runlevel)
280 init_entry(&store, t);
282 store.ut_type = RUN_LVL;
283 store.ut_pid = (runlevel & 0xFF) | ((previous & 0xFF) << 8);
284 strncpy(store.ut_user, "runlevel", sizeof(store.ut_user));
286 return write_entry_both(&store);
289 #define TIMEOUT_MSEC 50
291 static int write_to_terminal(const char *tty, const char *message) {
300 if ((fd = open(tty, O_WRONLY|O_NDELAY|O_NOCTTY|O_CLOEXEC)) < 0)
309 left = strlen(message);
311 end = now(CLOCK_MONOTONIC) + TIMEOUT_MSEC*USEC_PER_MSEC;
315 struct pollfd pollfd;
319 t = now(CLOCK_MONOTONIC);
328 pollfd.events = POLLOUT;
330 if ((k = poll(&pollfd, 1, (end - t) / USEC_PER_MSEC)) < 0)
338 if ((n = write(fd, p, left)) < 0) {
347 assert((size_t) n <= left);
356 close_nointr_nofail(fd);
361 int utmp_wall(const char *message, bool (*match_tty)(const char *tty)) {
363 char date[FORMAT_TIMESTAMP_MAX];
364 char *text = NULL, *hn = NULL, *un = NULL, *tty = NULL;
367 if (!(hn = gethostname_malloc()) ||
368 !(un = getlogname_malloc())) {
373 getttyname_harder(STDIN_FILENO, &tty);
377 "Broadcast message from %s@%s%s%s (%s):\r\n\r\n"
380 tty ? " on " : "", strempty(tty),
381 format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)),
391 while ((u = getutxent())) {
396 if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0)
399 if (path_startswith(u->ut_line, "/dev/"))
402 if (asprintf(&buf, "/dev/%s", u->ut_line) < 0) {
410 if (!match_tty || match_tty(path))
411 if ((q = write_to_terminal(path, text)) < 0)