oweals/busybox.git
4 years agorandomconfig fixes
Denys Vlasenko [Tue, 23 Jun 2020 01:13:55 +0000 (03:13 +0200)]
randomconfig fixes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agorandomconfig fixes
Denys Vlasenko [Tue, 23 Jun 2020 00:46:53 +0000 (02:46 +0200)]
randomconfig fixes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoexamples/udhcp/simple.script: fix IPv6 support when using udhcpc
Stefan Agner [Tue, 16 Jun 2020 08:06:27 +0000 (10:06 +0200)]
examples/udhcp/simple.script: fix IPv6 support when using udhcpc

The udhcpc script calls ip addr flush .. which flushes addresses
of any address family, including IPv6. However, busybox udhcpc is
IPv4 only and should not influence IPv6 addressing. Hence use ip
addr flush with family constraint.

The script particularly broke IPv6 SLAAC: Typically when udhcpc
calls the script the kernel already assigned the IPv6 link-local
address. The flush removes the link-local IPv6 address again and
prohibits proper IPv6 operation such as SLAAC since neighbor
discovery protocol relies on IPv6 link-local addressing.

Signed-off-by: Stefan Agner <stefan.agner@toradex.com>
[Taken from https://git.openembedded.org/openembedded-core/commit/meta/recipes-core/busybox/files/simple.script?id=b77541dbb2f442e51842f9d24c8745a6df2d1478]
Signed-off-by: Quentin Schulz <quentin.schulz@streamunlimited.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoudhcpc6: use correct multicast MAC
Uwe Glaeser [Sun, 21 Jun 2020 00:56:12 +0000 (02:56 +0200)]
udhcpc6: use correct multicast MAC

function                                             old     new   delta
static.MAC_DHCP6MCAST_ADDR                             -       6      +6

Signed-off-by: Uwe Glaeser <uwe.glaeser@dormakaba.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agonmeter: add %T (zero-based timestamp) format
Denys Vlasenko [Wed, 10 Jun 2020 13:37:39 +0000 (15:37 +0200)]
nmeter: add %T (zero-based timestamp) format

function                                             old     new   delta
collect_tv                                             -     132    +132
collect_monotonic                                      -      61     +61
nmeter_main                                          754     778     +24
gmtime                                                 -      21     +21
init_monotonic                                         -      18     +18
init_functions                                        44      48      +4
packed_usage                                       33432   33420     -12
collect_time                                         125      19    -106
------------------------------------------------------------------------------
(add/remove: 5/0 grow/shrink: 2/2 up/down: 260/-118)          Total: 142 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoMakefile.flags: restrict Wno-constant-logical-operand and Wno-string-plus-int options...
Biswapriyo Nath [Tue, 9 Jun 2020 17:35:12 +0000 (23:05 +0530)]
Makefile.flags: restrict Wno-constant-logical-operand and Wno-string-plus-int options for clang

these options were added in b4ef2e3467d8e980ccf13c9dd342459c013b455f commit
gcc shows unrecognized command-line option warnings

Signed-off-by: Biswapriyo Nath <nathbappai@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agodeluser: check if specified home is a directory before removing it
Sören Tempel [Tue, 9 Jun 2020 15:51:26 +0000 (17:51 +0200)]
deluser: check if specified home is a directory before removing it

On Alpine, some users use /dev/null as a home directory. When removing
such a user with `deluser --remove-home` this causes the /dev/null
device file to be removed which is undesirable. To prevent this pitfall,
check if the home directory specified for the user is an actual
directory (or a symlink to a directory).

Implementations of similar tools for other operating systems also
implement such checks. For instance, the OpenBSD rmuser(1)
implementation [0].

[0]: https://github.com/openbsd/src/blob/b69faa6c70c5bfcfdddc6138cd8e0ee18cc15b03/usr.sbin/adduser/rmuser.perl#L143-L151

function                                             old     new   delta
deluser_main                                         337     380     +43

Signed-off-by: Sören Tempel <soeren+git@soeren-tempel.net>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agodhcpc: refactor xmalloc_optname_optval to shrink binary size
Martin Lewis [Tue, 9 Jun 2020 21:59:54 +0000 (16:59 -0500)]
dhcpc: refactor xmalloc_optname_optval to shrink binary size

function                                             old     new   delta
len_of_option_as_string                               14      13      -1
dhcp_option_lengths                                   14      13      -1
udhcp_str2optset                                     641     637      -4
static.xmalloc_optname_optval                        777     718     -59
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/4 up/down: 0/-65)             Total: -65 bytes

Signed-off-by: Martin Lewis <martin.lewis.x84@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agohttpd: allow '-h' to work when daemonized with NOMMU enabled
Ron Yorston [Tue, 9 Jun 2020 15:38:21 +0000 (17:38 +0200)]
httpd: allow '-h' to work when daemonized with NOMMU enabled

Commit d1b75e184 (httpd: permit non-default home directory with NOMMU
enabled) only works when used with the '-f' (foreground) option.

When '-f' isn't specified and NOMMU is enabled bb_daemonize_or_rexec()
is called to daemonize the server.  Since the server process has been
re-execed the previous patch results in the xchdir() not being called.

Fix this by resetting the re_execed variable in this case.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoudhcp: comment out unused domain compression code
Denys Vlasenko [Tue, 9 Jun 2020 15:22:06 +0000 (17:22 +0200)]
udhcp: comment out unused domain compression code

