From 9569e629c8e73dcdc3f93b5fc6156db827d6b7b1 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Mon, 15 Feb 2021 13:08:54 +0200 Subject: cease inefficient /dev/urandom fiddling. Instead of reading /dev/urandom like a file (i.e. overhead / extra syscalls, etc.), just use getrandom(2) directly, lol. Signed-off-by: Gediminas Jakutis --- src/io.c | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) (limited to 'src/io.c') diff --git a/src/io.c b/src/io.c index 9dae2e1..682eea2 100644 --- a/src/io.c +++ b/src/io.c @@ -20,7 +20,6 @@ static int stream_open_out(struct stream * const in); static int stream_open_out_lite(struct stream * const in); static int stream_open_in(struct stream * const in); -static int stream_open_chardev(struct stream * const in); static int stream_flush(struct stream * const in); static int stream_flush_array(struct stream * const in); static int stream_flush_list(struct stream * const in); @@ -29,7 +28,7 @@ int stream_open(struct stream * const in) { int ret = 0; - try(!in || in->fd > 0 || !in->name, err, EINVAL, "invalid argunent"); + try(!in || in->fd > 0 || (!in->name && in->type != stream_randread), err, EINVAL, "invalid argunent"); switch (in->type) { case (stream_outlite): @@ -43,8 +42,8 @@ int stream_open(struct stream * const in) try(!in->name, err, EINVAL, "no filename given"); ret = stream_open_in(in); break; - case (stream_chardev): - ret = stream_open_chardev(in); + case (stream_randread): + ; /* NOOP */ break; default: try(0, err, EINVAL, "cannot open stream: stream is invalid"); @@ -58,7 +57,7 @@ int stream_close(struct stream * const in) { int ret = 0; - try(!in || in->fd < 0, early_err, EINVAL, "invalid argunent"); + try(!in || (in->fd < 0 && in->type != stream_randread), early_err, EINVAL, "invalid argunent"); if (in->type != stream_out) { goto out; @@ -166,19 +165,6 @@ err: return ret; } -static int stream_open_chardev(struct stream * const in) -{ - struct stat st; - int ret = 0; - - try(stat(in->name, &st), err, errno, "stat failed: %s", strerror(ret)); - in->fd = open(in->name, O_RDONLY); - try(in->fd < 0, err, errno, "failed opening input file: %s", strerror(ret)); - -err: - return ret; -} - int stream_flush(struct stream * const in) { int ret = 0; -- cgit v1.2.3