[icinga-checkins] icinga.org: icinga-nrpe/mfriedrich/fixes: use dup2() to explicitely reset std{in, out, err} to /dev/null #1834

git at icinga.org git at icinga.org
Sat Nov 12 14:43:51 CET 2011


Module: icinga-nrpe
Branch: mfriedrich/fixes
Commit: 331c1ab3cbaab6078c2a57acc3b1bbcdc499d8c9
URL:    https://git.icinga.org/?p=icinga-nrpe.git;a=commit;h=331c1ab3cbaab6078c2a57acc3b1bbcdc499d8c9

Author: Michael Friedrich <michael.friedrich at univie.ac.at>
Date:   Sat Nov 12 14:41:42 2011 +0100

use dup2() to explicitely reset std{in,out,err} to /dev/null #1834

refs #1834

---

 Changelog  |    2 +-
 THANKS     |    1 +
 src/nrpe.c |   36 +++++++++++++++++++++++++-----------
 3 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/Changelog b/Changelog
index d8d3704..7d4c77b 100644
--- a/Changelog
+++ b/Changelog
@@ -15,7 +15,7 @@ FIXES
 - fix undefined LOG_FTP and LOG_AUTHPRIV
 
 - fix SSL Lib detection on AIX
-
+- use dup2() to explicitely reset std{in,out,err} to /dev/null #1834
 
 
 2.12 - 03/10/2008
diff --git a/THANKS b/THANKS
index b09eb5e..267cd79 100644
--- a/THANKS
+++ b/THANKS
@@ -21,6 +21,7 @@ If we missed your name, let us know.
 * Ethan Galstad
 * Rene Klootwijk
 * Jacob L
+* Bret Lambert
 * Gerhard Lausser
 * Klas Lindfors
 * John Maag
diff --git a/src/nrpe.c b/src/nrpe.c
index fb600ec..d274dd4 100644
--- a/src/nrpe.c
+++ b/src/nrpe.c
@@ -98,7 +98,7 @@ int main(int argc, char **argv){
 #ifdef HAVE_SSL
 	DH *dh;
 	char seedfile[FILENAME_MAX];
-	int i,c;
+	int i,c,nullfd;
 #endif
 
 	/* set some environment variables */
@@ -268,8 +268,13 @@ int main(int argc, char **argv){
 		check_privileges();
 
 		/* redirect STDERR to /dev/null */
-		close(2);
-		open("/dev/null",O_WRONLY);
+		nullfd = open("/dev/null",O_WRONLY);
+		if (nullfd == -1) {
+			syslog(LOG_ERR, "Error: could not open /dev/null\n");
+			exit(STATE_CRITICAL);
+			}
+		dup2(nullfd, STDERR_FILENO);
+		close(nullfd);
 
 		/* handle the connection */
 		handle_connection(0);
@@ -281,15 +286,24 @@ int main(int argc, char **argv){
 		/* we're a daemon - set up a new process group */
 		setsid();
 
-		/* close standard file descriptors */
-		close(0);
-		close(1);
-		close(2);
-
 		/* redirect standard descriptors to /dev/null */
-		open("/dev/null",O_RDONLY);
-		open("/dev/null",O_WRONLY);
-		open("/dev/null",O_WRONLY);
+		nullfd = open("/dev/null",O_RDONLY);
+		if (nullfd == -1) {
+			syslog(LOG_ERR, "Error: could not open /dev/null\n");
+			exit(STATE_CRITICAL);
+			}
+		dup2(nullfd, STDIN_FILENO);
+		close(nullfd);
+
+		nullfd = open("/dev/null",O_WRONLY);
+		if (nullfd == -1) {
+			syslog(LOG_ERR, "Error: could not open /dev/null\n");
+			exit(STATE_CRITICAL);
+			}
+		dup2(nullfd, STDOUT_FILENO);
+		dup2(nullfd, STDERR_FILENO);
+		close(nullfd);
+
 
 		chdir("/");
 		/*umask(0);*/





More information about the icinga-checkins mailing list