3

I know that tty line discipline processes special characters (such as ^C, ^? and etc), but it is only about input from pty master device. But what about input from pty slave device? What tty line discipline does with input from pty slave device? Where can I find some code examples?

TwITe
  • 151

1 Answers1

0

Line discipline doesn't apply to pty master device, which is simply a interface to receive or send bytes to "generic tty".

A generic tty is basically a tty device (either /dev/ttyx or pty slave device) with a line discipline, which transform the bytes from another end (either kernel terminal emulator or pty master device) into certain signals or do line-buffer-editing

  • So the line discipline doesn't have its own device? But where bytes sent from pty master device goes to exactly? They can't be transferred directly to pty slave device, because in such case a program would see absolute special characters (like ^C) – TwITe Jul 12 '19 at 13:25
  • Line discipline is a property of the generic tty, not exposed as file handler by kenerl. Bytes sent from pty master to slave is firsly handled by line discipline, so slave don't always get the exactly same data send by master.@TwlTe https://unix.stackexchange.com/questions/116629/how-do-keyboard-input-and-text-output-work – 炸鱼薯条德里克 Jul 12 '19 at 15:00
  • Thanks, now I understand how tty line discipline intercepts data. But there is another question: how data being read from pts device? I've found write method: https://github.com/torvalds/linux/blob/master/drivers/tty/n_tty.c#L2301 which will call pty_write function of pts device: https://github.com/torvalds/linux/blob/master/drivers/tty/pty.c#L111.

    But I can't find read() method realization neither in tty line discipline nor in pts driver. So how scanf function behaves when trying to read from tty?

    – TwITe Jul 12 '19 at 15:51
  • Also, if tty line discipline is related to all tty devices, shouldn't it process ^C char when it comes to /dev/ttyN device and send SIGINT somewhere? Why master terminal gets ^C char or "^?"? – TwITe Jul 12 '19 at 16:25
  • ldisc is a property of a generic tty, if ttyN gets ^C byte, but ldisc sets to raw mode, then no signal would be send (usually in cooked mode SIGINT would be send to it's controlling session's foreground group, but you can always set ldisc to raw mode). Master gets whatever bytes send from slave, it doesn't have associated ldisc (or say the associated ldisc is always raw mode, so no effect). – 炸鱼薯条德里克 Jul 14 '19 at 02:46
  • yes, this really makes sense. But how can I check which mode is set of some tty device? stty shows me only settings of the current tty device (which is pts device on my gnome-terminal). I would want to know it for sure. my assumptions is that ldisc of pty master set to raw mode and ldisc of /dev/tty/N also set to raw mode, but I want to know it for sure – TwITe Jul 14 '19 at 08:12
  • Of course, ldisc of /dev/tty/N set to raw mode only when X windows system is loaded in current virtual terminal, because if x windows system is not loaded, we still need canonical mode in the virtual terminal – TwITe Jul 14 '19 at 08:27
  • most shell set it to raw because it really need raw mode to work, especially when they use GNU readline. If you want a full check, use ioctl syscall man7.org/linux/man-pages/man2/ioctl_tty.2.html Notice there's lots of states, not just raw/cooked binary opposition. X serve doesn't have much to do with this. At least I don't think Xorg set the state of ldisc – 炸鱼薯条德里克 Jul 14 '19 at 09:34
  • 1
    but /dev/tty2 should be set to raw mode while we are in X, otherwise special characters will be processed before they could have chance to be transferred to the terminal emulator, isn't it? So I think X does it when loads to the current Virtual Terminal – TwITe Jul 14 '19 at 10:04
  • X server's job is providing the concept of windows and send mouse or keyboard or other HID events to windows. It doesn't care about tty too much, it doesn't read bytes from tty device(although a tty might be its controlling terminal). For X-based userspace terminal emulator, they use pty, has nothing to do with ttyx. Kernel console is not exposed as kernel threads, but a process can have ttyx opened and read from it. – 炸鱼薯条德里克 Jul 14 '19 at 11:19
  • so you're saying that terminal emulator reads directly from /dev/ttyN device (and then writes bytes to its master device - ptmx), but not from X server? – TwITe Jul 14 '19 at 11:33
  • Didn't I just said has nothing to do with ttyx ? Also ptmx is just a multiplex interface. Each time you open it, you get an independent pty master. X-based terminal emulator of course receive events from X sever then write to the pty master which it's holding. – 炸鱼薯条德里克 Jul 14 '19 at 12:24
  • Then where from terminal emulator (e.g. xterm or gnome-terminal which is based on xterm) gets its input and why we need /dev/tty2 while we loaded into X windows? – TwITe Jul 14 '19 at 12:53
  • I emphasis that what I already said sometjing once, why you keep making the same mistake? 2. Gnome terminal doesn't based on xterm. 3. https://unix.stackexchange.com/questions/32884/which-virtual-terminal-is-a-given-x-process-running-on?r=SearchResults Possibily because X sever needs a controlling terminal, but I don't know why it has to be a Linux console
  • – 炸鱼薯条德里克 Jul 14 '19 at 13:07
  • Sorry, I missed part about pid events. So, we need a /dev/ttyN only when we loaded into the bash inside Virtual Terminal? Because if X is loaded it will handle pid events by itself. – TwITe Jul 14 '19 at 13:22
  • Bash can use pty (usually slave, as when you use userspace terminal emulator), or ttyN, it doesn't care, but it do need a tty as controlling terminal because it's a shell with job control functionality. I said HID, not pid, this reflects your attitude towards learning stuff and talking to people. TTY has nothing to do HID – 炸鱼薯条德里克 Jul 14 '19 at 13:32
  • Yes, HID, sorry. You really helped me understanding this stuff, thank you so much :) – TwITe Jul 14 '19 at 13:38