Patch to allow openssh-2.2.0-p1 to be started from /etc/inittab

Peter Eriksson peter at ifm.liu.se
Fri Jan 5 09:40:32 EST 2001


The following patch allows OpenSSH 2.2.0-p1 to be started (and managed)
from /etc/inittab (by "init") on systems which support that. This is
useful when you *really* want SSHD to always run since it will be
automatically restarted by "init" if it dies (and if "init" dies the
the systems dies :-).

I use a line (in /etc/inittab) like this on Solaris systems:

  ss:234:respawn:/usr/local/sbin/sshd

What the patch does is that it checks if it was started from process #1,
and then avoids the fork() to put itself into the background. It also
avoids writing to stderr and ignores errors from the setsid() call
(which will fail, atleast on Solaris 7 and 8).

- Peter Eriksson <peter at ifm.liu.se>


diff -c -r openssh-2.2.0p1/bsd-daemon.c openssh-2.2.0p1-pen1/bsd-daemon.c
*** openssh-2.2.0p1/bsd-daemon.c	Wed Aug 30 00:21:22 2000
--- openssh-2.2.0p1-pen1/bsd-daemon.c	Thu Jan  4 23:32:52 2001
***************
*** 70,74 ****
  	return (0);
  }
  
! #endif /* !HAVE_DAEMON */
  
--- 70,111 ----
  	return (0);
  }
  
! #endif
! 
! int
! sshd_daemon(nochdir, noclose)
! 	int nochdir, noclose;
! {
! 	int fd;
! 
! 	if (getppid() != 1)
! 	{
! 	    switch (fork()) {
! 	      case -1:
! 		return (-1);
! 	      case 0:
! 		break;
! 	      default:
! 		_exit(0);
! 	    }
! 	}
! 
! 	signal(SIGTTOU, SIG_IGN);
! 	signal(SIGTTIN, SIG_IGN);
! 	
! 	if (setsid() == -1 && getppid() != 1)
! 		return (-1);
! 
! 	if (!nochdir)
! 		(void)chdir("/");
! 
! 	if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
! 		(void)dup2(fd, STDIN_FILENO);
! 		(void)dup2(fd, STDOUT_FILENO);
! 		(void)dup2(fd, STDERR_FILENO);
! 		if (fd > 2)
! 			(void)close (fd);
! 	}
! 	return (0);
! }
  
diff -c -r openssh-2.2.0p1/bsd-daemon.h openssh-2.2.0p1-pen1/bsd-daemon.h
*** openssh-2.2.0p1/bsd-daemon.h	Fri Nov 19 05:32:34 1999
--- openssh-2.2.0p1-pen1/bsd-daemon.h	Thu Jan  4 23:31:35 2001
***************
*** 6,9 ****
--- 6,11 ----
  int daemon(int nochdir, int noclose);
  #endif /* !HAVE_DAEMON */
  
+ int sshd_daemon(int nochdir, int noclose);
+ 
  #endif /* _BSD_DAEMON_H */
diff -c -r openssh-2.2.0p1/sshd.c openssh-2.2.0p1-pen1/sshd.c
*** openssh-2.2.0p1/sshd.c	Tue Aug 29 02:05:50 2000
--- openssh-2.2.0p1-pen1/sshd.c	Thu Jan  4 23:30:46 2001
***************
*** 552,558 ****
  	log_init(av0,
  	    options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
  	    options.log_facility == -1 ? SYSLOG_FACILITY_AUTH : options.log_facility,
! 	    !silent && !inetd_flag);
  
  	/* Read server configuration options from the configuration file. */
  	read_server_config(&options, config_file_name);
--- 552,558 ----
  	log_init(av0,
  	    options.log_level == -1 ? SYSLOG_LEVEL_INFO : options.log_level,
  	    options.log_facility == -1 ? SYSLOG_FACILITY_AUTH : options.log_facility,
! 	    !silent && !inetd_flag && getppid() != 1);
  
  	/* Read server configuration options from the configuration file. */
  	read_server_config(&options, config_file_name);
***************
*** 633,639 ****
  	}
  
  	/* Initialize the log (it is reinitialized below in case we forked). */
! 	if (debug_flag && !inetd_flag)
  		log_stderr = 1;
  	log_init(av0, options.log_level, options.log_facility, log_stderr);
  
--- 633,639 ----
  	}
  
  	/* Initialize the log (it is reinitialized below in case we forked). */
! 	if (debug_flag && !inetd_flag && getppid() != 1)
  		log_stderr = 1;
  	log_init(av0, options.log_level, options.log_facility, log_stderr);
  
***************
*** 646,652 ****
  #ifdef TIOCNOTTY
  		int fd;
  #endif /* TIOCNOTTY */
! 		if (daemon(0, 0) < 0)
  			fatal("daemon() failed: %.200s", strerror(errno));
  
  		/* Disconnect from the controlling tty. */
--- 646,652 ----
  #ifdef TIOCNOTTY
  		int fd;
  #endif /* TIOCNOTTY */
! 		if (sshd_daemon(0, 0) < 0)
  			fatal("daemon() failed: %.200s", strerror(errno));
  
  		/* Disconnect from the controlling tty. */





More information about the openssh-unix-dev mailing list