summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordtucker@openbsd.org <dtucker@openbsd.org>2016-11-30 00:28:31 +0000
committerDamien Miller <djm@mindrot.org>2016-11-30 19:44:01 +1100
commit7fc4766ac78abae81ee75b22b7550720bfa28a33 (patch)
tree36cc862d4d493587327dc91f89cf96b22e8230ee
parentc9f880c195c65f1dddcbc4ce9d6bfea7747debcc (diff)
upstream commit
On startup, check to see if sshd is already daemonized and if so, skip the call to daemon() and do not rewrite the PidFile. This means that when sshd re-execs itself on SIGHUP the process ID will no longer change. Should address bz#2641. ok djm@ markus@. Upstream-ID: 5ea0355580056fb3b25c1fd6364307d9638a37b9
-rw-r--r--misc.c20
-rw-r--r--misc.h3
-rw-r--r--sshd.c15
3 files changed, 29 insertions, 9 deletions
diff --git a/misc.c b/misc.c
index 07d4179e4..65c9222aa 100644
--- a/misc.c
+++ b/misc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: misc.c,v 1.106 2016/10/23 22:04:05 dtucker Exp $ */ 1/* $OpenBSD: misc.c,v 1.107 2016/11/30 00:28:31 dtucker Exp $ */
2/* 2/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved. 3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved. 4 * Copyright (c) 2005,2006 Damien Miller. All rights reserved.
@@ -1251,3 +1251,21 @@ bind_permitted(int port, uid_t uid)
1251 return 0; 1251 return 0;
1252 return 1; 1252 return 1;
1253} 1253}
1254
1255/* returns 1 if process is already daemonized, 0 otherwise */
1256int
1257daemonized(void)
1258{
1259 int fd;
1260
1261 if ((fd = open(_PATH_TTY, O_RDONLY | O_NOCTTY)) >= 0) {
1262 close(fd);
1263 return 0; /* have controlling terminal */
1264 }
1265 if (getppid() != 1)
1266 return 0; /* parent is not init */
1267 if (getsid(0) != getpid())
1268 return 0; /* not session leader */
1269 debug3("already daemonized");
1270 return 1;
1271}
diff --git a/misc.h b/misc.h
index 3578e8ef5..c242f9011 100644
--- a/misc.h
+++ b/misc.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: misc.h,v 1.60 2016/10/23 22:04:05 dtucker Exp $ */ 1/* $OpenBSD: misc.h,v 1.61 2016/11/30 00:28:31 dtucker Exp $ */
2 2
3/* 3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
@@ -31,6 +31,7 @@ struct Forward {
31 31
32int forward_equals(const struct Forward *, const struct Forward *); 32int forward_equals(const struct Forward *, const struct Forward *);
33int bind_permitted(int, uid_t); 33int bind_permitted(int, uid_t);
34int daemonized(void);
34 35
35/* Common server and client forwarding options. */ 36/* Common server and client forwarding options. */
36struct ForwardOptions { 37struct ForwardOptions {
diff --git a/sshd.c b/sshd.c
index ce4a493ef..fafcd3400 100644
--- a/sshd.c
+++ b/sshd.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: sshd.c,v 1.477 2016/11/29 03:54:50 dtucker Exp $ */ 1/* $OpenBSD: sshd.c,v 1.478 2016/11/30 00:28:31 dtucker Exp $ */
2/* 2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
@@ -1343,7 +1343,7 @@ main(int ac, char **av)
1343 struct ssh *ssh = NULL; 1343 struct ssh *ssh = NULL;
1344 extern char *optarg; 1344 extern char *optarg;
1345 extern int optind; 1345 extern int optind;
1346 int r, opt, i, j, on = 1; 1346 int r, opt, i, j, on = 1, already_daemon;
1347 int sock_in = -1, sock_out = -1, newsock = -1; 1347 int sock_in = -1, sock_out = -1, newsock = -1;
1348 const char *remote_ip; 1348 const char *remote_ip;
1349 int remote_port; 1349 int remote_port;
@@ -1802,11 +1802,12 @@ main(int ac, char **av)
1802 log_init(__progname, options.log_level, options.log_facility, log_stderr); 1802 log_init(__progname, options.log_level, options.log_facility, log_stderr);
1803 1803
1804 /* 1804 /*
1805 * If not in debugging mode, and not started from inetd, disconnect 1805 * If not in debugging mode, not started from inetd and not already
1806 * from the controlling terminal, and fork. The original process 1806 * daemonized (eg re-exec via SIGHUP), disconnect from the controlling
1807 * exits. 1807 * terminal, and fork. The original process exits.
1808 */ 1808 */
1809 if (!(debug_flag || inetd_flag || no_daemon_flag)) { 1809 already_daemon = daemonized();
1810 if (!(debug_flag || inetd_flag || no_daemon_flag || already_daemon)) {
1810 1811
1811 if (daemon(0, 0) < 0) 1812 if (daemon(0, 0) < 0)
1812 fatal("daemon() failed: %.200s", strerror(errno)); 1813 fatal("daemon() failed: %.200s", strerror(errno));
@@ -1840,7 +1841,7 @@ main(int ac, char **av)
1840 * Write out the pid file after the sigterm handler 1841 * Write out the pid file after the sigterm handler
1841 * is setup and the listen sockets are bound 1842 * is setup and the listen sockets are bound
1842 */ 1843 */
1843 if (options.pid_file != NULL && !debug_flag) { 1844 if (options.pid_file != NULL && !debug_flag && !already_daemon) {
1844 FILE *f = fopen(options.pid_file, "w"); 1845 FILE *f = fopen(options.pid_file, "w");
1845 1846
1846 if (f == NULL) { 1847 if (f == NULL) {