Hi,
Ssh -f forks itself in the background. Very usefull if you would like to
e.g. tunnel munin over ssh. Now it's tricky to terminate one process if
you have multiple running.
It seems that ssh currently (looked at 5.1p1) has no write-pid-to-file
functionality
So I implemented a patch which do so. Tested it a little and it seems to
work. Hopefully it is of any use in my form or inspires the developers
to implement this kind of functionality in the ssh distribution.
Url:
http://www.vanheusden.com/Linux/openssh-5.1p1_writepidfile.diff.gz
And the patch itself:
diff -uNrBbd openssh-5.1p1.org/ssh.c openssh-5.1p1.new/ssh.c
--- openssh-5.1p1.org/ssh.c 2008-07-04 04:53:50.000000000 +0200
+++ openssh-5.1p1.new/ssh.c 2009-02-18 21:15:26.000000000 +0100
@@ -140,6 +140,9 @@
/* optional user configfile */
char *config = NULL;
+/* file to write the pid in after daemonizing */
+char *pid_file = NULL;
+
/*
* Name of the host we are connecting to. This is the name given on the
* command line, or the HostName specified for the user-supplied name in a
@@ -185,6 +188,7 @@
" [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p
port]\n"
" [-R [bind_address:]port:host:hostport] [-S ctl_path]\n"
" [-w local_tun[:remote_tun]] [user@]hostname [command]\n"
+" [-z pid_file]\n"
);
exit(255);
}
@@ -197,6 +201,26 @@
void muxclient(const char *);
void muxserver_listen(void);
+int write_pid_file(char *pid_file)
+{
+ char buffer[16];
+ int len;
+ int fd = open(pid_file, O_WRONLY | O_EXCL | O_CREAT, 0644);
+ if (fd == -1)
+ return -1;
+
+ len = snprintf(buffer, sizeof(buffer), "%d\n", getpid());
+ if (write(fd, buffer, len) != len)
+ {
+ close(fd);
+ return -1;
+ }
+
+ close(fd);
+
+ return 0;
+}
+
/*
* Main program for the ssh client.
*/
@@ -271,9 +295,12 @@
host = NULL;
again:
- while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvx"
+ while ((opt = getopt(ac, av, "1246ab:c:e:fgi:kl:m:no:p:qstvxz:"
"ACD:F:I:KL:MNO:PR:S:TVw:XY")) != -1) {
switch (opt) {
+ case 'z':
+ pid_file = optarg;
+ break;
case '1':
options.protocol = SSH_PROTO_1;
break;
@@ -833,6 +860,9 @@
if (options.control_path != NULL && muxserver_sock != -1)
unlink(options.control_path);
+ if (pid_file)
+ unlink(pid_file);
+
/*
* Send SIGHUP to proxy command if used. We don't wait() in
* case it hangs and instead rely on init to reap the child
@@ -1080,6 +1110,12 @@
fatal("daemon() failed: %.200s", strerror(errno));
}
+ if (pid_file)
+ {
+ if (write_pid_file(pid_file) == -1)
+ fatal("write_pid(%s) failed: %.200s", pid_file, strerror(errno));
+ }
+
/*
* If a command was specified on the command line, execute the
* command now. Otherwise request the server to start a shell.
@@ -1223,6 +1259,12 @@
fatal("daemon() failed: %.200s", strerror(errno));
}
+ if (pid_file)
+ {
+ if (write_pid_file(pid_file) == -1)
+ fatal("write_pid(%s) failed: %.200s", pid_file, strerror(errno));
+ }
+
return client_loop(tty_flag, tty_flag ?
options.escape_char : SSH_ESCAPECHAR_NONE, id);
}
Folkert van Heusden
--
MultiTail is a versatile tool for watching logfiles and output of
commands. Filtering, coloring, merging, diff-view, etc.
http://www.vanheusden.com/multitail/
----------------------------------------------------------------------
Phone: +31-6-41278122, PGP-key: 1F28D8AE, www.vanheusden.com