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