Wednesday, August 11, 2021

Re: Transferring ownership of SSH connection from process A to B, letting A quit nicely?

On Tue, Aug 10, 2021 at 12:13 PM mid <midsmail@protonmail.com> wrote:

> On Monday, August 9th, 2021 at 5:36 AM, Philip Guenther <
> guenther@gmail.com> wrote:
>
> > If you're 100% sure you have it right, then it should be easy to provide
> a
> > program that demonstrates
> > 1. passing an fd between processes
> > 2. using it successfully in the receiving process
> > 3. the sending process exiting
> > 4. attempts to us it failing the receiving process
>
> Not 100%, but I'm out of ideas, so here goes nothing.
>
> client.c (process A):
>
...

> Compiled with:
> cc -std=c99 -o server server.c
> cc -std=c99 -o client client.c
>
> `client` is also the shell of the user, but the results are the same if
> I call it from within a "real" shell, too.
>
> The server receives the correct FDs, and prints
> "Hello from the Server\n" correctly, too. But as soon as `client`
> exits, the SSH connection goes with it, instead of staying (as in,
> I get "Connection to localhost closed").
>

Your problems have nothing to do with fd passing but rather are around not
understanding how session management works.
The client is passing its stdin/stdout, which are either pipes or a
pseudo-tty connected to the ssh server and NOT the actual TCP socket
carrying the ssh connection. When the session leader process exits the
kernel will perform various cleanup operations (block tty access, send some
signals).

If you _really_ want to hack around in this area, you need to do a bunch of
reading and research. I recommend buying/borrowing a copy of
_Advanced_Programming_in_the_UNIX_Environment_ by W. Richard Stevens.


Philip Guenther

No comments:

Post a Comment