function                                             old     new   delta
attach_option                                        411     406      -5
dname_enc                                            381     167    -214
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-219)           Total: -219 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agosysctl: do report EACCES errors on write
Denys Vlasenko [Tue, 9 Jun 2020 13:58:32 +0000 (15:58 +0200)]
sysctl: do report EACCES errors on write

function                                             old     new   delta
sysctl_act_recursive                                 165     179     +14
sysctl_act_on_setting                                467     471      +4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 18/0)               Total: 18 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agounexpand: correct behavior for --first-only --tabs=4
Mark Edgar [Sat, 30 May 2020 17:05:46 +0000 (19:05 +0200)]
unexpand: correct behavior for --first-only --tabs=4

Prior to the patch, both -f and --first-only are in all cases either
no-op or ignored.
Without --tabs, --first-only is the default so specifying it is a no-op.
With --tabs, --all is implied, and --first-only is intended to reset this.

function                                             old     new   delta
expand_main                                          690     694      +4

Signed-off-by: Mark Edgar <medgar123@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoxstrndup: Use strndup instead of implementing it.
Martin Lewis [Sun, 8 Mar 2020 18:35:20 +0000 (13:35 -0500)]
xstrndup: Use strndup instead of implementing it.

Signed-off-by: Martin Lewis <martin.lewis.x84@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agodhcpc: code shrink in good_hostname
Martin Lewis [Mon, 4 May 2020 21:59:14 +0000 (16:59 -0500)]
dhcpc: code shrink in good_hostname

Incorporated valid_domain_label into good_hostname to simplify the implementation.

function                                             old     new   delta
static.xmalloc_optname_optval                        973     958     -15
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-15)             Total: -15 bytes
   text    data     bss     dec     hex filename
 993144   16915    1872 1011931   f70db busybox_old
 993129   16915    1872 1011916   f70cc busybox_unstripped

Signed-off-by: Martin Lewis <martin.lewis.x84@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoawk: disallow "str"++, closes bug 12981
Denys Vlasenko [Mon, 8 Jun 2020 23:33:54 +0000 (01:33 +0200)]
awk: disallow "str"++, closes bug 12981

function                                             old     new   delta
parse_expr                                           887     896      +9

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agowget: implement TLS verification with ENABLE_FEATURE_WGET_OPENSSL
Dimitri John Ledkov [Tue, 19 May 2020 17:20:39 +0000 (18:20 +0100)]
wget: implement TLS verification with ENABLE_FEATURE_WGET_OPENSSL

When ENABLE_FEATURE_WGET_OPENSSL is enabled, correctly implement TLS
verification by default. And only ignore verification errors, if
--no-check-certificate was passed.

Also note, that previously OPENSSL implementation did not implement
TLS verification, nor printed any warning messages that verification
was not performed.

Bug-Ubuntu: https://bugs.launchpad.net/bugs/1879533

CVE-2018-1000500

Signed-off-by: Dimitri John Ledkov <xnox@ubuntu.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash,hush: drop pointer check before calls to show_history
Ron Yorston [Mon, 16 Dec 2019 09:31:10 +0000 (09:31 +0000)]
ash,hush: drop pointer check before calls to show_history

show_history() checks that its argument in non-null so there's
no need to repeat the test at call sites.

function                                             old     new   delta
historycmd                                            25      17      -8
builtin_history                                       29      21      -8
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-16)             Total: -16 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agomim: run scripts from a specification file
Ron Yorston [Mon, 13 Jan 2020 10:33:46 +0000 (10:33 +0000)]
mim: run scripts from a specification file

mim runs scripts from a specification file which can be thought
of as an extremely limited Makefile. Neither make variables nor
dependencies are supported. By default the file 'Mimfile' is read.
An example:

   hello:
      echo hello $1

   clean:
      rm -rf *

The command 'mim' or 'mim hello' will echo 'hello'. Unlike 'make'
arguments after the first are available to the script; they don't
specify additional targets.

mim isn't enabled by default.  Enabling it increases the size of the
binary by about 500 bytes.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: fix build failure when command built-in is disabled
Ron Yorston [Thu, 27 Feb 2020 09:50:18 +0000 (09:50 +0000)]
ash: fix build failure when command built-in is disabled

Since commit 7eb8eecbb (ash: eval: Add assignment built-in support
again) building BusyBox with the 'command' built-in disabled fails.

parse_command_args() only needs to be called when the 'command'
built-in is run.  Which it won't be if it's disabled.

v2: Avoiding infinite loops is good, too.  Thanks, Harald van Dijk.

Reported-by: Deweloper <deweloper@wp.pl>
Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agohttpd: permit non-default home directory with NOMMU enabled
Ron Yorston [Tue, 7 Apr 2020 09:41:34 +0000 (10:41 +0100)]
httpd: permit non-default home directory with NOMMU enabled

When BusyBox is compiled with NOMMU enabled running httpd with
the '-h' option fails even if the specified directory exists:

   $ ls -d www
   www
   $ busybox httpd  -fvvvp 8080 -h www
   ...
   ... try to access http://localhost:8080/www
   ...
   httpd: can't change directory to 'www': No such file or directory

The parent process executes xchdir("www").  When a connection is accepted
it's handled by re-executing httpd in inetd mode.  The child process
inherits the current directory "www" and tries to change directory again
to "www", which fails.

