Commit Graph

868 Commits

Author SHA1 Message Date
Denys Vlasenko
e2dd2afc8e ash: eval: Always set localvar_stop
Upstream commit:

    Date: Thu, 31 May 2018 01:15:34 +0800
    eval: Always set localvar_stop

    The variable localvar_stop is set iff vlocal is true.  gcc doesn't
    get this so we get a spurious warning.

    This patch fixes this by always calling pushlocalvars with vlocal
    and making it only actually do the push if vlocal is non-zero.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-20 10:37:30 +01:00
Denys Vlasenko
3e729102a8 ash: eval: Replace with listsetvar with mklocal/setvareq
Upstream commit:

    Date: Sat, 19 May 2018 02:39:55 +0800
    eval: Replace with listsetvar with mklocal/setvareq

    This patch replaces listsetvar with mklocal/setvareq.  As we now
    determine special built-in status prior to variable assignment, we
    no longer have to do a second pass listsetvar.  Instead we will
    call setvareq directly instead of mklocal when necessary.

    In order to do this mklocal can now take a flag in order to mark
    a variable for export.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-20 09:36:51 +01:00
Denys Vlasenko
54bef2a8ef ash: eval: Fail immediately with redirections errors for simple command
Upstream commit:

    Date: Sat, 19 May 2018 02:39:54 +0800
    eval: Fail immediately with redirections errors for simple command

    Previously, dash would continue to perform variable expansions
    even if a redirection error occured.  This patch changes it so
    that it fails immediately.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-20 09:36:51 +01:00
Denys Vlasenko
7eb8eecbbc ash: eval: Add assignment built-in support again
Upstream commit:

    Date: Sat, 19 May 2018 02:39:52 +0800
    eval: Add assignment built-in support again

    This patch adds assignment built-in support that used to exist
    in dash prior to 0.3.8-15.  This is because it will soon be part
    of POSIX, and the semantics are now much better defined.

    Recognition is done at execution time, so even "command -- export"
    or "var=export; command $var" should work.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-20 09:36:51 +01:00
Denys Vlasenko
23bc562a05 ash,hush: add comment about masked SIGCHLD, handle SIG_IGNed SIGHUP as in bash
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-18 16:46:01 +01:00
Denys Vlasenko
47eb979404 ash: jobs: Only clear gotsigchld when waiting for everything
Upstream commit:

    Date: Sat, 19 May 2018 02:39:41 +0800
    jobs: Only clear gotsigchld when waiting for everything

    The gotsigchld flag is always cleared in dowait but not all callers
    of dowait will wait for everything.  In particular, when jp is set
    we only wait until the set job isn't running anymore.

    This patch fixes this by only clearing gotsigchld if jp is unset.
    It also changes the waitcmd to actually set jp which corresponds
    to the behaviour of bash/ksh93/mksh.

    The only other caller of dowait that doesn't wait for everything
    is the jobless reaper.  This is in fact redundant now that we wait
    after every simple command.  This patch removes it.

    Finally as every caller of dowait needs to wait until either the
    given job is not running, or until all terminated jobs have been
    processed, this patch moves the loop into dowait itself.

    Fixes: 03876c0743a5 ("eval: Reap zombies after built-in...")
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-18 15:37:43 +01:00
Denys Vlasenko
97edfc42f1 ash: jobs - Do not block when waiting on SIGCHLD
Upstream comment:

    Date: Mon, 7 May 2018 00:40:34 +0800
    jobs - Do not block when waiting on SIGCHLD

    Because of the nature of SIGCHLD, the process may have already been
    waited on and therefore we must be prepared for the case that wait
    may block.  So ensure that it doesn't by using WNOHANG.

    Furthermore, multiple jobs may have exited when gotsigchld is set.
    Therefore we need to wait until there are no zombies left.

    Lastly, waitforjob needs to be called with interrupts off and
    the original patch broke that.

    Fixes: 03876c0743a5 ("eval: Reap zombies after built-in...")
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

