Tomas Janousek fe0b9cf
344411: $RANDOM stays the same when job executed in the background
Tomas Janousek fe0b9cf
Tomas Janousek fe0b9cf
In bash 3.0, random was seeded whenever subshell_environment != 0.
Tomas Janousek fe0b9cf
Tomas Janousek fe0b9cf
In bash 3.2, random was seeded whenever subshell_environment != 0 &&
Tomas Janousek fe0b9cf
seeded_subshell == 0. And when it was seeded, seeded_subshell was set to 1.
Tomas Janousek fe0b9cf
Tomas Janousek fe0b9cf
Therefore, in 3.2, if you seeded random in a subshell and in this subshell
Tomas Janousek fe0b9cf
invoked another one, it wasn't reseeded as it should have been. A testcase for
Tomas Janousek fe0b9cf
that is this:
Tomas Janousek fe0b9cf
    ( echo $RANDOM; ( echo $RANDOM ); ( echo $RANDOM ) )
Tomas Janousek fe0b9cf
Tomas Janousek fe0b9cf
Tomas's patch (bash-3.2-rng.patch) changed the code to use subshell_level.
Tomas Janousek fe0b9cf
subshell_level is not increased for simple async commands, however. So,
Tomas Janousek fe0b9cf
although he fixed the previous case, he introduced another. Here's a testcase:
Tomas Janousek fe0b9cf
    echo $RANDOM; echo $RANDOM & echo $RANDOM &
Tomas Janousek fe0b9cf
Tomas Janousek fe0b9cf
I decided to just compare the pids, that should be safe enough.
Tomas Janousek fe0b9cf
Tomas Janousek fe0b9cf
Written-by: Tomas Janousek <tjanouse@redhat.com>
Tomas Janousek fe0b9cf
Reviewed-by: Tomas Mraz <tmraz@redhat.com>
Tomas Janousek fe0b9cf
Tomas Janousek fe0b9cf
--- bash-3.2/variables.c.344411	2007-11-06 19:26:42.000000000 +0100
Tomas Janousek fe0b9cf
+++ bash-3.2/variables.c	2007-11-06 20:27:25.000000000 +0100
Tomas Janousek fe0b9cf
@@ -1211,7 +1211,7 @@
Tomas Janousek fe0b9cf
      arrayind_t unused;
Tomas Janousek fe0b9cf
 {
Tomas Janousek fe0b9cf
   sbrand ((unsigned int)strtoul (value, (char **)NULL, 10));
Tomas Janousek fe0b9cf
-  seeded_subshell = subshell_level;
Tomas Janousek fe0b9cf
+  seeded_subshell = getpid();
Tomas Janousek fe0b9cf
   return (self);
Tomas Janousek fe0b9cf
 }
Tomas Janousek fe0b9cf
 
Tomas Janousek fe0b9cf
@@ -1221,10 +1221,10 @@
Tomas Janousek fe0b9cf
   int rv;
Tomas Janousek fe0b9cf
 
Tomas Janousek fe0b9cf
   /* Reset for command and process substitution. */
Tomas Janousek fe0b9cf
-  if (seeded_subshell < subshell_level)
Tomas Janousek fe0b9cf
+  if (seeded_subshell != getpid())
Tomas Janousek fe0b9cf
     {
Tomas Janousek fe0b9cf
       seed_random ();
Tomas Janousek fe0b9cf
-      seeded_subshell = subshell_level;
Tomas Janousek fe0b9cf
+      seeded_subshell = getpid();
Tomas Janousek fe0b9cf
     }
Tomas Janousek fe0b9cf
 
Tomas Janousek fe0b9cf
   do