]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
288a74cc | 2 | |
11c3a366 | 3 | #include <errno.h> |
07630cea | 4 | #include <fcntl.h> |
11c3a366 | 5 | #include <limits.h> |
23b27b39 LP |
6 | #include <linux/kd.h> |
7 | #include <linux/tiocl.h> | |
8 | #include <linux/vt.h> | |
9 | #include <poll.h> | |
10 | #include <signal.h> | |
11c3a366 TA |
11 | #include <stdarg.h> |
12 | #include <stddef.h> | |
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
15 | #include <sys/inotify.h> | |
23b27b39 | 16 | #include <sys/ioctl.h> |
11c3a366 TA |
17 | #include <sys/socket.h> |
18 | #include <sys/sysmacros.h> | |
19 | #include <sys/time.h> | |
07630cea | 20 | #include <sys/types.h> |
23b27b39 | 21 | #include <sys/utsname.h> |
288a74cc | 22 | #include <termios.h> |
07630cea | 23 | #include <unistd.h> |
288a74cc | 24 | |
b5efdb8a | 25 | #include "alloc-util.h" |
81f5e513 | 26 | #include "copy.h" |
f8360f33 | 27 | #include "def.h" |
acf553b0 | 28 | #include "env-util.h" |
3ffd4af2 | 29 | #include "fd-util.h" |
288a74cc | 30 | #include "fileio.h" |
f4f15635 | 31 | #include "fs-util.h" |
c004493c | 32 | #include "io-util.h" |
93cc7779 TA |
33 | #include "log.h" |
34 | #include "macro.h" | |
0cb8e3d1 | 35 | #include "namespace-util.h" |
6bedfcbb | 36 | #include "parse-util.h" |
c2b32159 LP |
37 | #include "path-util.h" |
38 | #include "proc-cmdline.h" | |
07630cea | 39 | #include "process-util.h" |
2583fbea | 40 | #include "socket-util.h" |
8fcde012 | 41 | #include "stat-util.h" |
07630cea | 42 | #include "string-util.h" |
6af62124 | 43 | #include "strv.h" |
3ffd4af2 | 44 | #include "terminal-util.h" |
07630cea LP |
45 | #include "time-util.h" |
46 | #include "util.h" | |
288a74cc RC |
47 | |
48 | static volatile unsigned cached_columns = 0; | |
49 | static volatile unsigned cached_lines = 0; | |
50 | ||
c6063244 LP |
51 | static volatile int cached_on_tty = -1; |
52 | static volatile int cached_colors_enabled = -1; | |
53 | static volatile int cached_underline_enabled = -1; | |
54 | ||
288a74cc RC |
55 | int chvt(int vt) { |
56 | _cleanup_close_ int fd; | |
57 | ||
0295642d LP |
58 | /* Switch to the specified vt number. If the VT is specified <= 0 switch to the VT the kernel log messages go, |
59 | * if that's configured. */ | |
60 | ||
0a8b555c | 61 | fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); |
288a74cc RC |
62 | if (fd < 0) |
63 | return -errno; | |
64 | ||
b9e74c39 | 65 | if (vt <= 0) { |
288a74cc RC |
66 | int tiocl[2] = { |
67 | TIOCL_GETKMSGREDIRECT, | |
68 | 0 | |
69 | }; | |
70 | ||
71 | if (ioctl(fd, TIOCLINUX, tiocl) < 0) | |
72 | return -errno; | |
73 | ||
74 | vt = tiocl[0] <= 0 ? 1 : tiocl[0]; | |
75 | } | |
76 | ||
77 | if (ioctl(fd, VT_ACTIVATE, vt) < 0) | |
78 | return -errno; | |
79 | ||
80 | return 0; | |
81 | } | |
82 | ||
83 | int read_one_char(FILE *f, char *ret, usec_t t, bool *need_nl) { | |
715bcf36 LP |
84 | _cleanup_free_ char *line = NULL; |
85 | struct termios old_termios; | |
86 | int r; | |
288a74cc RC |
87 | |
88 | assert(f); | |
89 | assert(ret); | |
90 | ||
715bcf36 | 91 | /* If this is a terminal, then switch canonical mode off, so that we can read a single character */ |
288a74cc | 92 | if (tcgetattr(fileno(f), &old_termios) >= 0) { |
715bcf36 | 93 | struct termios new_termios = old_termios; |
288a74cc RC |
94 | |
95 | new_termios.c_lflag &= ~ICANON; | |
96 | new_termios.c_cc[VMIN] = 1; | |
97 | new_termios.c_cc[VTIME] = 0; | |
98 | ||
99 | if (tcsetattr(fileno(f), TCSADRAIN, &new_termios) >= 0) { | |
03a7dbea | 100 | char c; |
288a74cc RC |
101 | |
102 | if (t != USEC_INFINITY) { | |
103 | if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0) { | |
d3f9790c | 104 | (void) tcsetattr(fileno(f), TCSADRAIN, &old_termios); |
288a74cc RC |
105 | return -ETIMEDOUT; |
106 | } | |
107 | } | |
108 | ||
03a7dbea | 109 | r = safe_fgetc(f, &c); |
d3f9790c | 110 | (void) tcsetattr(fileno(f), TCSADRAIN, &old_termios); |
d3f9790c LP |
111 | if (r < 0) |
112 | return r; | |
03a7dbea LP |
113 | if (r == 0) |
114 | return -EIO; | |
288a74cc RC |
115 | |
116 | if (need_nl) | |
117 | *need_nl = c != '\n'; | |
118 | ||
119 | *ret = c; | |
120 | return 0; | |
121 | } | |
122 | } | |
123 | ||
124 | if (t != USEC_INFINITY) { | |
125 | if (fd_wait_for_event(fileno(f), POLLIN, t) <= 0) | |
126 | return -ETIMEDOUT; | |
127 | } | |
128 | ||
715bcf36 | 129 | /* If this is not a terminal, then read a full line instead */ |
288a74cc | 130 | |
715bcf36 LP |
131 | r = read_line(f, 16, &line); /* longer than necessary, to eat up UTF-8 chars/vt100 key sequences */ |
132 | if (r < 0) | |
133 | return r; | |
134 | if (r == 0) | |
135 | return -EIO; | |
288a74cc RC |
136 | |
137 | if (strlen(line) != 1) | |
138 | return -EBADMSG; | |
139 | ||
140 | if (need_nl) | |
141 | *need_nl = false; | |
142 | ||
143 | *ret = line[0]; | |
144 | return 0; | |
145 | } | |
146 | ||
3c670f89 FB |
147 | #define DEFAULT_ASK_REFRESH_USEC (2*USEC_PER_SEC) |
148 | ||
149 | int ask_char(char *ret, const char *replies, const char *fmt, ...) { | |
288a74cc RC |
150 | int r; |
151 | ||
152 | assert(ret); | |
153 | assert(replies); | |
3c670f89 | 154 | assert(fmt); |
288a74cc RC |
155 | |
156 | for (;;) { | |
157 | va_list ap; | |
158 | char c; | |
159 | bool need_nl = true; | |
160 | ||
7565bb98 | 161 | if (colors_enabled()) |
1fc464f6 | 162 | fputs(ANSI_HIGHLIGHT, stdout); |
288a74cc | 163 | |
3c670f89 FB |
164 | putchar('\r'); |
165 | ||
166 | va_start(ap, fmt); | |
167 | vprintf(fmt, ap); | |
288a74cc RC |
168 | va_end(ap); |
169 | ||
7565bb98 | 170 | if (colors_enabled()) |
1fc464f6 | 171 | fputs(ANSI_NORMAL, stdout); |
288a74cc RC |
172 | |
173 | fflush(stdout); | |
174 | ||
3c670f89 | 175 | r = read_one_char(stdin, &c, DEFAULT_ASK_REFRESH_USEC, &need_nl); |
288a74cc RC |
176 | if (r < 0) { |
177 | ||
3c670f89 FB |
178 | if (r == -ETIMEDOUT) |
179 | continue; | |
180 | ||
288a74cc RC |
181 | if (r == -EBADMSG) { |
182 | puts("Bad input, please try again."); | |
183 | continue; | |
184 | } | |
185 | ||
186 | putchar('\n'); | |
187 | return r; | |
188 | } | |
189 | ||
190 | if (need_nl) | |
191 | putchar('\n'); | |
192 | ||
193 | if (strchr(replies, c)) { | |
194 | *ret = c; | |
195 | return 0; | |
196 | } | |
197 | ||
198 | puts("Read unexpected character, please try again."); | |
199 | } | |
200 | } | |
201 | ||
202 | int ask_string(char **ret, const char *text, ...) { | |
03d94294 ZJS |
203 | _cleanup_free_ char *line = NULL; |
204 | va_list ap; | |
715bcf36 LP |
205 | int r; |
206 | ||
288a74cc RC |
207 | assert(ret); |
208 | assert(text); | |
209 | ||
03d94294 ZJS |
210 | if (colors_enabled()) |
211 | fputs(ANSI_HIGHLIGHT, stdout); | |
288a74cc | 212 | |
03d94294 ZJS |
213 | va_start(ap, text); |
214 | vprintf(text, ap); | |
215 | va_end(ap); | |
288a74cc | 216 | |
03d94294 ZJS |
217 | if (colors_enabled()) |
218 | fputs(ANSI_NORMAL, stdout); | |
288a74cc | 219 | |
03d94294 | 220 | fflush(stdout); |
288a74cc | 221 | |
03d94294 ZJS |
222 | r = read_line(stdin, LONG_LINE_MAX, &line); |
223 | if (r < 0) | |
224 | return r; | |
225 | if (r == 0) | |
226 | return -EIO; | |
288a74cc | 227 | |
03d94294 ZJS |
228 | *ret = TAKE_PTR(line); |
229 | return 0; | |
288a74cc RC |
230 | } |
231 | ||
232 | int reset_terminal_fd(int fd, bool switch_to_text) { | |
233 | struct termios termios; | |
234 | int r = 0; | |
235 | ||
236 | /* Set terminal to some sane defaults */ | |
237 | ||
238 | assert(fd >= 0); | |
239 | ||
240 | /* We leave locked terminal attributes untouched, so that | |
241 | * Plymouth may set whatever it wants to set, and we don't | |
242 | * interfere with that. */ | |
243 | ||
244 | /* Disable exclusive mode, just in case */ | |
7d927c9a | 245 | (void) ioctl(fd, TIOCNXCL); |
288a74cc RC |
246 | |
247 | /* Switch to text mode */ | |
248 | if (switch_to_text) | |
7d927c9a | 249 | (void) ioctl(fd, KDSETMODE, KD_TEXT); |
288a74cc | 250 | |
73e669e0 | 251 | /* Set default keyboard mode */ |
c83f349c | 252 | (void) vt_reset_keyboard(fd); |
288a74cc RC |
253 | |
254 | if (tcgetattr(fd, &termios) < 0) { | |
255 | r = -errno; | |
256 | goto finish; | |
257 | } | |
258 | ||
259 | /* We only reset the stuff that matters to the software. How | |
260 | * hardware is set up we don't touch assuming that somebody | |
261 | * else will do that for us */ | |
262 | ||
263 | termios.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | IUCLC); | |
264 | termios.c_iflag |= ICRNL | IMAXBEL | IUTF8; | |
265 | termios.c_oflag |= ONLCR; | |
266 | termios.c_cflag |= CREAD; | |
267 | termios.c_lflag = ISIG | ICANON | IEXTEN | ECHO | ECHOE | ECHOK | ECHOCTL | ECHOPRT | ECHOKE; | |
268 | ||
269 | termios.c_cc[VINTR] = 03; /* ^C */ | |
270 | termios.c_cc[VQUIT] = 034; /* ^\ */ | |
271 | termios.c_cc[VERASE] = 0177; | |
272 | termios.c_cc[VKILL] = 025; /* ^X */ | |
273 | termios.c_cc[VEOF] = 04; /* ^D */ | |
274 | termios.c_cc[VSTART] = 021; /* ^Q */ | |
275 | termios.c_cc[VSTOP] = 023; /* ^S */ | |
276 | termios.c_cc[VSUSP] = 032; /* ^Z */ | |
277 | termios.c_cc[VLNEXT] = 026; /* ^V */ | |
278 | termios.c_cc[VWERASE] = 027; /* ^W */ | |
279 | termios.c_cc[VREPRINT] = 022; /* ^R */ | |
280 | termios.c_cc[VEOL] = 0; | |
281 | termios.c_cc[VEOL2] = 0; | |
282 | ||
283 | termios.c_cc[VTIME] = 0; | |
284 | termios.c_cc[VMIN] = 1; | |
285 | ||
286 | if (tcsetattr(fd, TCSANOW, &termios) < 0) | |
287 | r = -errno; | |
288 | ||
289 | finish: | |
290 | /* Just in case, flush all crap out */ | |
7d927c9a | 291 | (void) tcflush(fd, TCIOFLUSH); |
288a74cc RC |
292 | |
293 | return r; | |
294 | } | |
295 | ||
296 | int reset_terminal(const char *name) { | |
297 | _cleanup_close_ int fd = -1; | |
298 | ||
0a8b555c LP |
299 | /* We open the terminal with O_NONBLOCK here, to ensure we |
300 | * don't block on carrier if this is a terminal with carrier | |
301 | * configured. */ | |
302 | ||
303 | fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); | |
288a74cc RC |
304 | if (fd < 0) |
305 | return fd; | |
306 | ||
307 | return reset_terminal_fd(fd, true); | |
308 | } | |
309 | ||
310 | int open_terminal(const char *name, int mode) { | |
288a74cc | 311 | unsigned c = 0; |
87964ec7 | 312 | int fd; |
288a74cc RC |
313 | |
314 | /* | |
315 | * If a TTY is in the process of being closed opening it might | |
316 | * cause EIO. This is horribly awful, but unlikely to be | |
317 | * changed in the kernel. Hence we work around this problem by | |
318 | * retrying a couple of times. | |
319 | * | |
320 | * https://bugs.launchpad.net/ubuntu/+source/linux/+bug/554172/comments/245 | |
321 | */ | |
322 | ||
35bdab77 LP |
323 | if (mode & O_CREAT) |
324 | return -EINVAL; | |
288a74cc RC |
325 | |
326 | for (;;) { | |
327 | fd = open(name, mode, 0); | |
328 | if (fd >= 0) | |
329 | break; | |
330 | ||
331 | if (errno != EIO) | |
332 | return -errno; | |
333 | ||
334 | /* Max 1s in total */ | |
335 | if (c >= 20) | |
336 | return -errno; | |
337 | ||
338 | usleep(50 * USEC_PER_MSEC); | |
339 | c++; | |
340 | } | |
341 | ||
87964ec7 | 342 | if (isatty(fd) <= 0) { |
288a74cc RC |
343 | safe_close(fd); |
344 | return -ENOTTY; | |
345 | } | |
346 | ||
347 | return fd; | |
348 | } | |
349 | ||
350 | int acquire_terminal( | |
351 | const char *name, | |
8854d795 | 352 | AcquireTerminalFlags flags, |
288a74cc RC |
353 | usec_t timeout) { |
354 | ||
8854d795 LP |
355 | _cleanup_close_ int notify = -1, fd = -1; |
356 | usec_t ts = USEC_INFINITY; | |
357 | int r, wd = -1; | |
288a74cc RC |
358 | |
359 | assert(name); | |
8854d795 | 360 | assert(IN_SET(flags & ~ACQUIRE_TERMINAL_PERMISSIVE, ACQUIRE_TERMINAL_TRY, ACQUIRE_TERMINAL_FORCE, ACQUIRE_TERMINAL_WAIT)); |
288a74cc | 361 | |
8854d795 LP |
362 | /* We use inotify to be notified when the tty is closed. We create the watch before checking if we can actually |
363 | * acquire it, so that we don't lose any event. | |
288a74cc | 364 | * |
8854d795 LP |
365 | * Note: strictly speaking this actually watches for the device being closed, it does *not* really watch |
366 | * whether a tty loses its controlling process. However, unless some rogue process uses TIOCNOTTY on /dev/tty | |
f95dbcc2 ZJS |
367 | * *after* closing its tty otherwise this will not become a problem. As long as the administrator makes sure to |
368 | * not configure any service on the same tty as an untrusted user this should not be a problem. (Which they | |
8854d795 LP |
369 | * probably should not do anyway.) */ |
370 | ||
371 | if ((flags & ~ACQUIRE_TERMINAL_PERMISSIVE) == ACQUIRE_TERMINAL_WAIT) { | |
288a74cc | 372 | notify = inotify_init1(IN_CLOEXEC | (timeout != USEC_INFINITY ? IN_NONBLOCK : 0)); |
8854d795 LP |
373 | if (notify < 0) |
374 | return -errno; | |
288a74cc RC |
375 | |
376 | wd = inotify_add_watch(notify, name, IN_CLOSE); | |
8854d795 LP |
377 | if (wd < 0) |
378 | return -errno; | |
379 | ||
380 | if (timeout != USEC_INFINITY) | |
381 | ts = now(CLOCK_MONOTONIC); | |
288a74cc RC |
382 | } |
383 | ||
384 | for (;;) { | |
385 | struct sigaction sa_old, sa_new = { | |
386 | .sa_handler = SIG_IGN, | |
387 | .sa_flags = SA_RESTART, | |
388 | }; | |
389 | ||
390 | if (notify >= 0) { | |
391 | r = flush_fd(notify); | |
392 | if (r < 0) | |
8854d795 | 393 | return r; |
288a74cc RC |
394 | } |
395 | ||
8854d795 LP |
396 | /* We pass here O_NOCTTY only so that we can check the return value TIOCSCTTY and have a reliable way |
397 | * to figure out if we successfully became the controlling process of the tty */ | |
288a74cc RC |
398 | fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC); |
399 | if (fd < 0) | |
400 | return fd; | |
401 | ||
8854d795 | 402 | /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed if we already own the tty. */ |
288a74cc RC |
403 | assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0); |
404 | ||
405 | /* First, try to get the tty */ | |
8854d795 LP |
406 | r = ioctl(fd, TIOCSCTTY, |
407 | (flags & ~ACQUIRE_TERMINAL_PERMISSIVE) == ACQUIRE_TERMINAL_FORCE) < 0 ? -errno : 0; | |
288a74cc | 408 | |
8854d795 | 409 | /* Reset signal handler to old value */ |
288a74cc RC |
410 | assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0); |
411 | ||
8854d795 LP |
412 | /* Success? Exit the loop now! */ |
413 | if (r >= 0) | |
414 | break; | |
288a74cc | 415 | |
8854d795 LP |
416 | /* Any failure besides -EPERM? Fail, regardless of the mode. */ |
417 | if (r != -EPERM) | |
418 | return r; | |
288a74cc | 419 | |
8854d795 LP |
420 | if (flags & ACQUIRE_TERMINAL_PERMISSIVE) /* If we are in permissive mode, then EPERM is fine, turn this |
421 | * into a success. Note that EPERM is also returned if we | |
422 | * already are the owner of the TTY. */ | |
288a74cc RC |
423 | break; |
424 | ||
8854d795 LP |
425 | if (flags != ACQUIRE_TERMINAL_WAIT) /* If we are in TRY or FORCE mode, then propagate EPERM as EPERM */ |
426 | return r; | |
427 | ||
288a74cc | 428 | assert(notify >= 0); |
8854d795 | 429 | assert(wd >= 0); |
288a74cc RC |
430 | |
431 | for (;;) { | |
432 | union inotify_event_buffer buffer; | |
433 | struct inotify_event *e; | |
434 | ssize_t l; | |
435 | ||
436 | if (timeout != USEC_INFINITY) { | |
437 | usec_t n; | |
438 | ||
8854d795 LP |
439 | assert(ts != USEC_INFINITY); |
440 | ||
288a74cc | 441 | n = now(CLOCK_MONOTONIC); |
8854d795 LP |
442 | if (ts + timeout < n) |
443 | return -ETIMEDOUT; | |
288a74cc | 444 | |
f80da6f3 | 445 | r = fd_wait_for_event(notify, POLLIN, ts + timeout - n); |
288a74cc | 446 | if (r < 0) |
8854d795 LP |
447 | return r; |
448 | if (r == 0) | |
449 | return -ETIMEDOUT; | |
288a74cc RC |
450 | } |
451 | ||
452 | l = read(notify, &buffer, sizeof(buffer)); | |
453 | if (l < 0) { | |
3742095b | 454 | if (IN_SET(errno, EINTR, EAGAIN)) |
288a74cc RC |
455 | continue; |
456 | ||
8854d795 | 457 | return -errno; |
288a74cc RC |
458 | } |
459 | ||
460 | FOREACH_INOTIFY_EVENT(e, buffer, l) { | |
8854d795 LP |
461 | if (e->mask & IN_Q_OVERFLOW) /* If we hit an inotify queue overflow, simply check if the terminal is up for grabs now. */ |
462 | break; | |
463 | ||
464 | if (e->wd != wd || !(e->mask & IN_CLOSE)) /* Safety checks */ | |
465 | return -EIO; | |
288a74cc RC |
466 | } |
467 | ||
468 | break; | |
469 | } | |
470 | ||
8854d795 LP |
471 | /* We close the tty fd here since if the old session ended our handle will be dead. It's important that |
472 | * we do this after sleeping, so that we don't enter an endless loop. */ | |
288a74cc RC |
473 | fd = safe_close(fd); |
474 | } | |
475 | ||
c10d6bdb | 476 | return TAKE_FD(fd); |
288a74cc RC |
477 | } |
478 | ||
479 | int release_terminal(void) { | |
480 | static const struct sigaction sa_new = { | |
481 | .sa_handler = SIG_IGN, | |
482 | .sa_flags = SA_RESTART, | |
483 | }; | |
484 | ||
485 | _cleanup_close_ int fd = -1; | |
486 | struct sigaction sa_old; | |
87964ec7 | 487 | int r; |
288a74cc | 488 | |
0a8b555c | 489 | fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); |
288a74cc RC |
490 | if (fd < 0) |
491 | return -errno; | |
492 | ||
493 | /* Temporarily ignore SIGHUP, so that we don't get SIGHUP'ed | |
494 | * by our own TIOCNOTTY */ | |
495 | assert_se(sigaction(SIGHUP, &sa_new, &sa_old) == 0); | |
496 | ||
87964ec7 | 497 | r = ioctl(fd, TIOCNOTTY) < 0 ? -errno : 0; |
288a74cc RC |
498 | |
499 | assert_se(sigaction(SIGHUP, &sa_old, NULL) == 0); | |
500 | ||
501 | return r; | |
502 | } | |
503 | ||
504 | int terminal_vhangup_fd(int fd) { | |
505 | assert(fd >= 0); | |
506 | ||
507 | if (ioctl(fd, TIOCVHANGUP) < 0) | |
508 | return -errno; | |
509 | ||
510 | return 0; | |
511 | } | |
512 | ||
513 | int terminal_vhangup(const char *name) { | |
514 | _cleanup_close_ int fd; | |
515 | ||
0a8b555c | 516 | fd = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); |
288a74cc RC |
517 | if (fd < 0) |
518 | return fd; | |
519 | ||
520 | return terminal_vhangup_fd(fd); | |
521 | } | |
522 | ||
523 | int vt_disallocate(const char *name) { | |
ba5d26cc | 524 | const char *e; |
1ba23931 | 525 | int r; |
288a74cc RC |
526 | |
527 | /* Deallocate the VT if possible. If not possible | |
528 | * (i.e. because it is the active one), at least clear it | |
ba5d26cc | 529 | * entirely (including the scrollback buffer). */ |
288a74cc | 530 | |
27458ed6 LP |
531 | e = path_startswith(name, "/dev/"); |
532 | if (!e) | |
288a74cc RC |
533 | return -EINVAL; |
534 | ||
ba5d26cc ZJS |
535 | if (tty_is_vc(name)) { |
536 | _cleanup_close_ int fd = -1; | |
537 | unsigned u; | |
538 | const char *n; | |
288a74cc | 539 | |
ba5d26cc ZJS |
540 | n = startswith(e, "tty"); |
541 | if (!n) | |
542 | return -EINVAL; | |
288a74cc | 543 | |
ba5d26cc ZJS |
544 | r = safe_atou(n, &u); |
545 | if (r < 0) | |
546 | return r; | |
288a74cc | 547 | |
ba5d26cc ZJS |
548 | if (u <= 0) |
549 | return -EINVAL; | |
288a74cc | 550 | |
ba5d26cc ZJS |
551 | /* Try to deallocate */ |
552 | fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); | |
553 | if (fd < 0) | |
554 | return fd; | |
288a74cc | 555 | |
ba5d26cc ZJS |
556 | r = ioctl(fd, VT_DISALLOCATE, u); |
557 | if (r >= 0) | |
558 | return 0; | |
559 | if (errno != EBUSY) | |
560 | return -errno; | |
561 | } | |
288a74cc | 562 | |
ba5d26cc ZJS |
563 | /* So this is not a VT (in which case we cannot deallocate it), |
564 | * or we failed to deallocate. Let's at least clear the screen. */ | |
288a74cc | 565 | |
ba5d26cc ZJS |
566 | _cleanup_close_ int fd2 = open_terminal(name, O_RDWR|O_NOCTTY|O_CLOEXEC); |
567 | if (fd2 < 0) | |
568 | return fd2; | |
288a74cc | 569 | |
ba5d26cc ZJS |
570 | (void) loop_write(fd2, |
571 | "\033[r" /* clear scrolling region */ | |
572 | "\033[H" /* move home */ | |
573 | "\033[3J", /* clear screen including scrollback, requires Linux 2.6.40 */ | |
574 | 10, false); | |
288a74cc RC |
575 | return 0; |
576 | } | |
577 | ||
288a74cc RC |
578 | int make_console_stdio(void) { |
579 | int fd, r; | |
580 | ||
9281e703 LP |
581 | /* Make /dev/console the controlling terminal and stdin/stdout/stderr, if we can. If we can't use |
582 | * /dev/null instead. This is particularly useful if /dev/console is turned off, e.g. if console=null | |
583 | * is specified on the kernel command line. */ | |
288a74cc | 584 | |
8854d795 | 585 | fd = acquire_terminal("/dev/console", ACQUIRE_TERMINAL_FORCE|ACQUIRE_TERMINAL_PERMISSIVE, USEC_INFINITY); |
9281e703 LP |
586 | if (fd < 0) { |
587 | log_warning_errno(fd, "Failed to acquire terminal, using /dev/null stdin/stdout/stderr instead: %m"); | |
288a74cc | 588 | |
9281e703 LP |
589 | r = make_null_stdio(); |
590 | if (r < 0) | |
591 | return log_error_errno(r, "Failed to make /dev/null stdin/stdout/stderr: %m"); | |
3d18b167 | 592 | |
9281e703 LP |
593 | } else { |
594 | r = reset_terminal_fd(fd, true); | |
595 | if (r < 0) | |
596 | log_warning_errno(r, "Failed to reset terminal, ignoring: %m"); | |
288a74cc | 597 | |
9281e703 LP |
598 | r = rearrange_stdio(fd, fd, fd); /* This invalidates 'fd' both on success and on failure. */ |
599 | if (r < 0) | |
600 | return log_error_errno(r, "Failed to make terminal stdin/stdout/stderr: %m"); | |
601 | } | |
c6063244 | 602 | |
9281e703 | 603 | reset_terminal_feature_caches(); |
288a74cc RC |
604 | return 0; |
605 | } | |
606 | ||
288a74cc RC |
607 | bool tty_is_vc(const char *tty) { |
608 | assert(tty); | |
609 | ||
610 | return vtnr_from_tty(tty) >= 0; | |
611 | } | |
612 | ||
613 | bool tty_is_console(const char *tty) { | |
614 | assert(tty); | |
615 | ||
a119ec7c | 616 | return streq(skip_dev_prefix(tty), "console"); |
288a74cc RC |
617 | } |
618 | ||
619 | int vtnr_from_tty(const char *tty) { | |
620 | int i, r; | |
621 | ||
622 | assert(tty); | |
623 | ||
a119ec7c | 624 | tty = skip_dev_prefix(tty); |
288a74cc RC |
625 | |
626 | if (!startswith(tty, "tty") ) | |
627 | return -EINVAL; | |
628 | ||
629 | if (tty[3] < '0' || tty[3] > '9') | |
630 | return -EINVAL; | |
631 | ||
632 | r = safe_atoi(tty+3, &i); | |
633 | if (r < 0) | |
634 | return r; | |
635 | ||
636 | if (i < 0 || i > 63) | |
637 | return -EINVAL; | |
638 | ||
639 | return i; | |
640 | } | |
641 | ||
7b912648 LP |
642 | int resolve_dev_console(char **ret) { |
643 | _cleanup_free_ char *active = NULL; | |
288a74cc | 644 | char *tty; |
7b912648 | 645 | int r; |
288a74cc | 646 | |
7b912648 LP |
647 | assert(ret); |
648 | ||
649 | /* Resolve where /dev/console is pointing to, if /sys is actually ours (i.e. not read-only-mounted which is a | |
650 | * sign for container setups) */ | |
288a74cc RC |
651 | |
652 | if (path_is_read_only_fs("/sys") > 0) | |
7b912648 | 653 | return -ENOMEDIUM; |
288a74cc | 654 | |
7b912648 LP |
655 | r = read_one_line_file("/sys/class/tty/console/active", &active); |
656 | if (r < 0) | |
657 | return r; | |
288a74cc | 658 | |
7b912648 LP |
659 | /* If multiple log outputs are configured the last one is what /dev/console points to */ |
660 | tty = strrchr(active, ' '); | |
288a74cc RC |
661 | if (tty) |
662 | tty++; | |
663 | else | |
7b912648 | 664 | tty = active; |
288a74cc RC |
665 | |
666 | if (streq(tty, "tty0")) { | |
7b912648 | 667 | active = mfree(active); |
288a74cc RC |
668 | |
669 | /* Get the active VC (e.g. tty1) */ | |
7b912648 LP |
670 | r = read_one_line_file("/sys/class/tty/tty0/active", &active); |
671 | if (r < 0) | |
672 | return r; | |
673 | ||
674 | tty = active; | |
675 | } | |
676 | ||
ae2a15bc LP |
677 | if (tty == active) |
678 | *ret = TAKE_PTR(active); | |
679 | else { | |
7b912648 LP |
680 | char *tmp; |
681 | ||
682 | tmp = strdup(tty); | |
683 | if (!tmp) | |
684 | return -ENOMEM; | |
685 | ||
686 | *ret = tmp; | |
288a74cc RC |
687 | } |
688 | ||
7b912648 | 689 | return 0; |
288a74cc RC |
690 | } |
691 | ||
bef41af2 LP |
692 | int get_kernel_consoles(char ***ret) { |
693 | _cleanup_strv_free_ char **l = NULL; | |
6af62124 | 694 | _cleanup_free_ char *line = NULL; |
bef41af2 | 695 | const char *p; |
6af62124 WF |
696 | int r; |
697 | ||
bef41af2 LP |
698 | assert(ret); |
699 | ||
f95dbcc2 | 700 | /* If /sys is mounted read-only this means we are running in some kind of container environment. In that |
bef41af2 LP |
701 | * case /sys would reflect the host system, not us, hence ignore the data we can read from it. */ |
702 | if (path_is_read_only_fs("/sys") > 0) | |
703 | goto fallback; | |
6af62124 WF |
704 | |
705 | r = read_one_line_file("/sys/class/tty/console/active", &line); | |
706 | if (r < 0) | |
707 | return r; | |
708 | ||
bef41af2 | 709 | p = line; |
6af62124 | 710 | for (;;) { |
6abdec98 | 711 | _cleanup_free_ char *tty = NULL, *path = NULL; |
6af62124 | 712 | |
bef41af2 | 713 | r = extract_first_word(&p, &tty, NULL, 0); |
6af62124 WF |
714 | if (r < 0) |
715 | return r; | |
716 | if (r == 0) | |
717 | break; | |
718 | ||
719 | if (streq(tty, "tty0")) { | |
720 | tty = mfree(tty); | |
721 | r = read_one_line_file("/sys/class/tty/tty0/active", &tty); | |
722 | if (r < 0) | |
723 | return r; | |
724 | } | |
725 | ||
6abdec98 | 726 | path = path_join("/dev", tty); |
6af62124 WF |
727 | if (!path) |
728 | return -ENOMEM; | |
729 | ||
730 | if (access(path, F_OK) < 0) { | |
731 | log_debug_errno(errno, "Console device %s is not accessible, skipping: %m", path); | |
6af62124 WF |
732 | continue; |
733 | } | |
734 | ||
6abdec98 | 735 | r = strv_consume(&l, TAKE_PTR(path)); |
6af62124 WF |
736 | if (r < 0) |
737 | return r; | |
738 | } | |
739 | ||
bef41af2 | 740 | if (strv_isempty(l)) { |
6af62124 | 741 | log_debug("No devices found for system console"); |
bef41af2 | 742 | goto fallback; |
6af62124 WF |
743 | } |
744 | ||
ae2a15bc | 745 | *ret = TAKE_PTR(l); |
bef41af2 LP |
746 | |
747 | return 0; | |
748 | ||
749 | fallback: | |
750 | r = strv_extend(&l, "/dev/console"); | |
751 | if (r < 0) | |
752 | return r; | |
753 | ||
ae2a15bc | 754 | *ret = TAKE_PTR(l); |
bef41af2 | 755 | |
6af62124 WF |
756 | return 0; |
757 | } | |
758 | ||
288a74cc | 759 | bool tty_is_vc_resolve(const char *tty) { |
7b912648 | 760 | _cleanup_free_ char *resolved = NULL; |
288a74cc RC |
761 | |
762 | assert(tty); | |
763 | ||
a119ec7c | 764 | tty = skip_dev_prefix(tty); |
288a74cc RC |
765 | |
766 | if (streq(tty, "console")) { | |
7b912648 | 767 | if (resolve_dev_console(&resolved) < 0) |
288a74cc | 768 | return false; |
7b912648 LP |
769 | |
770 | tty = resolved; | |
288a74cc RC |
771 | } |
772 | ||
773 | return tty_is_vc(tty); | |
774 | } | |
775 | ||
776 | const char *default_term_for_tty(const char *tty) { | |
6af760f3 | 777 | return tty && tty_is_vc_resolve(tty) ? "linux" : "vt220"; |
288a74cc RC |
778 | } |
779 | ||
780 | int fd_columns(int fd) { | |
781 | struct winsize ws = {}; | |
782 | ||
783 | if (ioctl(fd, TIOCGWINSZ, &ws) < 0) | |
784 | return -errno; | |
785 | ||
786 | if (ws.ws_col <= 0) | |
787 | return -EIO; | |
788 | ||
789 | return ws.ws_col; | |
790 | } | |
791 | ||
792 | unsigned columns(void) { | |
793 | const char *e; | |
794 | int c; | |
795 | ||
c6063244 | 796 | if (cached_columns > 0) |
288a74cc RC |
797 | return cached_columns; |
798 | ||
799 | c = 0; | |
800 | e = getenv("COLUMNS"); | |
801 | if (e) | |
802 | (void) safe_atoi(e, &c); | |
803 | ||
d09a7135 | 804 | if (c <= 0 || c > USHRT_MAX) { |
288a74cc | 805 | c = fd_columns(STDOUT_FILENO); |
d09a7135 LP |
806 | if (c <= 0) |
807 | c = 80; | |
808 | } | |
288a74cc RC |
809 | |
810 | cached_columns = c; | |
811 | return cached_columns; | |
812 | } | |
813 | ||
814 | int fd_lines(int fd) { | |
815 | struct winsize ws = {}; | |
816 | ||
817 | if (ioctl(fd, TIOCGWINSZ, &ws) < 0) | |
818 | return -errno; | |
819 | ||
820 | if (ws.ws_row <= 0) | |
821 | return -EIO; | |
822 | ||
823 | return ws.ws_row; | |
824 | } | |
825 | ||
826 | unsigned lines(void) { | |
827 | const char *e; | |
828 | int l; | |
829 | ||
c6063244 | 830 | if (cached_lines > 0) |
288a74cc RC |
831 | return cached_lines; |
832 | ||
833 | l = 0; | |
834 | e = getenv("LINES"); | |
835 | if (e) | |
836 | (void) safe_atoi(e, &l); | |
837 | ||
d09a7135 | 838 | if (l <= 0 || l > USHRT_MAX) { |
288a74cc | 839 | l = fd_lines(STDOUT_FILENO); |
d09a7135 LP |
840 | if (l <= 0) |
841 | l = 24; | |
842 | } | |
288a74cc RC |
843 | |
844 | cached_lines = l; | |
845 | return cached_lines; | |
846 | } | |
847 | ||
848 | /* intended to be used as a SIGWINCH sighandler */ | |
849 | void columns_lines_cache_reset(int signum) { | |
850 | cached_columns = 0; | |
851 | cached_lines = 0; | |
852 | } | |
853 | ||
c6063244 LP |
854 | void reset_terminal_feature_caches(void) { |
855 | cached_columns = 0; | |
856 | cached_lines = 0; | |
857 | ||
858 | cached_colors_enabled = -1; | |
859 | cached_underline_enabled = -1; | |
860 | cached_on_tty = -1; | |
861 | } | |
288a74cc | 862 | |
c6063244 | 863 | bool on_tty(void) { |
8cd0356e LP |
864 | |
865 | /* We check both stdout and stderr, so that situations where pipes on the shell are used are reliably | |
866 | * recognized, regardless if only the output or the errors are piped to some place. Since on_tty() is generally | |
867 | * used to default to a safer, non-interactive, non-color mode of operation it's probably good to be defensive | |
868 | * here, and check for both. Note that we don't check for STDIN_FILENO, because it should fine to use fancy | |
869 | * terminal functionality when outputting stuff, even if the input is piped to us. */ | |
870 | ||
c6063244 | 871 | if (cached_on_tty < 0) |
8cd0356e LP |
872 | cached_on_tty = |
873 | isatty(STDOUT_FILENO) > 0 && | |
874 | isatty(STDERR_FILENO) > 0; | |
288a74cc RC |
875 | |
876 | return cached_on_tty; | |
877 | } | |
878 | ||
288a74cc | 879 | int getttyname_malloc(int fd, char **ret) { |
30222f4b | 880 | char path[PATH_MAX], *c; /* PATH_MAX is counted *with* the trailing NUL byte */ |
288a74cc RC |
881 | int r; |
882 | ||
883 | assert(fd >= 0); | |
884 | assert(ret); | |
885 | ||
30222f4b ZJS |
886 | r = ttyname_r(fd, path, sizeof path); /* positive error */ |
887 | assert(r >= 0); | |
888 | if (r == ERANGE) | |
889 | return -ENAMETOOLONG; | |
890 | if (r > 0) | |
891 | return -r; | |
288a74cc | 892 | |
30222f4b ZJS |
893 | c = strdup(skip_dev_prefix(path)); |
894 | if (!c) | |
895 | return -ENOMEM; | |
288a74cc | 896 | |
30222f4b | 897 | *ret = c; |
288a74cc RC |
898 | return 0; |
899 | } | |
900 | ||
f171decd LP |
901 | int getttyname_harder(int fd, char **ret) { |
902 | _cleanup_free_ char *s = NULL; | |
903 | int r; | |
288a74cc | 904 | |
f171decd LP |
905 | r = getttyname_malloc(fd, &s); |
906 | if (r < 0) | |
907 | return r; | |
288a74cc | 908 | |
f171decd LP |
909 | if (streq(s, "tty")) |
910 | return get_ctty(0, NULL, ret); | |
288a74cc | 911 | |
f171decd | 912 | *ret = TAKE_PTR(s); |
288a74cc RC |
913 | return 0; |
914 | } | |
915 | ||
916 | int get_ctty_devnr(pid_t pid, dev_t *d) { | |
917 | int r; | |
918 | _cleanup_free_ char *line = NULL; | |
919 | const char *p; | |
920 | unsigned long ttynr; | |
921 | ||
922 | assert(pid >= 0); | |
923 | ||
924 | p = procfs_file_alloca(pid, "stat"); | |
925 | r = read_one_line_file(p, &line); | |
926 | if (r < 0) | |
927 | return r; | |
928 | ||
929 | p = strrchr(line, ')'); | |
930 | if (!p) | |
931 | return -EIO; | |
932 | ||
933 | p++; | |
934 | ||
935 | if (sscanf(p, " " | |
936 | "%*c " /* state */ | |
937 | "%*d " /* ppid */ | |
938 | "%*d " /* pgrp */ | |
939 | "%*d " /* session */ | |
940 | "%lu ", /* ttynr */ | |
941 | &ttynr) != 1) | |
942 | return -EIO; | |
943 | ||
944 | if (major(ttynr) == 0 && minor(ttynr) == 0) | |
cfeaa44a | 945 | return -ENXIO; |
288a74cc RC |
946 | |
947 | if (d) | |
948 | *d = (dev_t) ttynr; | |
949 | ||
950 | return 0; | |
951 | } | |
952 | ||
54b22b26 LP |
953 | int get_ctty(pid_t pid, dev_t *ret_devnr, char **ret) { |
954 | _cleanup_free_ char *fn = NULL, *b = NULL; | |
288a74cc | 955 | dev_t devnr; |
54b22b26 | 956 | int r; |
288a74cc | 957 | |
54b22b26 LP |
958 | r = get_ctty_devnr(pid, &devnr); |
959 | if (r < 0) | |
960 | return r; | |
288a74cc | 961 | |
54b22b26 LP |
962 | r = device_path_make_canonical(S_IFCHR, devnr, &fn); |
963 | if (r < 0) { | |
964 | if (r != -ENOENT) /* No symlink for this in /dev/char/? */ | |
965 | return r; | |
288a74cc | 966 | |
288a74cc | 967 | if (major(devnr) == 136) { |
54b22b26 LP |
968 | /* This is an ugly hack: PTY devices are not listed in /dev/char/, as they don't follow the |
969 | * Linux device model. This means we have no nice way to match them up against their actual | |
970 | * device node. Let's hence do the check by the fixed, assigned major number. Normally we try | |
971 | * to avoid such fixed major/minor matches, but there appears to nother nice way to handle | |
972 | * this. */ | |
973 | ||
288a74cc RC |
974 | if (asprintf(&b, "pts/%u", minor(devnr)) < 0) |
975 | return -ENOMEM; | |
976 | } else { | |
54b22b26 LP |
977 | /* Probably something similar to the ptys which have no symlink in /dev/char/. Let's return |
978 | * something vaguely useful. */ | |
288a74cc | 979 | |
54b22b26 LP |
980 | r = device_path_make_major_minor(S_IFCHR, devnr, &fn); |
981 | if (r < 0) | |
982 | return r; | |
288a74cc | 983 | } |
54b22b26 | 984 | } |
288a74cc | 985 | |
54b22b26 LP |
986 | if (!b) { |
987 | const char *w; | |
988 | ||
989 | w = path_startswith(fn, "/dev/"); | |
990 | if (w) { | |
991 | b = strdup(w); | |
992 | if (!b) | |
993 | return -ENOMEM; | |
994 | } else | |
995 | b = TAKE_PTR(fn); | |
288a74cc RC |
996 | } |
997 | ||
54b22b26 LP |
998 | if (ret) |
999 | *ret = TAKE_PTR(b); | |
1000 | ||
1001 | if (ret_devnr) | |
1002 | *ret_devnr = devnr; | |
288a74cc RC |
1003 | |
1004 | return 0; | |
1005 | } | |
a07c35c3 | 1006 | |
66cb2fde LP |
1007 | int ptsname_malloc(int fd, char **ret) { |
1008 | size_t l = 100; | |
1009 | ||
1010 | assert(fd >= 0); | |
1011 | assert(ret); | |
1012 | ||
1013 | for (;;) { | |
1014 | char *c; | |
1015 | ||
1016 | c = new(char, l); | |
1017 | if (!c) | |
1018 | return -ENOMEM; | |
1019 | ||
1020 | if (ptsname_r(fd, c, l) == 0) { | |
1021 | *ret = c; | |
1022 | return 0; | |
1023 | } | |
1024 | if (errno != ERANGE) { | |
1025 | free(c); | |
1026 | return -errno; | |
1027 | } | |
1028 | ||
1029 | free(c); | |
1fd4c4ed LP |
1030 | |
1031 | if (l > SIZE_MAX / 2) | |
1032 | return -ENOMEM; | |
1033 | ||
66cb2fde LP |
1034 | l *= 2; |
1035 | } | |
1036 | } | |
1037 | ||
ae1d13db FB |
1038 | int openpt_allocate(int flags, char **ret_slave) { |
1039 | _cleanup_close_ int fd = -1; | |
1040 | _cleanup_free_ char *p = NULL; | |
1041 | int r; | |
1042 | ||
1043 | fd = posix_openpt(flags|O_NOCTTY|O_CLOEXEC); | |
1044 | if (fd < 0) | |
1045 | return -errno; | |
1046 | ||
1047 | if (ret_slave) { | |
1048 | r = ptsname_malloc(fd, &p); | |
1049 | if (r < 0) | |
1050 | return r; | |
1051 | ||
1052 | if (!path_startswith(p, "/dev/pts/")) | |
1053 | return -EINVAL; | |
1054 | } | |
1055 | ||
1056 | if (unlockpt(fd) < 0) | |
1057 | return -errno; | |
1058 | ||
1059 | if (ret_slave) | |
1060 | *ret_slave = TAKE_PTR(p); | |
1061 | ||
1062 | return TAKE_FD(fd); | |
1063 | } | |
1064 | ||
1065 | static int ptsname_namespace(int pty, char **ret) { | |
a07c35c3 LP |
1066 | int no = -1, r; |
1067 | ||
1068 | /* Like ptsname(), but doesn't assume that the path is | |
1069 | * accessible in the local namespace. */ | |
1070 | ||
1071 | r = ioctl(pty, TIOCGPTN, &no); | |
1072 | if (r < 0) | |
1073 | return -errno; | |
1074 | ||
1075 | if (no < 0) | |
1076 | return -EIO; | |
1077 | ||
1078 | if (asprintf(ret, "/dev/pts/%i", no) < 0) | |
1079 | return -ENOMEM; | |
1080 | ||
1081 | return 0; | |
1082 | } | |
66cb2fde | 1083 | |
ae1d13db FB |
1084 | int openpt_allocate_in_namespace(pid_t pid, int flags, char **ret_slave) { |
1085 | _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1, fd = -1; | |
66cb2fde | 1086 | _cleanup_close_pair_ int pair[2] = { -1, -1 }; |
66cb2fde LP |
1087 | pid_t child; |
1088 | int r; | |
1089 | ||
1090 | assert(pid > 0); | |
1091 | ||
1092 | r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, &usernsfd, &rootfd); | |
1093 | if (r < 0) | |
1094 | return r; | |
1095 | ||
1096 | if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0) | |
1097 | return -errno; | |
1098 | ||
1edcb6a9 LP |
1099 | r = namespace_fork("(sd-openptns)", "(sd-openpt)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG, |
1100 | pidnsfd, mntnsfd, -1, usernsfd, rootfd, &child); | |
4c253ed1 LP |
1101 | if (r < 0) |
1102 | return r; | |
1103 | if (r == 0) { | |
66cb2fde LP |
1104 | pair[0] = safe_close(pair[0]); |
1105 | ||
ae1d13db FB |
1106 | fd = openpt_allocate(flags, NULL); |
1107 | if (fd < 0) | |
66cb2fde LP |
1108 | _exit(EXIT_FAILURE); |
1109 | ||
ae1d13db | 1110 | if (send_one_fd(pair[1], fd, 0) < 0) |
66cb2fde LP |
1111 | _exit(EXIT_FAILURE); |
1112 | ||
1113 | _exit(EXIT_SUCCESS); | |
1114 | } | |
1115 | ||
1116 | pair[1] = safe_close(pair[1]); | |
1117 | ||
1edcb6a9 | 1118 | r = wait_for_terminate_and_check("(sd-openptns)", child, 0); |
66cb2fde LP |
1119 | if (r < 0) |
1120 | return r; | |
2e87a1fd | 1121 | if (r != EXIT_SUCCESS) |
66cb2fde LP |
1122 | return -EIO; |
1123 | ||
ae1d13db FB |
1124 | fd = receive_one_fd(pair[0], 0); |
1125 | if (fd < 0) | |
1126 | return fd; | |
1127 | ||
1128 | if (ret_slave) { | |
1129 | r = ptsname_namespace(fd, ret_slave); | |
1130 | if (r < 0) | |
1131 | return r; | |
1132 | } | |
1133 | ||
1134 | return TAKE_FD(fd); | |
66cb2fde | 1135 | } |
40e1f4ea LP |
1136 | |
1137 | int open_terminal_in_namespace(pid_t pid, const char *name, int mode) { | |
1138 | _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, usernsfd = -1, rootfd = -1; | |
1139 | _cleanup_close_pair_ int pair[2] = { -1, -1 }; | |
40e1f4ea LP |
1140 | pid_t child; |
1141 | int r; | |
1142 | ||
1143 | r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, &usernsfd, &rootfd); | |
1144 | if (r < 0) | |
1145 | return r; | |
1146 | ||
1147 | if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0) | |
1148 | return -errno; | |
1149 | ||
1edcb6a9 LP |
1150 | r = namespace_fork("(sd-terminalns)", "(sd-terminal)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG, |
1151 | pidnsfd, mntnsfd, -1, usernsfd, rootfd, &child); | |
4c253ed1 LP |
1152 | if (r < 0) |
1153 | return r; | |
1154 | if (r == 0) { | |
40e1f4ea LP |
1155 | int master; |
1156 | ||
1157 | pair[0] = safe_close(pair[0]); | |
1158 | ||
40e1f4ea LP |
1159 | master = open_terminal(name, mode|O_NOCTTY|O_CLOEXEC); |
1160 | if (master < 0) | |
1161 | _exit(EXIT_FAILURE); | |
1162 | ||
1163 | if (send_one_fd(pair[1], master, 0) < 0) | |
1164 | _exit(EXIT_FAILURE); | |
1165 | ||
1166 | _exit(EXIT_SUCCESS); | |
1167 | } | |
1168 | ||
1169 | pair[1] = safe_close(pair[1]); | |
1170 | ||
1edcb6a9 | 1171 | r = wait_for_terminate_and_check("(sd-terminalns)", child, 0); |
40e1f4ea LP |
1172 | if (r < 0) |
1173 | return r; | |
2e87a1fd | 1174 | if (r != EXIT_SUCCESS) |
40e1f4ea LP |
1175 | return -EIO; |
1176 | ||
1177 | return receive_one_fd(pair[0], 0); | |
1178 | } | |
40c9fe4c | 1179 | |
158fbf76 | 1180 | static bool getenv_terminal_is_dumb(void) { |
ac96418b LP |
1181 | const char *e; |
1182 | ||
ac96418b LP |
1183 | e = getenv("TERM"); |
1184 | if (!e) | |
1185 | return true; | |
1186 | ||
1187 | return streq(e, "dumb"); | |
1188 | } | |
1189 | ||
158fbf76 ZJS |
1190 | bool terminal_is_dumb(void) { |
1191 | if (!on_tty()) | |
1192 | return true; | |
1193 | ||
1194 | return getenv_terminal_is_dumb(); | |
1195 | } | |
1196 | ||
40c9fe4c | 1197 | bool colors_enabled(void) { |
40c9fe4c | 1198 | |
0295642d | 1199 | /* Returns true if colors are considered supported on our stdout. For that we check $SYSTEMD_COLORS first |
f95dbcc2 | 1200 | * (which is the explicit way to turn colors on/off). If that didn't work we turn colors off unless we are on a |
0295642d LP |
1201 | * TTY. And if we are on a TTY we turn it off if $TERM is set to "dumb". There's one special tweak though: if |
1202 | * we are PID 1 then we do not check whether we are connected to a TTY, because we don't keep /dev/console open | |
5238e957 | 1203 | * continuously due to fear of SAK, and hence things are a bit weird. */ |
0295642d | 1204 | |
c6063244 | 1205 | if (cached_colors_enabled < 0) { |
acf553b0 | 1206 | int val; |
ae5b3958 | 1207 | |
acf553b0 ZJS |
1208 | val = getenv_bool("SYSTEMD_COLORS"); |
1209 | if (val >= 0) | |
c6063244 | 1210 | cached_colors_enabled = val; |
df0ff127 | 1211 | else if (getpid_cached() == 1) |
158fbf76 | 1212 | /* PID1 outputs to the console without holding it open all the time */ |
c6063244 | 1213 | cached_colors_enabled = !getenv_terminal_is_dumb(); |
ae5b3958 | 1214 | else |
c6063244 | 1215 | cached_colors_enabled = !terminal_is_dumb(); |
40c9fe4c JS |
1216 | } |
1217 | ||
c6063244 | 1218 | return cached_colors_enabled; |
40c9fe4c | 1219 | } |
c83f349c | 1220 | |
c2b32159 LP |
1221 | bool dev_console_colors_enabled(void) { |
1222 | _cleanup_free_ char *s = NULL; | |
1223 | int b; | |
1224 | ||
1225 | /* Returns true if we assume that color is supported on /dev/console. | |
1226 | * | |
1227 | * For that we first check if we explicitly got told to use colors or not, by checking $SYSTEMD_COLORS. If that | |
f95dbcc2 ZJS |
1228 | * isn't set we check whether PID 1 has $TERM set, and if not, whether TERM is set on the kernel command |
1229 | * line. If we find $TERM set we assume color if it's not set to "dumb", similarly to how regular | |
c2b32159 LP |
1230 | * colors_enabled() operates. */ |
1231 | ||
1232 | b = getenv_bool("SYSTEMD_COLORS"); | |
1233 | if (b >= 0) | |
1234 | return b; | |
1235 | ||
1236 | if (getenv_for_pid(1, "TERM", &s) <= 0) | |
1237 | (void) proc_cmdline_get_key("TERM", 0, &s); | |
1238 | ||
1239 | return !streq_ptr(s, "dumb"); | |
1240 | } | |
1241 | ||
526664f6 | 1242 | bool underline_enabled(void) { |
526664f6 | 1243 | |
c6063244 | 1244 | if (cached_underline_enabled < 0) { |
526664f6 LP |
1245 | |
1246 | /* The Linux console doesn't support underlining, turn it off, but only there. */ | |
1247 | ||
c6063244 LP |
1248 | if (colors_enabled()) |
1249 | cached_underline_enabled = !streq_ptr(getenv("TERM"), "linux"); | |
526664f6 | 1250 | else |
c6063244 | 1251 | cached_underline_enabled = false; |
526664f6 LP |
1252 | } |
1253 | ||
c6063244 | 1254 | return cached_underline_enabled; |
526664f6 LP |
1255 | } |
1256 | ||
c83f349c LP |
1257 | int vt_default_utf8(void) { |
1258 | _cleanup_free_ char *b = NULL; | |
1259 | int r; | |
1260 | ||
1261 | /* Read the default VT UTF8 setting from the kernel */ | |
1262 | ||
1263 | r = read_one_line_file("/sys/module/vt/parameters/default_utf8", &b); | |
1264 | if (r < 0) | |
1265 | return r; | |
1266 | ||
1267 | return parse_boolean(b); | |
1268 | } | |
1269 | ||
1270 | int vt_reset_keyboard(int fd) { | |
15bba613 | 1271 | int kb; |
c83f349c LP |
1272 | |
1273 | /* If we can't read the default, then default to unicode. It's 2017 after all. */ | |
1274 | kb = vt_default_utf8() != 0 ? K_UNICODE : K_XLATE; | |
1275 | ||
1276 | if (ioctl(fd, KDSKBMODE, kb) < 0) | |
1277 | return -errno; | |
1278 | ||
1279 | return 0; | |
1280 | } | |
6179ede1 FB |
1281 | |
1282 | int vt_restore(int fd) { | |
1283 | static const struct vt_mode mode = { | |
1284 | .mode = VT_AUTO, | |
1285 | }; | |
1286 | int r, q = 0; | |
1287 | ||
1802d5f2 | 1288 | if (ioctl(fd, KDSETMODE, KD_TEXT) < 0) |
6179ede1 FB |
1289 | q = log_debug_errno(errno, "Failed to set VT in text mode, ignoring: %m"); |
1290 | ||
1291 | r = vt_reset_keyboard(fd); | |
1292 | if (r < 0) { | |
1293 | log_debug_errno(r, "Failed to reset keyboard mode, ignoring: %m"); | |
1294 | if (q >= 0) | |
1295 | q = r; | |
1296 | } | |
1297 | ||
1802d5f2 | 1298 | if (ioctl(fd, VT_SETMODE, &mode) < 0) { |
6179ede1 FB |
1299 | log_debug_errno(errno, "Failed to set VT_AUTO mode, ignoring: %m"); |
1300 | if (q >= 0) | |
1301 | q = -errno; | |
1302 | } | |
1303 | ||
1802d5f2 | 1304 | r = fchmod_and_chown(fd, TTY_MODE, 0, (gid_t) -1); |
6179ede1 | 1305 | if (r < 0) { |
1802d5f2 | 1306 | log_debug_errno(r, "Failed to chmod()/chown() VT, ignoring: %m"); |
6179ede1 | 1307 | if (q >= 0) |
1802d5f2 | 1308 | q = r; |
6179ede1 FB |
1309 | } |
1310 | ||
1311 | return q; | |
1312 | } | |
27dafac9 FB |
1313 | |
1314 | int vt_release(int fd, bool restore) { | |
1315 | assert(fd >= 0); | |
1316 | ||
1317 | /* This function releases the VT by acknowledging the VT-switch signal | |
1318 | * sent by the kernel and optionally reset the VT in text and auto | |
1319 | * VT-switching modes. */ | |
1320 | ||
1321 | if (ioctl(fd, VT_RELDISP, 1) < 0) | |
1322 | return -errno; | |
1323 | ||
1324 | if (restore) | |
1325 | return vt_restore(fd); | |
1326 | ||
1327 | return 0; | |
1328 | } | |
37b8d2f6 ZJS |
1329 | |
1330 | void get_log_colors(int priority, const char **on, const char **off, const char **highlight) { | |
1331 | /* Note that this will initialize output variables only when there's something to output. | |
1332 | * The caller must pre-initalize to "" or NULL as appropriate. */ | |
1333 | ||
1334 | if (priority <= LOG_ERR) { | |
1335 | if (on) | |
1336 | *on = ANSI_HIGHLIGHT_RED; | |
1337 | if (off) | |
1338 | *off = ANSI_NORMAL; | |
1339 | if (highlight) | |
1340 | *highlight = ANSI_HIGHLIGHT; | |
1341 | ||
0d0464d3 ZJS |
1342 | } else if (priority <= LOG_WARNING) { |
1343 | if (on) | |
1344 | *on = ANSI_HIGHLIGHT_YELLOW; | |
1345 | if (off) | |
1346 | *off = ANSI_NORMAL; | |
1347 | if (highlight) | |
1348 | *highlight = ANSI_HIGHLIGHT; | |
1349 | ||
37b8d2f6 ZJS |
1350 | } else if (priority <= LOG_NOTICE) { |
1351 | if (on) | |
1352 | *on = ANSI_HIGHLIGHT; | |
1353 | if (off) | |
1354 | *off = ANSI_NORMAL; | |
1355 | if (highlight) | |
1356 | *highlight = ANSI_HIGHLIGHT_RED; | |
1357 | ||
1358 | } else if (priority >= LOG_DEBUG) { | |
1359 | if (on) | |
1360 | *on = ANSI_GREY; | |
1361 | if (off) | |
1362 | *off = ANSI_NORMAL; | |
1363 | if (highlight) | |
1364 | *highlight = ANSI_HIGHLIGHT_RED; | |
1365 | } | |
1366 | } |