While at it, removed INT_ON/OFF in waitforjob() - it must be called
from INT_OFF region anyway.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-18 15:37:22 +01:00
Denys Vlasenko
d81af7216b ash: eval: Reap zombies after built-in commands and functions
Upstream commit:

    Date: Mon, 26 Mar 2018 23:55:50 +0800
    eval: Reap zombies after built-in commands and functions

    Currently dash does not reap dead children after built-in commands
    or functions.  This means that if you construct a loop consisting
    of solely built-in commands and functions, then zombies can hang
    around indefinitely.

    This patch fixes this by reaping when necessary after each built-in
    command and function.

    Reported-by: Denys Vlasenko <vda.linux@googlemail.com>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-18 14:28:30 +01:00
Denys Vlasenko
22c75924da ash: exec: Never rehash regular built-ins
Upstream commit:

    Date: Sat, 19 May 2018 02:39:51 +0800
    exec: Never rehash regular built-ins

    As regular (including special) built-ins can never be overridden,
    we should never remove them from the hash table.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-17 16:20:05 +01:00
Denys Vlasenko
6c4f87e411 ash: exec: Stricter pathopt parsing
Upstream comment:

    Date: Sat, 19 May 2018 02:39:50 +0800
    exec: Stricter pathopt parsing

    This patch changes the parsing of pathopt.  First of all only
    %builtin and %func (with arbitrary suffixes) will be recognised.
    Any other pathopt will be treated as a normal directory.

    Furthermore, pathopt can now be specified before the directory,
    rather than after it.  In fact, a future version may remove support
    for pathopt suffixes.

    Wherever the pathopt is placed, an optional % may be placed after
    it to terminate the pathopt.

    This is so that it is less likely that a genuine directory containing
    a % sign is parsed as a pathopt.

    Users of padvance outside of exec.c have also been modified:

    1) cd(1) will always treat % characters as part of the path.
    2) chkmail will continue to accept arbitrary pathopt.
    3) find_dot_file will ignore the %builtin pathopt instead of trying
    to do a stat in the accompanying directory (which is usually the
    current directory).

    The patch also removes the clearcmdentry optimisation where we
    attempt to only partially flush the table where possible.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-17 16:02:40 +01:00
Denys Vlasenko
b0d2dc7d62 ash: exec: Do not allocate stack string in padvance
Upstream commit:

    Date: Sat, 19 May 2018 02:39:48 +0800
    exec: Do not allocate stack string in padvance

    Many callers of padvance immediately free the allocated string
    so this patch moves the stalloc call to the caller.  Instead of
    returning the allocated string, padvance now returns the length
    to allocate (this may be longer than the actual string length,
    even including the NUL).  For the case where we would previously
    return NULL, we now return -1.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-17 15:59:44 +01:00
Denys Vlasenko
c55847fedb ash: memalloc: Add growstackto helper
Upstream commit:

    Date: Sat, 19 May 2018 02:39:46 +0800
    memalloc: Add growstackto helper

    This patch adds the growstackto helper which repeatedly calls
    growstackblock until the requested size is reached.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-17 15:59:08 +01:00
Denys Vlasenko
74aaf05170 ash: parser: Save/restore here-documents in command substitution
Upstream comment:

    Date: Sat, 19 May 2018 02:39:42 +0800
    parser: Save/restore here-documents in command substitution

    This patch changes the parsing of here-documents within command
    substitution, both old style and new style.  In particular, the
    original here-document list is saved upon the beginning of parsing
    command substitution and restored when exiting.

    This means that here-documents outside of command substitution
    can no longer be filled by text within it and vice-versa.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-17 12:11:26 +01:00
Denys Vlasenko
afc91faedd ash: mkinit: Split reset into exitreset and reset
Upstream commit:

    Date: Sat, 19 May 2018 02:39:40 +0800
    mkinit: Split reset into exitreset and reset

    Previously reset was called after exitshell.  This was changed
    so that it was called before exitshell because certain state needed
    to be reset in order for the EXIT trap to work.

    However, this caused issues because certain other states (such
    as local variables) should not be reset.  This patch fixes this
    by creating a new function exitreset that is called prior to
    exitshell and moving reset back to its original location.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-17 11:22:59 +01:00
