]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
shred,sort,shuf: don't use /dev/urandom by default
authorPádraig Brady <P@draigBrady.com>
Mon, 6 Apr 2009 07:42:15 +0000 (08:42 +0100)
committerPádraig Brady <P@draigBrady.com>
Tue, 7 Apr 2009 18:01:46 +0000 (19:01 +0100)
Suggestion from Steven Schveighoffer at:
http://savannah.gnu.org/patch/?6797
to greatly speed up the random passes done by shred.
* gl/lib/randread.c: Default to using the internal
pseudorandom generator, rather than reading /dev/urandom
* src/shred.c (usage): remove mention of /dev/urandom
* src/shuf.c (usage); ditto
* src/sort.c (usage): ditto
* doc/coreutils.text: Document the new behaviour
for aquiring random data.

NEWS
THANKS
doc/coreutils.texi
gl/lib/randread.c
src/shred.c
src/shuf.c
src/sort.c

diff --git a/NEWS b/NEWS
index 7c507efa2e639e2624318b39781e69f408b74ec0..de1db443751115d9ff6b6cdfd5e9ffbee4281e4b 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,13 @@ GNU coreutils NEWS                                    -*- outline -*-
   ls now aligns output correctly in the presence of abbreviated month
   names from the locale database that have differing widths.
 
+** Changes in behavior
+
+  shred, sort, shuf: now use an internal pseudorandom generator by default.
+  This is mainly noticable in shred where the 3 random passes it does by
+  default should proceed at the speed of the disk.  Previously /dev/urandom
+  was used if available, which is relatively slow on GNU/Linux systems.
+
 * Noteworthy changes in release 7.2 (2009-03-31) [stable]
 
 ** New features
diff --git a/THANKS b/THANKS
index 11f43ac68ba44b1b8f77c9efb6d5f46ae1bab4e2..6a918a4297aea77dab29cea3b3028bfcb45c5f56 100644 (file)
--- a/THANKS
+++ b/THANKS
@@ -525,6 +525,7 @@ Steve McIntyre                      steve@einval.com
 Steve Ward                          planet36@gmail.com
 Steven G. Johnson                   stevenj@alum.mit.edu
 Steven Mocking                      ufo@quicknet.nl
+Steven Schveighoffer                schveiguy@yahoo.com
 Steven P Watson                     steven@magelico.net
 Stuart Kemp                         skemp@peter.bmc.com
 Stuart Shelton                      stuart@shelton.me
index c6e66d5691718dc6b091b345d2c763057b9dabdb..6840aff7c1282782306afd9044bf205ec782748b 100644 (file)
@@ -1139,12 +1139,19 @@ sometimes need random data to do their work.  For example, @samp{sort
 -R} must choose a hash function at random, and it needs random data to
 make this selection.
 
-Normally these commands use the device file @file{/dev/urandom} as the
+By default these commands use an internal pseudorandom generator
+initialized by a small amount of entropy, but can be directed to use
+an external source with the @option{--random-source=@var{file}} option.
+An error is reported if @var{file} does not contain enough bytes.
+
+For example, the device file @file{/dev/urandom} could be used as the
 source of random data.  Typically, this device gathers environmental
 noise from device drivers and other sources into an entropy pool, and
 uses the pool to generate random bits.  If the pool is short of data,
 the device reuses the internal pool to produce more bits, using a
-cryptographically secure pseudorandom number generator.
+cryptographically secure pseudorandom number generator.  But be aware
+that this device is not designed for bulk random data generation
+and is relatively slow.
 
 @file{/dev/urandom} suffices for most practical uses, but applications
 requiring high-value or long-term protection of private data may
@@ -1152,21 +1159,10 @@ require an alternate data source like @file{/dev/random} or
 @file{/dev/arandom}.  The set of available sources depends on your
 operating system.
 
-To use such a source, specify the @option{--random-source=@var{file}}
-option, e.g., @samp{shuf --random-source=/dev/random}.  The contents
-of @var{file} should be as random as possible.  An error is reported
-if @var{file} does not contain enough bytes to randomize the input
-adequately.
-
 To reproduce the results of an earlier invocation of a command, you
 can save some random data into a file and then use that file as the
 random source in earlier and later invocations of the command.
 
