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:
committed by
Keith Winstein
parent
b245ed00ca
commit
9cade23616
@@ -37,6 +37,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
@@ -391,9 +392,20 @@ int run_server( const char *desired_ip, const char *desired_port,
|
|||||||
/* close file descriptors */
|
/* close file descriptors */
|
||||||
if ( !verbose ) {
|
if ( !verbose ) {
|
||||||
/* Necessary to properly detach on old versions of sshd (e.g. RHEL/CentOS 5.0). */
|
/* Necessary to properly detach on old versions of sshd (e.g. RHEL/CentOS 5.0). */
|
||||||
fclose( stdin );
|
int nullfd;
|
||||||
fclose( stdout );
|
|
||||||
fclose( stderr );
|
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 };
|
char utmp_entry[ 64 ] = { 0 };
|
||||||
|
|||||||
Reference in New Issue
Block a user