Commit Graph

4039 Commits

Author SHA1 Message Date
Dag-Erling Smørgrav fe836c5012 cp: Fix copying the root directory
When the source of the copy operation is the root directory, we should
neither append it to the destination path on FTS_D nor trim it back off
on FTS_DP.

PR:		291132
MFC after:	3 days
Fixes:          82fc0d09e8 ("cp: Partly restore symlink folllowing.")
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D53863
2025-11-22 13:11:59 +01:00
Jilles Tjoelker f44ac8cc9c sh: Fix job pointer invalidation with trapsasync
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
2025-11-19 21:30:39 +01:00
Dag-Erling Smørgrav 3c2643a7db sh: Don't assume EINTR means SIGALRM
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:          c4539460e3 ("sh: Improve error handling in read builtin:")
Reviewed by:	jilles, bdrewery
Differential Revision:	https://reviews.freebsd.org/D53761
2025-11-19 11:43:59 +01:00
Jilles Tjoelker 75a6c38e4d sh: Fix a double free in a rare scenario with pipes
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
2025-11-17 19:32:38 +01:00
Dag-Erling Smørgrav 38839c872e date: Improve nanosecond support
Add support for a field width, which defaults to 9 if unspecified or
zero.  If the width is not exactly 9, we have to either cut off digits
or append zeroes to make up the difference.  If the width is a dash,
we pick a width based on the clock's reported resolution.  This brings
us in line with GNU coreutils.

PR:		287080
MFC after:	1 week
Reviewed by:	0mp
Differential Revision:	https://reviews.freebsd.org/D53667
2025-11-11 15:58:35 +01:00
Jason A. Harmening 8b92a6ad59 rm(1): remove whiteouts when forcibly removing directories
Commit 2ed053cde5 changed UFS' VOP_RMDIR() behavior to no longer
ignore whiteouts when determining whether a directory is empty,
unless explicitly requested by the caller.  However, this also
necessitates a change to rm(1) to avoid breaking the expected
behavior when forcibly removing directory hierarchies via `rm -fr`.
I neglected to make this follow-on change despite discussing it
in the review for the breaking commit (D45987).

Finally address the breakage by making `rm -fr` imply FTS_WHITEOUT
when rm(1) reads directory contents via fts_read(3).  While here,
also fix a logic error which produces a spurious 'No error' warning
message on stdout for each deleted whiteout.

Reported by:	csjp
Reviewed by:	csjp, kib, olce
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D53640
2025-11-08 21:47:15 -06:00
Matthew Phillips f9e79facf8 sh: Implement simple parameter expansion in PS1 and PS2
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
2025-11-07 23:35:18 +01:00
Bryan Drewery 5d55553e16 sh tests: Fix racy test11.0
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:	e31fb97148 ("read builtin: Empty variables on timeout")
MFC after:	3 days
2025-11-07 09:37:19 -08:00
Mateusz Piotrowski e878ba8eea date.1: Lint with mandoc
Specifically, remove Tn macors, replace Li with Ql, and escape %N to
address date.1's rendering issues on man.freebsd.org.

PR:		290801
Reported by:	jinwookjeongg@gmail.com
MFC after:	1 week
Sponsored by:	Klara, Inc.
2025-11-05 13:36:35 +01:00
Dag-Erling Smørgrav 3d73146bae pwait: Add an option to print remaining processes
* On startup, insert all valid PIDs into a tree.
* In our main loop, whenever a process terminates, remove its PID
  from the tree.
* On exit, if the -p flag was specified, print the remaining PIDs.

MFC after:	3 days
Reviewed by:	bcr, markj
Differential Revision:	https://reviews.freebsd.org/D53293
2025-10-28 12:57:16 +01:00
Bryan Drewery ab4abce6c2 pwait: Avoid calling getpid(2) for each proc. 2025-10-23 17:01:58 -07:00
Dag-Erling Smørgrav 18997fb9ec ln: Simplify sameness checks
Sponsored by:	Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D52927
2025-10-07 18:23:58 +02:00
Dag-Erling Smørgrav 95de78614a cp: Simplify sameness checks
Sponsored by:	Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D52926
2025-10-07 18:23:58 +02:00
Lexi Winter bb0955ee4d bin/sh: Fix the build
Removing <bsd.prog.mk> was unintentional; put it back.

