Skip to content

batt: distinguish AC line status values - #499

Merged
laffer1 merged 1 commit into
masterfrom
master-batt-acline-status
Sep 3, 2026
Merged

batt: distinguish AC line status values#499
laffer1 merged 1 commit into
masterfrom
master-batt-acline-status

Conversation

@laffer1

@laffer1 laffer1 commented Sep 3, 2026

Copy link
Copy Markdown
Member

Summary

  • report AC-line value 0 as battery power and value 1 as plugged in
  • diagnose the kernel sentinel value -1 as unknown and reject other values
  • add regression coverage for offline, unknown, and invalid AC-line values

Master already flushes the test child output via exit(), so this PR does not duplicate the stable-branch harness fix.

Testing

  • bmake -C usr.bin/batt clean all
  • kyua test -k /usr/obj/usr/src/amd64.amd64/usr.bin/batt/tests/Kyuafile (9/9 passed)
  • git diff --cached --check -- usr.bin/batt

The repository C-analysis scripts were invoked. Their \.c$ filter skipped clang-tidy and Splint, so both were also run manually. Splint cannot parse the current system headers; clang-tidy/cppcheck reported pre-existing diagnostics in batt and its test harness, with no diagnostic specific to the new AC-line switch.

AI-Assisted-by: GPT-5.6 (Codex)

Summary by Sourcery

Distinguish supported AC-line status values and handle sentinel or invalid values explicitly.

New Features:

  • Report AC-line status 0 as battery power and status 1 as plugged in.

Bug Fixes:

  • Reject unknown and invalid AC-line status values with explicit errors instead of treating them as charging or drained.

Tests:

  • Add regression tests covering battery power, unknown, and invalid AC-line statuses.

Report battery power only for an offline AC line and diagnose unknown or invalid statuses. Add regression coverage for each AC line status path.

AI-Assisted-by: GPT-5.6 (Codex)
Signed-off-by: Lucas Holt <luke@foolishgames.com>
Copilot AI lite review requested due to automatic review settings September 3, 2026 23:19
@sourcery-ai

sourcery-ai Bot commented Sep 3, 2026

Copy link
Copy Markdown

Reviewer's Guide

The PR replaces the previous fallback AC-line handling with an explicit switch that distinguishes battery power, plugged-in, unknown, and invalid values, and adds ATF regression coverage for the new behavior.

Flow diagram for AC-line status handling

flowchart TD
    A[Read AC line status] --> B{AC line value}
    B -->|0| C[Print System running on battery power]
    B -->|1| D[Print System plugged in]
    B -->|-1| E[Exit with AC line status unknown]
    B -->|Other| F[Exit with unexpected AC line status value]
Loading

File-Level Changes

Change Details Files
Explicitly validate and report each supported AC-line status.
  • Map status 0 to battery-power output and status 1 to plugged-in output.
  • Treat the kernel sentinel -1 as an unknown error.
  • Reject all other AC-line values with an error.
usr.bin/batt/batt.c
Add regression tests for AC-line status handling.
  • Cover battery-power, unknown-sentinel, and invalid-value cases.
  • Verify exit status, stdout, and relevant stderr diagnostics.
  • Register the three new ATF test cases.
usr.bin/batt/tests/batt_test.c

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@laffer1
laffer1 merged commit 44eecad into master Sep 3, 2026
6 of 11 checks passed
@laffer1
laffer1 deleted the master-batt-acline-status branch September 3, 2026 23:20
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown

Claude finished @laffer1's task in 1m 45s —— View job


Review: batt: distinguish AC line status values (#499, merged)

  • Gather context
  • Review usr.bin/batt/batt.c and usr.bin/batt/tests/batt_test.c
  • Post review feedback

No specific question was included in the trigger comment beyond the PR description, so here is a code review of the merged changes per repo review guidelines (correctness, malloc/free symmetry, bounds checking, style(9), security).

Correctness — verified against the kernel side

I cross-checked the new switch in batt.c:110-121 against sys/dev/acpica/acpi_acad.c:

  • sc->status is initialized to -1 (acpi_acad_attach, line 179) and only updated from _PSR when the ACPI method returns a valid (non--1) value (acpi_acad_get_status, line 104/109).
  • _PSR returns 0 (offline/battery) or 1 (online/AC) per ACPI semantics, matching power_profile_set_state(newstatus ? PERFORMANCE : ECONOMY).

So the new mapping — 0 → "System running on battery power", 1 → "System plugged in", -1 → "AC line status unknown" (exit 1), anything else → "unexpected AC line status value" (exit 1) — is correctly grounded in the actual kernel sentinel/value semantics, not a guess.

Bounds/size checks: The len != sizeof(acline) guard before touching acline is correct and mirrors the existing pattern used for life/time/units elsewhere in the file.

malloc/free: batt.c itself does no dynamic allocation, so symmetry isn't a concern there. In the test harness (batt_test.c), read_fd()'s realloc() result is assigned directly back into data (line 135) — on a hypothetical realloc failure the original buffer would leak — but this is test-only code guarded immediately by ATF_REQUIRE(data != NULL), which aborts the test process, so there's no real-world impact. Not flagging as a required fix.

Style: The new AC-line branch uses an if / else if / else chain (batt.c:105-109) where sibling checks for life/time/units use separate top-level if statements. Purely cosmetic — no functional difference since errx() doesn't return — but worth noting for consistency if this code is touched again.

Security: No injection, overflow, or format-string issues. sysctlbyname usage and output are constant-format printf/puts calls only.

Tests: The new ATF cases (acline_status, battery_power_status, unknown_acline_status, invalid_acline_status) mock all four relevant cases (plugged in, on battery, unknown sentinel, invalid value) and check both exit status and stderr content — good coverage for this change.

Overall: no correctness, memory-safety, or style(9) issues found in the shipped diff.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change is narrowly scoped, handles the sysctl value domain explicitly, and includes targeted regression coverage for the new behaviors.

Pull request overview

This PR improves usr.bin/batt’s interpretation of the hw.acpi.acline sysctl by distinguishing the supported AC-line status values (0/1), explicitly handling the kernel sentinel -1, and rejecting any other unexpected values; it also adds regression tests to cover the new behavior.

Changes:

  • Update batt -t output to report AC-line status 0 as “System running on battery power” and 1 as “System plugged in”.
  • Treat AC-line status -1 as “unknown” (exit 1) and reject other values as unexpected (exit 1).
  • Add ATF regression tests for battery power, unknown, and invalid AC-line status values.
File summaries
File Description
usr.bin/batt/batt.c Switch-based handling of hw.acpi.acline with explicit mapping for 0/1, sentinel -1 error, and default rejection of other values.
usr.bin/batt/tests/batt_test.c Adds regression test cases for AC-line values 0 (battery), -1 (unknown), and 2 (invalid), and registers them in the test plan.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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.

2 participants