diff -ruN cfengine-2.0.5b1-old/configure.ac cfengine-2.0.5b1/configure.ac --- cfengine-2.0.5b1-old/configure.ac 2003-01-31 09:05:07.000000000 +0000 +++ cfengine-2.0.5b1/configure.ac 2003-02-04 15:24:01.000000000 +0000 @@ -333,7 +333,7 @@ AC_CHECK_FUNCS(uname gethostname realpath regcomp chflags) AC_CHECK_FUNCS(strstr putenv drand48 srand48 getaddrinfo) AC_CHECK_FUNCS(bcopy mkfifo statfs statvfs door) -AC_CHECK_FUNCS(sysinfo setsid strdup strrchr strerror snprintf) +AC_CHECK_FUNCS(sysinfo setsid strdup strrchr strerror snprintf sysconf) dnl ####################################################################### dnl Newer BSD systems don't have a compatible rtentry - use ortentry diff -ruN cfengine-2.0.5b1-old/src/Makefile.am cfengine-2.0.5b1/src/Makefile.am --- cfengine-2.0.5b1-old/src/Makefile.am 2003-01-29 09:25:39.000000000 +0000 +++ cfengine-2.0.5b1/src/Makefile.am 2003-02-04 13:47:46.000000000 +0000 @@ -126,7 +126,8 @@ granules.c \ eval.c \ item-ext.c \ - item.c + item.c \ + init.c cfexecd_LDADD = $(LEXLIB) @BERKELEY_DB_LIB@ @OPENSSL_LIB_LIB@ @@ -182,7 +183,8 @@ log.c \ popen.c \ patches.c \ - locks.c + locks.c \ + init.c cfenvgraph_SOURCES = \ cfenvgraph.c \ diff -ruN cfengine-2.0.5b1-old/src/cfenvd.c cfengine-2.0.5b1/src/cfenvd.c --- cfengine-2.0.5b1-old/src/cfenvd.c 2003-01-29 19:47:59.000000000 +0000 +++ cfengine-2.0.5b1/src/cfenvd.c 2003-02-04 13:47:46.000000000 +0000 @@ -553,13 +553,7 @@ if (!NO_FORK) { -#ifdef HAVE_SETSID - setsid(); -#endif - fclose (stdin); - fclose (stdout); - fclose (stderr); - closelog(); + ActAsDaemon(); } signal (SIGTERM,HandleSignal); /* Signal Handler */ diff -ruN cfengine-2.0.5b1-old/src/cfexecd.c cfengine-2.0.5b1/src/cfexecd.c --- cfengine-2.0.5b1-old/src/cfexecd.c 2003-02-04 13:47:45.000000000 +0000 +++ cfengine-2.0.5b1/src/cfexecd.c 2003-02-04 13:47:46.000000000 +0000 @@ -249,13 +249,7 @@ if (!NO_FORK) { -#ifdef HAVE_SETSID - setsid(); -#endif - fclose (stdin); - fclose (stdout); - fclose (stderr); - closelog(); + ActAsDaemon(); } signal(SIGINT,(void *)ExitCleanly); diff -ruN cfengine-2.0.5b1-old/src/cfservd.c cfengine-2.0.5b1/src/cfservd.c --- cfengine-2.0.5b1-old/src/cfservd.c 2003-02-04 13:47:45.000000000 +0000 +++ cfengine-2.0.5b1/src/cfservd.c 2003-02-04 13:47:46.000000000 +0000 @@ -553,23 +553,7 @@ if (!NO_FORK) { - int fd; -#ifdef HAVE_SETSID - setsid(); -#endif - fd = open("/dev/null", O_RDWR, 0); - fclose (stdin); - fclose (stdout); - fclose (stderr); - closelog(); - - if (fd != -1) - { - dup2(fd,STDIN_FILENO); - dup2(fd,STDOUT_FILENO); - dup2(fd,STDERR_FILENO); - close(fd); - } + ActAsDaemon(); } signal(SIGINT,(void*)ExitCleanly); diff -ruN cfengine-2.0.5b1-old/src/crypto.c cfengine-2.0.5b1/src/crypto.c --- cfengine-2.0.5b1-old/src/crypto.c 2002-08-23 08:03:19.000000000 +0100 +++ cfengine-2.0.5b1/src/crypto.c 2003-02-04 13:47:46.000000000 +0000 @@ -34,6 +34,43 @@ #include "cf.extern.h" #include "../pub/global.h" +/**********************************************************************/ + +void RandomSeed() + +{ static unsigned char digest[EVP_MAX_MD_SIZE+1]; + struct stat statbuf; + +/* Use the system database as the entropy source for random numbers */ + +Debug("RandomSeed() work directory is %s\n",VLOGDIR); + +snprintf(VBUFF,bufsize,"%s/randseed",VLOGDIR); + + if (stat(VBUFF,&statbuf) == -1) + { + snprintf(AVDB,bufsize,"%s/%s",WORKDIR,AVDB_FILE); + } + else + { + strcpy(AVDB,VBUFF); + } + +Verbose("Looking for a source of entropy in %s\n",AVDB); + +if (!RAND_load_file(AVDB,-1)) + { + snprintf(OUTPUT,bufsize,"Could not read sufficient randomness from %s\n",AVDB); + CfLog(cfverbose,OUTPUT,""); + } + +while (!RAND_status()) + { + MD5Random(digest); + RAND_seed((void *)digest,16); + } +} + /*********************************************************************/ void LoadSecretKeys() diff -ruN cfengine-2.0.5b1-old/src/init.c cfengine-2.0.5b1/src/init.c --- cfengine-2.0.5b1-old/src/init.c 2003-01-30 14:44:49.000000000 +0000 +++ cfengine-2.0.5b1/src/init.c 2003-02-04 13:48:58.000000000 +0000 @@ -96,43 +96,6 @@ /**********************************************************************/ -void RandomSeed() - -{ static unsigned char digest[EVP_MAX_MD_SIZE+1]; - struct stat statbuf; - -/* Use the system database as the entropy source for random numbers */ - -Debug("RandomSeed() work directory is %s\n",VLOGDIR); - -snprintf(VBUFF,bufsize,"%s/randseed",VLOGDIR); - - if (stat(VBUFF,&statbuf) == -1) - { - snprintf(AVDB,bufsize,"%s/%s",WORKDIR,AVDB_FILE); - } - else - { - strcpy(AVDB,VBUFF); - } - -Verbose("Looking for a source of entropy in %s\n",AVDB); - -if (!RAND_load_file(AVDB,-1)) - { - snprintf(OUTPUT,bufsize,"Could not read sufficient randomness from %s\n",AVDB); - CfLog(cfverbose,OUTPUT,""); - } - -while (!RAND_status()) - { - MD5Random(digest); - RAND_seed((void *)digest,16); - } -} - -/**********************************************************************/ - void SetSignals() @@ -163,3 +126,37 @@ } } +/**********************************************************************/ + +void ActAsDaemon(void) +{ + int fd, maxfd; +#ifdef HAVE_SETSID + setsid(); +#endif + closelog(); + fflush(NULL); + fd = open("/dev/null", O_RDWR, 0); + if (fd != -1) + { + dup2(fd,STDIN_FILENO); + dup2(fd,STDOUT_FILENO); + dup2(fd,STDERR_FILENO); + if (fd > STDERR_FILENO) close(fd); + } + +#ifdef HAVE_SYSCONF + maxfd = sysconf(_SC_OPEN_MAX); +#else +# ifdef _POXIX_OPEN_MAX + maxfd = _POSIX_OPEN_MAX; +# else + maxfd = 1024; +# endif +#endif + +for (fd=STDERR_FILENO+1; fd