Python TTY Management

I think I do not understand that the getty / agetty / mgetty program function is on the linux / unix machine. I can run the shell on tty with something like this:

TTY = '/dev/tty3' cpid = os.fork() if cpid == 0: os.closerange(0, 4) sys.stdin = open(TTY, 'r') sys.stdout = open(TTY, 'w') sys.stderr = open(TTY, 'w') os.execv(('/bin/bash',), ('bash',)) 

.. and if I switch to tty3, the shell will start, but some keystrokes are ignored / never sent to the shell. the shell knows that the TTY settings are incorrect, because bash will say something like "could not open tty, job control is disabled"

I know that the termios module has functions for changing TTY settings, which is what the 'tty' module uses, but I cannot find an example of how python correctly installs TTY and starts the shell. I feel that it should be something simple, but I don’t know where to look.

looking at the source for * etty programs did not help me - C looks like Greek to me: - /

Maybe they just do not need the right conditions? Has anyone replaced * etty with Python in the past and got an explanation they would like to share?

Thank you for your interest in my main question :)

+6
source share
2 answers

I see at least two things that you are missing - maybe more:

First, you must call setsid() in the child process after closing the old standard input / standard output and before opening a new TTY. This does two important things: it makes your new process the leader of a new session, and it disconnects it from its previous control terminal (just closing this terminal is not enough). This will mean that when you open a new tty, it will become the control terminal that you need.

Secondly, you need to set the TERM environment variable to match the new tty.

+4
source

You should take a look at the source of * tty * programs to see what they do.

I assume that they basically issue a bunch of ioctl commands to initialize the terminal in the mode that programs usually expect (for example, to enter, etc.). However, some of them may also ask for a username (and not a password, the login does this).

0
source

Source: https://habr.com/ru/post/887421/


All Articles