This patch resolves inconsistent behavior between the -k option and
other related flags, including -P. Previously, using -k resulted in
output displayed in 1024-byte blocks, which did not align with the
behavior of similar options such as -m and -g, where output is shown in
1M-blocks and 1G-blocks respectively.
The updated implementation ensures that -k now correctly displays sizes
in 1K-blocks. In addition, the patch incorporates the POSIX requirement
that when both -k -P are specified, the block size must be explicitly
forced to 1024-blocks.
Together, these changes make the behavior of -k consistent, predictable,
and compliant with the standard.
Signed-off-by: Ankush Mondal <mondalankush9851@gmail.com>
Reviewed by: imp
Pull Request: https://github.com/freebsd/freebsd-src/pull/1906
Found these changes by chance in an old patch file. Should have been
committed along with the ps(1) modifications done in March 2025.
No functional change (intended).
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
Mark `optletter` and `t_op::op_text` as `__non_string`, to avoid
warnings from clang 21 similar to:
bin/sh/options.h:77:36: error: initializer-string for character array is too long, array size is 19 but initializer has size 20 (including the null terminating character); did you mean to use the 'nonstring' attribute? [-Werror,-Wunterminated-string-initialization]
77 | const char optletter[NSHORTOPTS] = "efIimnsxvVECabupTPh";
| ^~~~~~~~~~~~~~~~~~~~~
bin/test/test.c:153:3: error: initializer-string for character array is too long, array size is 2 but initializer has size 3 (including the null terminating character); did you mean to use the 'nonstring' attribute? [-Werror,-Wunterminated-string-initialization]
153 | {"==", STREQ},
| ^~~~
MFC after: 3 days
Reviewed by: jilles
Differential Revision: https://reviews.freebsd.org/D54362
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
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
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
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
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
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
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
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
* 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
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
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
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
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
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
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
* 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
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
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
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
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
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
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
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
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
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
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
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
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
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
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