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