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