X-Git-Url: https://asedeno.scripts.mit.edu/gitweb/?a=blobdiff_plain;f=unix%2Fuxnoise.c;h=c42466f658e3a2e4b6d2c10333331ac40be3d80b;hb=095072fa46b2d7b8beafaddb2f873d2f500a1e10;hp=873a33136f3b704c612ddf785b0195b3f6533dfe;hpb=53eb27276616f7c467eb0e786e96f5cb4fe424bb;p=PuTTY.git diff --git a/unix/uxnoise.c b/unix/uxnoise.c index 873a3313..c42466f6 100644 --- a/unix/uxnoise.c +++ b/unix/uxnoise.c @@ -4,6 +4,9 @@ */ #include +#include +#include + #include #include #include @@ -32,6 +35,8 @@ static int read_dev_urandom(char *buf, int len) ngot += ret; } + close(fd); + return 1; } @@ -47,21 +52,37 @@ void noise_get_heavy(void (*func) (void *, int)) char buf[512]; FILE *fp; int ret; + int got_dev_urandom = 0; - if (read_dev_urandom(buf, 32)) + if (read_dev_urandom(buf, 32)) { + got_dev_urandom = 1; func(buf, 32); + } fp = popen("ps -axu 2>/dev/null", "r"); - while ( (ret = fread(buf, 1, sizeof(buf), fp)) > 0) - func(buf, ret); - pclose(fp); + if (fp) { + while ( (ret = fread(buf, 1, sizeof(buf), fp)) > 0) + func(buf, ret); + pclose(fp); + } else if (!got_dev_urandom) { + fprintf(stderr, "popen: %s\n" + "Unable to access fallback entropy source\n", strerror(errno)); + exit(1); + } fp = popen("ls -al /tmp 2>/dev/null", "r"); - while ( (ret = fread(buf, 1, sizeof(buf), fp)) > 0) - func(buf, ret); - pclose(fp); + if (fp) { + while ( (ret = fread(buf, 1, sizeof(buf), fp)) > 0) + func(buf, ret); + pclose(fp); + } else if (!got_dev_urandom) { + fprintf(stderr, "popen: %s\n" + "Unable to access fallback entropy source\n", strerror(errno)); + exit(1); + } read_random_seed(func); + random_save_seed(); } void random_save_seed(void) @@ -72,6 +93,7 @@ void random_save_seed(void) if (random_active) { random_get_savedata(&data, &len); write_random_seed(data, len); + sfree(data); } }