From 6b837b229fd02977d885a05c6fbb81cb2e0313f4 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sat, 13 Jul 2013 16:34:40 +0100 Subject: [PATCH] rand/noise.c (noise_filter): Use the machinery. Makes things a little more verbose but rather simpler. --- rand/noise.c | 75 ++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/rand/noise.c b/rand/noise.c index fb4c4531..3bece706 100644 --- a/rand/noise.c +++ b/rand/noise.c @@ -48,6 +48,7 @@ #include #include +#include #include #include "noise.h" @@ -228,13 +229,43 @@ void noise_setid(uid_t uid, gid_t gid) * This interface is Unix-specific. */ +struct noisekid { + rand_pool *r; + int good; + char buf[4096]; + int donep; + int ret; +}; + +static void kid_read(int fd, unsigned mode, void *p) +{ + struct noisekid *nk = p; + ssize_t sz; + int goodbits; + + noise_timer(nk->r); + if ((sz = read(fd, nk->buf, sizeof(nk->buf))) <= 0) + nk->donep = 1; + else { + goodbits = (sz * nk->good) / 128; + rand_add(nk->r, nk->buf, sz, goodbits); + nk->ret = 1; + } +} + +static void kid_dead(struct timeval *tv, void *p) + { struct noisekid *nk = p; nk->donep = 1; } + int noise_filter(rand_pool *r, int good, const char *c) { - char buf[4096]; pid_t kid; int fd[2]; struct timeval dead; int ret = 0; + struct noisekid nk = { 0 }; + sel_state sel; + sel_file sf; + sel_timer st; const char *env[] = { "PATH=/bin:/usr/bin:/sbin:/usr/sbin:/etc", 0 @@ -298,39 +329,13 @@ int noise_filter(rand_pool *r, int good, const char *c) /* --- Sort out my end of the deal --- */ close(fd[1]); - - /* --- Decide on the deadline --- */ - + sel_init(&sel); + nk.r = r; nk.good = good; TV_ADDL(&dead, &dead, 0, NOISE_KIDLIFE); - - /* --- Now read, and think --- */ - - for (;;) { - struct timeval now, diff; - fd_set rd; - - gettimeofday(&now, 0); - timer(r, &now); - if (TV_CMP(&now, >, &dead)) - break; - TV_SUB(&diff, &dead, &now); - - FD_ZERO(&rd); - FD_SET(fd[0], &rd); - - if (select(fd[0] + 1, &rd, 0, 0, &diff) < 0) - break; - if (FD_ISSET(fd[0], &rd)) { - ssize_t sz; - int goodbits; - - if ((sz = read(fd[0], buf, sizeof(buf))) <= 0) - break; - goodbits = (sz * good) / 128; - rand_add(r, buf, sz, goodbits); - ret = 1; - } - } + sel_initfile(&sel, &sf, fd[0], SEL_READ, kid_read, &nk); + sel_addfile(&sf); + sel_addtimer(&sel, &st, &dead, kid_dead, &nk); + while (!nk.donep && !sel_select(&sel)); /* --- We've finished with it: kill the child process --- * * @@ -340,11 +345,11 @@ int noise_filter(rand_pool *r, int good, const char *c) */ close(fd[0]); - BURN(buf); + BURN(nk.buf); noise_timer(r); kill(kid, SIGKILL); waitpid(kid, 0, 0); - return (ret); + return (nk.ret); } /* --- @noise_freewheel@ --- * -- 2.11.0