aboutsummaryrefslogtreecommitdiff
path: root/shell
diff options
context:
space:
mode:
authorRon Yorston <rmy@pobox.com>2014-05-06 20:41:10 +0100
committerRon Yorston <rmy@pobox.com>2014-05-06 20:41:10 +0100
commitd3bef66324a8ca5eed9ad7c15ead3a1cc9a9151e (patch)
tree4b364ba4b6b9e96c2629fe382fef0248d76833dd /shell
parent7905d97aeece18da362a5a1e066abff2d2e5c16b (diff)
parentd257608a8429b64e1a04c7cb6d99975eeb2c3955 (diff)
downloadbusybox-w32-d3bef66324a8ca5eed9ad7c15ead3a1cc9a9151e.tar.gz
busybox-w32-d3bef66324a8ca5eed9ad7c15ead3a1cc9a9151e.tar.bz2
busybox-w32-d3bef66324a8ca5eed9ad7c15ead3a1cc9a9151e.zip
Merge branch 'busybox' into merge
Conflicts: debianutils/which.c editors/vi.c libbb/executable.c
Diffstat (limited to 'shell')
-rw-r--r--shell/ash.c11
-rw-r--r--shell/ash_test/ash-misc/local1.right4
-rwxr-xr-xshell/ash_test/ash-misc/local1.tests11
-rw-r--r--shell/hush.c12
-rw-r--r--shell/random.c142
-rw-r--r--shell/random.h16
6 files changed, 177 insertions, 19 deletions
diff --git a/shell/ash.c b/shell/ash.c
index 19565d185..ce6e355eb 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -55,6 +55,7 @@
55#include <setjmp.h> 55#include <setjmp.h>
56#include <fnmatch.h> 56#include <fnmatch.h>
57#include <sys/times.h> 57#include <sys/times.h>
58#include <sys/utsname.h> /* for setting $HOSTNAME */
58 59
59#include "busybox.h" /* for applet_names */ 60#include "busybox.h" /* for applet_names */
60#include "unicode.h" 61#include "unicode.h"
@@ -9353,6 +9354,9 @@ mklocal(char *name)
9353 vp->flags |= VSTRFIXED|VTEXTFIXED; 9354 vp->flags |= VSTRFIXED|VTEXTFIXED;
9354 if (eq) 9355 if (eq)
9355 setvareq(name, 0); 9356 setvareq(name, 0);
9357 else
9358 /* "local VAR" unsets VAR: */
9359 setvar(name, NULL, 0);
9356 } 9360 }
9357 } 9361 }
9358 lvp->vp = vp; 9362 lvp->vp = vp;
@@ -12899,7 +12903,7 @@ find_command(char *name, struct cmdentry *entry, int act, const char *path)
12899 e = errno; 12903 e = errno;
12900 goto loop; 12904 goto loop;
12901 } 12905 }
12902 if (!execable_file(fullname)) { 12906 if (!file_is_executable(fullname)) {
12903 e = ENOEXEC; 12907 e = ENOEXEC;
12904 goto loop; 12908 goto loop;
12905 } 12909 }
@@ -13522,6 +13526,11 @@ init(void)
13522#if ENABLE_ASH_BASH_COMPAT 13526#if ENABLE_ASH_BASH_COMPAT
13523 p = lookupvar("SHLVL"); 13527 p = lookupvar("SHLVL");
13524 setvar("SHLVL", utoa((p ? atoi(p) : 0) + 1), VEXPORT); 13528 setvar("SHLVL", utoa((p ? atoi(p) : 0) + 1), VEXPORT);
13529 if (!lookupvar("HOSTNAME")) {
13530 struct utsname uts;
13531 uname(&uts);
13532 setvar2("HOSTNAME", uts.nodename);
13533 }
13525#endif 13534#endif
13526 p = lookupvar("PWD"); 13535 p = lookupvar("PWD");
13527 if (p) { 13536 if (p) {
diff --git a/shell/ash_test/ash-misc/local1.right b/shell/ash_test/ash-misc/local1.right
new file mode 100644
index 000000000..a2d121df6
--- /dev/null
+++ b/shell/ash_test/ash-misc/local1.right
@@ -0,0 +1,4 @@
1A1:'A'
2A2:''
3A3:''
4A4:'A'
diff --git a/shell/ash_test/ash-misc/local1.tests b/shell/ash_test/ash-misc/local1.tests
new file mode 100755
index 000000000..b1e675059
--- /dev/null
+++ b/shell/ash_test/ash-misc/local1.tests
@@ -0,0 +1,11 @@
1a=A
2f() {
3 local a
4 # the above line unsets $a
5 echo "A2:'$a'"
6 unset a
7 echo "A3:'$a'"
8}
9echo "A1:'$a'"
10f
11echo "A4:'$a'"
diff --git a/shell/hush.c b/shell/hush.c
index 927193450..e1d0ece29 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -91,6 +91,7 @@
91#if ENABLE_HUSH_CASE 91#if ENABLE_HUSH_CASE
92# include <fnmatch.h> 92# include <fnmatch.h>
93#endif 93#endif
94#include <sys/utsname.h> /* for setting $HOSTNAME */
94 95
95#include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */ 96#include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */
96#include "unicode.h" 97#include "unicode.h"
@@ -944,6 +945,7 @@ static const struct built_in_command bltins1[] = {
944 BLTIN("source" , builtin_source , "Run commands in a file"), 945 BLTIN("source" , builtin_source , "Run commands in a file"),
945#endif 946#endif
946 BLTIN("trap" , builtin_trap , "Trap signals"), 947 BLTIN("trap" , builtin_trap , "Trap signals"),
948 BLTIN("true" , builtin_true , NULL),
947 BLTIN("type" , builtin_type , "Show command type"), 949 BLTIN("type" , builtin_type , "Show command type"),
948 BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"), 950 BLTIN("ulimit" , shell_builtin_ulimit , "Control resource limits"),
949 BLTIN("umask" , builtin_umask , "Set file creation mask"), 951 BLTIN("umask" , builtin_umask , "Set file creation mask"),
@@ -7785,6 +7787,14 @@ int hush_main(int argc, char **argv)
7785 7787
7786 /* Export PWD */ 7788 /* Export PWD */
7787 set_pwd_var(/*exp:*/ 1); 7789 set_pwd_var(/*exp:*/ 1);
7790
7791#if ENABLE_HUSH_BASH_COMPAT
7792 /* Set (but not export) HOSTNAME unless already set */
7793 if (!get_local_var_value("HOSTNAME")) {
7794 struct utsname uts;
7795 uname(&uts);
7796 set_local_var_from_halves("HOSTNAME", uts.nodename);
7797 }
7788 /* bash also exports SHLVL and _, 7798 /* bash also exports SHLVL and _,
7789 * and sets (but doesn't export) the following variables: 7799 * and sets (but doesn't export) the following variables:
7790 * BASH=/bin/bash 7800 * BASH=/bin/bash
@@ -7793,7 +7803,6 @@ int hush_main(int argc, char **argv)
7793 * HOSTTYPE=i386 7803 * HOSTTYPE=i386
7794 * MACHTYPE=i386-pc-linux-gnu 7804 * MACHTYPE=i386-pc-linux-gnu
7795 * OSTYPE=linux-gnu 7805 * OSTYPE=linux-gnu
7796 * HOSTNAME=<xxxxxxxxxx>
7797 * PPID=<NNNNN> - we also do it elsewhere 7806 * PPID=<NNNNN> - we also do it elsewhere
7798 * EUID=<NNNNN> 7807 * EUID=<NNNNN>
7799 * UID=<NNNNN> 7808 * UID=<NNNNN>
@@ -7821,6 +7830,7 @@ int hush_main(int argc, char **argv)
7821 * PS2='> ' 7830 * PS2='> '
7822 * PS4='+ ' 7831 * PS4='+ '
7823 */ 7832 */
7833#endif
7824 7834
7825#if ENABLE_FEATURE_EDITING 7835#if ENABLE_FEATURE_EDITING
7826 G.line_input_state = new_line_input_t(FOR_SHELL); 7836 G.line_input_state = new_line_input_t(FOR_SHELL);
diff --git a/shell/random.c b/shell/random.c
index 853ab085a..5d3620516 100644
--- a/shell/random.c
+++ b/shell/random.c
@@ -6,17 +6,51 @@
6 * 6 *
7 * Licensed under GPLv2, see file LICENSE in this source tree. 7 * Licensed under GPLv2, see file LICENSE in this source tree.
8 */ 8 */
9#include "libbb.h" 9
10#include "random.h" 10/* For testing against dieharder, you need only random.{c,h}
11 * Howto:
12 * gcc -O2 -Wall -DRANDTEST random.c -o random
13 * ./random | dieharder -g 200 -a
14 */
15
16#if !defined RANDTEST
17
18# include "libbb.h"
19# include "random.h"
20# define RAND_BASH_MASK 0x7fff
21
22#else
23# include <stdint.h>
24# include <unistd.h>
25# include <stdio.h>
26# include <time.h>
27# define FAST_FUNC /* nothing */
28# define PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN /* nothing */
29# define POP_SAVED_FUNCTION_VISIBILITY /* nothing */
30# define monotonic_us() time(NULL)
31# include "random.h"
32# define RAND_BASH_MASK 0xffffffff /* off */
33#endif
11 34
12uint32_t FAST_FUNC 35uint32_t FAST_FUNC
13next_random(random_t *rnd) 36next_random(random_t *rnd)
14{ 37{
15 /* Galois LFSR parameter */ 38 /* Galois LFSR parameter:
16 /* Taps at 32 31 29 1: */ 39 * Taps at 32 31 29 1:
40 */
17 enum { MASK = 0x8000000b }; 41 enum { MASK = 0x8000000b };
18 /* Another example - taps at 32 31 30 10: */ 42 /* Another example - taps at 32 31 30 10: */
19 /* MASK = 0x00400007 */ 43 /* enum { MASK = 0x00400007 }; */
44
45 /* Xorshift parameters:
46 * Choices for a,b,c: 10,13,10; 8,9,22; 2,7,3; 23,3,24
47 * (given by algorithm author)
48 */
49 enum {
50 a = 2,
51 b = 7,
52 c = 3,
53 };
20 54
21 uint32_t t; 55 uint32_t t;
22 56
@@ -27,18 +61,100 @@ next_random(random_t *rnd)
27 INIT_RANDOM_T(rnd, getpid(), monotonic_us()); 61 INIT_RANDOM_T(rnd, getpid(), monotonic_us());
28 } 62 }
29 63
30 /* LCG has period of 2^32 and alternating lowest bit */ 64 /* LCG: period of 2^32, but quite weak:
65 * bit 0 alternates beetween 0 and 1 (pattern of length 2)
66 * bit 1 has a repeating pattern of length 4
67 * bit 2 has a repeating pattern of length 8
68 * etc...
69 */
31 rnd->LCG = 1664525 * rnd->LCG + 1013904223; 70 rnd->LCG = 1664525 * rnd->LCG + 1013904223;
32 /* Galois LFSR has period of 2^32-1 = 3 * 5 * 17 * 257 * 65537 */ 71
72 /* Galois LFSR:
73 * period of 2^32-1 = 3 * 5 * 17 * 257 * 65537.
74 * Successive values are right-shifted one bit
75 * and possibly xored with a sparse constant.
76 */
33 t = (rnd->galois_LFSR << 1); 77 t = (rnd->galois_LFSR << 1);
34 if (rnd->galois_LFSR < 0) /* if we just shifted 1 out of msb... */ 78 if (rnd->galois_LFSR < 0) /* if we just shifted 1 out of msb... */
35 t ^= MASK; 79 t ^= MASK;
36 rnd->galois_LFSR = t; 80 rnd->galois_LFSR = t;
37 /* Both are weak, combining them gives better randomness
38 * and ~2^64 period. & 0x7fff is probably bash compat
39 * for $RANDOM range. Combining with subtraction is
40 * just for fun. + and ^ would work equally well. */
41 t = (t - rnd->LCG) & 0x7fff;
42 81
43 return t; 82 /* http://en.wikipedia.org/wiki/Xorshift
83 * Moderately good statistical properties:
84 * fails the following "dieharder -g 200 -a" tests:
85 * diehard_operm5| 0
86 * diehard_oqso| 0
87 * diehard_count_1s_byt| 0
88 * diehard_3dsphere| 3
89 * diehard_squeeze| 0
90 * diehard_runs| 0
91 * diehard_runs| 0
92 * diehard_craps| 0
93 * diehard_craps| 0
94 * rgb_minimum_distance| 3
95 * rgb_minimum_distance| 4
96 * rgb_minimum_distance| 5
97 * rgb_permutations| 3
98 * rgb_permutations| 4
99 * rgb_permutations| 5
100 * dab_filltree| 32
101 * dab_filltree| 32
102 * dab_monobit2| 12
103 */
104 again:
105 t = rnd->xs64_x ^ (rnd->xs64_x << a);
106 rnd->xs64_x = rnd->xs64_y;
107 rnd->xs64_y = rnd->xs64_y ^ (rnd->xs64_y >> c) ^ t ^ (t >> b);
108 /*
109 * Period 2^64-1 = 2^32+1 * 2^32-1 has a common divisor with Galois LFSR.
110 * By skipping two possible states (0x1 and 0x2) we reduce period to
111 * 2^64-3 = 13 * 3889 * 364870227143809 which has no common divisors:
112 */
113 if (rnd->xs64_y == 0 && rnd->xs64_x <= 2)
114 goto again;
115
116 /* Combined LCG + Galois LFSR rng has 2^32 * 2^32-1 period.
117 * Strength:
118 * individually, both are extremely weak cryptographycally;
119 * when combined, they fail the following "dieharder -g 200 -a" tests:
120 * diehard_rank_6x8| 0
121 * diehard_oqso| 0
122 * diehard_dna| 0
123 * diehard_count_1s_byt| 0
124 * rgb_bitdist| 2
125 * dab_monobit2| 12
126 *
127 * Combining them with xorshift-64 increases period to
128 * 2^32 * 2^32-1 * 2^64-3
129 * which is about 2^128, or in base 10 ~3.40*10^38.
130 * Strength of the combination:
131 * passes all "dieharder -g 200 -a" tests.
132 *
133 * Combining with subtraction and addition is just for fun.
134 * It does not add meaningful strength, could use xor operation instead.
135 */
136 t = rnd->galois_LFSR - rnd->LCG + rnd->xs64_y;
137
138 /* bash compat $RANDOM range: */
139 return t & RAND_BASH_MASK;
44} 140}
141
142#ifdef RANDTEST
143static random_t rnd;
144
145int main(int argc, char **argv)
146{
147 int i;
148 uint32_t buf[4096];
149
150 for (;;) {
151 for (i = 0; i < sizeof(buf) / sizeof(buf[0]); i++) {
152 buf[i] = next_random(&rnd);
153 }
154 write(1, buf, sizeof(buf));
155 }
156
157 return 0;
158}
159
160#endif
diff --git a/shell/random.h b/shell/random.h
index 180c48abb..c4eb44c13 100644
--- a/shell/random.h
+++ b/shell/random.h
@@ -12,16 +12,24 @@
12PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN 12PUSH_AND_SET_FUNCTION_VISIBILITY_TO_HIDDEN
13 13
14typedef struct random_t { 14typedef struct random_t {
15 /* Random number generators */ 15 /* State of random number generators: */
16 int32_t galois_LFSR; /* Galois LFSR (fast but weak). signed! */ 16
17 uint32_t LCG; /* LCG (fast but weak) */ 17 /* Galois LFSR (fast but weak) */
18 int32_t galois_LFSR; /* must be signed! */
19
20 /* LCG (fast but weak) */
21 uint32_t LCG;
22
23 /* 64-bit xorshift (fast, moderate strength) */
24 uint32_t xs64_x;
25 uint32_t xs64_y;
18} random_t; 26} random_t;
19 27
20#define UNINITED_RANDOM_T(rnd) \ 28#define UNINITED_RANDOM_T(rnd) \
21 ((rnd)->galois_LFSR == 0) 29 ((rnd)->galois_LFSR == 0)
22 30
23#define INIT_RANDOM_T(rnd, nonzero, v) \ 31#define INIT_RANDOM_T(rnd, nonzero, v) \
24 ((rnd)->galois_LFSR = (nonzero), (rnd)->LCG = (v)) 32 ((rnd)->galois_LFSR = (rnd)->xs64_x = (nonzero), (rnd)->LCG = (rnd)->xs64_y = (v))
25 33
26#define CLEAR_RANDOM_T(rnd) \ 34#define CLEAR_RANDOM_T(rnd) \
27 ((rnd)->galois_LFSR = 0) 35 ((rnd)->galois_LFSR = 0)