Omit the call to xchdir() when httpd is re-executed.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agogrep: add proper support for pattern_list
Sören Tempel [Mon, 30 Mar 2020 09:02:08 +0000 (11:02 +0200)]
grep: add proper support for pattern_list

From POSIX.1-2008:

The pattern_list's value shall consist of one or more patterns
separated by <newline> characters;

As such, given patterns need to be split at newline characters. Without
doing so, busybox grep will interpret the newline as part of the pattern
which is not in accordance with POSIX.

See also: https://bugs.busybox.net/show_bug.cgi?id=12721

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agogrep: Fix -f FILE when FILE is empty and -x provided
Gray Wolf [Wed, 29 Apr 2020 13:49:17 +0000 (15:49 +0200)]
grep: Fix -f FILE when FILE is empty and -x provided

Grep currently special-cased empty pattern file to be the same as
pattern file with one empty line (empty pattern). That does mirror how
GNU grep behaves, except when -x is provided. In that case .* pattern
needs to be used instead.

Signed-off-by: Gray Wolf <wolf@wolfsden.cz>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoxargs: restore correct behaviour of -n option
Ron Yorston [Wed, 19 Feb 2020 11:01:39 +0000 (11:01 +0000)]
xargs: restore correct behaviour of -n option

Since commit 1ff7002b1 (xargs: fix handling of quoted arguments, closes
11441) the -n option hasn't worked properly:

   $ echo 1 2 3 | xargs -n 1 echo
   1
   2

   3

   $

Because state is now remembered between calls to process_stdin() it's
necessary to update the state before any premature return.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agobuild system: remove KBUILD_STR()
Jean-Philippe Brucker [Thu, 12 Mar 2020 16:19:45 +0000 (17:19 +0100)]
build system: remove KBUILD_STR()

When using GNU Make >=4.3, the KBUILD_STR() definition interferes badly
with dependency checks during build, and forces a complete rebuild every
time Make runs.

In if_changed_rule, Kconfig checks if the command used to build a file
has changed since last execution. The previous command is stored in the
generated .<file>.o.cmd file. For example applets/.applets.o.cmd defines
a "cmd_applets/applets.o" variable:

cmd_applets/applets.o := gcc ... -D"KBUILD_STR(s)=#s" ...

Here the '#' should be escaped with a backslash, otherwise GNU Make
interprets it as starting a comment, and ignore the rest of the
variable. As a result of this truncation, the previous command doesn't
equal the new command and Make rebuilds each target.

The problem started to appear when GNU Make 4.3 (released January 2020),
introduced a backward-incompatible fix to macros containing a '#'. While
the above use of '#', a simple Make variable, still needs to be escaped,
a '#' within a function invocation doesn't need to be escaped anymore.
As Martin Dorey explained on the GNU Make discussion [1], the above
declaration is generated from make-cmd, defined as:

