Bugzilla – Attachment 99 Details for
Bug 253
Patch to write process ID to a file when ssh sets itself into daemon mode
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Patch to write pid out to a file in daemon mode for ssh
openssh-3.2.2p1.withpid.diff (text/plain), 4.11 KB, created by
Evan Prodromou
on 2002-05-21 05:04:20 AEST
(
hide
)
Description:
Patch to write pid out to a file in daemon mode for ssh
Filename:
MIME Type:
Creator:
Evan Prodromou
Created:
2002-05-21 05:04:20 AEST
Size:
4.11 KB
patch
obsolete
>diff -Naur openssh-3.2.2p1/ssh.1 openssh-3.2.2p1.withpid/ssh.1 >--- openssh-3.2.2p1/ssh.1 Wed May 15 21:36:46 2002 >+++ openssh-3.2.2p1.withpid/ssh.1 Mon May 20 18:22:22 2002 >@@ -51,6 +51,7 @@ > .Op Fl afgknqstvxACNPTX1246 > .Op Fl b Ar bind_address > .Op Fl c Ar cipher_spec >+.Op Fl d Ar pidfile > .Op Fl e Ar escape_char > .Op Fl i Ar identity_file > .Op Fl l Ar login_name >@@ -421,6 +422,17 @@ > See > .Cm Ciphers > for more information. >+.It Fl d Ar pidfile >+Requests >+.Nm >+to write a file containing the process ID before going into the background. >+This implies >+.Fl n >+and >+.Fl f . >+This is useful to create tunnels to use in scripts, since they can >+then be killed with >+.Ic kill `cat pidfile` . > .It Fl e Ar ch|^ch|none > Sets the escape character for sessions with a pty (default: > .Ql ~ ) . >diff -Naur openssh-3.2.2p1/ssh.c openssh-3.2.2p1.withpid/ssh.c >--- openssh-3.2.2p1/ssh.c Tue Apr 23 11:09:46 2002 >+++ openssh-3.2.2p1.withpid/ssh.c Mon May 20 17:52:37 2002 >@@ -113,6 +113,14 @@ > int fork_after_authentication_flag = 0; > > /* >+ * Flag indicating that ssh should write its pid out to a file. Useful for >+ * fork()'d clients. >+ */ >+ >+int write_pid_flag = 0; >+char *pidfile = NULL; >+ >+/* > * General data structure for command line options and options configurable > * in configuration files. See readconf.h. > */ >@@ -180,6 +188,7 @@ > fprintf(stderr, " -P Don't allocate a privileged port.\n"); > fprintf(stderr, " -q Quiet; don't display any warning messages.\n"); > fprintf(stderr, " -f Fork into background after authentication.\n"); >+ fprintf(stderr, " -d file Write daemon pid to file (implies -f).\n"); > fprintf(stderr, " -e char Set escape character; ``none'' = disable (default: ~).\n"); > > fprintf(stderr, " -c cipher Select encryption algorithm\n"); >@@ -244,6 +253,8 @@ > static int ssh_session(void); > static int ssh_session2(void); > static void load_public_identity_files(void); >+static void write_pid(pid_t pid); >+static pid_t go_daemon(); > > /* > * Main program for the ssh client. >@@ -315,7 +326,7 @@ > > again: > while ((opt = getopt(ac, av, >- "1246ab:c:e:fgi:kl:m:no:p:qstvxACD:F:I:L:NPR:TVX")) != -1) { >+ "1246ab:c:d:e:fgi:kl:m:no:p:qstvxACD:F:I:L:NPR:TVX")) != -1) { > switch (opt) { > case '1': > options.protocol = SSH_PROTO_1; >@@ -336,6 +347,12 @@ > fork_after_authentication_flag = 1; > stdin_null_flag = 1; > break; >+ case 'd': >+ write_pid_flag = 1; >+ fork_after_authentication_flag = 1; >+ stdin_null_flag = 1; >+ pidfile = optarg; >+ break; > case 'x': > options.forward_x11 = 0; > break; >@@ -1001,8 +1018,7 @@ > > /* If requested, let ssh continue in the background. */ > if (fork_after_authentication_flag) >- if (daemon(1, 1) < 0) >- fatal("daemon() failed: %.200s", strerror(errno)); >+ go_daemon(); > > /* > * If a command was specified on the command line, execute the >@@ -1200,8 +1216,7 @@ > > /* If requested, let ssh continue in the background. */ > if (fork_after_authentication_flag) >- if (daemon(1, 1) < 0) >- fatal("daemon() failed: %.200s", strerror(errno)); >+ go_daemon(); > > return client_loop(tty_flag, tty_flag ? > options.escape_char : SSH_ESCAPECHAR_NONE, id); >@@ -1246,4 +1261,41 @@ > options.identity_files[i] = filename; > options.identity_keys[i] = public; > } >+} >+ >+/* We use this to go into daemon mode, rather than daemon(), because >+ * we want the parent to write out the pid file before exiting, to >+ * prevent any races for the pid file in scripts, etc. */ >+ >+static pid_t >+go_daemon() { >+ pid_t child_pid; >+ >+ child_pid = fork(); >+ >+ if (child_pid == -1) { >+ fatal("Couldn't fork child: %.200s", strerror(errno)); >+ } else if (child_pid == 0) { >+ setsid(); >+ } else { >+ if (write_pid_flag) >+ write_pid(child_pid); >+ exit(0); >+ } >+ >+ return child_pid; >+} >+ >+/* Simple function to write a pid to a file. */ >+ >+static void >+write_pid(pid_t pid) { >+ FILE* f; >+ f = fopen(pidfile, "wb"); /* no particular reason for "b", but sshd.c does it. */ >+ if (!f) { >+ fatal("Can't write pid file %.100s: %.200s", pidfile, strerror(errno)); >+ } else { >+ fprintf(f, "%u\n", (u_int) pid); >+ fclose(f); >+ } > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 253
: 99