aboutsummaryrefslogtreecommitdiff
path: root/bin/sh
Commit message (Collapse)AuthorAgeFilesLines
* sh: Fix job pointer invalidation with trapsasyncJilles Tjoelker2025-11-193-1/+15
| | | | | | | | | | | Calling dotrap() can do almost anything, including reallocating the jobtab array. Convert the job pointer to an index before calling dotrap() and then restore a proper job pointer afterwards. PR: 290330 Reported by: bdrewery Reviewed by: bdrewery Differential Revision: https://reviews.freebsd.org/D53793
* sh: Don't assume EINTR means SIGALRMDag-Erling Smørgrav2025-11-195-29/+112
| | | | | | | | | | | | | | | | | | | | | | | | | While waiting for input in the read builtin, if select() is interrupted but there is no pending signal, we act like we timed out, and return the same status as if we had been interrupted by SIGALRM, instead of looping until we actually do time out. * Replace the single select() call with a ppoll() loop. * Improve validation of the timeout value. We now accept things like "1h30m15s", which we used to silently truncate to "1h". The flip side is that we no longer accept things like "1hour" or "5sec". * Modify the existing `read -t 0` test case to verify that read returns immediately when there is input and fails immediately when there isn't. * Add a second test case which performs the same tests with a non-zero timeout value. PR: 290844 MFC after: 1 week Fixes: c4539460e3a4 ("sh: Improve error handling in read builtin:") Reviewed by: jilles, bdrewery Differential Revision: https://reviews.freebsd.org/D53761
* sh: Fix a double free in a rare scenario with pipesJilles Tjoelker2025-11-173-1/+9
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | The command sh -c 'sleep 3 | sleep 2 & sleep 3 & kill %1; wait %1' crashes (with appropriate sanitization such as putting MALLOC_CONF=abort:true,junk:true in the environment or compiling with -fsanitize=address). What happens here is that waitcmdloop() calls dowait() with a NULL job pointer, instructing dowait() to freejob() if it's a non-interactive shell and $! was not and cannot be referenced for it. However, waitcmdloop() then uses fields possibly freed by freejob() and calls freejob() again. This only occurs if the job being waited for is identified via % syntax ($! has never been referenced for it), it is a pipeline with two or more elements and another background job has been started before the wait command. That seems special enough for a bug to remain. Test scripts written by Jilles would almost always use $! and not % syntax. We can instead make waitcmdloop() pass its job pointer to dowait(), fixing up things for that (waitcmdloop() will have to call deljob() if it does not call freejob()). The crash from https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=290330#c2 appears to be the same bug. PR: 290330 Reported by: bdrewery Reviewed by: bdrewery Differential Revision: https://reviews.freebsd.org/D53773
* sh: Implement simple parameter expansion in PS1 and PS2Matthew Phillips2025-11-078-1/+181
| | | | | | | | | | This change follows a localized approach within getprompt() and avoids full parser reentry. While this means we don't support advanced expansions like ${parameter#pattern}, it provides POSIX-compliant basic parameter expansion without the complexity of making the parser reentrant. This is sufficient for the vast majority of use cases. PR: 46441
* sh tests: Fix racy test11.0Bryan Drewery2025-11-071-2/+2
| | | | | | | | | This was sometimes exiting while the child fifo was created resulting in [ENOTEMPTY] from rm. The child fifo isn't needed, just sleep. PR: 290837 Fixes: e31fb97148f ("read builtin: Empty variables on timeout") MFC after: 3 days
* bin/sh: Fix the buildLexi Winter2025-09-301-0/+2
| | | | | | | Removing <bsd.prog.mk> was unintentional; put it back. Fixes: d31e342bcc8e ("sh: Remove /.profile") MFC after: 1 day
* sh: Remove /.profileLexi Winter2025-09-301-9/+0
| | | | | | | | | | | | | root's home directory was moved to /root many years ago, so there's no reason to keep this old link. This brings sh in line with csh, where /.cshrc was removed in dcb65c5a94d4. Relnotes: yes Approved by: re (cperciva) MFC after: 1 day PR: 289097 Reviewed by: cperciva, jilles, bapt, emaste Differential Revision: https://reviews.freebsd.org/D52161
* init: Use root's home directory in single-user modeJilles Tjoelker2025-09-161-2/+0
| | | | | | | | | | | | | | | | | When starting single-user mode, use the home directory from user root as current directory and for the HOME environment variable. If the directory does not exist, set HOME=/. Also adjust /root/.profile to stop setting HOME, since it should always have been set now. This is intended to keep shell startup files working in single-user mode after /.profile has been removed. Reviewed by: emaste, ivy, kevans MFC after: 1 week Relnotes: yes Differential Revision: https://reviews.freebsd.org/D52527
* sh: Fix non-NO_ROOT installconfigLexi Winter2025-09-021-1/+1
| | | | | | | | | When building without NO_ROOT, ${TAG_ARGS} is not set, which means we pass ",config" as an install(1) argument. Only add the config tag if ${TAG_ARGS} is defined to begin with. Fixes: aa730a66bc74 ("sh: Add config tag to /.profile") Reported by: bz
* sh: Add config tag to /.profileLexi Winter2025-09-021-1/+1
| | | | | | | | | | | | | | | The file /.profile is installed as a hard link to /root/.profile. The file in /root has the config tag, but the file in / does not, which causes it to be overwritten by pkg upgrade. Since they're linked, this also overwrites the file in /root. Add the config tag to the file in / as well, so pkg treates both links as config files and won't overwrite them. PR: 289097 MFC after: 3 days Reviewed by: bapt, emaste Differential Revision: https://reviews.freebsd.org/D52162
* sh(1): Do not interpret chdir to "" as equivalent to chdir with no argumentXin LI2025-07-024-2/+5
| | | | | | | | | | | | | | | | | | | | A script that does the following: cd "${dir}" || exit 1 would incorrectly remain in the current directory when `${dir}` is an empty string under the current implementation. This behavior, while historical, is potentially dangerous, as it is likely not what the script author intended. Change the command to treat an empty string as an error and emit a diagnostic message to standard error, as required by IEEE Std 1003.1-2024. PR: standards/287440 Test Plan: kyua test bin/sh Relnotes: yes Differential Revision: https://reviews.freebsd.org/D50974
* tests: Update for jemalloc's option parsingEd Maste2025-06-171-2/+2
| | | | | | | | | | MALLOC_OPTIONS=J -> MALLOC_CONF=junk:true PR: 287357 Reviewed by: markj Event: Kitchener-Waterloo Hackathon 202506 Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D50910
* sh: Avoid referencing uninitialized memory in aliasJoseph Mingrone2025-05-161-0/+5
| | | | | | | | | | | | | | | | If run as alias '' uninitialized memory could be referenced. This is based on a fix from NetBSD. For more information, refer to https://github.com/NetBSD/src/commit/10cfed82c28 . Obtained from: NetBSD (Robert Elz <kre@netbsd.org>, 10cfed82c28) MFC after: 3 days Reported by: mckusick, Robert Elz <kre@netbsd.org> Reviewed by: emaste Differential Revision: https://reviews.freebsd.org/D50364
* sh.1: Remove a double wordGordon Bergling2025-05-011-2/+2
| | | | | | Remove the double word "the the" from the sh(1) manual page. MFC after: 3 days
* dot.shrc: words to words, should be singularGraham Perrin2025-04-281-1/+3
| | | | | | | Jump from one word to another. Not words to words. Reviewed by: imp Pull Request: https://github.com/freebsd/freebsd-src/pull/1565
* sh(1): Replace recommendation of use of -e with a noteMichael Osipov2025-03-081-6/+4
| | | | | | | | | This partially reverts b14cfdf665bb8b7b2898a4ee5b073ab87f8ea3d0 and has been discussed in D42719. Reviewed by: jrm (mentor), otis (mentor), mandree, ziaee (manpages) MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D45073
* sh.1: extend the section about getoptsPiotr Pawel Stefaniak2025-02-271-14/+46
| | | | | | | | | | Provide more details about the influence of optargs' first character on the shell's behavior in regard to invalid arguments. Also do some minor word-smithing. Original submission by rea@ Differential Revision: https://reviews.freebsd.org/D49106
* sh.1: document optional arguments to `:`Alexander Ziaee2025-02-241-3/+4
| | | | | | | | | | | TIL the `:` command accepts and discards arguments, and this is occasionally used to embed comments in interesting places. While here, fix another `...` argument that was not marked up. MFC after: 3 days Thanks: Community Discord Approved by: carlavilla, mhorne (mentors) Differential Revision: https://reviews.freebsd.org/D48398
* sh.1: update history + spdx + linter errorAlexander Ziaee2024-11-291-2/+5
| | | | | | | | | | | | + increase history consistency by adding "first appeared" + remove a skipped new paragraph macro to quiet linter + tag spdx MFC after: 3 days History source: www.in-ulm.de/~mascheck/various/ash/#bsd Reviewed by: mhorne,imp Pull Request: https://github.com/freebsd/freebsd-src/pull/1440
* sh(1): -l is only valid in cli option not as set -lBaptiste Daroussin2024-11-201-1/+1
|
* sh(1): add -l optionBaptiste Daroussin2024-11-204-15/+26
| | | | | | | | | | | | -l is required by LSB for login shell, all other shells: bash, zsh, oksh, mksh, ... implements it. with -l sh will act as a login shell and read the profile. MFC After: 1 week Obtained From: dash (3b7c8442bfe7c2fd0a6b0415df6ddf66a399fd55) Reviewed by: kib, lme Differential Revision: https://reviews.freebsd.org/D47681
* sh tests: Fix ktrace usage now that envvars are traced by defaultMark Johnston2024-11-172-2/+2
| | | | | | | | | | Some sh tests use ktrace to see whether a particular file, specified in the environment, was accessed by the shell. After commit 65a4daeaf324, this test matches the ktrace record generated by execve. Use ktrace to only log name lookups, to avoid such false matches. Fixes: 65a4daeaf324 ("ktrace: log execve(2) arguments and environment")
* Update Makefile.depend filesSimon J. Gerraty2024-10-141-1/+0
| | | | | | | After building packages we have a number of new and updated Makefile.depend files Reviewed by: stevek
* manuals: Fix typos in -offset for .Bd and .BlGraham Percival2024-10-031-2/+2
| | | | | | | | | | | | | | | | | The intended value is: -offset indent If there's any typo such that the value doesn't match the pre-defined strings, then the offset is the same width as the value. So by chance, "-offset -ident" ended up being a standard-width indent (since the default indent is 6 chars, and "-ident" also has 6 chars), whereas "-offset -indent" had a longer indent, and "-offset ident" had a shorter one. Signed-off-by: Graham Percival <gperciva@tarsnap.com> Reviewed by: mhorne, Alexander Ziaee <concussious.bugzilla@runbox.com> MFC after: 3 days Sponsored by: Tarsnap Backup Inc. Pull Request: https://github.com/freebsd/freebsd-src/pull/1436
* bin/sh: support RLIMIT_PIPEBUFKonstantin Belousov2024-09-201-1/+4
| | | | | | Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D46619
* Remove residual blank line at start of MakefileWarner Losh2024-07-1510-10/+0
| | | | | | | This is a residual of the $FreeBSD$ removal. MFC After: 3 days (though I'll just run the command on the branches) Sponsored by: Netflix
* sh(1): Grammar fix in jobs.cFu-Cheng Wang2024-02-131-1/+1
| | | | | Event: Advanced UNIX Programming Course (Fall’23) at NTHU. Pull Request: https://github.com/freebsd/freebsd-src/pull/992
* sh(1): Fix typoPei-Ju Chien2023-12-261-1/+1
| | | | | | | | Remove extra 'ing' from 'bookinging.' Event: Advanced UNIX Programming Course (Fall’23) at NTHU. Reviewed by: imp, Mina Galić Pull Request: https://github.com/freebsd/freebsd-src/pull/934
* sh(1): Fix typoKai-Yang Chen2023-12-261-1/+1
| | | | | | Event: Advanced UNIX Programming Course (Fall’23) at NTHU. Reviewed by: imp, @rilysh on github Pull Request: https://github.com/freebsd/freebsd-src/pull/935
* sh(1): Fix typoWEININGCHIU2023-12-261-1/+1
| | | | | | | | nozero -> non-zero Event: Advanced UNIX Programming Course (Fall’23) at NTHU. Reviewed by: imp, Mina Galić Pull Request: https://github.com/freebsd/freebsd-src/pull/936
* bin: Automated cleanup of cdefs and other formattingWarner Losh2023-11-2720-60/+0
| | | | | | | | | | | | | | | | Apply the following automated changes to try to eliminate no-longer-needed sys/cdefs.h includes as well as now-empty blank lines in a row. Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/ Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/ Remove /\n+#if.*\n#endif.*\n+/ Remove /^#if.*\n#endif.*\n/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/ Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/ Sponsored by: Netflix
* Remove copyright strings ifdef'd outWarner Losh2023-11-273-27/+0
| | | | | | | | | | | We've ifdef'd out the copyright strings for some time now. Go ahead and remove the ifdefs. Plus whatever other detritis was left over from other recent removals. These copyright strings are present in the comments and are largely from CSRG's attempt at adding their copyright to every binary file (which modern interpretations of the license doesn't require). Sponsored by: Netflix
* bin: Remove ancient SCCS tags.Warner Losh2023-11-2761-143/+0
| | | | | | | | Remove ancient SCCS tags from the tree, automated scripting, with two minor fixup to keep things compiling. All the common forms in the tree were removed with a perl script. Sponsored by: Netflix
* Fix /root permissions after 'make installworld'Thomas Eberhardt2023-11-161-0/+1
| | | | | | | | | | | | | | | | | According to /etc/mtree/BSD.root.dist /root should have 0750 permissions, but the build target 'make installworld' changes these to 0755. This is caused by the installation of the configuration files of sh(1) and csh(1). Correct this by specifying the correct default /root permissions. PR: 273342 Reviewed by: jilles Approved by: jilles MFC after: 2 weeks Differential Revision:https://reviews.freebsd.org/D42395
* sh: implement PS1 \D to print current timePiotr Pawel Stefaniak2023-09-232-0/+54
| | | | | | | | | | \D{format} yields the result of calling strftime(3) with the provided format and the current time. When PS4 can use this, it will enable us to easily generate timestamps when tracing script execution. Differential Revision: https://reviews.freebsd.org/D35840
* sh: also auto-complete functionsPiotr Pawel Stefaniak2023-09-121-0/+12
| | | | Differential Revision: https://reviews.freebsd.org/D40619
* sh: introduce a function to iterate over all hashed commandsPiotr Pawel Stefaniak2023-09-122-7/+41
| | | | | | | | | | | | | While aliases and built-ins are opportunistically stored in cmdtable, each function will be added to it immediately on definition. Factor out the hashing function, write the iterator function and make it use the hashing function. Add the cmdname pointer to struct cmdentry so that the command name can be exposed that way. Differential Revision: https://reviews.freebsd.org/D40619
* sh: tab-complete aliasesPiotr Pawel Stefaniak2023-09-121-0/+9
| | | | Differential Revision: https://reviews.freebsd.org/D40619
* sh: introduce a function to iterate over all aliasesPiotr Pawel Stefaniak2023-09-122-6/+24
| | | | | | | | | | | | | | | | | Currently the data structure holding alias information is opaque for consumers outside alias.c and there is no way to iterate over all aliases, which will become needed by a future commit. The new function "iteralias" takes a null pointer to return the first alias or an existing alias to return the next one, unless there is no alias to return, in which case it returns a null pointer. I slightly changed the static function hashalias so that it returns the index into the array holding link heads, and not the link head directly. In this form it's easier to use by iteralias and the slight adjustment in the three existing callers doesn't look too bad. Differential Revision: https://reviews.freebsd.org/D40619
* sh: reindent a for loop in parser.cPiotr Pawel Stefaniak2023-09-121-119/+123
| | | | | | | Reduce indentation level before a commit that will add new code here. Reviewed by: jilles Differential Revision: https://reviews.freebsd.org/D37926
* sh tests: Update $LINENO tests after $FreeBSD$ removalMark Johnston2023-08-232-4/+4
| | | | Fixes: d0b2dbfa0ecf ("Remove $FreeBSD$: one-line sh pattern")
* Remove $FreeBSD$: one-line nroff patternWarner Losh2023-08-161-1/+0
| | | | Remove /^\.\\"\s*\$FreeBSD\$$\n/
* Remove $FreeBSD$: one-line sh patternWarner Losh2023-08-16537-537/+0
| | | | Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/
* Remove $FreeBSD$: one-line .c patternWarner Losh2023-08-1626-52/+0
| | | | Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
* Remove $FreeBSD$: one-line .h patternWarner Losh2023-08-1623-23/+0
| | | | Remove /^\s*\*+\s*\$FreeBSD\$.*$\n/
* Remove $FreeBSD$: two-line .h patternWarner Losh2023-08-162-4/+0
| | | | Remove /^\s*\*\n \*\s+\$FreeBSD\$$\n/
* Separate dependencies on tools built for host.Simon J. Gerraty2023-06-301-3/+6
| | | | | | | | | When generated files depend on tools that need to be built for host, we need to carefully separate them for the DIRDEPS_BUILD so we only build them once. Reviewed by: stevek Sponsored by: Juniper Networks, Inc.
* sh: also auto-complete built-insPiotr Pawel Stefaniak2023-06-191-7/+25
| | | | | | | | Reported in a comment in PR 261728. Reported by: Oleg Reviewed by: jilles (previous version), bapt Differential Revision: https://reviews.freebsd.org/D39839
* sh: make smark a static variable instead of a local in main()Daniel Kolesa2023-06-081-1/+7
| | | | | | | | We are modifying it after setjmp and then accessing it after the jump, so it cannot be a local. MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D40415
* sh(1): initialize smark to zero in main()Daniel Kolesa2023-06-031-1/+1
| | | | | | | | | | | As popstackmark may be called on this without pushstackmark having been called, we need to initialize it so that we don't get a bogus comparison inside popstackmark, which would have resulted in a NULL pointer dereference. MFC After: 3 days Reviewed by: imp Differential Revision: https://reviews.freebsd.org/D40413