progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / rand / noise.c
index 2c30c13..c120e6f 100644 (file)
@@ -35,6 +35,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 
 #include <sys/types.h>
 #include <sys/time.h>
 /*----- Magical numbers ---------------------------------------------------*/
 
 #define NOISE_KIDLIFE 100000           /* @noise_filter@ child lifetime */
-#define MILLION 1000000                        /* One million */
+
+#if HAVE_CLOCK_GETTIME && _POSIX_TIMERS > 0
+#  define TIMESTRUCT timespec
+#  define tv_SEC tv_sec
+#  define tv_FRAC tv_nsec
+#  define TIMERES 1000000000
+#  if _POSIX_MONOTONIC_CLOCK > 0
+#    define GETTIME(tv) (clock_gettime(CLOCK_MONOTONIC, (tv)))
+#  else
+#    define GETTIME(tv) (clock_gettime(CLOCK_REALTIME, (tv)))
+#  endif
+#  define TOTIMEVAL(tv, xx)                                            \
+       ((tv)->tv_sec = (xx)->tv_sec,                                   \
+        (tv)->tv_usec = ((xx)->tv_nsec + 500)/1000)
+#else
+#  define TIMESTRUCT timeval
+#  define tv_SEC tv_sec
+#  define tv_FRAC tv_usec
+#  define TIMERES 1000000
+#  define GETTIME(tv) (gettimeofday((tv), 0))
+#  define TOTIMEVAL(tv, xx) (*(tv) = *(xx))
+#endif
 
 /*----- Noise source definition -------------------------------------------*/
 
@@ -106,20 +128,20 @@ static int bitcount(unsigned long x)
 /* --- @timer@ --- *
  *
  * Arguments:  @rand_pool *r@ = pointer to randomness pool
- *             @struct timeval *tv@ = pointer to time block
+ *             @const struct TIMESTRUCT *tv@ = pointer to time block
  *
  * Returns:    Nonzero if some randomness was contributed.
  *
  * Use:                Low-level timer contributor.
  */
 
-static int timer(rand_pool *r, struct timeval *tv)
+static int timer(rand_pool *r, const struct TIMESTRUCT *tv)
 {
   unsigned long x, d, dd;
   int de, dde;
   int ret;
 
-  x = tv->tv_usec + MILLION * tv->tv_sec;
+  x = tv->tv_FRAC + TIMERES*tv->tv_SEC;
   d = x ^ noise_last;
   dd = d ^ noise_diff;
   noise_last = x;
@@ -146,9 +168,8 @@ static int timer(rand_pool *r, struct timeval *tv)
 
 int noise_timer(rand_pool *r)
 {
-  struct timeval tv;
-  gettimeofday(&tv, 0);
-  return (timer(r, &tv));
+  struct TIMESTRUCT tv;
+  GETTIME(&tv); return (timer(r, &tv));
 }
 
 /* --- @noise_devrandom@ --- *
@@ -196,7 +217,7 @@ int noise_devrandom(rand_pool *r)
   /* --- OpenBSD-flavoured shinies --- */
 
   while (n < sizeof(buf)) {
-    nn = sizeof(buf) - nn;
+    nn = sizeof(buf) - n;
     if (nn > 256) nn = 256;
     if (getentropy(buf + n, nn)) break;
     n += nn;
@@ -319,6 +340,7 @@ int noise_filter(rand_pool *r, int good, const char *c)
   pid_t kid;
   int fd[2];
   struct timeval dead;
+  struct TIMESTRUCT now;
   int ret = 0;
   struct noisekid nk = { 0 };
   sel_state sel;
@@ -331,8 +353,8 @@ int noise_filter(rand_pool *r, int good, const char *c)
 
   /* --- Remember when this business started --- */
 
-  gettimeofday(&dead, 0);
-  timer(r, &dead);
+  GETTIME(&now); timer(r, &now);
+  TOTIMEVAL(&dead, &now);
 
   /* --- Create a pipe --- */
 
@@ -366,16 +388,15 @@ int noise_filter(rand_pool *r, int good, const char *c)
     /* --- Play games with uids --- */
 
     if (noise_gid != NOISE_NOSETGID) {
-      setgid(noise_gid);
-      setegid(noise_gid);
+      if (setgid(noise_gid) || setegid(noise_gid)
 #ifdef HAVE_SETGROUPS
-      setgroups(1, &noise_gid);
+         || setgroups(1, &noise_gid)
 #endif
+       ) _exit(127);
     }
 
     if (noise_uid != NOISE_NOSETUID) {
-      setuid(noise_uid);
-      seteuid(noise_uid);
+      if (setuid(noise_uid) || seteuid(noise_uid)) _exit(127);
     }
 
     /* --- Start the process up --- */