Denys Vlasenko
9ee5892798 ash: expand: Fix trailing newlines processing in backquote expanding
Upstream commit:

    Date: Mon, 29 Apr 2019 19:13:37 +0500
    expand: Fix trailing newlines processing in backquote expanding

    According to POSIX.1-2008 we should remove newlines only at the end of
    the substitution. Newlines-only substitions causes dash to remove
    newlines before beggining of the substitution. The following code:

        cat <<END
        1
        $(echo "")
        2
        END

    prints "1<newline>2" instead of expected "1<newline><newline>2".

    This patch fixes trailing newlines processing in backquote expanding.

    Signed-off-by: Nikolai Merinov <n.merinov@inango-systems.com>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-17 10:24:32 +01:00
Denys Vlasenko
c2ce888030 ash: parser: Only accept single-digit parameter expansion outside of braces
Upstream commit:

    Date: Mon, 27 May 2019 13:39:37 +0800
    parser: Only accept single-digit parameter expansion outside of braces

    This patch should fix the problem.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-17 10:15:35 +01:00
Denys Vlasenko
3f4847b6d9 ash: shell: Fix clang warnings about "string plus integer"
Upstream commit:

    Date: Sat, 15 Dec 2018 18:49:31 +0100
    shell: Fix clang warnings about "string plus integer"

    Building with clang results in some warnings about integer values being
    added to strings.

    While the code itself is fine and the warnings are indeed harmless,
    fixing them also makes the semantic more explicit: what it is actually
    being increased is the address which points to the start of the string
    in order to skip the initial character when some conditions are met.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-16 19:29:31 +01:00
Denys Vlasenko
e368d851e7 ash: eval: Use the correct expansion mode for fd redirection
Upstream comment:

    Date: Mon, 19 Nov 2018 18:00:32 +0800
    eval: Use the correct expansion mode for fd redirection

    It has been reported that

            echo test >&$EMPTY_VARIABLE

    causes dash to segfault.  This is a symptom of the bigger problem
    that dash tries to perform pathname expansion as well as field
    splitting on the word after >& and <&.  This is wrong and this
    patch fixes it to use the same expansions as done on a normal
    redirection.

    Reported-by: Andrej Shadura <andrew.shadura@collabora.co.uk>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-16 19:24:33 +01:00
Denys Vlasenko
4ace385809 ash: expand: Fix skipping of command substitution when trimming in evalvar
Upstream commit:

    Date: Mon, 28 May 2018 17:09:48 +0800
    expand: Fix skipping of command substitution when trimming in evalvar

    When we are trimming an unset variable in evalvar, any embedded
    command substitution that should have been skipped are not.  This
    can cause them to be evaluated later should there be other command
    substitutions in the same input word.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-16 19:22:32 +01:00
Denys Vlasenko
226b8a143d ash: main: Print \n upon EOF (CTRL-D) when run interactively
Upstream comment:

    Date: Fri, 7 Sep 2018 10:34:14 +0200
    main: Print \n upon EOF (CTRL-D) when run interactively

    Exiting dash via a ^D instead of with "exit" causes dash to forget to
    print a newline.

        sh-3.1$ sh
        sh-3.1$ ^D
        sh-3.1$ dash
        $ sh-3.1$

    It is more neat and tidy to send a newline similarly to what bash does,
    so it doesn't make the next prompt of the parent shell look ugly.

    Suggested by jidanni.

    Signed-off-by: Gerrit Pape <pape@smarden.org>
    Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
    [reworded the patch description]
    Signed-off-by: Andrej Shadura <andrew.shadura@collabora.co.uk>
    Bug-Debian: http://bugs.debian.org/476422
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-16 19:16:52 +01:00
Denys Vlasenko
e880b1fea8 ash: expand: Use HOME in tilde expansion when it is empty
Upstream commit:

    Date: Sun, 27 May 2018 17:31:57 +0800
    expand: Use HOME in tilde expansion when it is empty

    Currently if HOME is set to empty tilde expansion will fail, i.e.,
    it will remain as a literal tilde.  This patch changes it to
    return the empty string as required by POSIX.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-16 19:16:35 +01:00
