Package: openssh-client
Version: 3.9p1-3.1
Tags: experimental patch

When ssh -f is used, ssh forks into the background.  However, it's then
difficult to kill that backgrounded ssh automatically, from other scripts.
Thet attached patch tells ssh to write a pid file.  This happens irregardless
of whether -f is used, however.

This makes my ssh -MS wrapper more robust(ssh is buggy when -MNS pipe is used,
but not -f, and then you later reconnect to that master; the first slave works
fine, but subsequent slaves deadlock).
diff -u openssh-3.9p1/readconf.c openssh-3.9p1/readconf.c
--- openssh-3.9p1/readconf.c
+++ openssh-3.9p1/readconf.c
@@ -107,7 +107,7 @@
        oAddressFamily, oGssAuthentication, oGssDelegateCreds,
        oServerAliveInterval, oServerAliveCountMax, oIdentitiesOnly,
        oSendEnv, oControlPath, oControlMaster,
-       oProtocolKeepAlives, oSetupTimeOut,
+       oProtocolKeepAlives, oSetupTimeOut, oPidFile,
        oDeprecated, oUnsupported
 } OpCodes;
 
@@ -200,6 +200,7 @@
        { "controlmaster", oControlMaster },
        { "protocolkeepalives", oProtocolKeepAlives },
        { "setuptimeout", oSetupTimeOut },
+       { "pidfile", oPidFile },
        { NULL, oBadOption }
 };
 
@@ -782,6 +783,10 @@
                intptr = &options->setuptimeout;
                goto parse_int;
 
+       case oPidFile:
+               charptr = &options->pidfile;
+               goto parse_string;
+
        case oDeprecated:
                debug("%s line %d: Deprecated option \"%s\"",
                    filename, linenum, keyword);
@@ -926,6 +931,7 @@
        options->num_send_env = 0;
        options->control_path = NULL;
        options->control_master = -1;
+       options->pidfile = NULL;
 }
 
 /*
diff -u openssh-3.9p1/readconf.h openssh-3.9p1/readconf.h
--- openssh-3.9p1/readconf.h
+++ openssh-3.9p1/readconf.h
@@ -112,6 +112,7 @@
 
        char    *control_path;
        int     control_master;
+       char    *pidfile;
 }       Options;
 
 
diff -u openssh-3.9p1/ssh.1 openssh-3.9p1/ssh.1
--- openssh-3.9p1/ssh.1
+++ openssh-3.9p1/ssh.1
@@ -694,6 +694,7 @@
 .It NoHostAuthenticationForLocalhost
 .It NumberOfPasswordPrompts
 .It PasswordAuthentication
+.It PidFile
 .It Port
 .It PreferredAuthentications
 .It Protocol
diff -u openssh-3.9p1/ssh.c openssh-3.9p1/ssh.c
--- openssh-3.9p1/ssh.c
+++ openssh-3.9p1/ssh.c
@@ -870,6 +870,21 @@
        }
 }
 
+static void
+make_pidfile()
+{
+       /* Create pid file as requested. */
+       if (options.pidfile) {
+               FILE *pidfile = fopen(options.pidfile, "w");
+               if (!pidfile)
+                       fatal("open pidfile failed: %.200s", strerror(errno));
+               if (fprintf(pidfile, "%i", getpid()) < 0)
+                       fatal("write pidfile failed: %.200s", strerror(errno));
+               if (fclose(pidfile))
+                       fatal("close pidfile failed: %.200s", strerror(errno));
+       }
+}
+
 static int
 ssh_session(void)
 {
@@ -1002,6 +1017,8 @@
                packet_write_wait();
        }
 
+       make_pidfile();
+
        /* Enter the interactive session. */
        return client_loop(have_tty, tty_flag ?
            options.escape_char : SSH_ESCAPECHAR_NONE, 0);
@@ -1173,6 +1190,8 @@
                if (daemon(1, 1) < 0)
                        fatal("daemon() failed: %.200s", strerror(errno));
 
+       make_pidfile();
+
        return client_loop(tty_flag, tty_flag ?
            options.escape_char : SSH_ESCAPECHAR_NONE, id);
 }
diff -u openssh-3.9p1/ssh_config.5 openssh-3.9p1/ssh_config.5
--- openssh-3.9p1/ssh_config.5
+++ openssh-3.9p1/ssh_config.5
@@ -523,6 +523,8 @@
 .Dq no .
 The default is
 .Dq yes .
+.It Cm PidFile
+Specifies a file to write the pid of the process to.
 .It Cm Port
 Specifies the port number to connect on the remote host.
 Default is 22.
diff -u openssh-3.9p1/debian/changelog openssh-3.9p1/debian/changelog
--- openssh-3.9p1/debian/changelog
+++ openssh-3.9p1/debian/changelog
@@ -1,3 +1,10 @@
+openssh (1:3.9p1-3.1) experimental; urgency=low
+
+  * NMU.
+  * Add PidFile option; useful when -f is given.
+
+ -- Adam Heath <[EMAIL PROTECTED]>  Wed, 06 Apr 2005 12:37:30 -0500
+
 openssh (1:3.9p1-3) experimental; urgency=low
 
   * Explain how to run sshd from inittab in README.Debian (closes: #147360).

Reply via email to