1 /* 2 * Copyright (C) 2016 Etienne Champetier <champetier.etienne@gmail.com> 3 * 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. 8 * 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. 13 */ 14 #define _GNU_SOURCE 15 #include <errno.h> 16 #include <linux/random.h> 17 #include <stdio.h> 18 #include <stdlib.h> 19 #include <string.h> 20 #include <sys/syscall.h> 21 #include <unistd.h> 22 23 #define ERROR_EXIT(fmt, ...) do { \ 24 fprintf(stderr, fmt, ## __VA_ARGS__); \ 25 return EXIT_FAILURE; \ 26 } while (0) 27 28 static int usage(char *name) 29 { 30 fprintf(stderr, "Usage: %s <nb>\n", name); 31 fprintf(stderr, " => return <nb> bytes from getrandom()\n"); 32 return EXIT_FAILURE; 33 } 34 35 int main(int argc, char *argv[]) 36 { 37 if (argc != 2) 38 return usage(argv[0]); 39 40 if (isatty(STDOUT_FILENO)) 41 ERROR_EXIT("Not outputting random to a tty\n"); 42 43 int nbtot = atoi(argv[1]); 44 if (nbtot < 1) 45 ERROR_EXIT("Invalid <nb> param (must be > 0)\n"); 46 47 char buf[256]; 48 int len = sizeof(buf); 49 while (nbtot > 0) { 50 if (nbtot <= sizeof(buf)) 51 len = nbtot; 52 if (syscall(SYS_getrandom, buf, len, 0) == -1) 53 ERROR_EXIT("getrandom() failed: %m\n"); 54 if (write(STDOUT_FILENO, buf, len) != len) 55 ERROR_EXIT("write() failed: %m\n"); 56 nbtot -= sizeof(buf); 57 } 58 59 return 0; 60 } 61
This page was automatically generated by LXR 0.3.1. • OpenWrt