Commit Graph

744 Commits

Author SHA1 Message Date
Denys Vlasenko
87e039d016 hush: make getch/peek functions directly called
Indirect calls are more difficult to predict.
Unfortunately, on x64 direct call is 5 bytes while indirect "call (reg+ofs)"
is 3 bytes:

function                                             old     new   delta
i_getch                                                -      82     +82
i_peek                                                 -      63     +63
parse_stream                                        2531    2579     +48
parse_dollar                                         771     797     +26
parse_redirect                                       296     321     +25
add_till_closing_bracket                             408     420     +12
encode_string                                        256     265      +9
i_peek_and_eat_bkslash_nl                             93      99      +6
add_till_backquote                                   110     114      +4
parse_and_run_stream                                 139     141      +2
expand_vars_to_list                                 1143    1144      +1
static_peek                                            6       -      -6
setup_string_in_str                                   39      18     -21
setup_file_in_str                                     40      19     -21
static_get                                            27       -     -27
file_peek                                             52       -     -52
file_get                                              65       -     -65
------------------------------------------------------------------------------
(add/remove: 2/4 grow/shrink: 9/2 up/down: 278/-192)           Total: 86 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-08 22:35:05 +01:00
Denys Vlasenko
00a06b9715 hush: renumber PIPE_foo, make PIPE_SEQ = 0
PIPE_SEQ is used most often, having it zero makes code smaller:

function                                             old     new   delta
done_word                                            719     707     -12
parse_stream                                        2546    2531     -15

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-08 20:35:53 +01:00
Denys Vlasenko
6c635d62d4 hush: small optimization in run_list
I thought gcc can detect this itself. It doesn't.

function                                             old     new   delta
run_list                                            1030    1021      -9

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-08 20:26:11 +01:00
Denys Vlasenko
5cc9bf6a21 hush: deindent large block of code, no code changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-08 17:34:44 +01:00
Denys Vlasenko
1eada9ad8d hush: simplify insert_bg_jobs
function                                             old     new   delta
insert_bg_job                                        366     281     -85

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-08 17:28:45 +01:00
Denys Vlasenko
830ea35484 hush: make "wait %1" less likely to play with signal mask
Was playing with "sleep 3 | exit 3 & wait %1" and noticed that often
SIGCHLD arrives even before I get to signal masking. Can avoid it in this
case.

function                                             old     new   delta
wait_for_child_or_signal                             228     265     +37

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-08 04:59:11 +01:00
Denys Vlasenko
02affb4afd hush: rework "wait %jobspec" to work in non-interactive shells too
Also add tests. wait5.tests so far fails (but works for ash and dash).

function                                             old     new   delta
builtin_wait                                         305     283     -22

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-08 00:59:29 +01:00
Denys Vlasenko
26ad94bedc hush: "wait $!; echo $?" should return 127 if $! already exited
It would be nice to provide bash-like "remember las exitcode"
thingy, but it's a bit complex. For now, match ash and dash.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-07 23:07:21 +01:00
Denys Vlasenko
62b717b75e hush: implement "wait %jobspec"
function                                             old     new   delta
parse_jobspec                                          -      83     +83
job_exited_or_stopped                                  -      79     +79
builtin_wait                                         236     302     +66
wait_for_child_or_signal                             199     228     +29
checkjobs                                            142     158     +16
builtin_jobs                                          59      68      +9
process_wait_result                                  453     408     -45
builtin_fg_bg                                        272     203     -69
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 4/2 up/down: 282/-114)          Total: 168 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-07 22:12:18 +01:00
Denys Vlasenko
4e1c8b4f6a hush: factor out %jobspec parsing
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-07 20:06:40 +01:00
Denys Vlasenko
5d5a611df2 hush: comment fix
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-07 19:36:50 +01:00
Denys Vlasenko
4224647c8d hush: do not allow sh -c '{ echo boo }'
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-07 16:22:35 +01:00
Denys Vlasenko
aeaee43d5a hush: case logic for setting $? was still wrong
Resetting to 0 should happen in "esac". Matched branch must
still see previous $?.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-04 20:14:04 +01:00
Denys Vlasenko
30bfcf612b hush: non-matching "case" statement sets $? to 0
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-04 18:52:48 +01:00
Denys Vlasenko
672a55e606 hush: allow { cmd } to not be terminated by semicolon in some cases
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-11-04 18:46:14 +01:00
Denys Vlasenko
9db74e49e5 hush: fix "(sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $?"
"wait $!" may be just a bit too late: backgrounded $! is gone.
Do not bomb out in this case.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-10-28 22:39:12 +02:00
Denys Vlasenko
7e6753609d hush: fix "wait PID"
It was not properly interruptible, and did not update job status
(the exited processes were still thought of as running).

