Skip to content

OpenBSD: add sensor structure and related constants - #5510

Open
nalysius wants to merge 1 commit into
rust-lang:mainfrom
nalysius:add-openbsd-sensors-sensor
Open

OpenBSD: add sensor structure and related constants#5510
nalysius wants to merge 1 commit into
rust-lang:mainfrom
nalysius:add-openbsd-sensors-sensor

Conversation

@nalysius

@nalysius nalysius commented Sep 7, 2026

Copy link
Copy Markdown

Description

Add the structure sensor from sys/sensors.h and related SENSOR_* constants.

Source

https://github.com/openbsd/src/blob/e8afce5b5b9d68772098e66f10777f7b6abb530d/sys/sys/sensors.h

@rustbot label +stable-nominated

@rustbot

rustbot commented Sep 7, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred in an OpenBSD module

cc @semarie

@rustbot rustbot added O-bsd S-waiting-on-review stable-nominated This PR should be considered for cherry-pick to libc's stable release branch labels Sep 7, 2026
@nalysius
nalysius force-pushed the add-openbsd-sensors-sensor branch from e4b9467 to ad7c2d5 Compare September 7, 2026 13:29
Comment thread src/new/openbsd/sys/sensors.rs Outdated

s! {
pub struct sensor {
pub desc: [u8; 32],

@semarie semarie Sep 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

use the C type please. for example by using c_char instead of u8. it is important as not all architectures supported has the same signess.

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

Comment thread src/new/openbsd/sys/sensors.rs Outdated
pub desc: [u8; 32],
pub timeval: timeval,
pub value: i64,
pub type_: c_int,

@semarie semarie Sep 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

type and status are C enum (see c_enum! macro).

View changes since the review

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done.

@nalysius

nalysius commented Sep 7, 2026

Copy link
Copy Markdown
Author

I updated based on your remarks, and I declared the sensors module that I forgot the first time.

@nalysius
nalysius requested a review from semarie September 7, 2026 15:00
Comment thread src/new/openbsd/sys/sensors.rs Outdated
}
}

