=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/ssh/sshd.c,v retrieving revision 1.477 retrieving revision 1.478 diff -u -r1.477 -r1.478 --- src/usr.bin/ssh/sshd.c 2016/11/29 03:54:50 1.477 +++ src/usr.bin/ssh/sshd.c 2016/11/30 00:28:31 1.478 @@ -1,4 +1,4 @@ -/* $OpenBSD: sshd.c,v 1.477 2016/11/29 03:54:50 dtucker Exp $ */ +/* $OpenBSD: sshd.c,v 1.478 2016/11/30 00:28:31 dtucker Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1995 Tatu Ylonen , Espoo, Finland @@ -1268,7 +1268,7 @@ struct ssh *ssh = NULL; extern char *optarg; extern int optind; - int r, opt, i, j, on = 1; + int r, opt, i, j, on = 1, already_daemon; int sock_in = -1, sock_out = -1, newsock = -1; const char *remote_ip; int remote_port; @@ -1664,11 +1664,12 @@ log_init(__progname, options.log_level, options.log_facility, log_stderr); /* - * If not in debugging mode, and not started from inetd, disconnect - * from the controlling terminal, and fork. The original process - * exits. + * If not in debugging mode, not started from inetd and not already + * daemonized (eg re-exec via SIGHUP), disconnect from the controlling + * terminal, and fork. The original process exits. */ - if (!(debug_flag || inetd_flag || no_daemon_flag)) { + already_daemon = daemonized(); + if (!(debug_flag || inetd_flag || no_daemon_flag || already_daemon)) { if (daemon(0, 0) < 0) fatal("daemon() failed: %.200s", strerror(errno)); @@ -1701,7 +1702,7 @@ * Write out the pid file after the sigterm handler * is setup and the listen sockets are bound */ - if (options.pid_file != NULL && !debug_flag) { + if (options.pid_file != NULL && !debug_flag && !already_daemon) { FILE *f = fopen(options.pid_file, "w"); if (f == NULL) {