summaryrefslogtreecommitdiffstats
path: root/src/basic/terminal-util.c (follow)
Commit message (Collapse)AuthorAgeFilesLines
* util-lib/systemd-run: implement race-free PTY peer opening (#34953)Luca Boccassi2024-11-011-44/+107
|\ | | | | | | | | | | | | | | This makes use of the new TIOCGPTPEER pty ioctl() for directly opening a PTY peer, without going via path names. This is nice because it closes a race around allocating and opening the peer. And also has the nice benefit that if we acquired an fd originating from some other namespace/container, we can directly derive the peer fd from it, without having to reenter the namespace again.
| * terminal-util: add pty_open_peer() helperLennart Poettering2024-10-301-0/+57
| | | | | | | | | | This opens a pty peer in one go, and uses the new race-free TIOCGPTPEER ioctl() to do so – if it is available.
| * terminal-util: various minor modernizationsLennart Poettering2024-10-301-44/+50
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Various fixes: 1. Adds O_CLOEXEC for two socketpair()s where we forgot it. 2. Uses FORK_WAIT instead of manual wait_for_terminate_and_check() invocations. 3. Prefix opaque NULL/0 arguments with comments what they are. 4. Add a banch of assert()s, and change flag validation in open_terminal() to be assert() (since flags mistakes are programming errors, not runtime errors).
* | terminal-util: define ANSI_OSC as macro for the OSC terminal sequence prefixLennart Poettering2024-10-311-2/+2
| |
* | tree-wide: prefer generating 0x1B 0x5C as ANSI sequence "ST"Lennart Poettering2024-10-311-2/+2
|/ | | | | | | | | | | | | | | | | OSC sequences can be closed with one of three terminators: 1. ASCII code 7, aka BEL, aka ^G, aka \x07, aka \a 2. ASCII code 156, aka \x9c 2. Pair of ASCII code 27 followed by ASCII code 92, aka \x1b\x5c Of these, in some corner case scenarios BEL makes problem (see #34604). Hence switch away from that wherever we use it, and prefer the \x1b\x5c instead. That's preferable over \x9c, since the latter is also a valid UTF-8 codepoint. See discussion here for example: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda#the-escape-sequence Fixes: #34604
* varlinkctl: respect $COLUMNS when rebreaking lines and we are not connected ↵Lennart Poettering2024-10-211-9/+20
| | | | | | | | | | to a TTY Let's provide a mechanism to select the number of screen columns for rebreaking comments in Varlink IDL connected to a TTY, by honouring the $COLUMNS env var then too. Previously we'd only honour when connected to a TTY, but it's also useful otherwise for rebreaking ridiculously long comments, hence honour it in this case too.
* Move show_menu() to terminal-util.hDaan De Meyer2024-10-021-0/+47
|
* homed: wait for user input during firstbootMichael Ferrari2024-09-171-0/+19
| | | | | | | This mirrors the behavior of `systemd-firstboot` and allows bootup messages to settle down before user input is actually processed. See: https://github.com/systemd/systemd/issues/34448
* terminal-util: correct fd validity checkMike Yuan2024-09-081-1/+1
| | | | Follow-up for 14f594b995bbaea85456a4c26e5c07446a4c446e
* tree-wide: use isatty_safe() moreLennart Poettering2024-08-201-2/+2
|
* terminal-util: don't assume errno is correctly set when using isatty_safe()Lennart Poettering2024-08-201-3/+3
| | | | | | | | | let's instead generate ENOTTY on our own. This is more correct with out coding style (since we generally do not propagate errors via errno), and also addresses #34039 as side effect. (#34039 really needs to be fixed in musl though, too, this is just a work-around as side-effect). Fixes: #34039
* terminal-util: fix isatty_safe() on hung-up TTYsLennart Poettering2024-08-201-0/+6
| | | | | | | | | | glibc returs EIO on ttys that are hung up. That's not really correct, POSIX seems to disagree. Work around this in our code, and turn this into a clean "1", since a hung up tty doesn't stop being a tty just because it is hung up. Background: https://github.com/systemd/systemd/pull/34039
* terminal-util: do not query kernel cmdline for pty sizeMike Yuan2024-08-101-1/+4
| | | | This is pointless and noisy even for debug level.
* crash-handler: Call vhangup on /dev/console before spawning crash shellDaan De Meyer2024-08-071-0/+12
| | | | | | | | | When pid 1 crashes, the getty unit for the console will happily keep running which means we end up with two shells competing for the same tty. Let's call vhangup on /dev/console to kill every other process attached to the console before we spawn the crash shell. The getty units have Restart=always but lucky for us, pid 1 just crashed in fire and flames so it isn't actually able to restart the getty unit.
* terminal-util: add recognizable error if cols/rows of tty are initially not ↵Lennart Poettering2024-07-191-2/+2
| | | | | | | | initialized Various tty types come up with cols/rows not initialized (i.e. set to zero). Let's detect these cases, and return a better error than EIO, simply to make things easier to debug.
* terminal-util: extend timeout on background color requestLennart Poettering2024-07-191-1/+1
| | | | | I managed to hit the timeout a couple of times inside of slow qemu. Let's increase it a bit to 1/3s
* terminal-util: try to avoid reading more from terminal than we need in ↵Lennart Poettering2024-07-191-3/+6
| | | | get_default_background_color()
* tree-wide: acquire /dev/console lock around any attempts to reset TTYLennart Poettering2024-07-191-0/+14
|
* terminal-util: teach resolve_dev_console() to deal correctly with ↵Lennart Poettering2024-07-191-6/+20
| | | | | | | | /dev/console being a symlink /dev/console is sometimes a symlink in container managers. Let's handle that correctly, and resolve the symlink, and not consider the data from /sys/ in that case.
* terminal-util: move lock_dev_console() hereLennart Poettering2024-07-191-0/+17
| | | | | | | | It doesn't really make sense to have that in dev-setup.c, which is mostly about setting up /dev/, creating device nodes and stuff. let's move it to the other stuff that deals with /dev/console's peculiarities.
* terminal-util: refactor vt_disallocate()Lennart Poettering2024-07-191-40/+19
| | | | | | | | | | Numerous fixes: 1. use vtnr_from_tty() to parse out VT number from tty path 2. open tty for write only when we want to output just ansi sequences 3. open tty in asynchronous mode, and apply a timeout, just to be safe 4. propagate error from writing (most callers ignore it anyway, might as well pass it along correctly)
* terminal-util: simplify terminal_set_size_fd() a tiny bitLennart Poettering2024-07-191-2/+7
|
* terminal-util: split out color macros/helpers into its own headerLennart Poettering2024-07-191-0/+1
| | | | | | | | | This is a lot of stuff, and sometimes quite wild, let's turn this into its own header. All stuff color-related that just generates sequences is now in ansi-color.h (no .c file!), and everything more complex that probes/ineracts with terminals remains in termina-util.[ch]
* terminal-util: remove terminal_vhangup() because apparently unusedLennart Poettering2024-07-191-10/+0
|
* terminal-util: rename return parameters ret_xyzLennart Poettering2024-07-191-5/+5
|
* terminal-util: modernize vt_reset_keyboard() a bitLennart Poettering2024-07-191-3/+8
|
* terminal-util: don't export vt_reset_keyboard() + vt_default_utf8()Lennart Poettering2024-07-191-22/+22
|
* terminal-util: modernize terminal_reset_ansi_seq() a bitLennart Poettering2024-07-191-2/+2
| | | | | | Let's update the commentary a bit. Also, use a time-out of 100ms rather than 50ms for this, simply to unify on the same value used in vt_disallocate() in a similar case.
* terminal-util: don't export terminal_reset_ioctl()/terminal_reset_ansi_seq() ↵Lennart Poettering2024-07-191-102/+101
| | | | | | | anymore We only use them in terminal-util.c, hence make them static (and move them before their first using function).
* terminal-util: rename reset_terminal_fd() → terminal_reset_ioctl()Lennart Poettering2024-07-191-3/+3
| | | | | | Let's put "terminal_" as prefix, like with the other reset calls, and let's make clear that this only encapsulates the ioctl-based reset logic, not the ANSI sequence based reset logic.
* terminal-util: remove reset_terminal() as it is unusedLennart Poettering2024-07-191-14/+0
|
* terminal-util: don't issue "ESC c" sequence on reset, but only when erasing ↵Lennart Poettering2024-07-191-3/+3
| | | | | | | | | | | | | | | | | the screen ESC c is a (vaguely defined) "reset to initial state" ANSI sequence. Many terminals clear the screen in this case, but that's a bit drastic I think for most resets. ESC c was added to the reset logic in 00bc83a275fa3ca8d90579fe9597d8b651d47332 (i.e. very recently), and I don't think the effect was clear at that time. Let's keep the ESC c in place however when we actually want to clear the screen. Hence move it from reset_terminal_fd() into vt_disallocate(). Fixes: #33689
* terminal-util: add new helper terminal_reset_defensive() that combines ↵Lennart Poettering2024-07-191-0/+21
| | | | reset-by-ioctl and reset-by-sequence reasonably
* terminal-util: add terminal_is_pty_fd() helperLennart Poettering2024-07-191-0/+29
| | | | The helper checks if an fd references a pty
* terminal-util: unify code that resets /dev/console in common helperLennart Poettering2024-07-191-19/+26
| | | | We have pretty much the same code at two places, let's make it one.
* terminal-util: reset /dev/console via ansi seq also in make_console_stdio()Lennart Poettering2024-07-191-0/+4
| | | | | | This appears to have been the intention of 00bc83a275fa3ca8d90579fe9597d8b651d47332, judging by the comments on that.
* terminal-util: try to initialize rows/cols via ansi sequence in ↵Lennart Poettering2024-07-191-4/+5
| | | | | | make_console_stdio() Let's hook this up.
* terminal-util: add helper that adjust terminal width/height from data ↵Lennart Poettering2024-07-191-0/+33
| | | | acquired via ANSI sequences
* terminal-util: add helper that queries terminal sizes via ANSI sequenceLennart Poettering2024-07-191-0/+230
| | | | | | | | | | | | | When we are talking to a serial terminal quite commonly the dimensions are not set properly, because the serial protocol has not handshake or similar to transfer this information. However, we can derive the dimensions via ANSI sequences too, which should get us the right information, since ANSI sequences are interpreted by the final terminal, rather than an intermediary local tty driver (which is where TIOCGWINSZ is interpreted). This adds a helper call that gets the dimensions this way.
* terminal-util: rename set_terminal_cursor_position() → ↵Lennart Poettering2024-07-191-9/+3
| | | | | | | | terminal_set_cursor_position() Let's prefix these functions with the subsystem name, and clean them up a bit. Specifically, drop the error logging, it's entirely duplicative, since every single caller does it anyway.
* terminal-util: when querying bg color, ensure input fd and output fd refer ↵Lennart Poettering2024-07-191-2/+34
| | | | | | | | | | | to same tty Let's add an extra safety check: before issuing the ansi sequence to query the bg color, let's make sure input and output fd actually reference the same tty. because otherwise it's unlikely we'll be able to read back the response from the tty driver. This is mostly just paranoia.
* terminal-util: remember error code from tcsetattr()Lennart Poettering2024-07-191-1/+1
|
* terminal-util: turn off echo on stdin, not stdoutLennart Poettering2024-07-191-2/+2
| | | | | This doesn't make much of a different IRL, but it feels more right that an operation that happens in input is turned off via the input fd.
* terminal-util: don't process the same data twice when reading back bg color infoLennart Poettering2024-07-191-24/+43
| | | | | | If we only read partial information from the tty we ended up parsing it again and again, confusing the state machine. hence, return how much data we actually processed and drop it from the buffer.
* terminal-util: return correct error in chvt()Lennart Poettering2024-07-191-1/+1
|
* terminal-util: refuse a few more unexpected open flags in open_terminal()Lennart Poettering2024-07-191-1/+1
|
* terminal-util: trivial white space fixLennart Poettering2024-07-191-1/+1
|
* signal-util: use common definitions for ignore + default "struct sigaction"Lennart Poettering2024-07-121-15/+7
| | | | | | | | | | We use this at various places, let's unify this in one global constant. This changes flags in crash-handler.c in a tiny irrelevant way: we ask syscalls to be continued on signal arrival, which we previously didn't. But that shouldn't change anything, the only thing we'll do in the relevant process is call raise(), and that's it, hence there definitely are no syscalls to restart or not to restart.
* terminal-util: several cleanups for ColorModeYu Watanabe2024-06-241-54/+54
| | | | | | | - introduce or rename usual enum values _MAX and _INVALID, - introduce and use string table lookup functions, - split out implementation of get_color_mode() to _impl(), - add tests for get_color_mode().
* terminal-util: merge COLOR_ON with COLOR_24BITYu Watanabe2024-06-241-3/+3
| | | | | | | Currently, we assume that there is no restriction on coloring when COLOR_24BIT. Let's merge the two values. Follow-up for a5efbf468c96190c9562bc8121eda32310dfd112.