make-cmd = $(subst \#,\\\#,$(subst $$,$$$$,$(call escsq,$(cmd_$(1))))

Since GNU Make 4.3, the first argument of subst should not have a
backslash. make-cmd now looks for literally \# and doesn't find it, and
as a result doesn't add the backslash when generating .o.cmd files.

[1] http://savannah.gnu.org/bugs/?20513

We could fix it by changing make-cmd to "$(subst #,\#,...)", but to
avoid compatibility headaches, simply get rid of the KBUILD_STR
definition, as done in Linux by b42841b7bb62 ("kbuild: Get rid of
KBUILD_STR"). Quote the string arguments directly rather than asking the
preprocessor to quote them.

Signed-off-by: Jean-Philippe Brucker <jean-philippe@linaro.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agounzip: -d should create the dir
Lauri Kasanen [Wed, 15 Apr 2020 12:01:44 +0000 (15:01 +0300)]
unzip: -d should create the dir

The official Info-Zip unzip creates the dir if it doesn't exist.

Signed-off-by: Lauri Kasanen <cand@gmx.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoshells: fix exitcode_trapN tests to avoid races
Denys Vlasenko [Wed, 26 Feb 2020 08:05:52 +0000 (09:05 +0100)]
shells: fix exitcode_trapN tests to avoid races

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: expand: Fix multiple issues with EXP_DISCARD in evalvar
Denys Vlasenko [Sat, 22 Feb 2020 18:38:40 +0000 (19:38 +0100)]
ash: expand: Fix multiple issues with EXP_DISCARD in evalvar

Upstream commit:

    Date: Wed, 12 Sep 2018 14:27:16 +0800
    expand: Fix multiple issues with EXP_DISCARD in evalvar

    The commit 3cd538634f71538370f5af239f342aec48b7470b broke parameter
    expansion in multiple ways because the EXP_DISCARD flag wasn't set
    or tested for various cases:

            $ src/dash -c 'var=; echo ${var:+nonempty}'
            nonempty
            $ src/dash -u -c 'unset foo bar; echo ${foo+${bar}}'
            dash: 1: bar: parameter not set
            $ src/dash -c 'foo=bar; echo ${foo=BUG}; echo $foo'
            barBUG
            bar
            $

    This patch fixes them by introducing a new discard variable that
    tracks whether the extra word should be discarded or not when it
    is parsed.

Reported-by: Martijn Dekker <martijn@inlv.org>
    Fixes: 3cd538634f71 ("expand: Do not reprocess data when...")
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Reported-by: Martijn Dekker <martijn@inlv.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: expand: Do not reprocess data when expanding words
Denys Vlasenko [Mon, 24 Feb 2020 09:02:50 +0000 (10:02 +0100)]
ash: expand: Do not reprocess data when expanding words

Upstream patch:

    Date: Wed, 30 May 2018 02:06:03 +0800
    expand: Do not reprocess data when expanding words

    Currently various paths will reprocess data when performing word
    expansion.  For example, expari will skip backwards looking for
    the start of the arithmetic expansion, while evalvar will skip
    unexpanded words manually.

    This is cumbersome and error-prone.  This patch fixes this by
    making word expansions proceed in a linear fashion.  This means
    changing argstr and the various expansion functions such as expari
    and subevalvar to return the next character to be expanded.

    This is inspired by similar code from FreeBSD.  However, we take
    things one step further and completely remove the manual word
    skipping in evalvar.  This is accomplished by introducing a new
    EXP_DISCARD flag that tells argstr to only parse and not produce
    any actual expansions.

    Incidentally, argstr will now always NUL-terminate the expansion
    unless the EXP_WORD flag is set.  This is because all but one
    caller of argstr wants the result to be NUL-termianted.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Also includes two one-line follow-up fixes:

    expand: Eat closing brace for length parameter
            if (subtype == VSLENGTH) {
    +               p++;
                    if (flag & EXP_DISCARD)
    expand: Fix double-decrement in argstr
    -               newloc = expdest - (char *)stackblock() - end;
    +               newloc = q - (char *)stackblock() - end;

and changes in code for bash substring extensions.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: move TRACE statement in evalcommand()
Ron Yorston [Fri, 21 Feb 2020 16:16:56 +0000 (16:16 +0000)]
ash: move TRACE statement in evalcommand()

Following recent work on evalcommand() a TRACE statement to report
the status of a forked command was left in the wrong place.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: rename some function parameters to match dash
Denys Vlasenko [Mon, 24 Feb 2020 08:57:08 +0000 (09:57 +0100)]
ash: rename some function parameters to match dash

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: rename stack_nputstr() back to stnputs() to match dash
Denys Vlasenko [Sat, 22 Feb 2020 18:11:41 +0000 (19:11 +0100)]
ash: rename stack_nputstr() back to stnputs() to match dash

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: Expand here-documents in the current shell environment
Denys Vlasenko [Sat, 22 Feb 2020 19:25:03 +0000 (20:25 +0100)]
ash: Expand here-documents in the current shell environment

Upstream commit:

    Date: Sun, 11 Nov 2007 15:27:00 +0800
    Expand here-documents in the current shell environment

    Previously we always expanded here-documents in a subshell.  This is
    contrary to the POSIX specification and how other shells behave.  What's
    more this slows down many expansions due to the extra fork (however, it
    must be said that it is possible for it speed up certain expansions by
    running it simultaneously with the command on two CPUs).

    This patch move the expansion into the current shell environment.

    Test case:

            unset a
            cat <<- EOF > /dev/null
                    ${a=NOT}
            EOF
            echo ${a}BAD

    Old result:

            BAD

    New result:

            NOTBAD

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: parser: Do not push token back before parseheredoc
Denys Vlasenko [Sat, 22 Feb 2020 16:26:23 +0000 (17:26 +0100)]
ash: parser: Do not push token back before parseheredoc

Upstream commit:

    Date: Mon, 19 Nov 2018 18:43:58 +0800
    parser: Do not push token back before parseheredoc

    When we read the first token in list() we use peektoken instead
    of readtoken as the following code needs to use the same token
    again.  However, this is wrong when we're in a here-document as
    it will clobber the saved token without resetting the tokpushback
    flag.

    This patch fixes it by doing the tokpushback after parseheredoc
    and setting lasttoken again if parseheredoc was called.

Reported-by: Ron Yorston <rmy@frippery.org>
    Fixes: 7c245aa8ed33 ("[PARSER] Simplify EOF/newline handling in...")
    Fixes: ee5cbe9fd6bc ("[SHELL] Optimize dash -c "command" to avoid a fork")
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Tested-by: Simon Ser <contact@emersion.fr>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: parser: Fix old-style command substitution here-document crash
Denys Vlasenko [Sat, 22 Feb 2020 15:39:27 +0000 (16:39 +0100)]
ash: parser: Fix old-style command substitution here-document crash

Upstream commit:

    Date: Fri, 29 Mar 2019 13:49:59 +0800
    parser: Fix old-style command substitution here-document crash

    ... This is caused by the recent change to
    save/restore here-docment list around command substitutions.  In
    doing so we must finish existing here-documents prior to restoring
    the old here-document list.  This is done for new-style command
    substitutions but not for old-style.

    This patch fixes it by doing it for both.

    Fixes: 51e2d88d6e51 ("parser: Save/restore here-documents in...")
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agohush: fix negative_arith.tests: glob-protect dash in "$((arith))"
Denys Vlasenko [Fri, 21 Feb 2020 16:21:34 +0000 (17:21 +0100)]
hush: fix negative_arith.tests: glob-protect dash in "$((arith))"

function                                             old     new   delta
expand_vars_to_list                                 1026    1082     +56
parse_dollar                                         810     811      +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 57/0)               Total: 57 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: expand: Ensure result is escaped in cvtnum
Denys Vlasenko [Fri, 21 Feb 2020 15:30:44 +0000 (16:30 +0100)]
ash: expand: Ensure result is escaped in cvtnum

Upstream commit:

    Date: Fri, 1 Jun 2018 18:25:29 +0800
    expand: Ensure result is escaped in cvtnum

    The minus sign generated from arithmetic expansion is currently
    unquoted which causes anomalies when the result is used in where
    the quoting matters.

    This patch fixes it by explicitly calling memtodest on the result
    in cvtnum.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: memalloc: Avoid looping in growstackto
Denys Vlasenko [Fri, 21 Feb 2020 14:25:37 +0000 (15:25 +0100)]
ash: memalloc: Avoid looping in growstackto

Upstream commit:

    Date: Thu, 31 May 2018 01:51:48 +0800
    memalloc: Avoid looping in growstackto

    Currently growstackto will repeatedly call growstackblock until
    the requisite size is obtained.  This is wasteful.  This patch
    changes growstackblock to take a minimum size instead.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agohush: make "exit" in trap use pre-trap exitcode - fix for nested trap
Denys Vlasenko [Fri, 21 Feb 2020 01:55:53 +0000 (02:55 +0100)]
hush: make "exit" in trap use pre-trap exitcode - fix for nested trap

function                                             old     new   delta
check_and_run_traps                                  276     278      +2

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agohush: make "exit" in trap use pre-trap exitcode
Denys Vlasenko [Fri, 21 Feb 2020 01:18:06 +0000 (02:18 +0100)]
hush: make "exit" in trap use pre-trap exitcode

function                                             old     new   delta
check_and_run_traps                                  259     276     +17
builtin_exit                                          42      53     +11
hush_main                                           1086    1096     +10
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 38/0)               Total: 38 bytes

Fixes exitcode_trap2.tests.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: eval: Only restore exit status on exit/return
Denys Vlasenko [Thu, 20 Feb 2020 15:54:29 +0000 (16:54 +0100)]
ash: eval: Only restore exit status on exit/return

Upstream commit:

    Date: Fri, 14 Dec 2018 13:52:02 +0800
    eval: Only restore exit status on exit/return

    We unconditionally restore the saved status in exitreset, which
    is incorrect as we only want to do it for exitcmd and returncmd.
    This patch fixes the problem by introducing EXEND.

Reported-by: Martijn Dekker <martijn@inlv.org>
    Fixes: da30b4b78769 ("[BUILTIN] Exit without arguments in a trap...")
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: Return without arguments in a trap should use status outside traps
Denys Vlasenko [Thu, 20 Feb 2020 15:47:01 +0000 (16:47 +0100)]
ash: Return without arguments in a trap should use status outside traps

Fixes exitcode_trap4.tests.
Upstream commit:

    Date: Mon, 6 Oct 2014 21:51:26 +0800
    Return without arguments in a trap should use status outside traps

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

    Incidentally this also changes the behaviour of return without
    arguments in a loop conditional to use the last exit status in
    the body as opposed to the last command in the conditional when
    there is one.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agohush: implement "return NUM in trap sets $? after trap"
Denys Vlasenko [Thu, 20 Feb 2020 15:37:59 +0000 (16:37 +0100)]
hush: implement "return NUM in trap sets $? after trap"

function                                             old     new   delta
builtin_return                                        47      67     +20
check_and_run_traps                                  243     259     +16
run_pipe                                            1583    1597     +14
hush_main                                           1076    1086     +10
run_list                                            1054    1055      +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 5/0 up/down: 61/0)               Total: 61 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: return exit status of nofork applets (again)
Ron Yorston [Thu, 20 Feb 2020 08:23:03 +0000 (08:23 +0000)]
ash: return exit status of nofork applets (again)