Denys Vlasenko
a7b97e367c ash: builtin: Mark more regular built-ins
Upstream commit:

    Date: Sat, 19 May 2018 02:39:49 +0800
    builtin: Mark more regular built-ins

    This patch marks the following built-ins as regular, meaning that
    they cannot be overriden using PATH search:

            hash
            pwd
            type
            ulimit

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-16 19:14:45 +01:00
Denys Vlasenko
2bad3a305b ash: jobs: Replace some uses of fmtstr with stpcpy/stpncpy
Upstream commit:

    Date: Sat, 19 May 2018 02:39:45 +0800
    jobs: Replace some uses of fmtstr with stpcpy/stpncpy

    Some uses of fmtstr, particularly the ones without a format string,
    can be replaced with stpcpy or stpncpy.  This patch does that so
    we don't have to introduce unnecessary format strings in order to
    silence compiler warnings.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-16 19:14:45 +01:00
Denys Vlasenko
3f7fb2c89a ash: output: Fix fmtstr return value
Upstream commit:

    Date: Sat, 19 May 2018 02:39:44 +0800
    output: Fix fmtstr return value

    The function fmtstr is meant to return the actual length of output
    produced, rather than the untruncated length.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-16 19:14:45 +01:00
Denys Vlasenko
970470e235 ash: main: Only set savestatus in exitcmd
Upstream commit:

    Date: Sat, 19 May 2018 02:39:38 +0800
    main: Only set savestatus in exitcmd

    Currently exitcmd sets exitstatus and then savestatus if the latter
    was previously set.  In fact, as exitcmd always raises an exception
    and will either end up in the setjmp call in main() or exitshell(),
    where exitstatus is always replaced by savestatus if set, we only
    need to set savestatus.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-16 19:14:45 +01:00
Denys Vlasenko
4ccddc8fb3 ash: [BUILTIN] Exit without arguments in a trap should use status outside traps
Upstream commit:

    Date:   Mon Oct 6 10:39:47 2014 +0800
    [BUILTIN] Exit without arguments in a trap should use status outside traps

    POSIX now requires that exit without arguments in a trap should
    return the last command status prior to executing traps.  This
    patch implements this behaviour.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-16 19:14:45 +01:00
Denys Vlasenko
f7eea8c235 ash: parser: Fix incorrect eating of backslash newlines
Keeping up with upstream (in our case, 'before patch' code is not buggy).
Upstream commit:

    Date: Fri, 11 May 2018 23:41:25 +0800
    parser: Fix incorrect eating of backslash newlines

    With the introduction of synstack->syntax, a number of references
    to the syntax variable was missed during the conversion.  This
    causes backslash newlines to be incorrectly removed in single
    quote context.

    This patch also combines these calls into a new helper function
    pgetc_top.

    Fixes: ab1cecb40478 ("parser: Add syntax stack for recursive...")
    Reported-by: Leah Neukirchen <leah@vuxu.org>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-16 19:14:45 +01:00
Denys Vlasenko
acf79f9913 ash: use pgetc_eatbnl() in more places, take 2
Adding previously skipped "readtoken1(pgetc_eatbnl(), DQSYNTAX..." changes
from upstream commit:

    Date:   Thu Mar 8 08:37:11 2018 +0100
    Author: Harald van Dijk <harald@gigawatt.nl>
    parser: use pgetc_eatbnl() in more places

    dash has a pgetc_eatbnl function in parser.c which skips any
    backslash-newline combinations. It's not used everywhere it could be.
    There is also some duplicated backslash-newline handling elsewhere in
    parser.c. Replace most of the calls to pgetc() with calls to
    pgetc_eatbnl() and remove the duplicated backslash-newline handling.

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

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-02-16 19:14:45 +01:00
Ron Yorston
9e2a5668fd ash,hush: allow builtins to be tab-completed, closes 7532
function                                             old     new   delta
complete_cmd_dir_file                                678     830    +152
get_builtin_name                                       -      35     +35
optschanged                                          125     132      +7
hush_main                                           1069    1076      +7
save_command_ps_at_cur_history                        76      78      +2
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 4/0 up/down: 203/0)             Total: 203 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-01-29 15:23:17 +01:00
Ron Yorston
b0c711e64f ash: improve expandstr()
The dash maintainer recently posted a fix for issues with expanding
PS1.  These had already been fixed differently in BusyBox ash.  Borrow
a couple of improvements:

- Use a single call to setjmp() to trap errors in both readtoken1()
  and expandarg().

- In case of error set the prompt to the literal value of PS1 rather
  than the half-digested nonsense in stackblock() which might include
  ugly control characters.

