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
This commit is contained in:
tg 2008-11-30 10:33:40 +00:00
parent abb6bea6d8
commit c0703f5f26
4 changed files with 26 additions and 6 deletions

4
jobs.c
View File

@ -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)) {

4
main.c
View File

@ -13,7 +13,7 @@
#include <locale.h>
#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

6
sh.h
View File

@ -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 **);

18
var.c
View File

@ -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.