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