Mercurial > nomad > old-fuse
changeset 99:7f1f390107a7
common: fix random number generation on Linux
Use /dev/random instead of erroring out.
Signed-off-by: Holly Sipek <holly.sipek@gmail.com>
author | Holly Sipek <holly.sipek@gmail.com> |
---|---|
date | Sat, 17 Oct 2015 11:44:08 -0400 |
parents | c28529eb6fc7 |
children | 3aea54128fef |
files | src/common/rand.c |
diffstat | 1 files changed, 26 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/src/common/rand.c Sat Oct 17 11:32:14 2015 -0400 +++ b/src/common/rand.c Sat Oct 17 11:44:08 2015 -0400 @@ -1,5 +1,6 @@ /* * Copyright (c) 2015 Josef 'Jeff' Sipek <jeffpc@josefsipek.net> + * Copyright (c) 2015 Holly Sipek * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +21,12 @@ * SOFTWARE. */ +#include <fcntl.h> +#include <stdio.h> +#include <errno.h> +#include <assert.h> +#include <unistd.h> + #include <nomad/types.h> #include <nomad/rand.h> #include <nomad/config.h> @@ -29,7 +36,25 @@ #ifdef HAVE_ARC4RANDOM return arc4random(); #else -#error "Need a way to generate random uint32_t" + uint32_t ret; + int fd; + + fd = open("/dev/random", O_RDONLY); + if (fd == -1) { + fprintf(stderr, "Failed to get random number: %s", + strerror(errno)); + assert(0); + } + + if (read(fd, &ret, sizeof(ret)) != sizeof(ret)) { + fprintf(stderr, "Failed to get random number: %s", + strerror(errno)); + assert(0); + } + + close(fd); + + return ret; #endif }