| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <stdlib.h> |
| 5 |
#include <sys/utsname.h> |
| 6 |
#include <utmpx.h> |
| 7 |
|
| 8 |
#include "log.h" |
| 9 |
#include "memory-util.h" |
| 10 |
#include "string-util.h" |
| 11 |
#include "time-util.h" |
| 12 |
#include "utmp-wtmp.h" |
| 13 |
|
| 14 |
static void init_timestamp(struct utmpx *store, usec_t t) { |
| 15 |
assert(store); |
| 16 |
|
| 17 |
if (t <= 0) |
| 18 |
t = now(CLOCK_REALTIME); |
| 19 |
|
| 20 |
/* Silence static analyzers */ |
| 21 |
assert_cc(USEC_PER_SEC > 0); |
| 22 |
store->ut_tv.tv_sec = t / USEC_PER_SEC; |
| 23 |
store->ut_tv.tv_usec = t % USEC_PER_SEC; |
| 24 |
} |
| 25 |
|
| 26 |
static void init_entry(struct utmpx *store, usec_t t) { |
| 27 |
struct utsname uts = {}; |
| 28 |
|
| 29 |
assert(store); |
| 30 |
|
| 31 |
init_timestamp(store, t); |
| 32 |
|
| 33 |
if (uname(&uts) >= 0) |
| 34 |
strncpy(store->ut_host, uts.release, sizeof(store->ut_host)); |
| 35 |
|
| 36 |
strncpy(store->ut_line, "~", sizeof(store->ut_line)); /* or ~~ ? */ |
| 37 |
strncpy(store->ut_id, "~~", sizeof(store->ut_id)); |
| 38 |
} |
| 39 |
|
| 40 |
static int write_entry_utmp(const struct utmpx *store) { |
| 41 |
_unused_ _cleanup_(utxent_cleanup) bool utmpx = false; |
| 42 |
|
| 43 |
assert(store); |
| 44 |
|
| 45 |
/* utmp is similar to wtmp, but there is only one entry for |
| 46 |
* each entry type resp. user; i.e. basically a key/value |
| 47 |
* table. */ |
| 48 |
|
| 49 |
if (utmpxname(UTMPX_FILE) < 0) |
| 50 |
return -errno; |
| 51 |
|
| 52 |
utmpx = utxent_start(); |
| 53 |
|
| 54 |
if (pututxline(store)) |
| 55 |
return 0; |
| 56 |
if (errno == ENOENT) { |
| 57 |
/* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */ |
| 58 |
log_debug_errno(errno, "Not writing utmp: %m"); |
| 59 |
return 0; |
| 60 |
} |
| 61 |
return -errno; |
| 62 |
} |
| 63 |
|
| 64 |
static int write_entry_wtmp(const struct utmpx *store) { |
| 65 |
assert(store); |
| 66 |
|
| 67 |
/* wtmp is a simple append-only file where each entry is |
| 68 |
* simply appended to the end; i.e. basically a log. */ |
| 69 |
|
| 70 |
errno = 0; |
| 71 |
updwtmpx(WTMPX_FILE, store); |
| 72 |
if (errno == ENOENT) { |
| 73 |
/* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */ |
| 74 |
log_debug_errno(errno, "Not writing wtmp: %m"); |
| 75 |
return 0; |
| 76 |
} |
| 77 |
if (errno == EROFS) { |
| 78 |
log_warning_errno(errno, "Failed to write wtmp record, ignoring: %m"); |
| 79 |
return 0; |
| 80 |
} |
| 81 |
return -errno; |
| 82 |
} |
| 83 |
|
| 84 |
static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) { |
| 85 |
int r, s; |
| 86 |
|
| 87 |
r = write_entry_utmp(store_utmp); |
| 88 |
s = write_entry_wtmp(store_wtmp); |
| 89 |
return r < 0 ? r : s; |
| 90 |
} |
| 91 |
|
| 92 |
static int write_entry_both(const struct utmpx *store) { |
| 93 |
return write_utmp_wtmp(store, store); |
| 94 |
} |
| 95 |
|
| 96 |
int utmp_put_shutdown(void) { |
| 97 |
struct utmpx store = {}; |
| 98 |
|
| 99 |
init_entry(&store, 0); |
| 100 |
|
| 101 |
store.ut_type = RUN_LVL; |
| 102 |
strncpy(store.ut_user, "shutdown", sizeof(store.ut_user)); |
| 103 |
|
| 104 |
return write_entry_both(&store); |
| 105 |
} |
| 106 |
|
| 107 |
int utmp_put_reboot(usec_t t) { |
| 108 |
struct utmpx store = {}; |
| 109 |
|
| 110 |
init_entry(&store, t); |
| 111 |
|
| 112 |
store.ut_type = BOOT_TIME; |
| 113 |
strncpy(store.ut_user, "reboot", sizeof(store.ut_user)); |
| 114 |
|
| 115 |
return write_entry_both(&store); |
| 116 |
} |
| 117 |
|
| 118 |
static void copy_suffix(char *buf, size_t buf_size, const char *src) { |
| 119 |
size_t l; |
| 120 |
|
| 121 |
l = strlen(src); |
| 122 |
if (l < buf_size) |
| 123 |
strncpy(buf, src, buf_size); |
| 124 |
else |
| 125 |
memcpy(buf, src + l - buf_size, buf_size); |
| 126 |
} |
| 127 |
|
| 128 |
int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) { |
| 129 |
struct utmpx store = { |
| 130 |
.ut_type = INIT_PROCESS, |
| 131 |
.ut_pid = pid, |
| 132 |
.ut_session = sid, |
| 133 |
}; |
| 134 |
int r; |
| 135 |
|
| 136 |
assert(id); |
| 137 |
assert(ut_type != USER_PROCESS || user); |
| 138 |
|
| 139 |
init_timestamp(&store, 0); |
| 140 |
|
| 141 |
/* Copy the whole string if it fits, or just the suffix without the terminating NUL. */ |
| 142 |
copy_suffix(store.ut_id, sizeof(store.ut_id), id); |
| 143 |
|
| 144 |
if (line) |
| 145 |
strncpy_exact(store.ut_line, line, sizeof(store.ut_line)); |
| 146 |
|
| 147 |
r = write_entry_both(&store); |
| 148 |
if (r < 0) |
| 149 |
return r; |
| 150 |
|
| 151 |
if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) { |
| 152 |
store.ut_type = LOGIN_PROCESS; |
| 153 |
r = write_entry_both(&store); |
| 154 |
if (r < 0) |
| 155 |
return r; |
| 156 |
} |
| 157 |
|
| 158 |
if (ut_type == USER_PROCESS) { |
| 159 |
store.ut_type = USER_PROCESS; |
| 160 |
strncpy(store.ut_user, user, sizeof(store.ut_user)-1); |
| 161 |
r = write_entry_both(&store); |
| 162 |
if (r < 0) |
| 163 |
return r; |
| 164 |
} |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) { |
| 170 |
_unused_ _cleanup_(utxent_cleanup) bool utmpx = false; |
| 171 |
struct utmpx lookup = { |
| 172 |
.ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */ |
| 173 |
}, store, store_wtmp, *found; |
| 174 |
|
| 175 |
assert(id); |
| 176 |
|
| 177 |
utmpx = utxent_start(); |
| 178 |
|
| 179 |
/* Copy the whole string if it fits, or just the suffix without the terminating NUL. */ |
| 180 |
copy_suffix(lookup.ut_id, sizeof(lookup.ut_id), id); |
| 181 |
|
| 182 |
found = getutxid(&lookup); |
| 183 |
if (!found) |
| 184 |
return 0; |
| 185 |
|
| 186 |
if (found->ut_pid != pid) |
| 187 |
return 0; |
| 188 |
|
| 189 |
memcpy(&store, found, sizeof(store)); |
| 190 |
store.ut_type = DEAD_PROCESS; |
| 191 |
store.ut_exit.e_termination = code; |
| 192 |
store.ut_exit.e_exit = status; |
| 193 |
|
| 194 |
zero(store.ut_user); |
| 195 |
zero(store.ut_host); |
| 196 |
zero(store.ut_tv); |
| 197 |
|
| 198 |
memcpy(&store_wtmp, &store, sizeof(store_wtmp)); |
| 199 |
/* wtmp wants the current time */ |
| 200 |
init_timestamp(&store_wtmp, 0); |
| 201 |
|
| 202 |
return write_utmp_wtmp(&store, &store_wtmp); |
| 203 |
} |