function                                             old     new   delta
expandstr                                            353     300     -53

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2020-01-29 13:59:45 +01:00
Denys Vlasenko
af7169b4a7 clang/llvm 9 fix - do not eliminate a store to a fake "const"
This is *much* better (9 kbytes better) than dropping "*const"
optimization trick.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-10-25 12:12:22 +02:00
Denys Vlasenko
35e349de3c ash: add a FIXME comment
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-09-05 14:31:49 +02:00
Denys Vlasenko
2f9c124f7d ash: fix set -o to not show "nameless" options
Patch by Martijn Dekker <martijn@inlv.org>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-08-02 16:44:35 +02:00
Ron Yorston
943e81f5db ash: only catch unexpected exceptions in PS1 expansion
Commit d1a2fa2a4 (ash: catch error in arithmetic expansion in PS1)
catches all exceptions raised by expandarg().  Some exceptions, such as
the EXEXIT raised when command expansion is used, are expected:

   export PS1='$(echo "$ ")'

These should be processed normally or the shell hangs at the prompt.

function                                             old     new   delta
expandstr                                            344     353      +9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 9/0)                 Total: 9 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-31 13:44:32 +02:00
Denys Vlasenko
f3634584d0 ash,hush: show 'c' in $- if run in "sh -c CMD"
function                                             old     new   delta
options                                              552     599     +47
expand_one_var                                      2375    2385     +10
optletters_optnames                                   60      64      +4
hush_main                                           1108    1111      +3
ash_main                                            1150    1152      +2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 5/0 up/down: 66/0)               Total: 66 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-06-03 12:21:04 +02:00
Denys Vlasenko
897475ab02 ash: allocate line editing structure only if needed
function                                             old     new   delta
optschanged                                           91     128     +37
historycmd                                            13      17      +4
setcmd                                                80      78      -2
ash_main                                            1167    1150     -17
options                                              576     552     -24
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/3 up/down: 41/-43)             Total: -2 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-06-01 16:35:09 +02:00
Ron Yorston
d1a2fa2a4e ash: catch error in arithmetic expansion in PS1
Setting PS1 to:

   PS1='$((123+))'

causes the shell to enter an infinite error loop:

   sh: arithmetic syntax error

Catch any exception raised by expandarg() in expandstr() and allow
processing to continue.

function                                             old     new   delta
expandstr                                            262     344     +82
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 82/0)               Total: 82 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-04-19 13:21:34 +02:00
Ron Yorston
48645b8350 ash: prevent error in backquotes in PS1 from exiting shell
Setting PS1 to:

   PS1='`xxx(`'

causes the shell to terminate with the error:

   sh: syntax error: unexpected end of file (expecting ")")

This happens because old-style backquotes require the input to be reread
and thus call setinputstring() a second time.  Prevent the problem by
unwinding all recently opened files in expandstr().

function                                             old     new   delta
unwindfiles                                            -      22     +22
expandstr                                            247     262     +15
forkchild                                            631     625      -6
evalcommand                                         1694    1685      -9
ash_main                                            1346    1336     -10
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/3 up/down: 37/-25)             Total: 12 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-04-19 13:21:34 +02:00
Ron Yorston
1d37186fe2 ash: add bash-compatible EPOCH variables
Bash 5.0 added the dynamic variable EPOCHSECONDS and EPOCHREALTIME
which return the number of seconds since the Unix Epoch as an
integer or float.  These are useful for logging or tracing.

function                                             old     new   delta
change_epoch                                           -      78     +78
.rodata                                           175167  175235     +68
varinit_data                                         264     312     +48
change_seconds                                         -      24     +24
change_realtime                                        -      24     +24
------------------------------------------------------------------------------
(add/remove: 3/0 grow/shrink: 2/0 up/down: 242/0)             Total: 242 bytes
   text	   data	    bss	    dec	    hex	filename
 938508	   4203	   1888	 944599	  e69d7	busybox_old
 938702	   4203	   1888	 944793	  e6a99	busybox_unstripped

v2: Cast tv_sec and tv_usec to unsigned quantities.
    Add brackets to macros.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-04-16 18:29:52 +02:00