function                                             old     new   delta
process_wait_result                                    -     453    +453
wait_for_child_or_signal                               -     199    +199
run_list                                             996    1002      +6
checkjobs_and_fg_shell                                41      43      +2
builtin_wait                                         328     215    -113
checkjobs                                            516     142    -374
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 2/2 up/down: 660/-487)          Total: 173 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-10-28 21:59:09 +02:00
Denys Vlasenko
2eb0a7e1b9 ash: [SHELL] Expand ENV before using it
Upstream commit:

    Date: Sun, 13 Jul 2008 21:51:52 +0800
    [SHELL] Expand ENV before using it

    Per POSIX ENV needs to undergo parameter expansion.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-10-27 11:28:59 +02:00
Denys Vlasenko
b7adf7ac32 ash,hush: set exit code 127 in "sh /does/not/exist" case
Upstream commit 1 for ash:

    [ERROR] Allow the originator of EXERROR to set the exit status

    Some errors have exit status values specified by POSIX and it is
    therefore desirable to be able to set the exit status at the EXERROR
    source rather than in main.c.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Upstream commit 2 for ash:

    [INPUT] Use exit status 127 when the script to run does not exist

    This commit makes dash exit with return code 127 instead of 2 if
    started as non-interactive shell with a non-existent command_file
    specified as argument (or a directory), as documented in
     http://www.opengroup.org/onlinepubs/009695399/utilities/sh.html#tag_04_128_14

    The wrong exit code was reported by Clint Adams and Jari Aalto through
     http://bugs.debian.org/548743
     http://bugs.debian.org/548687

    Signed-off-by: Gerrit Pape <pape@smarden.org>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

NB: in fact, http://bugs.debian.org/548687 was not fixed by this:
"sh /dir/" thinks that EISDIR error on read is EOF, and exits 0.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-10-25 17:00:13 +02:00
Denys Vlasenko
7f0ebbc69e hush: add commented-out debug printouts in "memleak" built-in
Allocation addresses of malloc() are jittery,
thought I had a mem leak in hush, but it was malloc variability.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-10-03 17:42:53 +02:00
Denys Vlasenko
a769390da6 hush: fix a memory corruption when exported variable is modified
The construct such as this:

t=1
export t
t=new_value1

had a small probability of momentarily using free()d value.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-10-03 15:01:06 +02:00
Denys Vlasenko
04465dad66 hush: fix exitcode on exec failure with EACCES - should be 126
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-10-03 01:01:15 +02:00
Denys Vlasenko
80729a4472 whitespace fixes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-10-02 22:33:15 +02:00
Denys Vlasenko
61508d9624 hush: fix var3.tests
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-10-02 21:12:02 +02:00
Denys Vlasenko
ebedb9478d sh: do not print empty line at the end of "help" output
It's pointless.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-10-02 18:45:09 +02:00
Denys Vlasenko
04b46bced9 hush: 'return' should have effect earlier
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-10-01 22:28:03 +02:00
Denys Vlasenko
ed6ff5edfc hush: enable "msh is deprecated" message in msh stub
After giving a few more years for everyone to notice and migrate,
can nuke all remains of msh.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-09-30 12:28:37 +02:00
Denys Vlasenko
08755f9bcb hush: fix typo in comment
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-09-30 02:02:25 +02:00
Denys Vlasenko
4074d49557 hush: fix interactive input handling of backslash+newline
function                                             old     new   delta
fgetc_interactive                                      -     258    +258
i_peek_and_eat_bkslash_nl                             43      93     +50
static_peek2                                           7       -      -7
setup_string_in_str                                   46      39      -7
setup_file_in_str                                     47      40      -7
file_peek                                             72      52     -20
expand_vars_to_list                                 1167    1143     -24
file_peek2                                            74       -     -74
file_get                                             326      65    -261
------------------------------------------------------------------------------
(add/remove: 1/2 grow/shrink: 1/5 up/down: 308/-400)          Total: -92 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-09-30 01:49:53 +02:00
Denys Vlasenko
0448c55cc8 hush: G.user_input_buf[] is needed only if line editing is enabled
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-09-29 20:25:44 +02:00
Denys Vlasenko
46e6498b2a hush: speed optimizations
Make o_addchr() faster: do not call o_grow_by() each time.
Create i_getch_and_eat_bkslash_nl(), use it instead of peek+getch pair.