Since commit d81af7216 (ash: eval: Reap zombies after built-in commands
and functions) if the shell is compiled with SH_STANDALONE and SH_NOFORK
enabled nofork applets potentially return the incorrect status.

The status value returned by evalcommand() in this case is obtained from
exitstatus in a call to waitforjob(NULL).  This overwrites the status
set for nonfork applets.

If this commit seems familiar it's essentially a reversion of commit
5ccb0e92fa (ash: return exit status of nofork applets).  What was correct
in 2016 is wrong in 2020 and vice versa.

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: expand: Merge syntax/quotes in memtodest with flags
Denys Vlasenko [Thu, 20 Feb 2020 09:06:20 +0000 (10:06 +0100)]
ash: expand: Merge syntax/quotes in memtodest with flags

Upstream commit:

    Date: Mon, 28 May 2018 00:17:39 +0800
    expand: Merge syntax/quotes in memtodest with flags

    The function arguments syntax and quotes are both derived from
    the expansion flags.  As syntax is only used by memtodest we do
    not need to maintain it outside of the function at all.

    The only place that uses something other than BASESYNTAX or DQSYNTAX
    is exptilde.  However in that case DQSYNTAX has exactly the same
    effect as SQSYNTAX.

    This patch merges these two arguments into a single flags.  The
    macro QUOTES_KEEPNUL has been renamed to EXP_KEEPNUL in order
    to keep the namespace separate.

Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: eval: Always set localvar_stop
Denys Vlasenko [Thu, 20 Feb 2020 09:33:38 +0000 (10:33 +0100)]
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>
4 years agoash: eval: Replace with listsetvar with mklocal/setvareq
Denys Vlasenko [Wed, 19 Feb 2020 16:33:44 +0000 (17:33 +0100)]
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>
4 years agoash: eval: Fail immediately with redirections errors for simple command
Denys Vlasenko [Wed, 19 Feb 2020 14:30:20 +0000 (15:30 +0100)]
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>
4 years agoash: eval: Add assignment built-in support again
Denys Vlasenko [Wed, 19 Feb 2020 14:15:13 +0000 (15:15 +0100)]
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>
4 years agoash,hush: testcase for "exit" without arguments in a trap
Denys Vlasenko [Wed, 19 Feb 2020 11:10:41 +0000 (12:10 +0100)]
ash,hush: testcase for "exit" without arguments in a trap

