It's safer to close stdin/out/err by replacing their fd with /dev/null.

Otherwise the next opened fds are 0/1/2, and any code writing to
stdout/stderr might break things by writing to an unintentional fd.

Signed-off-by: Timo Sirainen <tss@iki.fi>
This commit is contained in:
Timo Sirainen
2012-08-11 23:13:54 +03:00
committed by Keith Winstein
parent b245ed00ca
commit 9cade23616
+15 -3
View File
@@ -37,6 +37,7 @@
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
@@ -391,9 +392,20 @@ int run_server( const char *desired_ip, const char *desired_port,
/* close file descriptors */
if ( !verbose ) {
/* Necessary to properly detach on old versions of sshd (e.g. RHEL/CentOS 5.0). */
fclose( stdin );
fclose( stdout );
fclose( stderr );
int nullfd;
nullfd = open( "/dev/null", O_RDWR );
if ( nullfd == -1 ) {
perror( "dup2" );
exit( 1 );
}
if ( dup2 ( nullfd, STDIN_FILENO ) < 0 ||
dup2 ( nullfd, STDOUT_FILENO ) < 0 ||
dup2 ( nullfd, STDERR_FILENO ) < 0 ) {
perror( "dup2" );
exit( 1 );
}
}
char utmp_entry[ 64 ] = { 0 };