function                                             old     new   delta
o_addchr                                              42      54     +12
parse_dollar                                         761     771     +10
o_grow_by                                             48      37     -11
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/1 up/down: 24/-11)             Total: 11 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-09-29 19:50:55 +02:00
Denys Vlasenko
657086a3dc hush: fix handling of by backslash-newline in $((arith)) and $(cmd)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-09-29 18:07:42 +02:00
Denys Vlasenko
d17a91db6e hush: rework input char buffering to allow more than one-deep peek
...this time with actual hush.c changes too :)

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-09-29 18:02:37 +02:00
Denys Vlasenko
78c9c736ab hush: fix 'eval ""' handling
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-09-29 01:44:17 +02:00
Denys Vlasenko
992e0ff7e9 hush: fix ". EMPTY_LINE" not setting $? to 0
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-09-29 01:27:09 +02:00
Denys Vlasenko
d6a37d86ba hush: document better where bad redirect syntax is detected
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-09-20 16:22:24 +02:00
Denys Vlasenko
aa3576a29b hush: fix "redirects can close script fd" bug
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-08-22 19:54:12 +02:00
Denys Vlasenko
7b25b1c5b2 hush: do not leak script fds into NOEXEC children
We set all opened script fds to CLOEXEC, thus making then go away
after fork+exec.
Unfortunately, CLOFORK does not exist. NOEXEC children will still see those fds open.

For one, "ls" applet is NOEXEC. Therefore running "ls -l /proc/self/fd"
in a script from standalone shell shows this:

lrwx------    1 root     root            64 Aug 20 15:17 0 -> /dev/pts/3
lrwx------    1 root     root            64 Aug 20 15:17 1 -> /dev/pts/3
lrwx------    1 root     root            64 Aug 20 15:17 2 -> /dev/pts/3
lr-x------    1 root     root            64 Aug 20 15:17 3 -> /path/to/top/level/script
lr-x------    1 root     root            64 Aug 20 15:17 4 -> /path/to/sourced/SCRIPT1
...

with as many open fds as there are ". SCRIPTn" nest levels.
Fix it by closing these fds after fork (only for NOEXEC children).

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-08-20 15:58:34 +02:00
Denys Vlasenko
869994cf4f hush: bit better comments in redirect code. No logic changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-08-20 15:16:00 +02:00
Denys Vlasenko
e9abe75fda hush: cmd and arithmetic also need the fix for FILE rewind
Discovered by running testsuite with a newest glibc

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-08-19 20:15:26 +02:00
Denys Vlasenko
215b0ca6e4 hush: fix a bug in FEATURE_SH_STANDALONE=y config. Closes 9186
Run this in a "sh SCRIPT":

sha256sum /dev/null
echo END

sha256sum is a NOEXEC applet. It runs in a forked child. Then child exit()s.
By this time, entire script is read, and buffered in a FILE object
from fopen("SCRIPT"). But fgetc() did not consume entire input.
exit() lseeks back by -9 bytes, from <eof> to 'e' in 'echo'.
(this may be libc-specific).
This change of fd position *is shared with the parent*!