hush fails this one

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash,hush: add comment about masked SIGCHLD, handle SIG_IGNed SIGHUP as in bash
Denys Vlasenko [Tue, 18 Feb 2020 15:46:01 +0000 (16:46 +0100)]
ash,hush: add comment about masked SIGCHLD, handle SIG_IGNed SIGHUP as in bash

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: jobs: Only clear gotsigchld when waiting for everything
Denys Vlasenko [Tue, 18 Feb 2020 14:37:43 +0000 (15:37 +0100)]
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>
4 years agoash: jobs - Do not block when waiting on SIGCHLD
Denys Vlasenko [Tue, 18 Feb 2020 13:37:56 +0000 (14:37 +0100)]
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>
4 years agoash: eval: Reap zombies after built-in commands and functions
Denys Vlasenko [Tue, 18 Feb 2020 13:28:30 +0000 (14:28 +0100)]
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>
4 years agoash: exec: Never rehash regular built-ins
Denys Vlasenko [Mon, 17 Feb 2020 15:20:05 +0000 (16:20 +0100)]
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>
4 years agoash: exec: Stricter pathopt parsing
Denys Vlasenko [Mon, 17 Feb 2020 15:02:40 +0000 (16:02 +0100)]
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>
4 years agoash: exec: Do not allocate stack string in padvance
Denys Vlasenko [Mon, 17 Feb 2020 14:27:41 +0000 (15:27 +0100)]
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>
4 years agoash: memalloc: Add growstackto helper
Denys Vlasenko [Mon, 17 Feb 2020 14:59:08 +0000 (15:59 +0100)]
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>
4 years agoash: parser: Save/restore here-documents in command substitution
Denys Vlasenko [Mon, 17 Feb 2020 11:11:26 +0000 (12:11 +0100)]
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>
4 years agoash: mkinit: Split reset into exitreset and reset
Denys Vlasenko [Mon, 17 Feb 2020 10:22:59 +0000 (11:22 +0100)]
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>
4 years agoash: expand: Fix trailing newlines processing in backquote expanding
Denys Vlasenko [Mon, 17 Feb 2020 09:24:32 +0000 (10:24 +0100)]
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>
4 years agoash: parser: Only accept single-digit parameter expansion outside of braces
Denys Vlasenko [Mon, 17 Feb 2020 09:15:35 +0000 (10:15 +0100)]
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>
4 years agoash: shell: Fix clang warnings about "string plus integer"
Denys Vlasenko [Sun, 16 Feb 2020 18:06:42 +0000 (19:06 +0100)]
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>
4 years agoash: eval: Use the correct expansion mode for fd redirection
Denys Vlasenko [Sun, 16 Feb 2020 18:02:22 +0000 (19:02 +0100)]
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>
4 years agoash: expand: Fix skipping of command substitution when trimming in evalvar
Denys Vlasenko [Sun, 16 Feb 2020 17:42:50 +0000 (18:42 +0100)]
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>
4 years agoash: main: Print \n upon EOF (CTRL-D) when run interactively
Denys Vlasenko [Sun, 16 Feb 2020 17:57:53 +0000 (18:57 +0100)]
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>
4 years agoash: expand: Use HOME in tilde expansion when it is empty
Denys Vlasenko [Sun, 16 Feb 2020 17:31:05 +0000 (18:31 +0100)]
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>
4 years agoash: builtin: Mark more regular built-ins
Denys Vlasenko [Sun, 16 Feb 2020 17:29:52 +0000 (18:29 +0100)]
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>
4 years agoash: jobs: Replace some uses of fmtstr with stpcpy/stpncpy
Denys Vlasenko [Sun, 16 Feb 2020 17:23:43 +0000 (18:23 +0100)]
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>
4 years agoash: output: Fix fmtstr return value
Denys Vlasenko [Sun, 16 Feb 2020 17:06:20 +0000 (18:06 +0100)]
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>
4 years agoash: main: Only set savestatus in exitcmd
Denys Vlasenko [Fri, 14 Feb 2020 16:32:22 +0000 (17:32 +0100)]
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>
4 years agoash: [BUILTIN] Exit without arguments in a trap should use status outside traps
Denys Vlasenko [Fri, 14 Feb 2020 16:27:18 +0000 (17:27 +0100)]
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>
4 years agoash: parser: Fix incorrect eating of backslash newlines
Denys Vlasenko [Fri, 14 Feb 2020 15:16:34 +0000 (16:16 +0100)]
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>
4 years agoash: use pgetc_eatbnl() in more places, take 2
Denys Vlasenko [Fri, 14 Feb 2020 15:12:06 +0000 (16:12 +0100)]
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>
4 years agofdisk: add HFS / HFS+ partition type
Denys Vlasenko [Fri, 14 Feb 2020 12:57:41 +0000 (13:57 +0100)]
fdisk: add HFS / HFS+ partition type

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agotftp: on download, open local file only when first bit of data arrived
Denys Vlasenko [Thu, 13 Feb 2020 14:27:23 +0000 (15:27 +0100)]
tftp: on download, open local file only when first bit of data arrived

