mktemp from lfs-utils

Robert Connolly robert at linuxfromscratch.org
Sun Apr 25 23:12:35 PDT 2004


I noticed we're using the --with-libc option, but it does nothing. Try 
--without-libc and the temp files are more random; it uses urandom. I'm not 
sure why glibc's mktemp functions are not random. Mktemp-1.5 is only working 
because of a gettimeofday calculation. Anyone want to verify this? Use strace 
on mktemp and see if its opening urandom. If its using libc it shouldn't open 
urandom and the tempfile will begin with a few XXX's, at least on my box. 
Anyway, this is the sysctl erandom patch for mktemp, the real one is sent to 
patches at . This only uses 1 syscall per use, no difference. Do not use the 
--with-libc configure option with this.

diff -Naur lfs-utils-0.5.orig/mktemp-1.5/random.c lfs-utils-0.5.frandom/
mktemp-1.5/random.c
--- lfs-utils-0.5.orig/mktemp-1.5/random.c      2001-10-01 00:21:34.00000000 
+0000
+++ lfs-utils-0.5.frandom/mktemp-1.5/random.c   2004-04-26 05:56:12.00000000 
+0000
@@ -26,6 +26,7 @@
  */

 #include "config.h"
+#include <sys/sysctl.h>

 #include <sys/types.h>
 #if defined(TIME_WITH_SYS_TIME) || defined(HAVE_SYS_TIME_H)
@@ -105,16 +106,17 @@
        SEED_T seed;
        struct timeval tv;

-#ifdef _PATH_RANDOM
-       int fd;
-
-       if ((fd = open(_PATH_RANDOM, O_RDONLY)) != -1 &&
-           read_loop(fd, &seed, sizeof(seed)) == sizeof(seed)) {
-               close(fd);
-               SRAND(seed);
-               return;
-       }
-#endif /* _PATH_RANDOM */
+        int i, mib[3];
+        size_t len;
+        mib[0] = CTL_KERN;
+        mib[1] = KERN_RANDOM;
+        mib[2] = RANDOM_ERANDOM;
+        len = 4;
+        for (i = 0; i < sizeof(seed) / 4; i++) {
+                if (sysctl(mib, 3, (char *)&((int *)seed)[i],
+                &len, NULL, 0) == -1)
+                break;
+        }

        /* Don't have /dev/random */
        /* XXX - check for gettimeofday() and use time() if none? */




More information about the hlfs-dev mailing list