Now parent can read more, and it thinks there is another "echo END".
End result: two "echo END"s are run.

Fix this by _exit()ing instead.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-08-19 18:43:06 +02:00
Denys Vlasenko
49117b4800 hush: fix a possible bug
Not sure this was actually a triggerable bug, but the code looked flaky.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2016-07-21 14:42:13 +02:00
Denys Vlasenko
85378cded7 hush: code shrink
function                                             old     new   delta
builtin_wait                                         347     328     -19

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2015-10-11 21:47:11 +02:00
Denys Vlasenko
550bf5b4a4 remove global "jmp_buf die_jmp" from !FEATURE_PREFER_APPLETS builds
function                                             old     new   delta
xfunc_has_died                                         -      21     +21
sleep_much                                             -      12     +12
sleep10                                                -       9      +9
die_func                                               -       4      +4
fflush_stdout_and_exit                                35      36      +1
builtin_type                                         121     119      -2
die_sleep                                              4       -      -4
xfunc_die                                             60      24     -36
hush_main                                           1128    1011    -117
die_jmp                                              156       -    -156
------------------------------------------------------------------------------
(add/remove: 4/2 grow/shrink: 1/3 up/down: 47/-315)          Total: -268 bytes
   text	   data	    bss	    dec	    hex	filename
 939992	    992	  17652	 958636	  ea0ac	busybox_old
 939880	    992	  17496	 958368	  e9fa0	busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2015-10-09 16:42:57 +02:00
Denys Vlasenko
d60752f8c9 build system: -fno-builtin-printf
Benefits are: drops reference to out-of-line putchar(), fixes a few cases
of failed string merge.

function                                             old     new   delta
i2cdump_main                                        1488    1502     +14
sha256_process_block64                               423     433     +10
sendmail_main                                       1183    1185      +2
list_table                                          1114    1116      +2
i2cdetect_main                                      1235    1237      +2
fdisk_main                                          2852    2854      +2
builtin_type                                         119     121      +2
unicode_conv_to_printable2                           325     324      -1
scan_recursive                                       380     378      -2
mkfs_minix_main                                     2687    2684      -3
buffer_fill_and_print                                178     169      -9
putchar                                              152       -    -152
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 7/4 up/down: 34/-167)          Total: -133 bytes
   text    data     bss     dec     hex filename
 937788     932   17676  956396   e97ec busybox_old
 937564     932   17676  956172   e970c busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2015-10-07 22:42:45 +02:00
Denys Vlasenko
5711a2a4ad libbb: more compact API for bb_parse_mode()
function                                             old     new   delta
make_device                                         2182    2188      +6
parse_command                                       1440    1443      +3
parse_params                                        1497    1499      +2
install_main                                         773     769      -4
mkdir_main                                           168     160      -8
getoptscmd                                           641     632      -9
builtin_umask                                        158     147     -11
bb_parse_mode                                        431     410     -21
umaskcmd                                             286     258     -28
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/6 up/down: 11/-81)            Total: -70 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2015-10-07 17:55:33 +02:00
Denys Vlasenko
6283f98283 hush: fix umask: umask(022) was setting umask(755)
Based on the patch by Rich Felker <dalias@libc.org>

function                                             old     new   delta
builtin_umask                                        121     161     +40
umaskcmd                                             318     279     -39

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2015-10-07 16:56:20 +02:00
Denys Vlasenko
b5be13ccd9 hush: fix a nommu bug where a part of function body is lost if run in a pipe
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2015-09-04 06:22:10 +02:00
Maninder Singh
97f2f7ca7f Removes stray empty line from code
This patch removes stray empty line from busybox code
reported by script find_stray_empty_lines

Signed-off-by: Maninder Singh <maninder1.s@samsung.com>
Signed-off-by: Akhilesh Kumar <akhilesh.k@samsung.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2015-07-13 03:25:46 +02:00
Maninder Singh
97c6491b4e typo fixes
Signed-off-by: Maninder Singh <maninder1.s@samsung.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2015-05-25 13:46:36 +02:00