From c0703f5f2600b8eacacd1c6142c17980a4e3c288 Mon Sep 17 00:00:00 2001 From: tg Date: Sun, 30 Nov 2008 10:33:40 +0000 Subject: [PATCH] if MKSH_SMALL and HAVE_ARC4RANDOM, there is no need to use rand()/srand(3) ever, since MKSH_SMALL is not required to be as close to compatibility as normal/generic shells; we can also get rid of time(3) calls --- jobs.c | 4 +++- main.c | 4 +++- sh.h | 6 ++++-- var.c | 18 ++++++++++++++++-- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/jobs.c b/jobs.c index 0869c16..6057e3e 100644 --- a/jobs.c +++ b/jobs.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.40 2008/11/12 00:54:49 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.41 2008/11/30 10:33:38 tg Exp $"); /* Order important! */ #define PRUNNING 0 @@ -364,8 +364,10 @@ exchild(struct op *t, int flags, /* used if XPCLOSE or XCCLOSE */ int close_fd) else p->pid = i; +#if !HAVE_ARC4RANDOM || !defined(MKSH_SMALL) /* Ensure next child gets a (slightly) different $RANDOM sequence */ change_random(((unsigned long)p->pid << 1) | (ischild ? 1 : 0)); +#endif /* job control set up */ if (Flag(FMONITOR) && !(flags&XXCOM)) { diff --git a/main.c b/main.c index ba5c6e4..3462e4d 100644 --- a/main.c +++ b/main.c @@ -13,7 +13,7 @@ #include #endif -__RCSID("$MirOS: src/bin/mksh/main.c,v 1.113 2008/11/15 09:00:19 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/main.c,v 1.114 2008/11/30 10:33:39 tg Exp $"); extern char **environ; @@ -227,8 +227,10 @@ main(int argc, const char *argv[]) setstr(pwd_v, current_wd, KSH_RETURN_ERROR); } ppid = getppid(); +#if !HAVE_ARC4RANDOM || !defined(MKSH_SMALL) change_random(((unsigned long)kshname) ^ ((unsigned long)time(NULL) * kshpid * ppid)); +#endif #if HAVE_ARC4RANDOM Flag(FARC4RANDOM) = 2; /* use arc4random(3) until $RANDOM is written */ #endif diff --git a/sh.h b/sh.h index 5d1628f..ca4f27f 100644 --- a/sh.h +++ b/sh.h @@ -103,9 +103,9 @@ #define __SCCSID(x) __IDSTRING(sccsid,x) #ifdef EXTERN -__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.261 2008/11/15 08:52:01 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.262 2008/11/30 10:33:39 tg Exp $"); #endif -#define MKSH_VERSION "R36 2008/11/11" +#define MKSH_VERSION "R36 2008/11/30" #ifndef MKSH_INCLUDES_ONLY @@ -1533,7 +1533,9 @@ const char *skip_wdvarname(const char *, int); int is_wdvarname(const char *, int); int is_wdvarassign(const char *); char **makenv(void); +#if !HAVE_ARC4RANDOM || !defined(MKSH_SMALL) void change_random(unsigned long); +#endif int array_ref_len(const char *); char *arrayname(const char *); void set_array(const char *, int, const char **); diff --git a/var.c b/var.c index 317aa63..8e540c6 100644 --- a/var.c +++ b/var.c @@ -2,7 +2,7 @@ #include "sh.h" -__RCSID("$MirOS: src/bin/mksh/var.c,v 1.62 2008/11/15 09:00:19 tg Exp $"); +__RCSID("$MirOS: src/bin/mksh/var.c,v 1.63 2008/11/30 10:33:40 tg Exp $"); /* * Variables @@ -895,7 +895,7 @@ makenv(void) * otherwise arc4random(3). We have static caches to make change_random * and writes to $RANDOM a cheap operation. */ -#if HAVE_ARC4RANDOM +#if HAVE_ARC4RANDOM && !defined(MKSH_SMALL) static uint32_t rnd_cache[2]; static char rnd_lastflag = 2; #endif @@ -903,6 +903,9 @@ static char rnd_lastflag = 2; static int rnd_get(void) { +#if HAVE_ARC4RANDOM && defined(MKSH_SMALL) + return (arc4random() & 0x7FFF); +#else #if HAVE_ARC4RANDOM #if HAVE_ARC4RANDOM_PUSHB uint32_t rv = 0; @@ -935,11 +938,19 @@ rnd_get(void) } #endif return (rand() & 0x7FFF); +#endif } static void rnd_set(unsigned long newval) { +#if HAVE_ARC4RANDOM && defined(MKSH_SMALL) +#if HAVE_ARC4RANDOM_PUSHB + arc4random_pushb(&newval, sizeof (newval)); +#else + arc4random_addrandom((void *)&newval, sizeof (newval)); +#endif +#else #if HAVE_ARC4RANDOM rnd_cache[0] ^= (newval << 15) | rand(); rnd_cache[1] ^= newval >> 17; @@ -951,8 +962,10 @@ rnd_set(unsigned long newval) rnd_lastflag = 0; #endif srand(newval & 0x7FFF); +#endif } +#if !HAVE_ARC4RANDOM || !defined(MKSH_SMALL) /* * Called after a fork in parent to bump the random number generator. * Done to ensure children will not get the same random number sequence @@ -980,6 +993,7 @@ change_random(unsigned long newval) srand(rval); } +#endif /* * handle special variables with side effects - PATH, SECONDS.