saraedum / forsake

GNU General Public License v3.0
0 stars 0 forks source link

Work around missing /proc/ entries on macOS #6

Open saraedum opened 1 year ago

saraedum commented 1 year ago

On macOS we cannot get the file descriptors of another process on the filesystem unfortunately so redirecting stdio does not work.

In principle we could just create a pipe for stdin, stdout, stderr and then just cat everything through these pipes. However, ipython queries the features of stdout (or does it just read termcap and such?) so this won't work unless we can trick ipython and friends into detecting the features of the other end of the pipe.


If you're interested in making this work, I am happy to assist. However, since I don't use macOS myself, I am realistically not going to put the effort in to make this happen.

saraedum commented 1 year ago

There are projects out there that completely reparent processes under a different terminal. However, they also have trouble with macOS, see https://github.com/nelhage/reptyr/issues/99

saraedum commented 1 year ago

The pipe approach almost works with a little hackery in forwarding termios requests back and forth. However, there is a rather fundamental problem in signal handling when running something like IPython. While a command is running in IPython, when the client receives a SIGINT (C-c), we would like to just forward it to the forked process. However, morallly, we would have to signal the tcgetpgrp so the keyboard interrupt is raised in the foreground python process and not in IPython itself. However, we cannot tcsetpgrp in the forked process without allocating a pty first. So essentially, to make things happen we have to implement our own little terminal multiplexer here.

Instead of reinventing the wheel here, we should probably use something like https://github.com/prompt-toolkit/ptterm to get the details right. Then again, we probably don't want to pull in a dependency on prompt toolkit so maybe we should just take the tricks from https://github.com/prompt-toolkit/ptterm/blob/master/ptterm/backends/posix.py.