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
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
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
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
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
* At the end of copy(), we always close to.dir, even though it can be
AT_FDCWD (in the file-to-file case) or even -1 (if we failed to open
or create the destination directory). While closing an invalid file
descriptor is harmless, it's still bad form.
* In the DIR_TO_DNE case, initialize to.dir to -1 to guard against the
case where mkdir() fails so we never assign anything to to.dir and
end up (harmlessly, luckily) closing stdin on our way to the exit.
Coverity ID: 1609954
Fixes: 82fc0d09e8
Sponsored by: Klara, Inc.
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D50391
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
* Make to.base large enough to hold a trailing separator.
* Remove the separator from warning and error messages.
* In the FILE_TO_FILE case, leave to.base empty.
* In the FILE_TO_DIR and DIR_TO_DNE cases, append a separator to
to.base once we've (optionally created and) opened it.
* Thus, in the file-to-file case, we print an empty string followed by
to.path, while in all other cases, to.base already contains the
necessary separator.
This fixes failures in tests that used cp and expected a specific
error message and were surprised to see "./" pop up.
Fixes: 82fc0d09e8
Sponsored by: Klara, Inc.
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D50357
Ensure that we are able to enter the destination directory after we
create it, even if the current umask would normally prevent it, and
that it has the expected permissions once we are done, even if we had
to tweak them to be able to enter it.
Fixes: 82fc0d09e8
Sponsored by: Klara, Inc.
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D50266
The test case was a) not doing what I intended it to do and b) missing
a third possible configuration. Fix the test case, and fix the code
by not setting the beneath flag (which restricts operations to the
destination tree) unless we are recursing.
Fixes: 82fc0d09e8
Sponsored by: Klara, Inc.
Reviewed by: allanjude, markj
Differential Revision: https://reviews.freebsd.org/D50257
* Correctly indent some comment blocks.
* Avoid using strcpy(), even when we know it's safe.
* Drop the (void) casts from fts_set() calls which can never fail.
Fixes: 82fc0d09e8
Sponsored by: Klara, Inc.
Reviewed by: allanjude, markj
Differential Revision: https://reviews.freebsd.org/D50256
As stated in the previous commit, option '-J' was introduced by commit
"Add -J to filter by matching jail IDs and names."
(13767130c7, r265229), which unfortunately talked about '-J'
being a filter while actually implementing it as a regular selection
option which adds to the processes to display.
The manual page's formulation hinted more at '-J' being a filter, which
it is not, or could be just considered ambiguous, because of the
presence of the "only" word. Consequently, remove it and reformulate.
Reviewed by: ziaee, dch
MFC after: 1 day
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D50194
* As a general rule, follow symbolic links in the destination as long
as the target is within the destination hierarchy.
* As a special case, allow the destination itself to be a symbolic
link, and even a dead one (in which case we create the target).
* The file-to-file case remains unrestricted.
Currently, if a symlink we aren't allowed to follow is encountered,
cp will behave just like it would if the file was not writable. We
should consider whether it would be better to replace the offending
link instead.
Fixes: 0729d1e8fd
MFC after: never
Relnotes: yes
Sponsored by: Klara, Inc.
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D50093
This is what POSIX mandates for option '-U' and arguably the behavior
that most users actually need in most cases. Before, '-U' would select
processes by their effective user IDs (which is the behavior mandated by
POSIX for option '-u').
Matching by real user IDs allows to list all processes belonging to the
passed users, including those temporarily having a different effective
user ID, which can happen if launched by a setuid executable or if using
some credentials-changing facility (such as seteuid() for root processes
or mac_do(4)/setcred(2)). Conversely, processes temporarily assuming
the identity of some of the passed users will not be listed anymore
(they do not "belong" to these users).
This change also makes '-U' consistent with '-G', the latter already
matching on real group IDs.
While here, remove the (non-compiled) code for tentative option '-R' as
its proposed behavior was the one established here for '-U'. Also, move
the compiled-out old code for '-U' under '-u' for reference, as this is
what the latter should do according to POSIX, even if it seems unlikely
we will want to change the behavior of '-u'.
Reviewed by: manpages (ziaee)
MFC after: 3 days
Relnotes: yes
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D49622 (code)
Differential Revision: https://reviews.freebsd.org/D49623 (manual page)
This puts our ps(1) in conformance with POSIX.
While here, replace ad-hoc initialization of 'uidlist' with a call to
expand_list().
***
Review of the ps(1) implementations in other BSDs, illumos, and Linux's
procps shows they already behave as prescribed by POSIX.
Previously, we would match processes with their effective user ID but
using our real user ID. While the real user ID is meant as the real
identity of a process, and is used, e.g., to perform accounting or be
permitted to send signals to specific targets, selecting processes to
display is arguably more akin to a kind of (advisory) access control.
ps(1) is not installed setuid, so normally the real and effective user
IDs of ps processes are the same. This may however not be the case when
ps(1) is launched by another setuid executable, and the launching
process may then logically expect that ps(1) lists the processes
corresponding to its effective UID.
MFC after: 3 days
Relnotes: yes
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D49619 (code)
Differential Revision: https://reviews.freebsd.org/D49620 (manual page)
When combined with other options affecting the selection of processes,
except for '-X' and '-x', option '-a' would have no effect (and '-A'
would reduce to just '-x'). This was in contradiction with the rule
applying to all other selection options stating that one process is
listed as soon as any of these options has been specified and selects
it, which is both mandated by POSIX and arguably a natural expectation.
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D49617 (code)
Differential Revision: https://reviews.freebsd.org/D49618 (manual page)
The ps(1) display's list of columns is now first built without taking
into account the '-O' options. In a second step, all columns passed via
'-O' are finally inserted after the built-so-far display's first PID
column (if it exists, else at start), in their order of appearance as
arguments to the '-O' options.
This new two-step procedure suppresses the following undesirable
behaviors:
- '-O' used to insert the columns of the default display even if the
user also passed other options to request specific columns.
- Each occurence of '-O' beyond the first would just insert the passed
keywords after those requested by the previous options, as if by '-o',
inconsistently with the behavior for the first occurence.
These behaviors had more annoying consequences:
- Where the columns of some '-O' occurence appear in the display used to
depend on the actual position of '-O' with respect to other options,
despite the natural expectation that they should go near a single PID
column considered as an anchor regardless of other options adding
columns.
- Columns specified with multiple '-O' options would not be grouped
together.
- It used to be impossible to specify custom headers but for the last
column for columns that are next to each other (i.e., specified by
a single '-O' occurence).
which are now all lifted.
With these changes, '-O' can still be used alone to amend the default
display, but can now be used also in conjunction with any specific
display, and in particular "canned" ones invoked by '-j', '-l', '-u' or
'-v'.
******
This part discusses other ps(1) implementations' behaviors and compares
them to the one established by this change.
NetBSD seems to be the only other BSD having refined the meaning of
ps(1)'s '-O' option. While the behavior there is similar to the new one
here on the surface, it differs on the following points:
1. If no options requesting a specific display are present before the
first '-O' option, the appearance of '-O' triggers the insertion of
the default display, regardless of whether such specific display
options appear *after* it.
2. If options requesting a specific display appear before the first '-O'
and none specify a PID column, columns listed in the first '-O' are
appended to them (as '-o' would do), but columns passed by further
'-O' options are then inserted next to the columns of the first '-O'
options.
Behavior of point 1 seems to have only one advantage: To allow to
customize the default display by first using '-O' and then other options
appending to it, but as the default display finishes with the COMMAND
column, it is unlikely that one wants to use '-o' or other specific
display options after '-O' (NetBSD's ps(1) does not suppress duplicate
columns). A much simpler and easy-to-understand way to reach that
effect in FreeBSD, if it really proves useful, would be to introduce
a new explicit option that inserts the default display.
The column-appending behavior of the first '-O' option in point 2 can be
also achieved by using '-o' instead. As '-O' is used to insert columns
after the PID one, which is located near the left in the default and all
"canned" displays, we found it more consistent and practical to push its
columns completely to the left on the absence of a PID column. The
effect of multiple '-O' options in NetBSD when no PID column has been
requested beforehand is also cumbersome and inconsistent with the
documentation (it is likely a bug).
Both NetBSD-specific behaviors exposed above also have the disadvantage
that the position of '-O' options with respect to other ones is
meaningful in ways that are not obvious and that are arguably not
desirable as '-O' is meant to append columns after an anchor (the PID
column).
Linux's procps-ng's ps(1) is very limited in its handling of '-O', and
more generally when mixing options tweaking the display. '-O' causes
insertion of the default display (like NetBSD does). If '-o' options
are specified, '-O' must come before them. '-O' is not usable with
canned display options. Additionally, only one '-O' option may appear
on the command line, limiting header customization.
The only case in which these implementations and ours behave in the same
way with respect to '-O' is if only a single '-O' option and no other
options changing the display are specified.
MFC after: 3 days
Relnotes: yes
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D49615 (code)
Differential Revision: https://reviews.freebsd.org/D49616 (manual page)
Now that removal of non-explicitly-requested duplicate columns work with
a O(n) algorithm, remove the ad-hoc optimization of crushing the canned
displays' formats after first use and constify their format strings.
No functional change intended.
This change could also be useful if/when allowing, e.g., to double
letters of canned displays to indicate their columns should not be
subject to automatic removal of duplicates (e.g., 'ps -ll').
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D49614
Before this change, when stacking up more columns in the display through
command-line options, if user requested to add some "canned" display
(through options '-j', '-l', '-u' or '-v'), columns in it that were
"duplicates" of already requested ones (meaning that they share the same
keyword, regardless of whether their headers have been customized) were
in the end omitted.
However, this mechanism did not work the other way around, i.e.,
requesting some canned display(s) first and then adding some columns
that are duplicates (through '-o' or '-O') would not remove them from
the canned display. Additionally, it did not take into account keyword
aliases (which also lead to displaying the same information).
This whole mechanism of removing columns from requested canned displays
when there are duplicates is useful in a number of scenarios:
1. When one wants the columns of a canned display, but with some of them
in a different order and at the edges of the bulk. This needs the
change here to move columns after the bulk (previously, only moving
them before the bulk would work).
2. To combine multiple canned displays to get more information without
repeating common columns. This part has been working before and
this behavior is unchanged.
3. In combination with requesting a canned display and additional
columns after it, ensure that a single COMMAND column appears at the
end of the display (to benefit from the fact that a last COMMAND
column can extend further to the right).
Point 2 above implies that, when multiple canned displays are requested,
we should keep the leftmost column with same keyword. However, columns
requested explicitly by '-o' have priority (as the natural extension of
point 1's behavior before this change), and in this case all matching
columns in canned displays must be suppressed.
To this end, when adding requested options to the display's list, we
stop trying to find an earlier matching column (which is incidentally
a O(n²) algorithm). Instead, we do a first pass over all requested
options once they have all been added to the display's list (in
scan_vars()). For each keyword, we note if it was requested at least
once explicitly (through '-o' or '-O'), in addition to setting
'needuser' and 'needcomm' (as before). Then, a second pass decides
whether to keep each column. A column is removed if it should not be
kept absolutely (i.e., it wasn't specified via '-o' or '-O') and there
is either a matching column that must be kept (determined during the
first pass), or we have seen one already (earlier canned displays take
precedence).
Matching columns are in fact not only those that have same keywords, but
also those that have keywords determined to be aliases to each other.
Some previous commits ensured that this determination is O(1) and in
practice just a few assembly instructions.
find_varentry() has been kept although its last caller has been removed
as next commit will reintroduce a call to it.
MFC after: 3 days
Relnotes: yes
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D49612
Differential Revision: https://reviews.freebsd.org/D49613 (manual page)
With this change, an alias keyword is resolved once and for all by
merging specifications of the keyword (or chain of keywords) it points
to. This merge in particular determines the final attributes of the
alias keyword, as well as the final keyword it points to, which uniquely
determines which information is printed (all non-alias keywords are
assumed to display different data).
Also, the alias resolving code has been moved into the resolve_alias()
function and helpers (e.g., merge_alias()). Aliases are still resolved
lazily as needed by default. The new top-level resolve_aliases()
function is used to resolve them at once at program startup if ps(1) has
been compiled with PS_CHECK_KEYWORDS defined. Else, it can also be
called directly from a debugger.
This is in preparation for removing columns that actually display the
same information in a subsequent commit, as this requires being able to
(quickly) determine if they are aliases to each other.
***
The merge process is now explicit and more flexible. Previously, all
fields of the resolved keyword were unconditionally used for the alias
(the pointer to an alias keyword structure was replaced by one to the
aliased keyword's one). Now, field 'final_kw' on the alias keyword will
store a pointer to the aliased keyword structure (and not only its name,
as a subsequent commit will need the structure address). Fields
'header', 'field' and 'flag' are taken from the aliased keyword if they
have default values (NULL or 0), else the alias' values prevail. This
allows an alias to override one or more of these fields. All fields
after 'oproc', because they describe the information to display
consistently with each other, are always taken from the aliased keyword.
merge_alias() checks that the values of these fields in the alias
keyword structure are unspecified (NULL, or some neutral value like
0 and UNSPEC).
While here, parsefmt() was reworked to avoid any direct recursion and
the break-up/recombination steps that were used when processing aliases.
The latter was due to the mutual recursion with findvar() and its
odd-for-that-purpose signature. findvar() has been removed in the
process. Simplification of parsefmt() also allows to be more precise
with the errors reported (in particular, the case of an empty keyword
with a specific header would just be reported as a "keyword not found"
message).
While here, introduce the check_keywords() function, performing sanity
checks on the declared keywords, currently only validating that they are
declared in alphabetical order. As for resolve_aliases(), this function
is called at startup on PS_CHECK_KEYWORDS, else it is available to be
called from a debugger.
Ideally, alias resolution should be done at compile time. In practice,
it seems doing so at runtime was never a problem (there are only a few
aliases compared to all available keywords, and there's currently at
most one level of aliasing). With the changes here, it seems very
unlikely to become one even if many more keywords, aliases or aliasing
levels are added.
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
This is in preparation for a change in how aliases are resolved, itself
in preparation for being able to quickly identify columns displaying the
same information (same final keyword).
Add the new UNSPEC ("unspecified") type, as the keywords' type field is
only used by the kvar() and rvar() output routines, and has no meaning
for alias keywords. In particular, this will allow to check that no
specific type is associated to any alias. An immediate benefit is that
now most keywords have UNSPEC as their "type", which now makes
kvar()/rvar() explicitly fail (instead of trying to print a character,
as the previous CHAR type was requesting). A developer introducing new
keywords via copy-paste will thus be reminded that it also needs to set
'type' meaningfully if using kvar()/rvar() as the output routine.
Rename field 'alias' of keywords ('VAR' type) into 'aliased'. Move it
just after the keyword's name, as it makes it easier to spot aliases in
the list. Make it a union, as a subsequent commit will... alias it with
a pointer to another 'VAR' structure. Turn aliases' header string (""
for all aliases) into NULL. It is currently not used, but will be when
introducing the new "merge" procedure for aliases (where it will mean:
Use the header of the aliased keyword).
While here, rename vars[] into the more descriptive keywords[].
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
Building on the previous commit that moved the 'width' field from VAR
(structure representing the meaning of a keyword) to VARENT (structure
representing an actual column in a display), it is now possible to
declare the field 'var' of VARENT as a constant reference.
At this point, it could be possible to constify the var[] array
containing all the recognized keywords. However, we do not do it as
a subsequent commit will change the way alias keywords are resolved,
causing their fields to be modified to reflect the final values to use
after alias resolution.
In parsefmt(), forget about allocating a new VAR for the selected
keyword to be pointed by the corresponding column (VARENT), and instead
just keep a pointer to that structure in var[].
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D49611
Some column's width depends not only on the information displayed but
also on the its header, which may have been customized. Consequently,
that width is a property of the actual column and should not be attached
to the keyword specifying which information to print.
This change fixes a bug where multiple columns displaying the same
information (same keyword) but with different headers would have the
same width, although they should not if they have long enough headers
that have different lengths (the width computed from the largest header
would be applied to all the corresponding keyword's columns).
Remove sizevars(), as the 'width' field is now initialized directly in
parsefmt(), which creates the VARENT structures.
While here, remove var[]'s sentinel line, and consequently adjust the
upper limit in showkey()'s loop and findvar()'s binary search.
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D49610
The only information that find_varentry() needs and uses is
a keyword/var name. The rest of the fields in the passed VAR are
unused.
Changing its signature will ease introducing new calls to
find_varentry() in subsequent commits, as there no VAR object will exist
to be passed but just a name.
Reviewed by: kib
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D49609
This is in preparation for changing the behavior of the '-O' option.
While here, reformat the definition of 'struct varent', fix formatting
of that of 'struct var' and expand slightly their herald comments.
More reformatting/commenting in 'ps.h'.
No functional change intended.
Reviewed by: kib
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D49607
Now that it is officially documented in the manual page.
Reviewed by: kib
MFC after: 3 days
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D49606