]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/ngircd.c
Merge branch 'ServerMode'
[ngircd-alex.git] / src / ngircd / ngircd.c
index 74a998800f364376a87efb290cd2d0dc030a2ab4..2135ec4d708ab72eed7141237725c56300423b95 100644 (file)
@@ -60,6 +60,8 @@ static void Pidfile_Delete PARAMS(( void ));
 
 static void Fill_Version PARAMS(( void ));
 
+static void Random_Init PARAMS(( void ));
+
 static void Setup_FDStreams PARAMS(( int fd ));
 
 static bool NGIRCd_Init PARAMS(( bool ));
@@ -262,6 +264,8 @@ main( int argc, const char *argv[] )
                NGIRCd_SignalRestart = false;
                NGIRCd_SignalQuit = false;
 
+               Random_Init();
+
                /* Initialize modules, part I */
                Log_Init( ! NGIRCd_NoDaemon );
                Conf_Init( );
@@ -289,8 +293,6 @@ main( int argc, const char *argv[] )
                        exit(1);
                }
 
-               srandom(getpid());
-
                /* Create protocol and server identification. The syntax
                 * used by ngIRCd in PASS commands and the known "extended
                 * flags" are described in doc/Protocol.txt. */
@@ -564,6 +566,37 @@ NGIRCd_getNobodyID(uid_t *uid, gid_t *gid )
 } /* NGIRCd_getNobodyID */
 
 
+static bool
+Random_Init_Kern(const char *file)
+{
+       unsigned int seed;
+       bool ret = false;
+       int fd = open(file, O_RDONLY);
+       if (fd >= 0) {
+               if (read(fd, &seed, sizeof(seed)) == sizeof(seed))
+                       ret = true;
+               close(fd);
+               srand(seed);
+       }
+       return ret;
+}
+
+/**
+ * Initialize libc rand(3) number generator
+ */
+static void
+Random_Init(void)
+{
+       if (Random_Init_Kern("/dev/urandom"))
+               return;
+       if (Random_Init_Kern("/dev/random"))
+               return;
+       if (Random_Init_Kern("/dev/arandom"))
+               return;
+       srand(rand() ^ (unsigned)getpid() ^ (unsigned)time(NULL));
+}
+
+
 /**
  * Initialize ngIRCd daemon.
  *
@@ -673,10 +706,8 @@ NGIRCd_Init( bool NGIRCd_NoDaemon )
 
                /* Detach stdin, stdout and stderr */
                Setup_FDStreams(fd);
-               if (fd > 2) {
+               if (fd > 2)
                        close(fd);
-                       fd = -1;
-               }
        }
        pid = getpid();