aboutsummaryrefslogtreecommitdiff
path: root/bin/sh
Commit message (Collapse)AuthorAgeFilesLines
* sh/tests: Add a second kind of binary scripts without #!Jilles Tjoelker2021-01-032-0/+11
| | | | | | | | One of the reasons for git commit e0f5c1387df23c8c4811f5b24a7ef6ecac51a71a was to make "actually portable executables" work. Add a test that is more like those. MFC after: 1 week
* sh: Explain duplicate tcsetpgrp() callsJilles Tjoelker2020-12-261-1/+6
| | | | This is a comment change only.
* sh: Write absolute path in command -vV and typeJilles Tjoelker2020-09-015-11/+64
| | | | | | | | | | | | POSIX is pretty clear that command -v, command -V and type shall write absolute pathnames. Therefore, we need to prepend the current directory's name to relative pathnames. This can happen either when PATH contains a relative pathname or when the operand contains a slash but is not an absolute pathname. Notes: svn path=/head/; revision=365037
* sh: Keep ignored SIGINT/SIGQUIT after set in a background jobJilles Tjoelker2020-08-285-2/+27
| | | | | | | | | | | | | | | | | | If job control is not enabled, a background job (... &) ignores SIGINT and SIGQUIT, but this can be reverted using the trap builtin in the same shell environment. Using the set builtin to change options would also revert SIGINT and SIGQUIT to their previous dispositions. This broke due to r317298. Calling setsignal() reverts the effect of ignoresig(). Reported by: bdrewery MFC after: 1 week Notes: svn path=/head/; revision=364919
* sh(1): print a newline when ^D quits shPiotr Pawel Stefaniak2020-07-271-0/+4
| | | | | | | | | | | | | | I've always found this a little bit confusing: > sh $ ^D> sh $ ^D> Reviewed by: 0mp, jilles MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D25813 Notes: svn path=/head/; revision=363621
* sh: Do not ignore INTOFF during a trapJilles Tjoelker2020-07-091-6/+1
| | | | | | | | | | | | | | | | | | | | INTOFF postpones SIGINT processing and INTON enables it again. This is important so an interactive shell can return to the top level prompt when Ctrl+C is pressed. Given that INTON is automatically done when a builtin completes, the part where onsig() ignores suppressint when in_dotrap is true is both unnecessary and unsafe. If the trap is for some other signal than SIGINT, arbitrary code could have been interrupted. Historically, INTOFF remained in effect for longer. Reviewed by: bdrewery MFC after: 2 weeks Differential Revision: https://reviews.freebsd.org/D25270 Notes: svn path=/head/; revision=363057
* Fix description of the "\$" sequence for PS1Mateusz Piotrowski2020-07-061-3/+3
| | | | | | | | | | | | The manual page documents "\$" to expand to either "$" or "#" followed by a single space. In reality, the single space character is not appended. PR: 247791 Submitted by: kd-dev@pm.me MFC after: 7 days Notes: svn path=/head/; revision=362957
* sh/tests: Re-enable bin.sh.execution.functional_test.bg12.0Jilles Tjoelker2020-06-281-15/+1
| | | | | | | | | | This reverts r362646. PR: 247559 MFC after: 1 week Notes: svn path=/head/; revision=362738
* sh/tests: Fix flaky execution/bg12.0Jilles Tjoelker2020-06-281-1/+1
| | | | | | | | | | | | | | | | | | | | | | When job control is not enabled, the shell ignores SIGINT while waiting for a foreground process unless that process exits on SIGINT. In this case, the foreground process is sleep and it does not exit on SIGINT because the signal is only sent to the shell. Depending on order of events, this could cause the SIGINT to be unexpectedly ignored. On lightly loaded bare metal, the chance of this happening tends to be less than 0.01% but with higher loads and/or virtualization it becomes more likely. Starting the sleep in background and using the wait builtin ensures SIGINT will not be ignored. PR: 247559 Reported by: lwhsu MFC after: 1 week Notes: svn path=/head/; revision=362737
* Temporarily skip flakey bin.sh.execution.functional_test.bg12 in CILi-Wen Hsu2020-06-261-1/+15
| | | | | | | | PR: 238870 Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=362646
* sh/tests: Add tests for SIGINT in non-jobc background commandsJilles Tjoelker2020-06-143-0/+30
| | | | | | | | | | If job control is not enabled, background commands shall ignore SIGINT and SIGQUIT, and it shall be possible to override that ignore in the same shell. MFC after: 1 week Notes: svn path=/head/; revision=362182
* vfs: add restrictions to read(2) of a directory [1/2]Kyle Evans2020-06-041-0/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Historically, we've allowed read() of a directory and some filesystems will accommodate (e.g. ufs/ffs, msdosfs). From the history department staffed by Warner: <<EOF pdp-7 unix seemed to allow reading directories, but they were weird, special things there so I'm unsure (my pdp-7 assembler sucks). 1st Edition's sources are lost, mostly. The kernel allows it. The reconstructed sources from 2nd or 3rd edition read it though. V6 to V7 changed the filesystem format, and should have been a warning, but reading directories weren't materially changed. 4.1b BSD introduced readdir because of UFS. UFS broke all directory reading programs in 1983. ls, du, find, etc all had to be rewritten. readdir() and friends were introduced here. SysVr3 picked up readdir() in 1987 for the AT&T fork of Unix. SysVr4 updated all the directory reading programs in 1988 because different filesystem types were introduced. In the 90s, these interfaces became completely ubiquitous as PDP-11s running V7 faded from view and all the folks that initially started on V7 upgraded to SysV. Linux never supported this (though I've not done the software archeology to check) because it has always had a pathological diversity of filesystems. EOF Disallowing read(2) on a directory has the side-effect of masking application bugs from relying on other implementation's behavior (e.g. Linux) of rejecting these with EISDIR across the board, but allowing it has been a vector for at least one stack disclosure bug in the past[0]. By POSIX, this is implementation-defined whether read() handles directories or not. Popular implementations have chosen to reject them, and this seems sensible: the data you're reading from a directory is not structured in some unified way across filesystem implementations like with readdir(2), so it is impossible for applications to portably rely on this. With this patch, we will reject most read(2) of a dirfd with EISDIR. Users that know what they're doing can conscientiously set bsd.security.allow_read_dir=1 to allow read(2) of directories, as it has proven useful for debugging or recovery. A future commit will further limit the sysctl to allow only the system root to read(2) directories, to make it at least relatively safe to leave on for longer periods of time. While we're adding logic pertaining to directory vnodes to vn_io_fault, an additional assertion has also been added to ensure that we're not reaching vn_io_fault with any write request on a directory vnode. Such request would be a logical error in the kernel, and must be debugged rather than allowing it to potentially silently error out. Commented out shell aliases have been placed in root's chsrc/shrc to promote awareness that grep may become noisy after this change, depending on your usage. A tentative MFC plan has been put together to try and make it as trivial as possible to identify issues and collect reports; note that this will be strongly re-evaluated. Tentatively, I will MFC this knob with the default as it is in HEAD to improve our odds of actually getting reports. The future priv(9) to further restrict the sysctl WILL NOT BE MERGED BACK, so the knob will be a faithful reversion on stable/12. We will go into the merge acknowledging that the sysctl default may be flipped back to restore historical behavior at *any* point if it's warranted. [0] https://www.freebsd.org/security/advisories/FreeBSD-SA-19:10.ufs.asc PR: 246412 Reviewed by: mckusick, kib, emaste, jilles, cy, phk, imp (all previous) Reviewed by: rgrimes (latest version) MFC after: 1 month (note the MFC plan mentioned above) Relnotes: absolutely, but will amend previous RELNOTES entry Differential Revision: https://reviews.freebsd.org/D24596 Notes: svn path=/head/; revision=361798
* sh: Allow more scripts without #!Jilles Tjoelker2020-05-303-1/+42
| | | | | | | | | | | | | | | | | | | Austin Group bugs #1226 and #1250 changed the requirements for shell scripts without #! (POSIX does not specify #!; this is about the shell execution when execve(2) returns an [ENOEXEC] error). POSIX says we shall allow execution if the initial part intended to be parsed by the shell consists of characters and does not contain the NUL character. This allows concatenating a shell script (ending with exec or exit) and a binary payload. In order to reject common binary files such as PNG images, check that there is a lowercase letter or expansion before the last newline before the NUL character, in addition to the check for the newline character suggested by POSIX. Notes: svn path=/head/; revision=361647
* sh: Remove a comment that was obsoleted by r358152Jilles Tjoelker2020-05-221-3/+1
| | | | | | | | | | | Since r358152, the read builtin has used a buffer. Also, remove a space at the end of the line in a comment. No functional change is intended. Notes: svn path=/head/; revision=361384
* sh: Fix double INTON with vforkJilles Tjoelker2020-05-163-1/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | The shell maintains a count of the number of times SIGINT processing has been disabled via INTOFF, so SIGINT processing resumes when all disables have enabled again (INTON). If an error occurs in a vfork() child, the processing of the error enables SIGINT processing again, and the INTON in vforkexecshell() causes the count to become negative. As a result, a later INTOFF may not actually disable SIGINT processing. This might cause memory corruption if a SIGINT arrives at an inopportune time. As of r360452, it causes the shell to abort when it would unsafely allocate or free memory in certain ways. Note that various places such as errors in non-special builtins unconditionally reset the count to 0, so the problem might still not always be visible. PR: 246497 Reported by: jbeich MFC after: 2 weeks Notes: svn path=/head/; revision=361112
* sh/tests: Test some obscure cases with aliasing keywordsJilles Tjoelker2020-05-125-0/+21
| | | | Notes: svn path=/head/; revision=360992
* sh: Assert INTOFF rather than applying it in ck*Jilles Tjoelker2020-04-281-6/+13
| | | | | | | | | | | | | As I noted in https://reviews.freebsd.org/D22756, INTOFF should be in effect when calling ckmalloc/ckrealloc/ckfree to avoid memory leaks and double frees. Therefore, change the functions to check if INTOFF is in effect instead of applying it. Reviewed by: bdrewery Differential Revision: https://reviews.freebsd.org/D24599 Notes: svn path=/head/; revision=360452
* sh: Remove remnants to compile out fc completelyJilles Tjoelker2020-04-222-10/+3
| | | | | | | | | | | | | | r360139 made compiling with NO_HISTORY work. This #define does not remove the fc and bind builtins completely but makes them always write an error message. However, there was also some code in builtins.def and mkbuiltins to remove the fc builtin entirely (but not the bind builtin). The additional build system complication to make this work seems not worth it, so remove that code. Notes: svn path=/head/; revision=360210
* Fix build with NO_HISTORY setBryan Drewery2020-04-211-3/+3
| | | | | | | | Reviewed by: jilles Differential Revision: https://reviews.freebsd.org/D24458 Notes: svn path=/head/; revision=360139
* [sh] Fix a "may be unused" warning on mips-gccAdrian Chadd2020-04-161-0/+1
| | | | | | | | | | | | mips-gcc for mips32 was complaining that c was potentially used before being set. Setting it to 0 before calling fdgetsc() looks like the right thing to do in this instance; there's an explicit check for c == 0 later on. Tested: mips-gcc mips32 build, running /bin/sh on mips32 Notes: svn path=/head/; revision=360028
* Bump WARNS for sh(1).Edward Tomasz Napierala2020-04-011-2/+0
| | | | | | | | | | Reviewed by: jilles MFC after: 2 weeks Sponsored by: DARPA Differential Revision: https://reviews.freebsd.org/D24181 Notes: svn path=/head/; revision=359511
* sh: remove duplicate el definitionKyle Evans2020-03-281-2/+0
| | | | | | | | | | | | el is declared extern in myhistedit.h and defined in histedit.c. Remove the duplicate definition in input.c to appease the -fno-common build. -fno-common will become the default in GCC10/LLVM11. MFC after: 3 days Notes: svn path=/head/; revision=359398
* sh: fix read builtin on 32-bit systemsKyle Evans2020-02-221-1/+1
| | | | | | | | | | | | | | | | | | Specifically, any system with a 32-bit size_t; -residue is calculated as a 32-bit *then* promoted to the 64-bit off_t and the result is ultimately wrong. This resulted in what would appear to be truncated output, as only the first line would be read. Correct it by just making residue an off_t to begin with, since this is what lseek will take anyways. Reported by: antoine, dim Triaged by: cem Tested by: kevans X-MFC-With: r358152 Notes: svn path=/head/; revision=358235
* Improve performance of "read" built-in command when using a seekableHiroki Sato2020-02-201-1/+73
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | fd. The read built-in command calls read(2) with a 1-byte buffer because newline characters need to be detected even on a byte stream which comes from a non-seekable file descriptor. Because of this, the following script calls >6,000 read(2) to show a 6KiB file: while read IN; do echo "$IN"; done < /COPYRIGHT When the input byte stream is seekable, it is possible to read a data block and then reposition the file pointer to where a newline character found. This change adds a small buffer to do this and reduces the number of read(2) calls. Theoretically, multiple built-in commands reading the same seekable byte stream in a single pipe chain can share the buffer. However, this change just makes a single invocation of the read built-in allocate a buffer and deallocate it every time for simplicity. Although this causes read(2) to read the same regions multiple times, the performance penalty should be small compared to the reduction of read(2) calls. Reviewed by: jilles MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D23747 Notes: svn path=/head/; revision=358152
* sh: Fix rare memory leak with SIGINTJilles Tjoelker2020-01-011-1/+4
| | | | | | | | | | | | | | | | | | If getcwd() failed earlier on but later succeeded in the pwd builtin, there was no INTOFF protection between calling savestr() and storing its result. It is quite rare for getcwd() to fail, and rarer for it to succeed later in the same directory. Found via code inspection for changing ckmalloc() and similar to assert INTOFF protection instead of applying it directly (which protects against corrupting malloc's internal state but allows memory leaks or double frees). MFC after: 1 week Notes: svn path=/head/; revision=356251
* sh: Test that executing various binary files is rejectedJilles Tjoelker2019-12-305-0/+64
| | | | | | | | | | | | | | | If executing a file fails with an [ENOEXEC] error, the shell executes the file as a shell script, except that this execution may instead result in an error message if the file is binary. Per a recent Austin Group interpretation, we will need to change this to allow a concatenation of a shell script and a binary payload. See Austin Group bugs #1226 and #1250. MFC after: 1 week Notes: svn path=/head/; revision=356208
* Do not use our custom completion function, it is not needed anymoreBaptiste Daroussin2019-09-161-1/+1
| | | | Notes: svn path=/head/; revision=352385
* Fix .depend files to work for build tools.Bryan Drewery2019-06-151-0/+1
| | | | | | | | | | This is somewhat of a follow-up to r335746. MFC after: 2 weeks Sponsored by: DellEMC Notes: svn path=/head/; revision=349062
* sh/tests: Improve failure messages of expansion/arith15.0Jilles Tjoelker2019-03-071-3/+3
| | | | Notes: svn path=/head/; revision=344902
* sh: Add set -o pipefailJilles Tjoelker2019-02-2411-9/+82
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The pipefail option allows checking the exit status of all commands in a pipeline more easily, at a limited cost of complexity in sh itself. It works similarly to the option in bash, ksh93 and mksh. Like ksh93 and unlike bash and mksh, the state of the option is saved when a pipeline is started. Therefore, even in the case of commands like A | B & a later change of the option does not change the exit status, the same way (A | B) & works. Since SIGPIPE is not handled specially, more work in the script is required for a proper exit status for pipelines containing commands such as head that may terminate successfully without reading all input. This can be something like ( cmd1 r=$? if [ "$r" -gt 128 ] && [ "$(kill -l "$r")" = PIPE ]; then exit 0 else exit "$r" fi ) | head PR: 224270 Relnotes: yes Notes: svn path=/head/; revision=344502
* sh: Send normal output from bind builtin to stdoutJilles Tjoelker2019-02-193-1/+30
| | | | | | | | PR: 233343 Submitted by: Yuichiro NAITO (original version) Notes: svn path=/head/; revision=344306
* sh: Restore $((x)) error checking after fix for $((-9223372036854775808))Jilles Tjoelker2019-02-107-3/+62
| | | | | | | | | | | | | | | | | | SVN r342880 was designed to fix $((-9223372036854775808)) and things like $((0x8000000000000000)) but also broke error detection for values of variables without dollar sign ($((x))). For compatibility, overflow in plain literals continues to be ignored and the value is clamped to the boundary (except 9223372036854775808 which is changed to -9223372036854775808). Reviewed by: se (although he would like error checking to be removed) MFC after: 2 weeks X-MFC-with: r342880 Differential Revision: https://reviews.freebsd.org/D18926 Notes: svn path=/head/; revision=343981
* Comment out the default sh(1) aliases for root, introduced in r343416.Edward Tomasz Napierala2019-01-251-7/+7
| | | | | | | | | | | The rest of this stuff is still to be discussed, but I think at this point we have the agreement that the aliases should go. MFC after: 2 weeks Sponsored by: DARPA, AFRL Notes: svn path=/head/; revision=343440
* Install .shrc for root, and set PS1 for the toor account.Edward Tomasz Napierala2019-01-243-3/+48
| | | | | | | | | | Reviewed by: jilles MFC after: 2 weeks Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D18872 Notes: svn path=/head/; revision=343416
* Make sh(1) support \u in PS1. This removes one fork/exec on interactiveEdward Tomasz Napierala2019-01-242-1/+64
| | | | | | | | | | | | shell startup. Reviewed by: 0mp (man page), jilles MFC after: 2 weeks Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D18790 Notes: svn path=/head/; revision=343399
* Don't mess with BLOCKSIZE in shell startup files - it's set by login.conf(5);Edward Tomasz Napierala2019-01-201-4/+0
| | | | | | | | | | | | | there's no need to even mention it in shell rc files. Not that it's wrong; just pointless and somewhat misleading. Reviewed by: jilles MFC after: 2 weeks Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D18809 Notes: svn path=/head/; revision=343231
* sh: Send libedit "ferr" output to fd 2Jilles Tjoelker2019-01-201-5/+3
| | | | | | | | | | The libedit "fout" output must be sent to fd 2 since it contains prompts that POSIX says must be sent to fd 2. However, the libedit "ferr" output receives error messages such as from "bind" that make no sense to send to fd 1. Notes: svn path=/head/; revision=343215
* Fix an edge case when parsing large numbers which resulted in inconsistentDag-Erling Smørgrav2019-01-093-2/+22
| | | | | | | | | | | results between an expression that refers to a variable by name and the same expression that includes the same variable by value. Submitted by: se@ MFC after: 1 week Notes: svn path=/head/; revision=342880
* sh: Add test for exported but unset variablesJilles Tjoelker2019-01-032-0/+6
| | | | | | | PR: 233545 Notes: svn path=/head/; revision=342741
* sh: Do not place exported but unset variables into the environmentJilles Tjoelker2019-01-031-2/+2
| | | | | | | | | PR: 233545 Submitted by: Jan Beich Obtained from: NetBSD Notes: svn path=/head/; revision=342740
* Make sh(1) collapse $HOME into "~" in PS1.Edward Tomasz Napierala2018-12-281-2/+20
| | | | | | | | | | Reviewed by: jilles MFC after: 2 weeks Sponsored by: DARPA, AFRL Differential Revision: https://reviews.freebsd.org/D18663 Notes: svn path=/head/; revision=342577
* sh(1): Remove -c string from set builtin documentationJilles Tjoelker2018-12-081-3/+3
| | | | | | | | | Altering the -c string at run time does not make sense and is not possible. MFC after: 1 week Notes: svn path=/head/; revision=341725
* sh: Fix ${param?} default error messageJilles Tjoelker2018-11-283-2/+15
| | | | | | | | | | | | | | | If word in ${param?word} is missing, the shell shall write a default error message. So expanding ${param?} when param is not set should write an error message like sh: param: parameter not set This was broken by r316417. PR: 233585 Notes: svn path=/head/; revision=341164
* sh: Use 126 and 127 exit status for failures opening a scriptJilles Tjoelker2018-11-274-4/+18
| | | | | | | | | | This affects scripts named on the command line, named with a '.' special builtin and found via the PATH %func autoloading mechanism. PR: 231986 Notes: svn path=/head/; revision=341097
* sh: Unify EXERROR and EXEXECJilles Tjoelker2018-11-097-44/+25
| | | | | | | | | | | The difference between EXERROR and EXEXEC was that EXEXEC passed along exitstatus and EXERROR set exitstatus to 2 in the handling code. By changing the places that raised EXERROR to set exitstatus to 2, the handling of EXERROR and EXEXEC becomes the same. Notes: svn path=/head/; revision=340284
* sh: Use exitstatus instead of exerrno to pass EXEXEC statusJilles Tjoelker2018-10-275-13/+10
| | | | | | | No functional change is intended. Notes: svn path=/head/; revision=339822
* sh: Fix formal overflow in pointer arithmeticJilles Tjoelker2018-09-051-2/+2
| | | | | | | | | | | | The intention is to lower the value of the pointer, which according to ubsan cannot be done by adding an unsigned quantity. Reported by: kevans Approved by: re (kib) MFC after: 1 week Notes: svn path=/head/; revision=338473
* Finish moving dot.cshrc and dot.profile to bin/csh/ and bin/sh/.Brad Davis2018-08-292-1/+25
| | | | | | | | Approved by: re (gjb), will (mentor) Differential Revision: https://reviews.freebsd.org/D16770 Notes: svn path=/head/; revision=338374
* Revert parts of r337849 and r337857Brad Davis2018-08-152-25/+1
| | | | | | | | | | | | This fixes the build and I will redo these changes as part of a future review that organizes them differently. The way I tried to do it here could be done better. Sorry for the noise. Approved by: will (mentor) Differential Revision: https://reviews.freebsd.org/D16737 Notes: svn path=/head/; revision=337882
* Fix build after r337849Brad Davis2018-08-151-1/+3
| | | | | | | | | | | | This moves the symlink creation to after where the files are installed. This also inverts the shell change so that it only happens if MK_TCSH is on. Approved by: will (mentor) Differential Revision: https://reviews.freebsd.org/D16725 Notes: svn path=/head/; revision=337857