Bug 253 - Patch to write process ID to a file when ssh sets itself into daemon mode
Summary: Patch to write process ID to a file when ssh sets itself into daemon mode
Status: CLOSED WONTFIX
Alias: None
Product: Portable OpenSSH
Classification: Unclassified
Component: ssh (show other bugs)
Version: -current
Hardware: ix86 Linux
: P2 enhancement
Assignee: OpenSSH Bugzilla mailing list
URL: http://evan.prodromou.san-francisco.c...
Keywords:
Depends on:
Blocks:
 
Reported: 2002-05-21 05:01 AEST by Evan Prodromou
Modified: 2006-10-07 11:35 AEST (History)
0 users

See Also:


Attachments
Patch to write pid out to a file in daemon mode for ssh (4.11 KB, patch)
2002-05-21 05:04 AEST, Evan Prodromou
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Evan Prodromou 2002-05-21 05:01:53 AEST
I find it necessary on occasion to write scripts that set up an ssh tunnel, do
some commands using that tunnel, and then tear down the tunnel. Tunnelling POP3
connections with fetchmail is a good example:

ssh -f -N -L 10110:mailhost:110 mailhost

fetchmail localhost

[tear down tunnel]

The -f option here is key, because you want to be sure that the tunnel is set up
before running fetchmail.

However, it's difficult to tear down the tunnel, since it's hard to find out
what the backgrounded ssh process's PID is. I've written a patch to allow a
command-line option, "-d pidfile", which writes the process ID of the fork()'d
child to a file, so the script can then just kill `cat pidfile`.

In order to do this, I had to unfold the calls to daemon() in ssh.c to actually
do the fork(), exit() and setsid() manually, since we want to be sure that the
pidfile exists before the parent exits, to avoid races. This works fine on
Linux, but I'm unsure if daemon() does any other fancy things on other operating
systems.

I'm also not sure what the proper behavior of the parent should be if it can't
write the pidfile. Currently, it does _not_ kill the child, although this might
be the Right Thing.

I also changed the manual page to include mention of the -d option.

The patch should apply cleanly by changing to the source directory and running:

patch -Np1 < /path/to/openssh-3.2.2p1.withpid.diff

I hope this is useful to someone else besides me.
Comment 1 Evan Prodromou 2002-05-21 05:04:20 AEST
Created attachment 99 [details]
Patch to write pid out to a file in daemon mode for ssh
Comment 2 Jeremy Peterson 2003-11-22 16:26:56 AEDT
I have the same problem. This patch would be tremendously helpful.

I guess this enhancement was lost?
Comment 3 Damien Miller 2005-02-09 15:58:28 AEDT
We have something functionally equivalent in the tree now. If you use the
"ControlPath" or -S options to ssh, then you can also use -O commands to find
out the pid of a running ssh or to request it to exit. E.g.

ssh -NnfS ~/.ssh/blah.ctl user@blah
(ssh authenticated and then forks)

Check the PID:
ssh -S ~/.ssh/blah.ctl -O check user@blah
Master running (pid=21068)

Terminate backgrounded ssh:
ssh -S ~/.ssh/blah.ctl -O exit user@blah
Exit request sent.
Comment 4 Darren Tucker 2006-10-07 11:35:24 AEST
Change all RESOLVED bug to CLOSED with the exception of the ones fixed post-4.4.