pub const sensor_type_s: [&'static str; SENSOR_MAX_TYPES as usize + 1] = [

@semarie semarie Sep 7, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

&str couldn't be used here. If I understood the error of the CI (local CI on OpenBSD), it is not FFI compatible: it means that the Rust layout for it is not the same than C, and so can't be used.

I am unsure how to resolv it. maybe just not export sensor_type_s.
else maybe @tgross35 will have some tips

View changes since the review

@nalysius nalysius Sep 7, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Would this be better?

pub const sensor_type_s: [*const c_char; SENSOR_MAX_TYPES as usize + 1] = [
    cstr(b"temp\0"),
    cstr(b"fan\0"),
    /* ... */
];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think that definition should work, but I wouldn't bother adding it unless there's a usecase

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I work on an i3 status bar project (which motivated this PR), and I use the sensor struct and the constants, but to be honest I don't use this one. I ported it because it was part of the sensor ecosystem in the header. I will remove it.

@tgross35 tgross35 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM but would also like @semarie's approval. Please squash also

View changes since this review

@semarie

semarie commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

@tgross35 I will ping you once it is fine on my side. For now I have CI issues that I need to look at (if related to these changes or not). Thanks.

@semarie semarie left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

After running the CI on it, there are several points to address.

And outside the commentaries, libc-test/build/main.rs should also be adapted:

  • include sys/sensors.h in the headers included for testing the C values
  • register the field rename for sensor->type_
  • declare the enums

For this part, I have the following diff you could reuse:

diff --git a/libc-test/build/main.rs b/libc-test/build/main.rs
index 6a2ff1252..bb0e47ee1 100755
--- a/libc-test/build/main.rs
+++ b/libc-test/build/main.rs
@@ -411,6 +411,7 @@ fn test_openbsd(t: &Target) {
         "wchar.h",
         "ctype.h",
         "dirent.h",
+        "sys/sensors.h",
         "sys/socket.h",
         (x86_64, "machine/fpu.h"),
         "net/if.h",
@@ -506,6 +507,10 @@ fn test_openbsd(t: &Target) {

             "sa_sigaction" if struct_ == "sigaction" => "sa_handler".to_string(),

+            // Field is named `type` in C but that is a Rust keyword,
+            // so these fields are translated to `type_` in the bindings.
+            "type_" if struct_ == "sensor" => "type".to_string(),
+
             _ => return None,
         };
         Some(replacement)
@@ -526,6 +531,13 @@ fn test_openbsd(t: &Target) {
     cfg.rename_struct_ty(|ty| ty.ends_with("_t").then_some(ty.to_string()));
     cfg.rename_union_ty(|ty| ty.ends_with("_t").then_some(ty.to_string()));

+    cfg.alias_is_c_enum(move |ty| {
+        match ty {
+            "sensor_type" | "sensor_status" => true,
+            _ => false,
+        }
+    });
+
     cfg.skip_struct(move |struct_| {
         match struct_.ident() {
             // Extern types

View changes since this review

Comment thread src/new/openbsd/sys/sensors.rs Outdated
Comment thread libc-test/semver/openbsd.txt Outdated
SENSOR_STATUS_UNKNOWN
SENSOR_STATUS_UNSPEC
SENSOR_STATUS_WARN
SENSOR_TYPE_ACCEL

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it is not the exported name: SENSOR_ACCEL is the right one.
same for all SENSOR_TYPE_* names.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed.

Comment thread libc-test/semver/openbsd.txt Outdated
SENSOR_FINVALID
SENSOR_FUNKNOWN
SENSOR_MAX_TYPES
SENSOR_STATUS_CRIT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

it is not the exported name:  SENSOR_S_CRIT is the right one.
same for all SENSOR_STATUS_* names.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed.

Comment thread libc-test/semver/openbsd.txt Outdated
SENSOR_TYPE_TEMP
SENSOR_TYPE_TIMEDELTA
SENSOR_TYPE_VELOCITY
SENSOR_TYPE_VOLTSAC

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

SENSOR_TYPE_VOLTSAC -> SENSOR_VOLTS_AC

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed.

Comment thread libc-test/semver/openbsd.txt Outdated
SENSOR_TYPE_TIMEDELTA
SENSOR_TYPE_VELOCITY
SENSOR_TYPE_VOLTSAC
SENSOR_TYPE_VOLTSDC

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

SENSOR_TYPE_VOLTSDC -> SENSOR_VOLTS_DC

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed.

@nalysius

nalysius commented Sep 9, 2026

Copy link
Copy Markdown
Author

Most issues came from when I moved the constants to enums, I should have been more careful, sorry. Thank you for the main.rs patch.

@nalysius

nalysius commented Sep 9, 2026

Copy link
Copy Markdown
Author

The CI fails without any info, the error seems to be empty. Am I missing something?

@semarie

semarie commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

@nalysius

nalysius commented Sep 9, 2026

Copy link
Copy Markdown
Author

I discovered the script ci/style.py. I can't run it completely on OpenBSD nor in an Alpine VM since it requires +nightly and glibc, but at least the formatting worked. It should be better this time.

@semarie semarie left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM but please squash the commits
Cc @tgross35

View changes since this review

The structure sensor and the SENSOR_* constants have been added,
as well as the sensor_status and sensor_type enumerations.

Ref: https://github.com/openbsd/src/blob/e8afce5b5b9d68772098e66f10777f7b6abb530d/sys/sys/sensors.h
@nalysius
nalysius force-pushed the add-openbsd-sensors-sensor branch from 74cde7d to b4769f4 Compare September 9, 2026 11:54
@rustbot

rustbot commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@nalysius

nalysius commented Sep 9, 2026

Copy link
Copy Markdown
Author

Commits have been squashed. Thanks for your support in this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-bsd S-waiting-on-review stable-nominated This PR should be considered for cherry-pick to libc's stable release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants