2 * Copyright (C) 2016 Etienne Champetier <champetier.etienne@gmail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
16 #include <linux/random.h>
20 #include <sys/syscall.h>
23 #define ERROR_EXIT(fmt, ...) do { \
24 fprintf(stderr, fmt, ## __VA_ARGS__); \
25 return EXIT_FAILURE; \
28 static int usage(char *name)
30 fprintf(stderr, "Usage: %s <nb>\n", name);
31 fprintf(stderr, " => return <nb> bytes from getrandom()\n");
35 int main(int argc, char *argv[])
38 return usage(argv[0]);
40 if (isatty(STDOUT_FILENO))
41 ERROR_EXIT("Not outputting random to a tty\n");
43 int nbtot = atoi(argv[1]);
45 ERROR_EXIT("Invalid <nb> param (must be > 0)\n");
48 int len = sizeof(buf);
50 if (nbtot <= sizeof(buf))
52 if (syscall(SYS_getrandom, buf, len, 0) == -1)
53 ERROR_EXIT("getrandom() failed: %s\n", strerror(errno));
54 if (write(STDOUT_FILENO, buf, len) != len)
55 ERROR_EXIT("write() failed: %s\n", strerror(errno));