terminology poll: what would you call the part of the operating system that is responsible for managing the pseudoterminal device? (handling ioctls, tracking the foreground process, remembering the terminal window size, turning `Ctrl+C` into a SIGTERM, handling `Ctrl+S` and `Ctrl+W`, etc)

(also open to hearing that you think that it's not just one thing or anything else really)

line discipline
4.7%
terminal driver
51.4%
what?
39.6%
other
4.2%
Poll ended at .

all of the code for this stuff (terminal ioctls, process groups, window size, signals, Ctrl+W, etc) in Linux seems to be in `drivers/tty` which kinda makes me want to call it this component the “TTY driver" (which helpfully implies the existence of a TTY device, which is an important thing)

(2/?)

@b0rk "the tty driver" is absolutely the normal thing we refer to that code as. But usually with more profanity.

@b0rk

IIRC old days each interactive terminal was controlled through a pair of drivers:

The tty driver contained the interfaces that the application program (cat, vi, etc) would interact with, supporting the ioctl calls.

Then inside the kernel the tty driver connected with the driver for the actual device, usually a serial port. Or the tty driver could connect to a pty (pseudo terminal) driver openable by another application e.g. the telnet server process, or (later) a terminal window.

@b0rk PTY is the "pseudo terminal driver" and my name is in that source code, so I vote "terminal driver".
@b0rk I always call it the tty driver but line discipline is more technically correct
@b0rk "TTY driver" because on some level this thing still thinks it's puppeting a VT100.
@b0rk 'TTY', that is TeleType, one of these things:
@Scali @b0rk Wow, I spent a lot of time sitting in front of one of those things back in the day, either on a phone line or locally connected to an ICL 🤣
@ChrisWarwick @b0rk Yes, it was my first printer (on a C64 that is). My dad brought it home from work, they were getting rid of it. But those things were originally used to interface with AT&T UNIX machines, among others. Character displays came later.
@b0rk I voted for line discipline, but I think it's sort of a "helpful fiction" thing here. I would not expect line discipline and process group leader tracking to be significantly related in a single subsystem (I could be wrong), but the behaviors of the involved systems are /outwardly/ so "obviously" connected that we think the whole of terminal behavior is a thing, when in fact it's a helpful-but-bogus abstraction.

@rjbs I think it's mostly all in drivers/tty in linux, so the code does seem to be mostly in the same place?

like here's some stuff about process group leader tracking: https://github.com/torvalds/linux/blob/9d7a0577c9db35c4cc52db90bc415ea248446472/drivers/tty/tty_jobctrl.c#L98-L108

and I think ctrl+w is handled something like here: https://github.com/torvalds/linux/blob/9d7a0577c9db35c4cc52db90bc415ea248446472/drivers/tty/n_tty.c#L1258-L1263

linux/drivers/tty/tty_jobctrl.c at 9d7a0577c9db35c4cc52db90bc415ea248446472 · torvalds/linux

Linux kernel source tree. Contribute to torvalds/linux development by creating an account on GitHub.

GitHub
@b0rk Thanks, shows what I know!
@rjbs thanks for the comment, I had no idea where this stuff was and you made me look it up!
@b0rk Cunningham's Law at work.
@rjbs @b0rk At this point when I hear `line discipline' I think of the latter-day Reseach Unix stream I/O system. We called the stackable modules line disciplines, but the original motivation was to make the terminal driver a reusable module and we still used that name too.
@b0rk Some of those things are part of the line discipline, but not all of them, and the distinction is largely confusing and unnecessary today. But they're not part of a "driver" per se, they're part of the kernel's terminal abstraction (and in particular they're part of common code that is shared between both the pty "driver" and the drivers for hardware terminals like the UEFI console and serial ports). You could say maybe "tty layer" and not get too many complaints.
@wollman what do you mean that they're not part of a “driver”? as far I can tell in Linux most of the code for this stuff is in `drivers/tty`, is the stuff In drivers/tty not a “driver”?

@b0rk I can't speak for the organization of the Linux kernel's source code; I'm not a Linux kernel developer. In Unix, it was in `sys/tty.c`, which has a comment at the front:

/*
* general TTY subroutines
*/

i.e., it's a library of functions to implement the things required by TTY drivers. As more functions have been added over time, functions have been split out into other source files (there are 7 sys/tty*.c files by 4.3BSD in 1986) but it's always been core device-independent kernel code.

@wollman what do you mean by "Unix”? i never know what people mean when they say that, I always think of Unix as meaning "any POSIX system” but I guess that's not what you mean
@b0rk I mean "The Unix®️ Operating System, published by AT&T Bell Laboratories in the 1970s and later by various other AT&T business units and their licensees and successors ever since." The copyright was owned by Caldera for a brief moment, as a result of which, it is now possible to freely read and distribute the source to heritage Unix releases like Seventh Edition and 4.2BSD. (The trademark now belongs to The Open Group and can be licensed even by non-AT&T-derived implementations.)

@b0rk I feel it's not just one thing but that's because I know too much about the Unix underpinnings, where it is (at least traditionally) a combination of a kernel driver (allocation, ioctls, etc) and the line discipline (handling Ctrl characters and their actions).

(In practice I think there is some combination of a generic tty driver and then specific drivers for eg ptys, serial terminals, the video console, etc. Line disciplines are generic across all of them.)

@cks hm yeah in Linux it seems like there's a bunch of generic code and then specific subfolders for "vt", "serial", "serdev", "ipwireless", and "hvc" (not sure what the last 3 are)

https://github.com/torvalds/linux/tree/master/drivers/tty

linux/drivers/tty at master · torvalds/linux

Linux kernel source tree. Contribute to torvalds/linux development by creating an account on GitHub.

GitHub
@b0rk @cks hvc is for paravirtualized consoles provided by a hypervisor like kvm, xen or IBM's PR/SM on Z

@b0rk

Is it even in the operating system?

My terminal program (kitty most recently) has a config to say things like "when I type ctrl-c - send 'interrupt'" - so clearly it's not just the OS on the machine I am ssh'd into that handles that.

In my "understanding" (which may be entirely wrong, lol) - it's a game of telephone. I hit ctrl-C - and a sequence of entities get to decide what happens. My local keyboard driver might remap that before handing it to kitty, who may or may not pass that thru ssh to the server I am logged into, terminal driver, pty, shells, etc etc. - with any element in the chain having the capacity to pass that along or change it. So - there's no single entity to pin the blame on.

@tbortels ooh do you know what the name of that config parameter is? would love to learn more about that, I'm surprised!
@b0rk @tbortels Probably referring to 'copy_or_interrupt' - if there's selected text, do a copy, if not, send an interrupt.

@gomijacogeo @b0rk

I think that was the original/default setting - and it was breaking vim. And while I am a wily haxxor, I try to avoid vim configs because it never ends well for me.

@tbortels @gomijacogeo ah I see -- yeah I guess the deal is that kitty can make ctrl+c copy to the clipboard if it wants, but it can't map Ctrl+C to something _other_ than interrupt, like for example it can't make `Ctrl+C` send a different signal. only the operating system can do that

(but it would be extremely weird for someone to reconfigure Ctrl+C at the OS level to do something other than "interrupt" so it's fair of kitty to assume that's what it does)

@b0rk @tbortels I think you could statically remap ^C to be anything just like any other key remapping, but the change-behavior-if-text-is-selected was a special kitty clever hack/(mis?)feature.

@b0rk @gomijacogeo

So - some of the actions you can map to any key (not just ctrl-C) are "send_key", "send_text" (woo macros!) and "send_signal" to "Send the specified SIGNAL to the foreground process in the active window" - they give SIGTERM as the example.

The OS (or the program in the foreground) could choose to ignore most signals, of course. This is why I picked "other" - I think the responsibility for "what happens when I ctrl-C" is a group consensus thing, with the message getting passed along the chain until someone decides to stop passing it along.

I'm surprised this thing can't take out the trash and feed my dog.

https://sw.kovidgoyal.net/kitty/actions/#miscellaneous

Mappable actions

The actions described below can be mapped to any key press or mouse action using the map and mouse_map directives in kitty.conf. For configuration examples, see the default shortcut links for each ...

kitty
@tbortels thanks I'm very excited to be wrong about this! gonna try to figure out how it works
@tbortels @b0rk This is probably a case where kitty was being too clever for its own good. (this is probably another theory/archaeology thing that'll make bork's teeth itch) It sounds like kitty was taking upon itself to send signals out-of-band rather than just send the ASCII 0x3 (aka ^C) char in-band for the child's tty driver's line-discipline to make the determination. Since vim uses raw mode, it expects to see ^C as just another input character; getting a SIGNINT however is like if someone typed 'pkill vim' in another window.
@gomijacogeo @tbortels it looks like it's implemented here, it's interesting that the comments say that kitty actually can't be 100% sure which one is the foreground process right now when it's deciding which PID to send the signal to https://github.com/kovidgoyal/kitty/blob/cb490da9487089a09eb10a4edb1129e1bca08d52/kitty/child.py#L465-L481
kitty/kitty/child.py at cb490da9487089a09eb10a4edb1129e1bca08d52 · kovidgoyal/kitty

Cross-platform, fast, feature-rich, GPU based terminal - kovidgoyal/kitty

GitHub

@b0rk @gomijacogeo

Heh - that's terrifying. Good find.

@gomijacogeo @b0rk

You're probably right - the "i'll just look at context and do the right thing" seemed suspiciously like magic - and it was, at least for my vim config.

Is ok - I kinda prefer "cut" and "interrupt" to be different keys.

@b0rk

Here's the relevant line in ~/.config/kitty.kitty.conf:

map ctrl+c interrupt

I had to set it explicitly because IIRC it was trying to do a "cut" for cut and paste rather than interrupt by default in vim. I assume this makes kitty replace "send ASCII decimal 3" with "send the xterm terminal escape codes for break". Or somesuch equivalent.

To save a google for anyone wanting to dig deeper: https://sw.kovidgoyal.net/kitty/conf/

I rather like kitty. 😃

kitty.conf

kitty is highly customizable, everything from keyboard shortcuts, to rendering frames-per-second. See below for an overview of all customization possibilities. You can open the config file within k...

kitty
@b0rk I'd probably go with 'tty driver' conversationally, but use 'tty subsystem' if I were writing a book or article as 'driver' has a specific meaning and is a little too narrow if you're being pedantic. For me 'line discipline' is more of a descriptive bucket (it's a collection of mode settings) rather than an active entity. The best bad analogy I can come up with is 'car' vs 'trim level' (e.g. you can get the 4cyl, manual, and AM/FM radio, or the 6cyl, auto, sunroof, and XM radio w/ nav system - if you want the nav, you're getting a sunroof, so you buy a trim level, but you don't drive it).
@b0rk I always saw this as a sort of "TTY Subsystem" that comprises multiple different parts.

@b0rk
> the part of the operating system that is responsible for managing the pseudoterminal device

Gotta admit that this is so obscure that, if it is different from “terminal driver”, I don't understand what would make the difference.

@b0rk
> line discipline

I don't know what term I would use for it.

I do know that “line discipline” strongly sounds like a *protocol* (like UTF-8 or SMTP), not a part of an operating system. If someone pointed to a software subsystem and said “that's the line discipline”, that would just confuse; it sounds like saying
“that's the handshake” and sounds like they bungled the sentence.

For a software subsystem *implementing* “line discipline”, I'd demand a better term.

@bignose yeah I don't think "line discipline" is the correct term here but even if it were I still probably wouldn't use it because it's such a confusing name for a software system

@b0rk
I think they were planning to have multiple line disciplines to choose from. I vaguely recall something about an "old ldisc" vs. "new ldisc". If you use a serial port for PPP it would use the "ppp ldisc".

To be explicit, the line discipline is the code that decides what to do with the incoming characters, such as ^U and ^C. And which ones to send. The tty driver actually sends and receives them from the serial port.
@bignose