-Some old-fashioned or stripped-down operating systems lack support for
-@command{/dev/urandom}.  On these systems commands like @command{shuf}
-by default fall back on an internal pseudorandom generator initialized
-by a small amount of entropy.
-
 @node Target directory
 @section Target directory
 
index b81a4510b352e0d67f0863e4970866252308c61a..798d4e0a36d921406916771fe9517333a4a3aa91 100644 (file)
 # define ALIGNED_POINTER(ptr, type) ((size_t) (ptr) % alignof (type) == 0)
 #endif
 
-#ifndef DEFAULT_RANDOM_FILE
-# define DEFAULT_RANDOM_FILE "/dev/urandom"
-#endif
-
 /* The maximum buffer size used for reads of random data.  Using the
    value 2 * ISAAC_BYTES makes this the largest power of two that
    would not otherwise cause struct randread_source to grow.  */
 /* A source of random data for generating random buffers.  */
 struct randread_source
 {
-  /* Stream to read random bytes from.  If null, the behavior is
-     undefined; the current implementation uses ISAAC in this case,
-     but this is for old-fashioned implementations that lack
-     /dev/urandom and callers should not rely on this.  */
+  /* Stream to read random bytes from.  If null, the current
+     implementation uses an internal PRNG (ISAAC).  */
   FILE *source;
 
   /* Function to call, and its argument, if there is an input error or
@@ -147,18 +141,14 @@ randread_new (char const *name, size_t bytes_bound)
     return simple_new (NULL, NULL);
   else
     {
-      char const *file_name = (name ? name : DEFAULT_RANDOM_FILE);
-      FILE *source = fopen_safer (file_name, "rb");
+      FILE *source = NULL;
       struct randread_source *s;
 
-      if (! source)
-       {
-         if (name)
-           return NULL;
-         file_name = NULL;
-       }
+      if (name)
+       if (! (source = fopen_safer (name, "rb")))
+         return NULL;
 
-      s = simple_new (source, file_name);
+      s = simple_new (source, name);
 
       if (source)
        setvbuf (source, s->buf.c, _IOFBF, MIN (sizeof s->buf.c, bytes_bound));
index 4b2b8e92f01c846a23037ec5b6477d6491d4a5f4..cf40bdc4c833d718c05f22df8e554263f0a924e7 100644 (file)
@@ -167,7 +167,7 @@ Mandatory arguments to long options are mandatory for short options too.\n\
       printf (_("\
   -f, --force    change permissions to allow writing if necessary\n\
   -n, --iterations=N  overwrite N times instead of the default (%d)\n\
-      --random-source=FILE  get random bytes from FILE (default /dev/urandom)\n\
+      --random-source=FILE  get random bytes from FILE\n\
   -s, --size=N   shred this many bytes (suffixes like K, M, G accepted)\n\
 "), DEFAULT_PASSES);
       fputs (_("\
index 977eedc0a5f00d6bec27bcb94e80197ff3fdb380..b221d033853b8a5def6dbcc32bc806e4c0c0e4d8 100644 (file)
@@ -62,7 +62,7 @@ Mandatory arguments to long options are mandatory for short options too.\n\
   -i, --input-range=LO-HI   treat each number LO through HI as an input line\n\
   -n, --head-count=COUNT    output at most COUNT lines\n\
   -o, --output=FILE         write result to FILE instead of standard output\n\
-      --random-source=FILE  get random bytes from FILE (default /dev/urandom)\n\
+      --random-source=FILE  get random bytes from FILE\n\
   -z, --zero-terminated     end lines with 0 byte, not newline\n\
 "), stdout);
       fputs (HELP_OPTION_DESCRIPTION, stdout);
index 5b63a25bbdde66018a7a06f906d309d9ccb06df6..2e6ce877d85d2a0130c4e1d4deb95d3327a0fd72 100644 (file)
@@ -339,7 +339,7 @@ Ordering options:\n\
       fputs (_("\
   -n, --numeric-sort          compare according to string numerical value\n\
   -R, --random-sort           sort by random hash of keys\n\
-      --random-source=FILE    get random bytes from FILE (default /dev/urandom)\n\
+      --random-source=FILE    get random bytes from FILE\n\
   -r, --reverse               reverse the result of comparisons\n\
 "), stdout);
       fputs (_("\