Ron Yorston
d96c69d876 ash: an unset dynamic variable should not be dynamic
Commit b28d4c346 (ash: [VAR] Move unsetvar functionality into setvareq)
dropped the code that caused dynamic variables to lose their special
properties when unset.  Add it back again.

function                                             old     new   delta
setvareq                                             346     360     +14
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 14/0)               Total: 14 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-04-16 18:29:52 +02:00
Ron Yorston
e48559eae3 ash: distinguish 'wait -n' from other bashisms
Add a specific define to indicate which bash compatibility code
implements 'wait -n'.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-04-02 13:43:40 +02:00
Denys Vlasenko
966f087ab4 ash: add "wait -n" bashism
function                                             old     new   delta
waitcmd                                              205     288     +83
dowait                                               405     444     +39
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 122/0)             Total: 122 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-03-27 15:52:17 +01:00
Ron Yorston
f55161ad27 ash: eval: avoid leaking memory associated with redirections. Closes 7748
The following constructs result in ever-increasing memory usage:

   while true; do { true; } </dev/null; done
   while true; do ( true; ) </dev/null; done

For comparison, bash displays static memory usage in both cases.

This has been fixed in dash by commit 2bc6caa.  The maintainer
writes:

   I have simplified evaltree so that it simply sets the stack mark
   unconditionally.  This allows us to remove the stack marks in the
   functions called by evaltree.

Closes BusyBox bug 7748.

function                                             old     new   delta
evaltree                                             606     632     +26
evalcommand                                         1724    1696     -28
evalcase                                             382     351     -31
evalfor                                              230     196     -34
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/3 up/down: 26/-93)            Total: -67 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-02-25 18:57:59 +01:00
Denys Vlasenko
b097a84d62 config: update size information
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-12-28 03:20:17 +01:00
Denys Vlasenko
b437df1157 inetd: suppress aliasing warning
function                                             old     new   delta
sigprocmask2                                           -       8      +8
wait_for_child_or_signal                             213     218      +5
dowait                                               424     429      +5
block_CHLD_HUP_ALRM                                   62      59      -3
sigprocmask_SIG_SETMASK                               16       -     -16
------------------------------------------------------------------------------
(add/remove: 1/1 grow/shrink: 2/1 up/down: 18/-19)             Total: -1 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-12-08 15:35:24 +01:00
Denys Vlasenko
136fe9bede suppress gcc 8 aliasing warnings
function                                             old     new   delta
sigprocmask_SIG_SETMASK                                -      16     +16
wait_for_child_or_signal                             221     213      -8
dowait                                               432     424      -8

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-12-08 13:49:15 +01:00
Ron Yorston
71df2d3589 hush: allow hush to run embedded scripts
Embedded scripts require a shell to be present in the BusyBox
binary.  Allow either ash or hush to be used for this purpose.
If both are enabled ash takes precedence.

The size of the binary is unchanged in the default configuration:
both ash and hush are present but support for embedded scripts
isn't compiled into hush.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-11-27 16:13:07 +01:00
Christoph Schulz
03ad7ae081 ash: reset tokpushback before prompting while parsing heredoc
The parser reads from an already freed memory location, thereby causing
unpredictable results, in the following situation:

- ENABLE_ASH_EXPAND_PRMT is enabled
- heredoc is being parsed
- command substitution is used within heredoc

Examples where this bug crops up are (PS2 is set to "> "):

$ cat <<EOF
> `echo abc`
> EOF
-sh: O: not found

$ cat <<EOF
> $(echo abc)
> EOF
-sh: {garbage}: not found

The presumable reason is that setprompt_if() causes a nested expansion when
ENABLE_ASH_EXPAND_PRMT is enabled, therefore leaving "wordtext" in an unusable
state. However, when parseheredoc() is called, "tokpushback" is non-zero, which
causes the next call to xxreadtoken() to return TWORD, causing the caller to
use the invalid "wordtoken" instead of reading the next valid token.

The call chain is:

list()
-> peektoken() [sets tokpushback to 1]
-> parseheredoc()
   -> setprompt_if()
      -> pushstackmark()
      -> expandstr()
         -> readtoken1()
            [sets lasttoken to TWORD, wordtoken points to expanded prompt]
      -> popstackmark() [invalidates wordtoken, leaves lasttoken as is]
   -> readtoken1()
      -> ...parsebackq
         -> list()
            -> andor()
               -> pipeline()
                  -> readtoken()
                     -> xxreadtoken()
                        [tokpushback non-zero, reuse lasttoken and wordtext]

