Bug 279200 - sysrc(8) fails to perform set operations for += and -= in -c (check only) mode.
Summary: sysrc(8) fails to perform set operations for += and -= in -c (check only) mode.
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 14.0-RELEASE
Hardware: Any Any
: Normal Affects Many People
Assignee: freebsd-rc (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-05-21 14:17 UTC by crest
Modified: 2025-04-28 18:21 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description crest 2024-05-21 14:17:58 UTC
The sysrc command does not perform required set union/difference operations when asked to check if a variable contains certain values (or not) e.g. the following sequences of commands returns failure when it shouldn't:

    sysrc jail_list="jail1 jail2 jail3" && sysrc -c jail_list+=" jail2"
Comment 1 crest 2024-06-10 19:32:02 UTC
If I understand the code correctly the problem is that the case $mode in APPEND), REMOVE), *) is never reached if $CHECK_ONLY is set since sysrc:791 will continue the loop early.
Comment 2 Michal Scigocki 2025-04-14 07:21:20 UTC
I had a look, and it looks like the check logic does not account for APPEND and SUBTRACT operations. It just compares the existing value in the rc.conf file(s) to the value of the variable supplied on the command line.

The APPEND/SUBTRACT logic does peform the union/difference calculation. The check logic can be moved after the APPEND/SUBTRACT logic, and use the "before" and "new" variables for the check comparison to get the correct result.

I have created some regression tests for sysrc and a patch for this problem, and made a pull request with the the changes to github: https://github.com/freebsd/freebsd-src/pull/1664
Comment 3 commit-hook freebsd_committer freebsd_triage 2025-04-28 18:21:55 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=c97460c4d97db1bd46dab9f2bdbc90cd1ed7757f

commit c97460c4d97db1bd46dab9f2bdbc90cd1ed7757f
Author:     Michal Scigocki <michal.os@hotmail.com>
AuthorDate: 2025-04-14 07:01:06 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2025-04-28 18:18:08 +0000

    sysrc: Fix check flag logic for append and subtract

    When using sysrc with the check flag (-c), the append (+=) and subtract
    (-=) operations result in incorrect return values because on the check
    path the necessary union/difference calculation logic is not performed.

    However, the correct union/difference calculation is already performed
    when running without the check flag. We fix the issue on the check path
    by using the results from the existing union/difference calculation in
    the check logic to get the correct return values.

    PR:             279200
    Reviewed by:    markj
    MFC after:      1 month
    Pull Request:   https://github.com/freebsd/freebsd-src/pull/1664

 usr.sbin/sysrc/sysrc | 52 +++++++++++++++++++++++++---------------------------
 1 file changed, 25 insertions(+), 27 deletions(-)