From 7df27c110a0f0a97a940283e20fc52b267f6fdab Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Tue, 17 Sep 2024 19:32:05 +0200 Subject: Use rand(3) when stdin is a TTY MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- README | 7 ++++--- TODO | 1 - config.mk | 1 + deadshred.1 | 9 +++++---- deadshred.c | 14 +++++++++++++- 5 files changed, 23 insertions(+), 9 deletions(-) diff --git a/README b/README index f9de47b..e15cfdc 100644 --- a/README +++ b/README @@ -2,7 +2,7 @@ NAME deadshred - override the contents of a device that may be broken SYNOPSIS - deadshred device < random-source + deadshred device [< random-source] DESCRIPTION The deadshred utility fills a file or block devices with @@ -22,8 +22,9 @@ OPERANDS a block device. STDIN - The standard input shall be an unless source of either random - data or a particular byte to fill the device with. + Unless the standard input is a terminal device, it shall be an + unless source of either random data or a particular byte to + fill the device with. NOTES While the deadshred utility is designed for block devices, it diff --git a/TODO b/TODO index 041198a..797110b 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,5 @@ Add `-n iterations` Add shred map for continuing later (print to stdout on SIGTERM) -Use rand(3) if stdin is a TTY Enhance progress printout with: exact bytes count diff --git a/config.mk b/config.mk index aeb8cec..d2d807d 100644 --- a/config.mk +++ b/config.mk @@ -6,3 +6,4 @@ CC = c99 CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_GNU_SOURCE CFLAGS = LDFLAGS = -lsimple +#libsimple>=1.7 diff --git a/deadshred.1 b/deadshred.1 index eb321d5..be586b2 100644 --- a/deadshred.1 +++ b/deadshred.1 @@ -5,8 +5,8 @@ deadshred \- override the contents of a device that may be broken .SH SYNOPSIS .B deadshred .I device -< -.I random-source +[< +.IR random-source ] .SH DESCRIPTION The @@ -30,8 +30,9 @@ The file to override. Must be either a regular file or a block device. .SH STDIN -The standard input shall be an unless source of either random -data or a particular byte to fill the device with. +Unless the standard input is a terminal device, it shall be an +unless source of either random data or a particular byte to +fill the device with. .SH NOTES While the diff --git a/deadshred.c b/deadshred.c index 5323d15..5f8f36b 100644 --- a/deadshred.c +++ b/deadshred.c @@ -4,7 +4,7 @@ #include -USAGE("file"); +USAGE("device [< random-source]"); struct span { @@ -23,6 +23,7 @@ static off_t total_size = 0; static char reservoir[128U << 10]; static size_t reservoir_off = sizeof(reservoir); +static int use_stdin; static char total_size_1000[256]; static char total_size_1024[256]; @@ -60,6 +61,12 @@ ensure_random(size_t needed) if (sizeof(reservoir) - reservoir_off >= needed) return; + if (!use_stdin) { + libsimple_random_bytes(&libsimple_random_bits, NULL, reservoir, reservoir_off); + reservoir_off = 0; + return; + } + for (off = 0; off < reservoir_off;) { r = read(STDIN_FILENO, &reservoir[off], reservoir_off - off); if (r <= 0) { @@ -206,6 +213,11 @@ main(int argc, char *argv[]) if (fd < 0) eprintf("open %s O_WRONLY:", argv[0]); + use_stdin = !isatty(STDIN_FILENO); + if (!use_stdin) { + libsimple_srand(); + } + spans = emalloc(sizeof(*spans)); spans[0].start = 0; spans[0].end = total_size = filesize(fd, argv[0]); -- cgit v1.2.3-70-g09d2