]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
e537352b | 2 | |
e537352b | 3 | #include <errno.h> |
a8fbdf54 TA |
4 | #include <stddef.h> |
5 | #include <stdio.h> | |
6 | #include <stdlib.h> | |
a8fbdf54 | 7 | #include <sys/time.h> |
e537352b | 8 | #include <sys/utsname.h> |
ef2f1067 | 9 | #include <unistd.h> |
07630cea | 10 | #include <utmpx.h> |
e537352b | 11 | |
b5efdb8a | 12 | #include "alloc-util.h" |
22ecfa83 | 13 | #include "errno-util.h" |
07630cea | 14 | #include "hostname-util.h" |
e537352b | 15 | #include "macro.h" |
0a970718 | 16 | #include "memory-util.h" |
9eb977db | 17 | #include "path-util.h" |
07630cea | 18 | #include "string-util.h" |
a8fbdf54 | 19 | #include "time-util.h" |
b1d4f8e1 | 20 | #include "user-util.h" |
958b66ea | 21 | #include "utmp-wtmp.h" |
e537352b LP |
22 | |
23 | int utmp_get_runlevel(int *runlevel, int *previous) { | |
d7ac0952 | 24 | _unused_ _cleanup_(utxent_cleanup) bool utmpx = false; |
b92bea5d | 25 | struct utmpx *found, lookup = { .ut_type = RUN_LVL }; |
e537352b LP |
26 | const char *e; |
27 | ||
28 | assert(runlevel); | |
29 | ||
30 | /* If these values are set in the environment this takes | |
31 | * precedence. Presumably, sysvinit does this to work around a | |
32 | * race condition that would otherwise exist where we'd always | |
33 | * go to disk and hence might read runlevel data that might be | |
ca1d199b | 34 | * very new and not apply to the current script being executed. */ |
e537352b | 35 | |
e7fb33ff | 36 | e = getenv("RUNLEVEL"); |
54d04cd1 | 37 | if (!isempty(e)) { |
e537352b | 38 | *runlevel = e[0]; |
54d04cd1 ZJS |
39 | if (previous) |
40 | *previous = 0; | |
e537352b LP |
41 | |
42 | return 0; | |
43 | } | |
44 | ||
45 | if (utmpxname(_PATH_UTMPX) < 0) | |
46 | return -errno; | |
47 | ||
c2a99093 | 48 | utmpx = utxent_start(); |
e537352b | 49 | |
e7fb33ff LP |
50 | found = getutxid(&lookup); |
51 | if (!found) | |
c2a99093 | 52 | return -errno; |
e537352b | 53 | |
c2a99093 ZJS |
54 | *runlevel = found->ut_pid & 0xFF; |
55 | if (previous) | |
56 | *previous = (found->ut_pid >> 8) & 0xFF; | |
e537352b | 57 | |
c2a99093 | 58 | return 0; |
e537352b LP |
59 | } |
60 | ||
169c1bda | 61 | static void init_timestamp(struct utmpx *store, usec_t t) { |
e537352b LP |
62 | assert(store); |
63 | ||
871d7de4 LP |
64 | if (t <= 0) |
65 | t = now(CLOCK_REALTIME); | |
e537352b | 66 | |
871d7de4 LP |
67 | store->ut_tv.tv_sec = t / USEC_PER_SEC; |
68 | store->ut_tv.tv_usec = t % USEC_PER_SEC; | |
169c1bda LP |
69 | } |
70 | ||
71 | static void init_entry(struct utmpx *store, usec_t t) { | |
b92bea5d | 72 | struct utsname uts = {}; |
169c1bda LP |
73 | |
74 | assert(store); | |
75 | ||
76 | init_timestamp(store, t); | |
77 | ||
e537352b LP |
78 | if (uname(&uts) >= 0) |
79 | strncpy(store->ut_host, uts.release, sizeof(store->ut_host)); | |
80 | ||
81 | strncpy(store->ut_line, "~", sizeof(store->ut_line)); /* or ~~ ? */ | |
82 | strncpy(store->ut_id, "~~", sizeof(store->ut_id)); | |
83 | } | |
84 | ||
85 | static int write_entry_utmp(const struct utmpx *store) { | |
d7ac0952 | 86 | _unused_ _cleanup_(utxent_cleanup) bool utmpx = false; |
e537352b LP |
87 | |
88 | assert(store); | |
89 | ||
90 | /* utmp is similar to wtmp, but there is only one entry for | |
91 | * each entry type resp. user; i.e. basically a key/value | |
92 | * table. */ | |
93 | ||
94 | if (utmpxname(_PATH_UTMPX) < 0) | |
95 | return -errno; | |
96 | ||
c2a99093 | 97 | utmpx = utxent_start(); |
e537352b | 98 | |
ca1d199b ZJS |
99 | if (pututxline(store)) |
100 | return 0; | |
101 | if (errno == ENOENT) { | |
102 | /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */ | |
103 | log_debug_errno(errno, "Not writing utmp: %m"); | |
104 | return 0; | |
105 | } | |
106 | return -errno; | |
e537352b LP |
107 | } |
108 | ||
109 | static int write_entry_wtmp(const struct utmpx *store) { | |
110 | assert(store); | |
111 | ||
112 | /* wtmp is a simple append-only file where each entry is | |
ca1d199b | 113 | * simply appended to the end; i.e. basically a log. */ |
e537352b LP |
114 | |
115 | errno = 0; | |
116 | updwtmpx(_PATH_WTMPX, store); | |
ca1d199b ZJS |
117 | if (errno == ENOENT) { |
118 | /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */ | |
119 | log_debug_errno(errno, "Not writing wtmp: %m"); | |
120 | return 0; | |
121 | } | |
122 | if (errno == EROFS) { | |
123 | log_warning_errno(errno, "Failed to write wtmp record, ignoring: %m"); | |
124 | return 0; | |
125 | } | |
e537352b LP |
126 | return -errno; |
127 | } | |
128 | ||
4743137a | 129 | static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) { |
e537352b LP |
130 | int r, s; |
131 | ||
4743137a MS |
132 | r = write_entry_utmp(store_utmp); |
133 | s = write_entry_wtmp(store_wtmp); | |
ca1d199b | 134 | return r < 0 ? r : s; |
e537352b LP |
135 | } |
136 | ||
4743137a MS |
137 | static int write_entry_both(const struct utmpx *store) { |
138 | return write_utmp_wtmp(store, store); | |
139 | } | |
140 | ||
0ad26e09 | 141 | int utmp_put_shutdown(void) { |
863f3ce0 | 142 | struct utmpx store = {}; |
e537352b | 143 | |
0ad26e09 | 144 | init_entry(&store, 0); |
e537352b LP |
145 | |
146 | store.ut_type = RUN_LVL; | |
147 | strncpy(store.ut_user, "shutdown", sizeof(store.ut_user)); | |
148 | ||
149 | return write_entry_both(&store); | |
150 | } | |
151 | ||
871d7de4 | 152 | int utmp_put_reboot(usec_t t) { |
863f3ce0 | 153 | struct utmpx store = {}; |
e537352b | 154 | |
871d7de4 | 155 | init_entry(&store, t); |
e537352b LP |
156 | |
157 | store.ut_type = BOOT_TIME; | |
55e39f40 | 158 | strncpy(store.ut_user, "reboot", sizeof(store.ut_user)); |
e537352b LP |
159 | |
160 | return write_entry_both(&store); | |
161 | } | |
162 | ||
f1d553e9 | 163 | static void copy_suffix(char *buf, size_t buf_size, const char *src) { |
169c1bda LP |
164 | size_t l; |
165 | ||
f1d553e9 ZJS |
166 | l = strlen(src); |
167 | if (l < buf_size) | |
168 | strncpy(buf, src, buf_size); | |
169 | else | |
170 | memcpy(buf, src + l - buf_size, buf_size); | |
169c1bda LP |
171 | } |
172 | ||
023a4f67 | 173 | int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line, int ut_type, const char *user) { |
863f3ce0 TG |
174 | struct utmpx store = { |
175 | .ut_type = INIT_PROCESS, | |
176 | .ut_pid = pid, | |
177 | .ut_session = sid, | |
178 | }; | |
023a4f67 | 179 | int r; |
169c1bda LP |
180 | |
181 | assert(id); | |
d42b81f9 | 182 | assert(ut_type != USER_PROCESS || user); |
169c1bda | 183 | |
0ad26e09 | 184 | init_timestamp(&store, 0); |
169c1bda | 185 | |
f1d553e9 ZJS |
186 | /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */ |
187 | copy_suffix(store.ut_id, sizeof(store.ut_id), id); | |
169c1bda LP |
188 | |
189 | if (line) | |
6695c200 | 190 | strncpy_exact(store.ut_line, line, sizeof(store.ut_line)); |
169c1bda | 191 | |
023a4f67 LP |
192 | r = write_entry_both(&store); |
193 | if (r < 0) | |
194 | return r; | |
195 | ||
3742095b | 196 | if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) { |
023a4f67 LP |
197 | store.ut_type = LOGIN_PROCESS; |
198 | r = write_entry_both(&store); | |
199 | if (r < 0) | |
200 | return r; | |
201 | } | |
202 | ||
203 | if (ut_type == USER_PROCESS) { | |
204 | store.ut_type = USER_PROCESS; | |
205 | strncpy(store.ut_user, user, sizeof(store.ut_user)-1); | |
206 | r = write_entry_both(&store); | |
207 | if (r < 0) | |
208 | return r; | |
209 | } | |
210 | ||
211 | return 0; | |
169c1bda LP |
212 | } |
213 | ||
214 | int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) { | |
d7ac0952 | 215 | _unused_ _cleanup_(utxent_cleanup) bool utmpx = false; |
863f3ce0 TG |
216 | struct utmpx lookup = { |
217 | .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */ | |
218 | }, store, store_wtmp, *found; | |
169c1bda LP |
219 | |
220 | assert(id); | |
221 | ||
bbd239f6 | 222 | utmpx = utxent_start(); |
169c1bda | 223 | |
f1d553e9 | 224 | /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */ |
1568430c | 225 | copy_suffix(lookup.ut_id, sizeof(lookup.ut_id), id); |
169c1bda | 226 | |
e7fb33ff LP |
227 | found = getutxid(&lookup); |
228 | if (!found) | |
169c1bda LP |
229 | return 0; |
230 | ||
231 | if (found->ut_pid != pid) | |
232 | return 0; | |
233 | ||
fa4ad7ce | 234 | memcpy(&store, found, sizeof(store)); |
169c1bda LP |
235 | store.ut_type = DEAD_PROCESS; |
236 | store.ut_exit.e_termination = code; | |
237 | store.ut_exit.e_exit = status; | |
238 | ||
239 | zero(store.ut_user); | |
240 | zero(store.ut_host); | |
241 | zero(store.ut_tv); | |
242 | ||
4743137a MS |
243 | memcpy(&store_wtmp, &store, sizeof(store_wtmp)); |
244 | /* wtmp wants the current time */ | |
245 | init_timestamp(&store_wtmp, 0); | |
246 | ||
247 | return write_utmp_wtmp(&store, &store_wtmp); | |
169c1bda LP |
248 | } |
249 | ||
0ad26e09 | 250 | int utmp_put_runlevel(int runlevel, int previous) { |
863f3ce0 | 251 | struct utmpx store = {}; |
e537352b LP |
252 | int r; |
253 | ||
254 | assert(runlevel > 0); | |
255 | ||
256 | if (previous <= 0) { | |
257 | /* Find the old runlevel automatically */ | |
258 | ||
e7fb33ff LP |
259 | r = utmp_get_runlevel(&previous, NULL); |
260 | if (r < 0) { | |
d7fc909d LP |
261 | if (r != -ESRCH) |
262 | return r; | |
263 | ||
264 | previous = 0; | |
265 | } | |
e537352b LP |
266 | } |
267 | ||
4927fcae LP |
268 | if (previous == runlevel) |
269 | return 0; | |
270 | ||
0ad26e09 | 271 | init_entry(&store, 0); |
e537352b LP |
272 | |
273 | store.ut_type = RUN_LVL; | |
274 | store.ut_pid = (runlevel & 0xFF) | ((previous & 0xFF) << 8); | |
275 | strncpy(store.ut_user, "runlevel", sizeof(store.ut_user)); | |
276 | ||
277 | return write_entry_both(&store); | |
278 | } |