Fixes:		d31e342bcc ("sh: Remove /.profile")
MFC after:	1 day
2025-09-30 07:26:28 +01:00
Lexi Winter d31e342bcc sh: Remove /.profile
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 dcb65c5a94.

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
2025-09-30 07:02:06 +01:00
Dag-Erling Smørgrav 3aac05f566 pwait: Fix timeout unit parser
The timeout parser would check the first character after the number and
ignore any subsequent ones.

While here, switch to bool for booleans and fix some style nits.

MFC after:	1 week
Reviewed by:	0mp, markj
Differential Revision:	https://reviews.freebsd.org/D52612
2025-09-23 14:56:04 +02:00
Jilles Tjoelker 779812d669 init: Use root's home directory in single-user mode
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
2025-09-16 22:36:41 +02:00
Dag-Erling Smørgrav 9b8224b950 cp: Drop test helper
Now that stat(1) can report whether a file is sparse, we no longer need
a helper program for our tests.

Sponsored by:	Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D52482
2025-09-16 15:38:26 +02:00
Baptiste Daroussin 245f2abf94 timeout(1): add license header to unit test
Requested by:	kevans
2025-09-03 11:52:46 +02:00
Lexi Winter b197d2abcb sh: Fix non-NO_ROOT installconfig
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:	aa730a66bc ("sh: Add config tag to /.profile")
Reported by:	bz
2025-09-02 12:47:36 +01:00
Lexi Winter aa730a66bc sh: Add config tag to /.profile
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
2025-09-02 09:22:56 +01:00
Michael Osipov 21628c79d5 date(1): Improve manpage around '-I'
* ISO 8601 defines two formats: basic and extended, clarify that we
  output the extended format only.
* Clarify that ISO 8601 is only aware of timezone /offsets/, not timezones,
  it has no relation to the Olson timezone database (TZ environment variable).

Reviewed by:	otis (mentor), des
MFC after:	3 days
Differential Revision:	https://reviews.freebsd.org/D52314
2025-09-01 18:51:45 +02:00
Alexander Ziaee abed9ab6ea uuidgen.1: Correct SYNOPSIS, alphabetize options
Group options that can be used together accordingly in SYNOPSIS.
While here, tag SPDX and fix a parenthetical typo in EXAMPLES.

MFC after:	3 days
2025-08-27 12:17:42 -04:00
Benedict Reuschling fcc3096f00 Fix grammar in freebsd-version(1)
The man page for freebsd-version describes four options and using the
distinction between two (using neither) does not apply in that context.
Change it to replace "neither" to fix the grammo.

PR:			265808
Event:			Oslo Hackathon 2025
Approved by:		des
Differential Revision:	https://reviews.freebsd.org/D52180
2025-08-27 14:31:01 +00:00
Alexander Ziaee 9c215e77b2 freebsd-version.8: Add back freebsd-update paragraph
Someone may be working on making a pkg backend for freebsd-update
so we can keep it.

This reverts commit e9792345d3.
2025-08-06 12:32:39 -04:00
Alexander Ziaee e9792345d3 freebsd-version.8: Remove freebsd-update paragraph
For the sunset of freebsd-update in 15.0R, remove a paragraph
claiming this utility should work even if using freebsd-update.

MFC:			never
Reviewed by:		0mp, bcr
Differential Revision:	https://reviews.freebsd.org/D51160
2025-08-05 18:05:29 -04:00
Bojan Novković aae23170c8 libutil: Move cpuset(1) domain policy parsing code into libutil
cpuset(1) implements a domainset(9) policy parser that is used to
translate a <policy>:<domain_list> string into a domainset_t mask
and a valid domainset policy. This change moves the domainset parsing
code into a new cpuset.c function - 'domainset_parselist'.

The existing cpuset.c code was refactored into a generalized list
parsing function which is now used to parse both CPU sets and domain
sets. This is the same approach used in cpuset(1).

