ash,hush: fix $RANDOM in children being repeated
function old new delta next_random 46 68 +22 forkshell 248 263 +15 expand_vars_to_list 2118 2131 +13 run_pipe 1775 1782 +7 popstring 134 140 +6 builtin_umask 123 121 -2 ash_main 1356 1336 -20 get_local_var_value 125 104 -21 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 5/3 up/down: 63/-43) Total: 20 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
3c39e702d0
commit
76ace254e1
17
shell/ash.c
17
shell/ash.c
@ -4725,10 +4725,12 @@ forkshell(struct job *jp, union node *n, int mode)
|
|||||||
freejob(jp);
|
freejob(jp);
|
||||||
ash_msg_and_raise_error("can't fork");
|
ash_msg_and_raise_error("can't fork");
|
||||||
}
|
}
|
||||||
if (pid == 0)
|
if (pid == 0) {
|
||||||
|
CLEAR_RANDOM_T(&random_gen); /* or else $RANDOM repeats in child */
|
||||||
forkchild(jp, n, mode);
|
forkchild(jp, n, mode);
|
||||||
else
|
} else {
|
||||||
forkparent(jp, n, mode, pid);
|
forkparent(jp, n, mode, pid);
|
||||||
|
}
|
||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -10079,12 +10081,6 @@ setcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
|
|||||||
static void FAST_FUNC
|
static void FAST_FUNC
|
||||||
change_random(const char *value)
|
change_random(const char *value)
|
||||||
{
|
{
|
||||||
/* Galois LFSR parameter */
|
|
||||||
/* Taps at 32 31 29 1: */
|
|
||||||
enum { MASK = 0x8000000b };
|
|
||||||
/* Another example - taps at 32 31 30 10: */
|
|
||||||
/* MASK = 0x00400007 */
|
|
||||||
|
|
||||||
uint32_t t;
|
uint32_t t;
|
||||||
|
|
||||||
if (value == NULL) {
|
if (value == NULL) {
|
||||||
@ -13268,11 +13264,6 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
|
|||||||
#endif
|
#endif
|
||||||
rootpid = getpid();
|
rootpid = getpid();
|
||||||
|
|
||||||
#if ENABLE_ASH_RANDOM_SUPPORT
|
|
||||||
/* Can use monotonic_ns() for better randomness but for now it is
|
|
||||||
* not used anywhere else in busybox... so avoid bloat */
|
|
||||||
INIT_RANDOM_T(&random_gen, rootpid, monotonic_us());
|
|
||||||
#endif
|
|
||||||
init();
|
init();
|
||||||
setstackmark(&smark);
|
setstackmark(&smark);
|
||||||
procargs(argv);
|
procargs(argv);
|
||||||
|
@ -88,6 +88,8 @@
|
|||||||
#include "match.h"
|
#include "match.h"
|
||||||
#if ENABLE_HUSH_RANDOM_SUPPORT
|
#if ENABLE_HUSH_RANDOM_SUPPORT
|
||||||
# include "random.h"
|
# include "random.h"
|
||||||
|
#else
|
||||||
|
# define CLEAR_RANDOM_T(rnd) ((void)0)
|
||||||
#endif
|
#endif
|
||||||
#ifndef PIPE_BUF
|
#ifndef PIPE_BUF
|
||||||
# define PIPE_BUF 4096 /* amount of buffering in a pipe */
|
# define PIPE_BUF 4096 /* amount of buffering in a pipe */
|
||||||
@ -1319,8 +1321,6 @@ static const char *get_local_var_value(const char *name)
|
|||||||
// bash compat: UID? EUID?
|
// bash compat: UID? EUID?
|
||||||
#if ENABLE_HUSH_RANDOM_SUPPORT
|
#if ENABLE_HUSH_RANDOM_SUPPORT
|
||||||
if (strcmp(name, "RANDOM") == 0) {
|
if (strcmp(name, "RANDOM") == 0) {
|
||||||
if (G.random_gen.galois_LFSR == 0)
|
|
||||||
INIT_RANDOM_T(&G.random_gen, G.root_pid, monotonic_us());
|
|
||||||
return utoa(next_random(&G.random_gen));
|
return utoa(next_random(&G.random_gen));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -4000,6 +4000,7 @@ static NOINLINE int run_pipe(struct pipe *pi)
|
|||||||
if (!command->pid) { /* child */
|
if (!command->pid) { /* child */
|
||||||
#if ENABLE_HUSH_JOB
|
#if ENABLE_HUSH_JOB
|
||||||
disable_restore_tty_pgrp_on_exit();
|
disable_restore_tty_pgrp_on_exit();
|
||||||
|
CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */
|
||||||
|
|
||||||
/* Every child adds itself to new process group
|
/* Every child adds itself to new process group
|
||||||
* with pgid == pid_of_first_child_in_pipe */
|
* with pgid == pid_of_first_child_in_pipe */
|
||||||
@ -5207,6 +5208,7 @@ static FILE *generate_stream_from_string(const char *s)
|
|||||||
+ (1 << SIGTTIN)
|
+ (1 << SIGTTIN)
|
||||||
+ (1 << SIGTTOU)
|
+ (1 << SIGTTOU)
|
||||||
, SIG_IGN);
|
, SIG_IGN);
|
||||||
|
CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */
|
||||||
close(channel[0]); /* NB: close _first_, then move fd! */
|
close(channel[0]); /* NB: close _first_, then move fd! */
|
||||||
xmove_fd(channel[1], 1);
|
xmove_fd(channel[1], 1);
|
||||||
/* Prevent it from trying to handle ctrl-z etc */
|
/* Prevent it from trying to handle ctrl-z etc */
|
||||||
|
@ -258,7 +258,7 @@ static int
|
|||||||
arith_lookup_val(v_n_t *t, a_e_h_t *math_hooks)
|
arith_lookup_val(v_n_t *t, a_e_h_t *math_hooks)
|
||||||
{
|
{
|
||||||
if (t->var) {
|
if (t->var) {
|
||||||
const char * p = lookupvar(t->var);
|
const char *p = lookupvar(t->var);
|
||||||
|
|
||||||
if (p) {
|
if (p) {
|
||||||
int errcode;
|
int errcode;
|
||||||
|
@ -20,6 +20,13 @@ next_random(random_t *rnd)
|
|||||||
|
|
||||||
uint32_t t;
|
uint32_t t;
|
||||||
|
|
||||||
|
if (UNINITED_RANDOM_T(rnd)) {
|
||||||
|
/* Can use monotonic_ns() for better randomness but for now
|
||||||
|
* it is not used anywhere else in busybox... so avoid bloat
|
||||||
|
*/
|
||||||
|
INIT_RANDOM_T(rnd, getpid(), monotonic_us());
|
||||||
|
}
|
||||||
|
|
||||||
/* LCG has period of 2^32 and alternating lowest bit */
|
/* LCG has period of 2^32 and alternating lowest bit */
|
||||||
rnd->LCG = 1664525 * rnd->LCG + 1013904223;
|
rnd->LCG = 1664525 * rnd->LCG + 1013904223;
|
||||||
/* Galois LFSR has period of 2^32-1 = 3 * 5 * 17 * 257 * 65537 */
|
/* Galois LFSR has period of 2^32-1 = 3 * 5 * 17 * 257 * 65537 */
|
||||||
|
@ -13,7 +13,13 @@ typedef struct random_t {
|
|||||||
uint32_t LCG; /* LCG (fast but weak) */
|
uint32_t LCG; /* LCG (fast but weak) */
|
||||||
} random_t;
|
} random_t;
|
||||||
|
|
||||||
|
#define UNINITED_RANDOM_T(rnd) \
|
||||||
|
((rnd)->galois_LFSR == 0)
|
||||||
|
|
||||||
#define INIT_RANDOM_T(rnd, nonzero, v) \
|
#define INIT_RANDOM_T(rnd, nonzero, v) \
|
||||||
((rnd)->galois_LFSR = (nonzero), (rnd)->LCG = (v))
|
((rnd)->galois_LFSR = (nonzero), (rnd)->LCG = (v))
|
||||||
|
|
||||||
|
#define CLEAR_RANDOM_T(rnd) \
|
||||||
|
((rnd)->galois_LFSR = 0)
|
||||||
|
|
||||||
uint32_t next_random(random_t *rnd) FAST_FUNC;
|
uint32_t next_random(random_t *rnd) FAST_FUNC;
|
||||||
|
Loading…
Reference in New Issue
Block a user