No reason to potentially clobber existing file before absolutely necessary.

function                                             old     new   delta
tftp_protocol                                       1947    2020     +73
tftp_main                                            393     376     -17
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 73/-17)             Total: 56 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agotftp: fix thinko in code shrink
Denys Vlasenko [Thu, 13 Feb 2020 14:03:12 +0000 (15:03 +0100)]
tftp: fix thinko in code shrink

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agotftp: code shrink
Denys Vlasenko [Thu, 13 Feb 2020 12:01:43 +0000 (13:01 +0100)]
tftp: code shrink

function                                             old     new   delta
tftp_protocol                                       1949    1947      -2

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agotftpd: show requested file name in open error message
Denys Vlasenko [Thu, 13 Feb 2020 11:58:46 +0000 (12:58 +0100)]
tftpd: show requested file name in open error message

function                                             old     new   delta
tftp_protocol                                       1902    1949     +47

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoawk: fix more "length" cases, closes 12486
Denys Vlasenko [Sun, 2 Feb 2020 22:28:55 +0000 (23:28 +0100)]
awk: fix more "length" cases, closes 12486

function                                             old     new   delta
next_token                                           808     831     +23

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash,hush: allow builtins to be tab-completed, closes 7532
Ron Yorston [Tue, 21 Jan 2020 16:01:58 +0000 (16:01 +0000)]
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>
4 years agoxargs: fix handling of quoted arguments, closes 11441
Ron Yorston [Fri, 24 Jan 2020 13:16:45 +0000 (13:16 +0000)]
xargs: fix handling of quoted arguments, closes 11441

As reported in bug 11441 when presented with a large number of quoted
arguments xargs can return 'argument line too long':

   seq 10000 29999 | sed -e 's/^/"/' -e 's/$/"/' | busybox xargs echo

This happens because the variant of process_stdin() which handles quoted
arguments doesn't preserve state between calls.  If the allowed number
of characters is exceeded part way through a quoted argument the next
call to process_stdin() incorrectly treats the terminating quote as a
starting quote, thus quoting all of the argument separators.

function                                             old     new   delta
process_stdin                                        274     303     +29
xargs_main                                           731     745     +14
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 43/0)               Total: 43 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agovi: fixes to string search in colon commands, closes 10321
Ron Yorston [Thu, 23 Jan 2020 15:31:19 +0000 (15:31 +0000)]
vi: fixes to string search in colon commands, closes 10321

Handling of string searches in colon commands (e.g ':/pat1/,/pat2/cmd')
differ from standard vi:

- As reported in bug 10321 such searches can't be repeated using the
  'n' command.  This is because the last search pattern isn't updated.

- The search also can't be repeated using the command '://' because
  an empty search pattern doesn't imply the use of the last search
  pattern.

- Such searches should start on the line after the current line,
  otherwise '://' never moves to the next occurrence of the pattern.
  This can also affect other cases where line ranges are specified
  using search patterns.

Fix these various issues.

function                                             old     new   delta
get_one_address                                      325     342     +17

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoash: improve expandstr()
Ron Yorston [Thu, 23 Jan 2020 11:26:08 +0000 (11:26 +0000)]
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>
4 years agosyslogd: add config option to include milliseconds in timestamps
Peter Korsgaard [Mon, 27 Jan 2020 14:36:41 +0000 (15:36 +0100)]
syslogd: add config option to include milliseconds in timestamps

For some use cases, having logs with more than 1 second accuracy can be
helpful.  Add an option to include milliseconds when adding a timestamp in
HH:MM:SS.mmm format, similar to syslog-ng with fraq_digits(3) or journalctl
-o short-precise.

For simplicity, abuse the remaining space in the buffer used by ctime to add
the millieconds (overwriting year).

function                                             old     new   delta
timestamp_and_log                                    401     448     +47

Signed-off-by: Peter Korsgaard <peter@korsgaard.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
4 years agoudhcpd: mangle hostnames starting with dash ("-option")
Denys Vlasenko [Tue, 14 Jan 2020 16:05:48 +0000 (17:05 +0100)]
udhcpd: mangle hostnames starting with dash ("-option")

function                                             old     new   delta
add_lease                                            316     328     +12

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agowhois: limit total length of response to 32+2 kb
Denys Vlasenko [Tue, 3 Dec 2019 13:52:17 +0000 (14:52 +0100)]
whois: limit total length of response to 32+2 kb

function                                             old     new   delta
query                                                517     554     +37

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agoinit: improve handling of signals racing with each other
Denys Vlasenko [Tue, 3 Dec 2019 12:48:55 +0000 (13:48 +0100)]
init: improve handling of signals racing with each other

Before this change, a request to reboot could be "overwritten" by e.g.
SIGHUP.

