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