From 26af872a9c0b54164486209e6aea0d8138788f34 Mon Sep 17 00:00:00 2001 From: ballen4705 <ballen4705@4ea69e1a-61f1-4043-bf83-b5c94c648137> Date: Thu, 24 Oct 2002 15:03:15 +0000 Subject: [PATCH] Closed all file descriptors after forking and then reopened them pointing to /dev/null git-svn-id: https://smartmontools.svn.sourceforge.net/svnroot/smartmontools/trunk@114 4ea69e1a-61f1-4043-bf83-b5c94c648137 --- sm5/smartd.c | 27 ++++++++++++++++++++------- sm5/smartd.cpp | 27 ++++++++++++++++++++------- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/sm5/smartd.c b/sm5/smartd.c index 36262fa64..bcf3ca2be 100644 --- a/sm5/smartd.c +++ b/sm5/smartd.c @@ -37,23 +37,36 @@ #include "ataprint.h" extern const char *CVSid1, *CVSid2; -const char *CVSid3="$Id: smartd.c,v 1.25 2002/10/24 11:38:11 ballen4705 Exp $" +const char *CVSid3="$Id: smartd.c,v 1.26 2002/10/24 15:03:15 ballen4705 Exp $" CVSID1 CVSID4 CVSID7; int daemon_init(void){ pid_t pid; - + int i; + if ( (pid = fork()) < 0) // unable to fork! - return -1; + exit(1); else if (pid != 0) - // we are the parent process -- exit + // we are the parent process -- exit cleanly exit (0); // from here on, we are the child process - setsid (); - chdir("/"); + setsid(); + + // close any open file descriptors + for (i=getdtablesize();i>=0;--i) + close(i); + + // redirect any IO attempts to /dev/null + // open stdin + i=open("/dev/null",O_RDWR); + // stdout + dup(i); + // stderr + dup(i); umask(0); + chdir("/"); return(0); } @@ -88,7 +101,7 @@ void pout(char *fmt, ...){ void printhead(){ - printout(LOG_INFO,"smartd version %d.%d-%d - S.M.A.R.T. Daemon\n" + printout(LOG_INFO,"smartd version %d.%d-%d - S.M.A.R.T. Daemon.\n" "Home page is %s\n\n", RELEASE_MAJOR, RELEASE_MINOR,SMARTMONTOOLS_VERSION,PROJECTHOME); } diff --git a/sm5/smartd.cpp b/sm5/smartd.cpp index f7ac3107f..de8484437 100644 --- a/sm5/smartd.cpp +++ b/sm5/smartd.cpp @@ -37,23 +37,36 @@ #include "ataprint.h" extern const char *CVSid1, *CVSid2; -const char *CVSid3="$Id: smartd.cpp,v 1.25 2002/10/24 11:38:11 ballen4705 Exp $" +const char *CVSid3="$Id: smartd.cpp,v 1.26 2002/10/24 15:03:15 ballen4705 Exp $" CVSID1 CVSID4 CVSID7; int daemon_init(void){ pid_t pid; - + int i; + if ( (pid = fork()) < 0) // unable to fork! - return -1; + exit(1); else if (pid != 0) - // we are the parent process -- exit + // we are the parent process -- exit cleanly exit (0); // from here on, we are the child process - setsid (); - chdir("/"); + setsid(); + + // close any open file descriptors + for (i=getdtablesize();i>=0;--i) + close(i); + + // redirect any IO attempts to /dev/null + // open stdin + i=open("/dev/null",O_RDWR); + // stdout + dup(i); + // stderr + dup(i); umask(0); + chdir("/"); return(0); } @@ -88,7 +101,7 @@ void pout(char *fmt, ...){ void printhead(){ - printout(LOG_INFO,"smartd version %d.%d-%d - S.M.A.R.T. Daemon\n" + printout(LOG_INFO,"smartd version %d.%d-%d - S.M.A.R.T. Daemon.\n" "Home page is %s\n\n", RELEASE_MAJOR, RELEASE_MINOR,SMARTMONTOOLS_VERSION,PROJECTHOME); } -- GitLab