Reviewed by:	markj, ziaee (manpages)
Differential Revision:	https://reviews.freebsd.org/D46607
2025-07-27 18:31:48 +02:00
Alan Somers cc61a0793a manpages: prefer linking to xo_options.7 over xo_parse_args.3
Most libxo-enabled utilities link to xo_parse_args.3.  But that man page
doesn't actually describe how to format command-line arguments.  Rather,
xo_options.7 does.  I suspect that these utilities link to the former
because the latter man page did not exist until libxo-0.7.2
(8a6eceff3c).  And newer utilities have probably been copy/pasting the
same text from older ones.  But the superior man page does exist now,
so we should link to it instead.

MFC after:	2 weeks
Sponsored by:	ConnectWise
Reviewed by:	emaste
Differential Revision: https://reviews.freebsd.org/D51354
2025-07-16 12:02:24 -06:00
Dag-Erling Smørgrav 89990e28e6 cp: Add descriptions to all test cases.
While here, touch a few test cases up.

Sponsored by:	Klara, Inc.
Reviewed by:	markj
Differential Revision:	https://reviews.freebsd.org/D51124
2025-07-09 19:10:59 +02:00
Dag-Erling Smørgrav 2d6b33f801 cp: Add an option to visit sources in order.
This adds a --sort option which makes cp pass a comparison function to
FTS, ensuring that sources are visited and traversed in a predictable
order.  This will help make certain test cases more reliable.

Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D51214
2025-07-09 19:07:13 +02:00
Dag-Erling Smørgrav c3efa16dc9 cp: Add GNU-compatible long options.
While here, fully switch boolean variables from int to bool, and clean
up the manual page a little.

Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D51213
2025-07-09 19:07:13 +02:00
Dag-Erling Smørgrav c9fcffff6c cp: Fix typo in test case.
Fixes:		eb439266b4
Sponsored by:	Klara, Inc.
2025-07-02 15:09:04 +02:00
Dag-Erling Smørgrav eb439266b4 cp: Don't rely on FTS_DP to keep track of depth.
In normal operation, we get an FTS_D entry when we enter a directory
and a matching FTS_DP entry when we leave it.  However, if an error
occurs either changing to or reading a directory, we may get an FTS_D
entry followed by FTS_DNR or even FTS_ERR instead.  Since FTS_ERR can
also occur for non-directory entries, the only reliable way to keep
track of when we leave a directory is to compare fts_level to our own
depth counter.

This fixes a rare assertion when attempting to recursively copy a
directory tree containing a directory which is either not readable or
not searchable.

While here, also add a test case for directory loops.

Fixes:		82fc0d09e8
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D51096
2025-07-02 12:22:28 +02:00
Xin LI 119fb2a288 sh(1): Do not interpret chdir to "" as equivalent to chdir with no argument
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
2025-07-01 22:09:29 -07:00
Kyle Evans 88a2d48d39 pkill: better document ancestor-hiding
More prominently note that we'll hide ancestors in the first two
paragraphs that provide an overview of pkill/pgrep.  It also seems
worth noting in the description of -a that we won't ever unhide the
pgrep/pkill command itself, as mypid is skipped in all process iteration
by way of PSKIP().

Reviewed by:	allanjude, des
Sponsored by:	Klara, Inc.
Differential Revision:	https://reviews.freebsd.org/D51009
2025-06-24 09:49:31 -05:00
Dag-Erling Smørgrav 0e0df1fc67 cp: Fix improper use of O_PATH.
This does not appear to make any practical difference at the moment, but
technically `O_PATH` means “I'm not going to use this descriptor for any
other purposes than vnode lookups”, so using it to read the directory's
ACLs is improper and might fail in the future.

Fixes:		82fc0d09e8
Sponsored by:	Klara, Inc.
Reviewed by:	kevans
Differential Revision:	https://reviews.freebsd.org/D50984
2025-06-23 22:38:50 +02:00
Ricardo Branco 3ef02a3d2e kill: Fix support for kill -0
Signed-off-by: Ricardo Branco <rbranco@suse.de>

Fixes:		36679f7d7b ("kill: Use POSIX str2sig()")
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1733
2025-06-20 22:24:10 +00:00
Olivier Certner 1d8f8f3e36 ps(1), top(1): Priority: Let 0 be the first timesharing level
Change the origin from PZERO to PUSER.