function                                             old     new   delta
init_main                                            709     793     +84
packed_usage                                       33273   33337     +64
run_actions                                          109     117      +8
stop_handler                                          87      88      +1
check_delayed_sigs                                   340     335      -5
run                                                  214     198     -16
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/2 up/down: 157/-21)           Total: 136 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agoinit: if tcgetattr() fails, don't even try to tcsetattr()
Denys Vlasenko [Mon, 2 Dec 2019 15:39:54 +0000 (16:39 +0100)]
init: if tcgetattr() fails, don't even try to tcsetattr()

function                                             old     new   delta
set_sane_term                                        111     114      +3

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agohush: fix preprocessor directives indentation
Denys Vlasenko [Thu, 28 Nov 2019 09:28:14 +0000 (10:28 +0100)]
hush: fix preprocessor directives indentation

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agogrep: add -R
Tomi Leppanen [Mon, 25 Nov 2019 15:59:52 +0000 (17:59 +0200)]
grep: add -R

This adds -R option to grep similar to GNU grep. It is the same as -r
but also dereferences symbolic links to directories.

function                                             old     new   delta
grep_main                                            834     850     +16
packed_usage                                       33362   33368      +6
grep_file                                           1440    1441      +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 23/0)               Total: 23 bytes

Signed-off-by: Tomi Leppanen <tomi.leppanen@jolla.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agobc: fix comparison bug, closes 12336
Denys Vlasenko [Sat, 23 Nov 2019 16:25:21 +0000 (17:25 +0100)]
bc: fix comparison bug, closes 12336

function                                             old     new   delta
bc_num_cmp                                           249     259     +10

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agoRemove stime() function calls
Alistair Francis [Tue, 19 Nov 2019 12:06:40 +0000 (13:06 +0100)]
Remove stime() function calls

stime() has been deprecated in glibc 2.31 and replaced with
clock_settime(). Let's replace the stime() function calls with
clock_settime() in preperation.

function                                             old     new   delta
rdate_main                                           197     224     +27
clock_settime                                          -      27     +27
date_main                                            926     941     +15
stime                                                 37       -     -37
------------------------------------------------------------------------------
(add/remove: 2/2 grow/shrink: 2/0 up/down: 69/-37)             Total: 32 bytes

Signed-off-by: Alistair Francis <alistair.francis@wdc.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agoUpdated inittab example documentation
Denys Vlasenko [Sun, 17 Nov 2019 16:48:53 +0000 (17:48 +0100)]
Updated inittab example documentation

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agochgrp: correct the usage for non-desktop chgrp calls
Liu, Shuang (ADITG/ESM) [Wed, 13 Nov 2019 14:36:20 +0000 (14:36 +0000)]
chgrp: correct the usage for non-desktop chgrp calls

When IF_DESKTOP is not defined, chown and chgrp only takes option -R -h,
However the usage output of chgrp is wrong:

$ ./busybox.nosuid chown
Usage: chown [-Rh]... USER[:[GRP]] FILE...

$ ./busybox.nosuid chgrp
Usage: chgrp [-RhLHP]... GROUP FILE...

$ ./busybox.nosuid chgrp -H group dummy
chgrp: invalid option -- 'H'
Usage: chgrp [-RhLHP]... GROUP FILE...

The chgrp is now a wrapper of chown, so the recognized options shall be the same.
This is introduced by 34425389e09353a8dacdd6b23a62553f699c544c

I would expect the correct behavior shall be the same as chown.
So suggest the below patch, the behavior shall be:

$ ./busybox.nosuid chgrp
Usage: chgrp [-Rh]... GROUP FILE...

Signed-off-by: Shuang Liu <sliu@de.adit-jv.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agotaskset: tighten the check for stride values
Denys Vlasenko [Sat, 9 Nov 2019 16:32:43 +0000 (17:32 +0100)]
taskset: tighten the check for stride values

function                                             old     new   delta
taskset_main                                         986     987      +1

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agotaskset: implement stride argument
Denys Vlasenko [Sat, 9 Nov 2019 16:05:14 +0000 (17:05 +0100)]
taskset: implement stride argument

function                                             old     new   delta
taskset_main                                         925     986     +61

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agounxz: show -t in --help
Denys Vlasenko [Fri, 8 Nov 2019 15:37:28 +0000 (16:37 +0100)]
unxz: show -t in --help

function                                             old     new   delta
packed_usage                                       33236   33247     +11

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agotaskset: update comment
Denys Vlasenko [Fri, 8 Nov 2019 15:32:37 +0000 (16:32 +0100)]
taskset: update comment

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agotaskset: add support for taking/printing CPU list (-c option)
Denys Vlasenko [Fri, 1 Nov 2019 14:44:49 +0000 (15:44 +0100)]
taskset: add support for taking/printing CPU list (-c option)

function                                             old     new   delta
taskset_main                                         511     855    +344

Based on patch by Fryderyk Wrobel <frd1996@gmail.com>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
5 years agohush: restore redirected stdin
Denys Vlasenko [Fri, 1 Nov 2019 13:16:07 +0000 (14:16 +0100)]
hush: restore redirected stdin

function                                             old     new   delta
restore_redirects                                     52      95     +43
save_fd_on_redirect                                  243     253     +10
hfopen                                                90      99      +9
fgetc_interactive                                    259     261      +2
builtin_type                                         117     115      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/1 up/down: 64/-2)              Total: 62 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>