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