Skip to content

chcpu: refactor#4396

Draft
cgoesche wants to merge 11 commits into
util-linux:masterfrom
cgoesche:refactor_chcpu
Draft

chcpu: refactor#4396
cgoesche wants to merge 11 commits into
util-linux:masterfrom
cgoesche:refactor_chcpu

Conversation

@cgoesche
Copy link
Copy Markdown
Collaborator

@cgoesche cgoesche commented Jun 2, 2026

This is a temporary copy of my chcpu_tests branch which saved the refactor work and bug fixes that were initially added to PR #4390 . However, as suggested by @karelzak it is better to keep it simple and introduce these fixes in another iteration. Once the initial PR is merged, this branch will be rebased and commits will be adjusted accordingly to properly implement the changes.

cgoesche added 5 commits June 1, 2026 11:17
This patch makes it possible to configure the CPUs on a Linux
system other than the one on which the chcpu command is called.
To achieve this users can simply define the root directory of
the sys/ directory with the --sysroot command line option. It
is also beneficial for regression tests, as these make use of
sysfs tar archive dumps.

To properly implement this new feature, the syspath initialization
had to be deferred until after all option arguments, especially
--sysroot, have been parsed, which simplified the path access check.

Along the way a small refactoring of the enable,disable,configure &
deconfigure option parsing had been done, more precisely the CPU list
option argument is now saved in a variable 'cpu_list_arg' to defer
the CPU list validity check until after all option arguments have
been parsed to simplify the logic. Lastly, getopt(3)'s 'optarg' variable
is used instead of argv[optind-1] to store the options argument, which
is more idiomatic and readable.

Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
@cgoesche cgoesche marked this pull request as draft June 2, 2026 11:59
cgoesche added 6 commits June 2, 2026 18:45
Previously, cpu_enable() and cpu_configure() compared the number
of fails with ctx->maxcpus and if equal it returned -1, which
culminated over to an exit code of 1 (failure). Otherwise, if
there were fails but less than ctx->maxcpus the fucntions return
code 1 would result in an exit code 64 indicating partial success.

Since ctx->maxcpus describes the maximum number of CPUs supported
by the kernel, in practice, this failure mode isn't really useful
as it means that a user would need to fail the configuration of
every possible CPU. This is why chcpu(8) almost always returns 64.

It is better to store the actual amount of requested CPUs and then
compare the amount of fails to it and provide more meaningful exit
codes.

Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Let's stay consistent with the function contract and return -1 and
print a warning with warn[x]() in case of failure.

Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant