Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prohibit reading from /dev/tty

Tools like sudo read from /dev/tty to read a password.

I would like to avoid this.

The subprocess should not be able to read /dev/tty. The subprocess should fail immediately instead of waiting for input for ever.

I am using the subprocess module of Python. The subprocess should fail if it tries to read from /dev/tty.

Remember: The tool sudo is just an example. A fancy command line argument to sudo does not solve my problem. This should work for all linux command line tools.

Question: How to make any tool fail as soon as it wants to read from /dev/tty (called via the subprocess module of Python)?

Background: This is a normal linux user process, not root.

like image 436
guettli Avatar asked Jun 27 '26 02:06

guettli


1 Answers

Since python3.2 Popen takes an argument start_new_session which will cause the executed process to be started detached from the current controlling terminal by calling setsid() prior to executing the child process.

So all you should need is to start the process with start_new_session=True

like image 132
mata Avatar answered Jun 28 '26 17:06

mata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!