Note that in almost all other contexts, each call to setprompt_if() is preceded
by setting "tokpushback" to zero. One exception is "oldstyle" backquote parsing
in readtoken1(), but there "tokpushback" is reset afterwards. The other
exception is nlprompt(), but this function is only used within readtoken1()
(but in contexts where no nested calls to xxreadtoken() occur) and xxreadtoken()
(where "tokpushback" is guaranteed to be zero).

function                                             old     new   delta
parseheredoc                                         124     131      +7

Signed-off-by: Christoph Schulz <develop@kristov.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-11-20 17:45:52 +01:00
Ron Yorston
3778898f97 Treat custom and applet scripts as applets
BusyBox has support for embedded shell scripts.  Two types can be
distinguished:  custom scripts and scripts implementing applets.

Custom scripts should be placed in the 'embed' directory at build
time.  They are given a default applet configuration and appear
as applets to the user but no further configuration is possible.

Applet scripts are integrated with the BusyBox build system and
are intended to be used to ship standard applets that just happen
to be implemented as scripts.  They can be configured at build time
and appear just like native applets.

Such scripts should be placed in the 'applets_sh' directory.  A stub
C program should be written to provide the usual applet configuration
details and placed in a suitable subsystem directory.  It may be
helpful to have a configuration option to enable any dependencies the
script requires:  see the 'nologin' applet for an example.

function                                             old     new   delta
scripted_main                                          -      41     +41
applet_names                                        2773    2781      +8
applet_main                                         1600    1604      +4
i2cdetect_main                                       672     674      +2
applet_suid                                          100     101      +1
applet_install_loc                                   200     201      +1
applet_flags                                         100     101      +1
packed_usage                                       33180   33179      -1
tryexec                                              159     152      -7
evalcommand                                         1661    1653      -8
script_names                                           9       -      -9
packed_scripts                                       123     114      -9
complete_cmd_dir_file                                826     811     -15
shellexec                                            271     254     -17
find_command                                        1007     990     -17
busybox_main                                         642     624     -18
run_applet_and_exit                                  100      78     -22
find_script_by_name                                   51       -     -51
------------------------------------------------------------------------------
(add/remove: 1/2 grow/shrink: 6/9 up/down: 58/-174)          Total: -116 bytes
   text	   data	    bss	    dec	    hex	filename
 950034	    477	   7296	 957807	  e9d6f	busybox_old
 949918	    477	   7296	 957691	  e9cfb	busybox_unstripped

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-11-17 21:16:33 +01:00
Ron Yorston
e6a63bf683 ash: ensure variables are fully initialised when unset
When a variable is unset by calling setvar(name, NULL, 0) the code
to initialise the new, empty variable fails to initialise the last
character of the string.

Attempts to read the contents of the unset variable will result
in the uninitialised character at the end of the string being
accessed.

For example, running BusyBox under Valgrind and unsetting PATH:

$ valgrind ./busybox_unstripped sh
==21249== Memcheck, a memory error detector
==21249== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==21249== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
==21249== Command: ./busybox_unstripped sh
==21249==
/data2/git/build_fix_8721 $ unset PATH
/data2/git/build_fix_8721 $ 0
==21249== Conditional jump or move depends on uninitialised value(s)
==21249==    at 0x451371: path_advance (ash.c:2555)
==21249==    by 0x456E22: find_command (ash.c:13407)
==21249==    by 0x458425: evalcommand (ash.c:10139)
==21249==    by 0x454CBC: evaltree (ash.c:9131)
==21249==    by 0x456C80: cmdloop (ash.c:13164)

Closes https://bugs.busybox.net/show_bug.cgi?id=8721

v2: On the dash mailing list Harald van Dijk was kind enough to point
    out a flaw in my reasoning and provide an alternative patch.  Sadly
    his patch adds 2 bytes of bloat.  Using xzalloc to zero the whole
    string gives a bloat of -3 bytes.

function                                             old     new   delta
setvar                                               172     169      -3

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-11-16 17:28:01 +01:00