]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
e537352b | 2 | |
e537352b | 3 | #include <errno.h> |
07630cea LP |
4 | #include <fcntl.h> |
5 | #include <poll.h> | |
a8fbdf54 TA |
6 | #include <stddef.h> |
7 | #include <stdio.h> | |
8 | #include <stdlib.h> | |
a8fbdf54 | 9 | #include <sys/time.h> |
e537352b | 10 | #include <sys/utsname.h> |
ef2f1067 | 11 | #include <unistd.h> |
07630cea | 12 | #include <utmpx.h> |
e537352b | 13 | |
b5efdb8a | 14 | #include "alloc-util.h" |
3ffd4af2 | 15 | #include "fd-util.h" |
07630cea | 16 | #include "hostname-util.h" |
0f2d351f | 17 | #include "io-util.h" |
e537352b | 18 | #include "macro.h" |
0a970718 | 19 | #include "memory-util.h" |
9eb977db | 20 | #include "path-util.h" |
07630cea | 21 | #include "string-util.h" |
288a74cc | 22 | #include "terminal-util.h" |
a8fbdf54 | 23 | #include "time-util.h" |
b1d4f8e1 | 24 | #include "user-util.h" |
958b66ea | 25 | #include "utmp-wtmp.h" |
e537352b LP |
26 | |
27 | int utmp_get_runlevel(int *runlevel, int *previous) { | |
c2a99093 | 28 | _cleanup_(utxent_cleanup) bool utmpx = false; |
b92bea5d | 29 | struct utmpx *found, lookup = { .ut_type = RUN_LVL }; |
e537352b LP |
30 | const char *e; |
31 | ||
32 | assert(runlevel); | |
33 | ||
34 | /* If these values are set in the environment this takes | |
35 | * precedence. Presumably, sysvinit does this to work around a | |
36 | * race condition that would otherwise exist where we'd always | |
37 | * go to disk and hence might read runlevel data that might be | |
ca1d199b | 38 | * very new and not apply to the current script being executed. */ |
e537352b | 39 | |
e7fb33ff LP |
40 | e = getenv("RUNLEVEL"); |
41 | if (e && e[0] > 0) { | |
e537352b LP |
42 | *runlevel = e[0]; |
43 | ||
44 | if (previous) { | |
45 | /* $PREVLEVEL seems to be an Upstart thing */ | |
46 | ||
e7fb33ff LP |
47 | e = getenv("PREVLEVEL"); |
48 | if (e && e[0] > 0) | |
e537352b LP |
49 | *previous = e[0]; |
50 | else | |
51 | *previous = 0; | |
52 | } | |
53 | ||
54 | return 0; | |
55 | } | |
56 | ||
57 | if (utmpxname(_PATH_UTMPX) < 0) | |
58 | return -errno; | |
59 | ||
c2a99093 | 60 | utmpx = utxent_start(); |
e537352b | 61 | |
e7fb33ff LP |
62 | found = getutxid(&lookup); |
63 | if (!found) | |
c2a99093 | 64 | return -errno; |
e537352b | 65 | |
c2a99093 ZJS |
66 | *runlevel = found->ut_pid & 0xFF; |
67 | if (previous) | |
68 | *previous = (found->ut_pid >> 8) & 0xFF; | |
e537352b | 69 | |
c2a99093 | 70 | return 0; |
e537352b LP |
71 | } |
72 | ||
169c1bda | 73 | static void init_timestamp(struct utmpx *store, usec_t t) { |
e537352b LP |
74 | assert(store); |
75 | ||
871d7de4 LP |
76 | if (t <= 0) |
77 | t = now(CLOCK_REALTIME); | |
e537352b | 78 | |
871d7de4 LP |
79 | store->ut_tv.tv_sec = t / USEC_PER_SEC; |
80 | store->ut_tv.tv_usec = t % USEC_PER_SEC; | |
169c1bda LP |
81 | } |
82 | ||
83 | static void init_entry(struct utmpx *store, usec_t t) { | |
b92bea5d | 84 | struct utsname uts = {}; |
169c1bda LP |
85 | |
86 | assert(store); | |
87 | ||
88 | init_timestamp(store, t); | |
89 | ||
e537352b LP |
90 | if (uname(&uts) >= 0) |
91 | strncpy(store->ut_host, uts.release, sizeof(store->ut_host)); | |
92 | ||
93 | strncpy(store->ut_line, "~", sizeof(store->ut_line)); /* or ~~ ? */ | |
94 | strncpy(store->ut_id, "~~", sizeof(store->ut_id)); | |
95 | } | |
96 | ||
97 | static int write_entry_utmp(const struct utmpx *store) { | |
c2a99093 | 98 | _cleanup_(utxent_cleanup) bool utmpx = false; |
e537352b LP |
99 | |
100 | assert(store); | |
101 | ||
102 | /* utmp is similar to wtmp, but there is only one entry for | |
103 | * each entry type resp. user; i.e. basically a key/value | |
104 | * table. */ | |
105 | ||
106 | if (utmpxname(_PATH_UTMPX) < 0) | |
107 | return -errno; | |
108 | ||
c2a99093 | 109 | utmpx = utxent_start(); |
e537352b | 110 | |
ca1d199b ZJS |
111 | if (pututxline(store)) |
112 | return 0; | |
113 | if (errno == ENOENT) { | |
114 | /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */ | |
115 | log_debug_errno(errno, "Not writing utmp: %m"); | |
116 | return 0; | |
117 | } | |
118 | return -errno; | |
e537352b LP |
119 | } |
120 | ||
121 | static int write_entry_wtmp(const struct utmpx *store) { | |
122 | assert(store); | |
123 | ||
124 | /* wtmp is a simple append-only file where each entry is | |
ca1d199b | 125 | * simply appended to the end; i.e. basically a log. */ |
e537352b LP |
126 | |
127 | errno = 0; | |
128 | updwtmpx(_PATH_WTMPX, store); | |
ca1d199b ZJS |
129 | if (errno == ENOENT) { |
130 | /* If utmp/wtmp have been disabled, that's a good thing, hence ignore the error. */ | |
131 | log_debug_errno(errno, "Not writing wtmp: %m"); | |
132 | return 0; | |
133 | } | |
134 | if (errno == EROFS) { | |
135 | log_warning_errno(errno, "Failed to write wtmp record, ignoring: %m"); | |
136 | return 0; | |
137 | } | |
e537352b LP |
138 | return -errno; |
139 | } | |
140 | ||
4743137a | 141 | static int write_utmp_wtmp(const struct utmpx *store_utmp, const struct utmpx *store_wtmp) { |
e537352b LP |
142 | int r, s; |
143 | ||
4743137a MS |
144 | r = write_entry_utmp(store_utmp); |
145 | s = write_entry_wtmp(store_wtmp); | |
ca1d199b | 146 | return r < 0 ? r : s; |
e537352b LP |
147 | } |
148 | ||
4743137a MS |
149 | static int write_entry_both(const struct utmpx *store) { |
150 | return write_utmp_wtmp(store, store); | |
151 | } | |
152 | ||
0ad26e09 | 153 | int utmp_put_shutdown(void) { |
863f3ce0 | 154 | struct utmpx store = {}; |
e537352b | 155 | |
0ad26e09 | 156 | init_entry(&store, 0); |
e537352b LP |
157 | |
158 | store.ut_type = RUN_LVL; | |
159 | strncpy(store.ut_user, "shutdown", sizeof(store.ut_user)); | |
160 | ||
161 | return write_entry_both(&store); | |
162 | } | |
163 | ||
871d7de4 | 164 | int utmp_put_reboot(usec_t t) { |
863f3ce0 | 165 | struct utmpx store = {}; |
e537352b | 166 | |
871d7de4 | 167 | init_entry(&store, t); |
e537352b LP |
168 | |
169 | store.ut_type = BOOT_TIME; | |
55e39f40 | 170 | strncpy(store.ut_user, "reboot", sizeof(store.ut_user)); |
e537352b LP |
171 | |
172 | return write_entry_both(&store); | |
173 | } | |
174 | ||
f1d553e9 | 175 | static void copy_suffix(char *buf, size_t buf_size, const char *src) { |
169c1bda LP |
176 | size_t l; |
177 | ||
f1d553e9 ZJS |
178 | l = strlen(src); |
179 | if (l < buf_size) | |
180 | strncpy(buf, src, buf_size); | |
181 | else | |
182 | memcpy(buf, src + l - buf_size, buf_size); | |
169c1bda LP |
183 | } |
184 | ||
023a4f67 | 185 | 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 |
186 | struct utmpx store = { |
187 | .ut_type = INIT_PROCESS, | |
188 | .ut_pid = pid, | |
189 | .ut_session = sid, | |
190 | }; | |
023a4f67 | 191 | int r; |
169c1bda LP |
192 | |
193 | assert(id); | |
194 | ||
0ad26e09 | 195 | init_timestamp(&store, 0); |
169c1bda | 196 | |
f1d553e9 ZJS |
197 | /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */ |
198 | copy_suffix(store.ut_id, sizeof(store.ut_id), id); | |
169c1bda LP |
199 | |
200 | if (line) | |
6695c200 | 201 | strncpy_exact(store.ut_line, line, sizeof(store.ut_line)); |
169c1bda | 202 | |
023a4f67 LP |
203 | r = write_entry_both(&store); |
204 | if (r < 0) | |
205 | return r; | |
206 | ||
3742095b | 207 | if (IN_SET(ut_type, LOGIN_PROCESS, USER_PROCESS)) { |
023a4f67 LP |
208 | store.ut_type = LOGIN_PROCESS; |
209 | r = write_entry_both(&store); | |
210 | if (r < 0) | |
211 | return r; | |
212 | } | |
213 | ||
214 | if (ut_type == USER_PROCESS) { | |
215 | store.ut_type = USER_PROCESS; | |
216 | strncpy(store.ut_user, user, sizeof(store.ut_user)-1); | |
217 | r = write_entry_both(&store); | |
218 | if (r < 0) | |
219 | return r; | |
220 | } | |
221 | ||
222 | return 0; | |
169c1bda LP |
223 | } |
224 | ||
225 | int utmp_put_dead_process(const char *id, pid_t pid, int code, int status) { | |
863f3ce0 TG |
226 | struct utmpx lookup = { |
227 | .ut_type = INIT_PROCESS /* looks for DEAD_PROCESS, LOGIN_PROCESS, USER_PROCESS, too */ | |
228 | }, store, store_wtmp, *found; | |
169c1bda LP |
229 | |
230 | assert(id); | |
231 | ||
232 | setutxent(); | |
233 | ||
f1d553e9 ZJS |
234 | /* Copy the whole string if it fits, or just the suffix without the terminating NUL. */ |
235 | copy_suffix(store.ut_id, sizeof(store.ut_id), id); | |
169c1bda | 236 | |
e7fb33ff LP |
237 | found = getutxid(&lookup); |
238 | if (!found) | |
169c1bda LP |
239 | return 0; |
240 | ||
241 | if (found->ut_pid != pid) | |
242 | return 0; | |
243 | ||
fa4ad7ce | 244 | memcpy(&store, found, sizeof(store)); |
169c1bda LP |
245 | store.ut_type = DEAD_PROCESS; |
246 | store.ut_exit.e_termination = code; | |
247 | store.ut_exit.e_exit = status; | |
248 | ||
249 | zero(store.ut_user); | |
250 | zero(store.ut_host); | |
251 | zero(store.ut_tv); | |
252 | ||
4743137a MS |
253 | memcpy(&store_wtmp, &store, sizeof(store_wtmp)); |
254 | /* wtmp wants the current time */ | |
255 | init_timestamp(&store_wtmp, 0); | |
256 | ||
257 | return write_utmp_wtmp(&store, &store_wtmp); | |
169c1bda LP |
258 | } |
259 | ||
0ad26e09 | 260 | int utmp_put_runlevel(int runlevel, int previous) { |
863f3ce0 | 261 | struct utmpx store = {}; |
e537352b LP |
262 | int r; |
263 | ||
264 | assert(runlevel > 0); | |
265 | ||
266 | if (previous <= 0) { | |
267 | /* Find the old runlevel automatically */ | |
268 | ||
e7fb33ff LP |
269 | r = utmp_get_runlevel(&previous, NULL); |
270 | if (r < 0) { | |
d7fc909d LP |
271 | if (r != -ESRCH) |
272 | return r; | |
273 | ||
274 | previous = 0; | |
275 | } | |
e537352b LP |
276 | } |
277 | ||
4927fcae LP |
278 | if (previous == runlevel) |
279 | return 0; | |
280 | ||
0ad26e09 | 281 | init_entry(&store, 0); |
e537352b LP |
282 | |
283 | store.ut_type = RUN_LVL; | |
284 | store.ut_pid = (runlevel & 0xFF) | ((previous & 0xFF) << 8); | |
285 | strncpy(store.ut_user, "runlevel", sizeof(store.ut_user)); | |
286 | ||
287 | return write_entry_both(&store); | |
288 | } | |
ef2f1067 | 289 | |
0f2d351f | 290 | #define TIMEOUT_USEC (50 * USEC_PER_MSEC) |
ef2f1067 LP |
291 | |
292 | static int write_to_terminal(const char *tty, const char *message) { | |
7fd1b19b | 293 | _cleanup_close_ int fd = -1; |
ef2f1067 LP |
294 | const char *p; |
295 | size_t left; | |
296 | usec_t end; | |
297 | ||
298 | assert(tty); | |
299 | assert(message); | |
300 | ||
db4a47e9 | 301 | fd = open(tty, O_WRONLY|O_NONBLOCK|O_NOCTTY|O_CLOEXEC); |
e62d8c39 | 302 | if (fd < 0 || !isatty(fd)) |
ef2f1067 LP |
303 | return -errno; |
304 | ||
ef2f1067 LP |
305 | p = message; |
306 | left = strlen(message); | |
307 | ||
0f2d351f | 308 | end = now(CLOCK_MONOTONIC) + TIMEOUT_USEC; |
ef2f1067 LP |
309 | |
310 | while (left > 0) { | |
311 | ssize_t n; | |
ef2f1067 LP |
312 | usec_t t; |
313 | int k; | |
314 | ||
315 | t = now(CLOCK_MONOTONIC); | |
316 | ||
e62d8c39 ZJS |
317 | if (t >= end) |
318 | return -ETIME; | |
ef2f1067 | 319 | |
0f2d351f | 320 | k = fd_wait_for_event(fd, POLLOUT, end - t); |
e62d8c39 | 321 | if (k < 0) |
0f2d351f | 322 | return k; |
e62d8c39 ZJS |
323 | if (k == 0) |
324 | return -ETIME; | |
ef2f1067 | 325 | |
e62d8c39 ZJS |
326 | n = write(fd, p, left); |
327 | if (n < 0) { | |
ef2f1067 LP |
328 | if (errno == EAGAIN) |
329 | continue; | |
330 | ||
e62d8c39 | 331 | return -errno; |
ef2f1067 LP |
332 | } |
333 | ||
334 | assert((size_t) n <= left); | |
335 | ||
336 | p += n; | |
337 | left -= n; | |
338 | } | |
339 | ||
e62d8c39 | 340 | return 0; |
ef2f1067 LP |
341 | } |
342 | ||
99f710dd DM |
343 | int utmp_wall( |
344 | const char *message, | |
345 | const char *username, | |
346 | const char *origin_tty, | |
347 | bool (*match_tty)(const char *tty, void *userdata), | |
348 | void *userdata) { | |
349 | ||
350 | _cleanup_free_ char *text = NULL, *hn = NULL, *un = NULL, *stdin_tty = NULL; | |
11620592 | 351 | char date[FORMAT_TIMESTAMP_MAX]; |
e7fb33ff | 352 | struct utmpx *u; |
ef2f1067 | 353 | int r; |
ef2f1067 | 354 | |
e7fb33ff | 355 | hn = gethostname_malloc(); |
9003d9b0 | 356 | if (!hn) |
e7fb33ff | 357 | return -ENOMEM; |
9003d9b0 ST |
358 | if (!username) { |
359 | un = getlogname_malloc(); | |
360 | if (!un) | |
361 | return -ENOMEM; | |
362 | } | |
ef2f1067 | 363 | |
99f710dd DM |
364 | if (!origin_tty) { |
365 | getttyname_harder(STDIN_FILENO, &stdin_tty); | |
366 | origin_tty = stdin_tty; | |
367 | } | |
ef2f1067 LP |
368 | |
369 | if (asprintf(&text, | |
370 | "\a\r\n" | |
11620592 | 371 | "Broadcast message from %s@%s%s%s (%s):\r\n\r\n" |
ef2f1067 | 372 | "%s\r\n\r\n", |
9003d9b0 | 373 | un ?: username, hn, |
99f710dd | 374 | origin_tty ? " on " : "", strempty(origin_tty), |
11620592 | 375 | format_timestamp(date, sizeof(date), now(CLOCK_REALTIME)), |
e7fb33ff LP |
376 | message) < 0) |
377 | return -ENOMEM; | |
ef2f1067 LP |
378 | |
379 | setutxent(); | |
380 | ||
381 | r = 0; | |
382 | ||
383 | while ((u = getutxent())) { | |
e7fb33ff | 384 | _cleanup_free_ char *buf = NULL; |
ef2f1067 | 385 | const char *path; |
e7fb33ff | 386 | int q; |
ef2f1067 LP |
387 | |
388 | if (u->ut_type != USER_PROCESS || u->ut_user[0] == 0) | |
389 | continue; | |
390 | ||
fbd0b64f | 391 | /* this access is fine, because STRLEN("/dev/") << 32 (UT_LINESIZE) */ |
ef2f1067 LP |
392 | if (path_startswith(u->ut_line, "/dev/")) |
393 | path = u->ut_line; | |
394 | else { | |
e7fb33ff LP |
395 | if (asprintf(&buf, "/dev/%.*s", (int) sizeof(u->ut_line), u->ut_line) < 0) |
396 | return -ENOMEM; | |
ef2f1067 LP |
397 | |
398 | path = buf; | |
399 | } | |
400 | ||
99f710dd | 401 | if (!match_tty || match_tty(path, userdata)) { |
e7fb33ff LP |
402 | q = write_to_terminal(path, text); |
403 | if (q < 0) | |
7af53310 | 404 | r = q; |
e7fb33ff | 405 | } |
ef2f1067 LP |
406 | } |
407 | ||
ef2f1067 LP |
408 | return r; |
409 | } |