Doing so allows users to immediately detect if some thread is running
under a high priority (kernel or realtime) or under a low one
(timesharing or idle).

MFC after:      1 month
Event:          Kitchener-Waterloo Hackathon 202506
Sponsored by:   The FreeBSD Foundation
2025-06-17 22:09:39 -04:00
Ed Maste 4f33d07300 tests: Update for jemalloc's option parsing
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
2025-06-17 11:52:17 -04:00
K Rin 65cda8a11d bin/cat: add test for reading data from stdin
Reviewed by: imp
Pull Request: https://github.com/freebsd/freebsd-src/pull/1671
Closes: https://github.com/freebsd/freebsd-src/pull/1671
2025-06-13 08:25:54 -06:00
Ricardo Branco 36679f7d7b kill: Use POSIX str2sig()
Reviewed by: imp, kib, des, jilles
Pull Request: https://github.com/freebsd/freebsd-src/pull/1696
2025-06-11 17:16:22 -06:00
Dag-Erling Smørgrav acded1a4c4 cp: Improve -p flag tests.
In addition to verifying that ACLs and flags are copied with the -p
flag, verify that they are not copied without it.

Sponsored by:	Klara, Inc.
Reviewed by:	allanjude
Differential Revision:	https://reviews.freebsd.org/D50714
2025-06-10 08:08:51 +02:00
Konstantin Belousov 4078e0d1d6 timeout(1) tests: atf_fail requires a reason
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D50752
2025-06-10 02:52:41 +03:00
Konstantin Belousov 81ef001250 timeout(1): pass full 32bit error return code from the exited child
Switch to use waitid(2) to receive siginfo_t with the complete error
code from the exited process.

Tested by:	pho
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D50752
2025-06-10 02:52:34 +03:00
Konstantin Belousov aa8cdb7cae timeout(1): only start the child command after the parent is fully set up
Since the default disposition for SIGCHLD is ignore, the prematurely
exited child would cause SIGCHLD dropped.  This makes timeout(1) hang,
because REAP_STATUS reports a zombie not waited for, but SIGCHLD for it
was already lost, so the main loop cannot exit, instead calling into
sigsuspend().

Reported and tested by:	pho
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D50752
2025-06-10 02:51:19 +03:00
Konstantin Belousov bff05e8a8c timeout(1): print errno when signalling syscalls failed
Tested by:	pho
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D50752
2025-06-10 02:51:13 +03:00
Konstantin Belousov 15e4b8d5ef timeout(1): silence warnings for ESRCH
It is possible for the child to become zombie and then there is nothing
to signal.

Reported and tested by:	pho
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D50752
2025-06-10 02:51:06 +03:00
Dag-Erling Smørgrav 81df032813 cp: Improve to_root test case.
Since the bug we're testing involved path name construction, we don't
need to be root to test it; we can simply check the error message.

Fixes:		537fbf70f1
Sponsored by:	Klara, Inc.
Reviewed by:	allanjude
Differential Revision:	https://reviews.freebsd.org/D50628
2025-05-31 20:08:12 +02:00
Dag-Erling Smørgrav 537fbf70f1 cp: Fix copying to root directory.
The trailing-slash logic would strip the target down to an empty
string, which we would then fail to stat, and subsequently attempt
(and fail) to create.

Fixes:		82fc0d09e8
Sponsored by:	Klara, Inc.
Reviewed by:	allanjude
Differential Revision:	https://reviews.freebsd.org/D50604
2025-05-30 17:44:39 +02:00
Dag-Erling Smørgrav 1a6a36a98a atf: Don't be deterred by weird umasks.
If the current umask is weird, ATF goes to great lengths to tell us that
it can't possibly work in these conditions, instead of just dealing with
it.  This makes it unreasonably hard to use ATF to test how our own code
handles unusual umasks.

MFC after:	1 week
Sponsored by:	Klara, Inc.
Reviewed by:	igoro, kevans, ngie
Differential Revision:	https://reviews.freebsd.org/D50267
2025-05-24 12:03:39 +02:00