diff options
author | Peter Wu <lekensteyn@gmail.com> | 2013-01-28 23:56:27 +0100 |
---|---|---|
committer | Peter Wu <lekensteyn@gmail.com> | 2013-01-28 23:56:27 +0100 |
commit | 48fd809d8c94bd5d1af885fa1dd79b3cc9fa9015 (patch) | |
tree | a2a3c068de2e2646dcf78a996e29e6c8b8ae2cd6 /entropy-watcher.c | |
parent | 18d81a4e8c430f47a4d5e59fd8c7c0591e7d894a (diff) | |
download | c-files-48fd809d8c94bd5d1af885fa1dd79b3cc9fa9015.tar.gz |
Add random entropy watcher
Diffstat (limited to 'entropy-watcher.c')
-rw-r--r-- | entropy-watcher.c | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/entropy-watcher.c b/entropy-watcher.c new file mode 100644 index 0000000..2d6fe86 --- /dev/null +++ b/entropy-watcher.c @@ -0,0 +1,55 @@ +/** + * Watch available kernel entropy. By default, it prints the available entrophy + * every second, but this can be changed with arg1 (in milliseconds). You can + * also press Enter to immediately print the current entrophy. + * + * Author: Peter Wu <lekensteyn@gmail.com> + * Date: 2013-01-28 + */ +#include <unistd.h> +#include <fcntl.h> +#include <stdio.h> +#include <poll.h> +#include <stdlib.h> +#include <stdbool.h> + +#define FILENAME "/proc/sys/kernel/random/entropy_avail" + +int main(int argc, char **argv) { + char buff[32]; + ssize_t r; + struct pollfd pfd; + int fd; + int print_interval_ms; + + print_interval_ms = 1000; + if (argc > 1) { + print_interval_ms = atoi(argv[1]); + } + + fd = open(FILENAME, O_RDONLY); + if (fd < 0) { + perror(FILENAME); + return 1; + } + + pfd.fd = STDIN_FILENO; + pfd.events = POLLIN; + + while ((r = read(fd, buff, sizeof(buff))) > 0) { + write(STDOUT_FILENO, buff, r - 1); + poll(&pfd, 1, print_interval_ms); + lseek(fd, 0, SEEK_SET); + if (pfd.revents == POLLIN) { /* pressed enter? */ + r = read(STDIN_FILENO, buff, sizeof(buff)); + if (r > 0 && buff[r - 1] == '\n') + continue; + } + buff[0] = '\n'; + write(STDOUT_FILENO, buff, 1); + } + + close(fd); + + return 0; +} |