summaryrefslogtreecommitdiff
path: root/getrandom.c
blob: ad3a7afde117a8098438e89f1f6e38722c3a5096 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * Copyright (C) 2016 Etienne Champetier <champetier.etienne@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
#define _GNU_SOURCE
#include <errno.h>
#include <linux/random.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/syscall.h>
#include <unistd.h>

#define ERROR_EXIT(fmt, ...) do { \
		fprintf(stderr, fmt, ## __VA_ARGS__); \
		return EXIT_FAILURE; \
	} while (0)

static int usage(char *name)
{
	fprintf(stderr, "Usage: %s <nb>\n", name);
	fprintf(stderr, " => return <nb> bytes from getrandom()\n");
	return EXIT_FAILURE;
}

int main(int argc, char *argv[])
{
	if (argc != 2)
		return usage(argv[0]);

	if (isatty(STDOUT_FILENO))
		ERROR_EXIT("Not outputting random to a tty\n");

	int nbtot = atoi(argv[1]);
	if (nbtot < 1)
		ERROR_EXIT("Invalid <nb> param (must be > 0)\n");

	char buf[256];
	int len = sizeof(buf);
	while (nbtot > 0) {
		if (nbtot <= sizeof(buf))
			len = nbtot;
		if (syscall(SYS_getrandom, buf, len, 0) == -1)
			ERROR_EXIT("getrandom() failed: %m\n");
		if (write(STDOUT_FILENO, buf, len) != len)
			ERROR_EXIT("write() failed: %m\n");
		nbtot -= sizeof(buf);
	}

	return 0;
}