diff --git a/README.md b/README.md index 477f747..c17ed31 100644 --- a/README.md +++ b/README.md @@ -206,13 +206,15 @@ is removed. A controller on a motherboard chip follows only that chip when its hwmon number changes. When the chip is absent, for example because its driver is not loaded, the fan keeps its saved controller and is not moved to another chip -with a channel of the same name. A controller saved in the older -`/sys/class/hwmon/hwmonN` form names a number rather than a chip, so it is no -longer driven: select that fan's controller again once. -CPU and auxiliary hwmon sensors are saved by their device as well. Sensors -saved in the older `/sys/class/hwmon/hwmonN` form are converted once, when fan -control starts after every plugin has installed, to the chip holding that -number then. +with a channel of the same name. +CPU and auxiliary hwmon sensors are saved by their device as well. +Fan controllers, their labels, and sensors saved in the older +`/sys/class/hwmon/hwmonN` form name a number rather than a chip. The first +time fan control starts after this update, once every plugin has installed, +they are converted to the chip holding that number then. One whose number is +absent at that moment is never converted later, when another chip may hold +the number: such a controller is not driven until you select that fan's +controller again. The settings page keeps a saved controller, CPU sensor, or auxiliary sensor that is not detected selected and marks it; saving asks for confirmation first. The fan cannot use it until it is back and fan control restarts, or diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php index 1c11f2d..dca6dab 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php @@ -90,20 +90,26 @@ function find_moved_pwm_path(string $old_path): ?string { } // A legacy /sys/class/hwmon path names a number, whatever chip holds it, not -// a device: a controller saved that way is never resolved or driven (the user -// picks it again), and a fan is never moved to another chip with a channel of -// the same name (a chip whose driver has not loaded yet, #41). +// a device: it is never driven or followed to another number. The first +// migration after the upgrade (FCP_CLASS_FLAG) pins controllers, labels and +// sensors saved that way to the chip holding that number then, the chip +// stable releases resolved them to; it runs once every plugin has installed +// (rc.fanctrlplus2 boot), so drivers have normally bound their chips by then. +// A number absent then stays saved, and a controller undriven until the user +// picks it again: it is never resolved later, when another chip may hold it +// (#41). function fcp_class_hwmon_path(string $path): bool { return strpos($path, '/class/hwmon/') !== false; } -// A hwmon temperature sensor saved by its /sys/class/hwmon index is pinned, -// once, to the device holding that index. Migration runs once every plugin -// has installed (rc.fanctrlplus2 boot), so drivers have normally bound their -// chips by then. A device path follows its device when its hwmon number -// changes, as a controller does; any other source is kept as saved. -function fcp_pin_sensor(string $path): string { - if (fcp_class_hwmon_path($path)) return realpath($path) ?: $path; +const FCP_CLASS_FLAG = '.hwmon_numbers_converted'; + +// A hwmon temperature sensor saved by its /sys/class/hwmon index is pinned to +// the device holding that index on the first migration only +// (fcp_class_hwmon_path). A device path follows its device when its hwmon +// number changes, as a controller does; any other source is kept as saved. +function fcp_pin_sensor(string $path, bool $by_number): string { + if ($by_number && fcp_class_hwmon_path($path)) return realpath($path) ?: $path; return str_starts_with($path, '/') ? (find_moved_pwm_path($path) ?? $path) : $path; } @@ -181,6 +187,77 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw $labelFile = "$cfgpath/pwm_labels.cfg"; $pwms = list_pwm($hwmon_glob); $identities = fcp_pwm_identity_map($pwms); + $flag = "$cfgpath/".FCP_CLASS_FLAG; + // The flag is written first: if it cannot be, nothing is converted by + // number, rather than again at a later start when the numbers may differ. + // Nothing to convert without a config directory: no flag, no log. + $by_number = is_dir($cfgpath) && !file_exists($flag); + if ($by_number && !@touch($flag)) { + log_migrate("migrate: cannot write $flag; hwmon numbers are not converted"); + $by_number = false; + } + + // --- cfgs --- + // A fan assigned to the current path while an old one still pointed at + // the vanished path keeps it: two loops must not drive the same PWM. + $cfgfiles = glob("$cfgpath/{$plugin}_*.cfg") ?: []; + $configs = []; $in_use = []; $pinned = []; + foreach ($cfgfiles as $cfgfile) { + $ini = @parse_ini_file($cfgfile); + if (!$ini) continue; + $configs[$cfgfile] = $ini; + $path = (string)($ini['controller'] ?? ''); + $source = fcp_controller_source($ini); + $resolved = strpos($source, 'usb:') === 0 ? ($identities[$source] ?? null) : (is_file($source) ? $source : null); + // Already-current manual assignments win over stale migrated ones. + if ($resolved === $path && $path !== '' && !isset($in_use[$path])) $in_use[$path] = $cfgfile; + } + // Then, on the first run, a device some class path names (a number) goes + // to a fan saved on it: first one whose saved device moved there (it names + // a device), then one saved on that class path, then one blanked but bound + // to a device or serial; one blanked on a class path claims nothing, so + // file order cannot hand it a fan's device. Devices no class path names + // are left to the migration below, as on later runs. + if ($by_number) { + $target = []; $is_class = []; $named = []; + foreach ($configs as $cfgfile => $ini) { + $source = fcp_controller_source($ini); + if (!preg_match('#^usb:|/pwm\d+$#', $source)) continue; + $target[$cfgfile] = fcp_controller_target($source, $identities, true); + $is_class[$cfgfile] = fcp_class_hwmon_path($source); + if ($target[$cfgfile] !== null && $is_class[$cfgfile]) $named[$target[$cfgfile]] = true; + } + // Tiers: saved on a moved device, saved on a class path, then blanked + // but bound to a device or serial. A blank class config never claims. + foreach ([[false, false], [true, false], [false, true]] as [$by_class, $blank]) foreach ($target as $cfgfile => $device) { + if ($device === null || !isset($named[$device]) || $is_class[$cfgfile] !== $by_class) continue; + if (((string)($configs[$cfgfile]['controller'] ?? '') === '') === $blank) $in_use[$device] ??= $cfgfile; + } + } + foreach ($configs as $cfgfile => $ini) { + $original = $ini; + foreach (['cpu_sensor', 'aux_sensor'] as $k) { + if (($ini[$k] ?? '') === '') continue; + $ini[$k] = implode(',', array_map(fn($s) => fcp_pin_sensor($s, $by_number), explode(',', (string)$ini[$k]))); + if ($ini[$k] !== $original[$k]) log_migrate("migrate cfg: $cfgfile $k: {$original[$k]} → {$ini[$k]}"); + } + fcp_migrate_controller($ini, $cfgfile, $in_use, $identities, $by_number); + $was = fcp_controller_source($original); + $now = (string)($ini['controller'] ?? ''); + if (fcp_class_hwmon_path($was) && $now !== '' && !fcp_class_hwmon_path($now)) $pinned[$was] = $now; + if ($ini === $original) continue; + // Only these fields change. Re-serializing the entire INI would + // re-escape free-text values that are also sourced by the shell. + $buf = (string)file_get_contents($cfgfile); + foreach (['controller', 'controller_identity', 'cpu_sensor', 'aux_sensor'] as $k) { + if (($ini[$k] ?? null) === ($original[$k] ?? null)) continue; + $v = str_replace(["\\", '"', '$', '`', "\r", "\n"], ["\\\\", '\\"', '\\$', '\\`', '', ''], (string)$ini[$k]); + $line = "$k=\"$v\""; + $buf = preg_replace_callback('/^[ \t]*'.$k.'[ \t]*=.*$/m', function () use ($line) { return $line; }, $buf, -1, $count); + if ($count === 0) $buf = rtrim($buf, "\n")."\n".$line."\n"; + } + safe_rewrite($cfgfile, $buf); + } // --- labels --- if (is_file($labelFile)) { @@ -202,7 +279,8 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw } $stable = strpos($old_path, 'usb:') === 0; - $new_path = $stable ? ($identities[$old_path] ?? null) : find_moved_pwm_path($old_path); + // A class label follows only its controller pinned above. + $new_path = $stable ? ($identities[$old_path] ?? null) : ($pinned[$old_path] ?? find_moved_pwm_path($old_path)); if ($new_path === null) { log_migrate("migrate label: no match, keep $old_path"); $out[]=$line; continue; } $is_current = $stable || $new_path === $old_path; $key = fcp_unique_pwm_identity($new_path, $identities) ?? $new_path; @@ -230,54 +308,31 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw } if ($changed) safe_rewrite($labelFile, implode("\n", $out)); } +} +// The saved binding migration resolves: the identity, else the path. +function fcp_controller_source(array $ini): string { + $identity = (string)($ini['controller_identity'] ?? ''); + return $identity !== '' ? $identity : trim((string)($ini['controller'] ?? ''), " \t\n\r\0\x0B\"'"); +} - // --- cfgs --- - // A fan assigned to the current path while an old one still pointed at - // the vanished path keeps it: two loops must not drive the same PWM. - $cfgfiles = glob("$cfgpath/{$plugin}_*.cfg") ?: []; - $configs = []; $in_use = []; - foreach ($cfgfiles as $cfgfile) { - $ini = @parse_ini_file($cfgfile); - if (!$ini) continue; - $configs[$cfgfile] = $ini; - $path = (string)($ini['controller'] ?? ''); - $identity = (string)($ini['controller_identity'] ?? ''); - $source = $identity !== '' ? $identity : $path; - $resolved = strpos($source, 'usb:') === 0 ? ($identities[$source] ?? null) : (is_file($source) ? $source : null); - // Already-current manual assignments win over stale migrated ones. - if ($resolved === $path && $path !== '' && !isset($in_use[$path])) $in_use[$path] = $cfgfile; - } - foreach ($configs as $cfgfile => $ini) { - $original = $ini; - foreach (['cpu_sensor', 'aux_sensor'] as $k) { - if (($ini[$k] ?? '') === '') continue; - $ini[$k] = implode(',', array_map('fcp_pin_sensor', explode(',', (string)$ini[$k]))); - if ($ini[$k] !== $original[$k]) log_migrate("migrate cfg: $cfgfile $k: {$original[$k]} → {$ini[$k]}"); - } - fcp_migrate_controller($ini, $cfgfile, $in_use, $identities); - if ($ini === $original) continue; - // Only these fields change. Re-serializing the entire INI would - // re-escape free-text values that are also sourced by the shell. - $buf = (string)file_get_contents($cfgfile); - foreach (['controller', 'controller_identity', 'cpu_sensor', 'aux_sensor'] as $k) { - if (($ini[$k] ?? null) === ($original[$k] ?? null)) continue; - $v = str_replace(["\\", '"', '$', '`', "\r", "\n"], ["\\\\", '\\"', '\\$', '\\`', '', ''], (string)$ini[$k]); - $line = "$k=\"$v\""; - $buf = preg_replace_callback('/^[ \t]*'.$k.'[ \t]*=.*$/m', function () use ($line) { return $line; }, $buf, -1, $count); - if ($count === 0) $buf = rtrim($buf, "\n")."\n".$line."\n"; - } - safe_rewrite($cfgfile, $buf); - } +// Where that binding is now: a USB controller by its serial, a class path by +// number on the first run only, a device path where its chip moved. +function fcp_controller_target(string $source, array $identities, bool $by_number): ?string { + if (strpos($source, 'usb:') === 0) return $identities[$source] ?? null; + if (fcp_class_hwmon_path($source)) return $by_number ? (realpath($source) ?: null) : null; + return find_moved_pwm_path($source); } + // Resolves one configuration's controller in $ini. A saved path that no longer -// exists, or a /sys/class one, is kept for the settings page to show, until -// the user picks another controller; nothing is moved to another device. -function fcp_migrate_controller(array &$ini, string $cfgfile, array &$in_use, array $identities): void { +// exists, or a /sys/class one not pinned on the first run ($by_number, +// fcp_class_hwmon_path), is kept for the settings page to show, until the user +// picks another controller; nothing is moved to another device. +function fcp_migrate_controller(array &$ini, string $cfgfile, array &$in_use, array $identities, bool $by_number): void { $old_path = trim((string)($ini['controller'] ?? ''), " \t\n\r\0\x0B\"'"); $identity = (string)($ini['controller_identity'] ?? ''); if ($identity === '' && !preg_match('#/pwm\d+$#', $old_path)) return; - $source = $identity !== '' ? $identity : $old_path; - $new_path = strpos($source, 'usb:') === 0 ? ($identities[$source] ?? null) : find_moved_pwm_path($source); + $source = fcp_controller_source($ini); + $new_path = fcp_controller_target($source, $identities, $by_number); if ($new_path !== null && isset($in_use[$new_path]) && $in_use[$new_path] !== $cfgfile) { log_migrate("migrate cfg: keep $old_path in $cfgfile, $new_path is already controlled"); if ($identity === '' && !is_file($old_path)) return; diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/FanBlockRender.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/FanBlockRender.php index caf8573..6f2696a 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/FanBlockRender.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/FanBlockRender.php @@ -141,6 +141,13 @@ function fcp_sensor_missing(string $sensor): bool { return !str_starts_with($sensor, '/') || find_moved_pwm_path($sensor) === null; } +// The option text for a saved sensor the page did not list. One saved by its +// hwmon number is never read again, so it asks to be selected again. +function fcp_saved_sensor_text(string $sensor): string { + if (fcp_class_hwmon_path($sensor)) return "Select again: $sensor (saved by hwmon number)"; + return (fcp_sensor_missing($sensor) ? 'Not detected: ' : '') . $sensor; +} + function render_fan_block($cfg, $i, $pwms, $disks, $pwm_labels, $cpu_sensors, $aux_sensors = []) { $curve_error = ''; try { $curve_rules = fcp_curve_rules($cfg); } @@ -358,7 +365,7 @@ class="interval-input fcp-interval-input" - + @@ -402,7 +409,7 @@ class="interval-input fcp-interval-input" - + diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/aux_sensors.sh b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/aux_sensors.sh index 5347fcd..4b85783 100755 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/aux_sensors.sh +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/aux_sensors.sh @@ -148,7 +148,9 @@ aux_read_sensor() { *) # An hwmon input reading exactly 0 is the long-standing marker for a # sensor that is present but not populated, not a reading of 0 °C. - [[ -f "$sensor" ]] || return 0 + # A /sys/class/hwmon path names a number, whatever chip holds it now: + # never read (fcp_class_hwmon_path in Common.php). + [[ -f "$sensor" && "$sensor" != */class/hwmon/* ]] || return 0 raw=$(cat "$sensor" 2>/dev/null) [[ "$raw" =~ ^-?[0-9]+$ ]] || return 0 (( raw == 0 )) && return 0 diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/fanctrlplus2_loop.sh b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/fanctrlplus2_loop.sh index 0129a6a..acdf5c2 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/fanctrlplus2_loop.sh +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/fanctrlplus2_loop.sh @@ -81,7 +81,8 @@ while true; do # observation of it while this one waits, and an older sample must not undo # that (see the observation time in curve_control.sh). cpu_sampled=0 - if [[ "${cpu_enable:-0}" == "1" && -n "$cpu_sensor" && -f "$cpu_sensor" ]]; then + # A /sys/class/hwmon sensor is never read: see aux_sensors.sh. + if [[ "${cpu_enable:-0}" == "1" && -f "$cpu_sensor" && "$cpu_sensor" != */class/hwmon/* ]]; then fcp_stamp_now cpu_stamp=$fcp_now cpu_raw=$(cat "$cpu_sensor") diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/fanctrlplus2_refresh_single.sh b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/fanctrlplus2_refresh_single.sh index 0832dae..e300a0b 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/fanctrlplus2_refresh_single.sh +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/fanctrlplus2_refresh_single.sh @@ -51,7 +51,8 @@ mkdir -p "/var/tmp/${plugin}" || # began, so a sample older than a newer observation another run published while # this one waited cannot undo it (see the observation time in curve_control.sh). cpu_sampled=0 -if [[ "${cpu_enable:-0}" == "1" && -n "$cpu_sensor" && -f "$cpu_sensor" ]]; then +# A /sys/class/hwmon sensor is never read: see aux_sensors.sh. +if [[ "${cpu_enable:-0}" == "1" && -f "$cpu_sensor" && "$cpu_sensor" != */class/hwmon/* ]]; then fcp_stamp_now cpu_stamp=$fcp_now cpu_raw=$(cat "$cpu_sensor") diff --git a/tests/aux_sensor_read_test.sh b/tests/aux_sensor_read_test.sh index 0c92384..950a933 100755 --- a/tests/aux_sensor_read_test.sh +++ b/tests/aux_sensor_read_test.sh @@ -416,6 +416,12 @@ expect_equal "50" "$(aux_hottest_reading <<< "$readings")" \ expect_equal "" "$(aux_unread_sensors <<< "$(aux_temp_readings "$tmp/temp3_input")")" \ "When every sensor reads, none is named." +# A sensor saved by hwmon number is never read: another chip may hold it. +mkdir -p "$tmp/class/hwmon/hwmon7" +echo 45000 > "$tmp/class/hwmon/hwmon7/temp1_input" +expect_equal "$tmp/class/hwmon/hwmon7/temp1_input" "$(aux_unread_sensors <<< "$(aux_temp_readings "$tmp/class/hwmon/hwmon7/temp1_input")")" \ + "A /sys/class/hwmon sensor must be reported unread, not read by number." + if (( failures > 0 )); then printf '%d aux sensor assertion(s) failed.\n' "$failures" >&2 exit 1 diff --git a/tests/curve_manual_test.sh b/tests/curve_manual_test.sh index dca9300..ea87434 100755 --- a/tests/curve_manual_test.sh +++ b/tests/curve_manual_test.sh @@ -359,6 +359,17 @@ expect_equal "1 77" "$rc $(cat "$tmp/pwm1")" "Run Now refuses a path that is not expect_equal yes "$(said 'not the USB controller saved')" "The refusal names the missing USB controller." write_cfg '' +# A CPU sensor saved by its hwmon number is never read: another chip may hold it. +write_cfg "$rule_latch" +mkdir -p "$tmp/class/hwmon/hwmon3" +printf '60000\n' > "$tmp/class/hwmon/hwmon3/temp1_input" +sed -i "s#^cpu_sensor=.*#cpu_sensor=\"$tmp/class/hwmon/hwmon3/temp1_input\"#" "$FCP_CONFIG_DIR/fanctrlplus2_$custom.cfg" +fresh 100 +run_manual 60000 +# No reading: the run applies the no-source output (0 here), not the 60 °C emergency. +expect_equal "0 0" "$rc $(cat "$tmp/pwm1")" "Run Now must not read a CPU sensor saved by its hwmon number." +write_cfg '' + if (( failures > 0 )); then exit 1 fi diff --git a/tests/fan_response_loop_test.sh b/tests/fan_response_loop_test.sh index 8b9fda9..013dab6 100755 --- a/tests/fan_response_loop_test.sh +++ b/tests/fan_response_loop_test.sh @@ -89,4 +89,10 @@ grep -q 'RPM changed from 1000 to 3300' "$tmp/notifications" || { echo 'The real run 40000 30000 3300 1000 1 grep -q 'RPM changed from 3300 to 1000' "$tmp/notifications" || { echo 'Leaving full speed reported as mode 0 lost the RPM baseline.' >&2; exit 1; } +# A CPU sensor saved by its hwmon number is never read: another chip may hold it. +mkdir -p "$tmp/class/hwmon/hwmon3" +echo 40000 > "$tmp/class/hwmon/hwmon3/temp1_input" +sed -i "s#^cpu_sensor=.*#cpu_sensor=\"$tmp/class/hwmon/hwmon3/temp1_input\"#" "$tmp/fan.cfg" +run 40000 40000 1000 1000 '' +[[ $(cat "$tmp/pwm1") == 77 ]] || { echo "The loop must hold Idle (77) without reading a CPU sensor saved by its hwmon number, got $(cat "$tmp/pwm1")." >&2; exit 1; } echo 'fan response loop test passed' diff --git a/tests/missing_binding_ui_test.php b/tests/missing_binding_ui_test.php index 871749e..f3dda70 100644 --- a/tests/missing_binding_ui_test.php +++ b/tests/missing_binding_ui_test.php @@ -36,6 +36,9 @@ $class = '/sys/class/hwmon/hwmon2/pwm2'; $opt = $option($render(['controller' => $class]), $class); $check($flagged($opt) && strpos($opt, 'Select again') !== false, 'A controller saved by hwmon number must be flagged for selecting again.'); +$classTemp = '/sys/class/hwmon/hwmon2/temp1_input'; +$html = $render(['controller' => $pwms[0]['sensor'], 'cpu_enable' => '1', 'cpu_sensor' => $classTemp, 'aux_enable' => '1', 'aux_sensor' => $classTemp]); +$check(substr_count($html, "Select again: $classTemp (saved by hwmon number)") === 2, 'CPU and aux sensors saved by hwmon number must ask to be selected again.'); // Blanked controllers (unresolved, or a conflict loser) show their identity // only when that device is absent. diff --git a/tests/pwm_path_migration_test.php b/tests/pwm_path_migration_test.php index daed7f1..57b145e 100644 --- a/tests/pwm_path_migration_test.php +++ b/tests/pwm_path_migration_test.php @@ -126,12 +126,19 @@ $failures[] = 'Once its chip loads, the fan must bind to it again.'; } -// Legacy /sys/class bindings name an index, not a device. A controller is -// never resolved through one; a sensor is pinned to the device holding it. +// Legacy /sys/class bindings name an index, not a device. The first migration +// pins controllers, their labels and sensors to the device holding it; an +// absent index is kept, and never resolved on later runs. $legacy = "$root/legacy"; @mkdir($legacy); $temp = "$sys/devices/platform/it87.2624/hwmon/hwmon2/temp1_input"; +$classPwm = "$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm2"; touch($temp); +$classPwm3 = "$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm3"; +$classPwm4 = "$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm4"; +touch($classPwm3); +touch($classPwm4); +file_put_contents("$legacy/pwm_labels.cfg", "$class/hwmon2/pwm2=Front intake\n$class/hwmon7/pwm2=Absent\n$classPwm3=Saved\n$class/hwmon2/pwm3=Old\n$class/hwmon2/pwm4=Blank\n$class/hwmon2/pwm5=Mixed\n"); file_put_contents("$legacy/fanctrlplus2_old.cfg", implode("\n", [ 'custom="old"', "controller=\"$class/hwmon2/pwm2\"", @@ -139,10 +146,74 @@ "aux_sensor=\"storcli:c0:roc,$class/hwmon2/temp1_input,$class/hwmon7/temp1_input\"", 'note="a \\` b"', ]) . "\n"); +// 2026.11.12 saved a class controller as its own identity, or only as its +// identity when its chip was missing at a start. +file_put_contents("$legacy/fanctrlplus2_ided.cfg", "custom=\"ided\"\ncontroller=\"$class/hwmon2/pwm3\"\ncontroller_identity=\"$class/hwmon2/pwm3\"\n"); +file_put_contents("$legacy/fanctrlplus2_blank.cfg", "custom=\"blank\"\ncontroller=\"\"\ncontroller_identity=\"$class/hwmon2/pwm4\"\n"); +// 2026.11.12 blanked a conflict loser and kept the class path as its identity; +// it sorts first, but the winner saved on that path keeps the device. +file_put_contents("$legacy/fanctrlplus2_aloser.cfg", "custom=\"aloser\"\ncontroller=\"\"\ncontroller_identity=\"$class/hwmon2/pwm2\"\n"); +file_put_contents("$legacy/fanctrlplus2_zloser.cfg", "custom=\"zloser\"\ncontroller=\"\"\ncontroller_identity=\"$class/hwmon2/pwm2\"\n"); +// A device a fan holds by its current path beats a class path naming it, +// whatever the file order; a class controller whose identity names another +// device follows the identity. +$dev = "$sys/devices/platform/it87.2624/hwmon/hwmon2"; +foreach ([5, 6, 7] as $n) touch("$dev/pwm$n"); +file_put_contents("$legacy/fanctrlplus2_acls.cfg", "custom=\"acls\"\ncontroller=\"$class/hwmon2/pwm7\"\n"); +file_put_contents("$legacy/fanctrlplus2_bdev.cfg", "custom=\"bdev\"\ncontroller=\"$dev/pwm7\"\ncontroller_identity=\"$dev/pwm7\"\n"); +file_put_contents("$legacy/fanctrlplus2_amix.cfg", "custom=\"amix\"\ncontroller=\"$class/hwmon2/pwm5\"\ncontroller_identity=\"$dev/pwm6\"\n"); +// bman was saved before its chip moved from hwmon9: not current, so it is +// not claimed first, and amix must not hold pwm5 against it. +file_put_contents("$legacy/fanctrlplus2_bman.cfg", "custom=\"bman\"\ncontroller=\"$sys/devices/platform/it87.2624/hwmon/hwmon9/pwm5\"\n"); +// A fan whose saved device moved beats a blanked loser and a class path that +// name the same device, whatever the file order. +foreach ([8, 9] as $n) touch("$dev/pwm$n"); +file_put_contents("$legacy/fanctrlplus2_alose8.cfg", "custom=\"alose8\"\ncontroller=\"\"\ncontroller_identity=\"$class/hwmon2/pwm8\"\n"); +file_put_contents("$legacy/fanctrlplus2_zwin8.cfg", "custom=\"zwin8\"\ncontroller=\"$sys/devices/platform/it87.2624/hwmon/hwmon9/pwm8\"\n"); +file_put_contents("$legacy/fanctrlplus2_acls9.cfg", "custom=\"acls9\"\ncontroller=\"$class/hwmon2/pwm9\"\n"); +file_put_contents("$legacy/fanctrlplus2_zwin9.cfg", "custom=\"zwin9\"\ncontroller=\"$sys/devices/platform/it87.2624/hwmon/hwmon9/pwm9\"\n"); +// A class controller whose identity names a moved device claims that device +// by its identity, ahead of a class path naming it. +foreach ([11, 12] as $n) touch("$dev/pwm$n"); +file_put_contents("$legacy/fanctrlplus2_aay.cfg", "custom=\"aay\"\ncontroller=\"$class/hwmon2/pwm12\"\n"); +file_put_contents("$legacy/fanctrlplus2_zzx.cfg", "custom=\"zzx\"\ncontroller=\"$class/hwmon2/pwm11\"\ncontroller_identity=\"$sys/devices/platform/it87.2624/hwmon/hwmon9/pwm12\"\n"); +// A blank form whose number is absent stays as saved, and so does its label. +file_put_contents("$legacy/fanctrlplus2_gone.cfg", "custom=\"gone\"\ncontroller=\"\"\ncontroller_identity=\"$class/hwmon7/pwm2\"\n"); +file_put_contents("$legacy/fanctrlplus2_absent.cfg", "custom=\"absent\"\ncontroller=\"$class/hwmon7/pwm2\"\n"); +file_put_contents("$legacy/fanctrlplus2_twin.cfg", "custom=\"twin\"\ncontroller=\"$class/hwmon2/pwm2\"\n"); migrate_cfg_and_labels('fanctrlplus2', $legacy, "$class/hwmon*"); $cfgLegacy = parse_ini_file("$legacy/fanctrlplus2_old.cfg"); -if ($cfgLegacy['controller'] !== "$class/hwmon2/pwm2") { - $failures[] = 'A /sys/class controller must be kept as saved, for the user to pick again, not resolved to the chip at that index.'; +if ($cfgLegacy['controller'] !== $classPwm) { + $failures[] = 'A /sys/class controller must be pinned to the device holding that index, got ' . $cfgLegacy['controller']; +} +foreach (['aloser', 'zloser'] as $name) { + $got = parse_ini_file("$legacy/fanctrlplus2_$name.cfg")['controller']; + if ($got !== '') $failures[] = "A blanked conflict loser ($name) must not take the device from the fan saved on it, got $got"; +} +$c = fn($name) => parse_ini_file("$legacy/fanctrlplus2_$name.cfg")['controller']; +if ($c('bdev') !== "$dev/pwm7" || $c('acls') !== "$class/hwmon2/pwm7") { + $failures[] = "A fan on its current device path must keep it over a class path naming it, got bdev={$c('bdev')} acls={$c('acls')}"; +} +if ($c('bman') !== "$dev/pwm5" || $c('amix') !== "$dev/pwm6") { + $failures[] = "A class controller with another identity must not hold the device its path names, got bman={$c('bman')} amix={$c('amix')}"; +} +if ($c('zwin8') !== "$dev/pwm8" || $c('alose8') !== '' || $c('zwin9') !== "$dev/pwm9" || $c('acls9') !== "$class/hwmon2/pwm9") { + $failures[] = "A moved device binding must beat a blanked loser and a class path, got zwin8={$c('zwin8')} alose8={$c('alose8')} zwin9={$c('zwin9')} acls9={$c('acls9')}"; +} +if ($c('zzx') !== "$dev/pwm12" || $c('aay') !== "$class/hwmon2/pwm12") { + $failures[] = "A moved identity must claim its device ahead of a class path, got zzx={$c('zzx')} aay={$c('aay')}"; +} +$got = parse_ini_file("$legacy/fanctrlplus2_gone.cfg"); +if ($got['controller'] !== '' || $got['controller_identity'] !== "$class/hwmon7/pwm2") { + $failures[] = "A blank-form controller whose number is absent must stay as saved, got {$got['controller']} / {$got['controller_identity']}"; +} +foreach (['ided' => $classPwm3, 'blank' => $classPwm4] as $name => $want) { + $got = parse_ini_file("$legacy/fanctrlplus2_$name.cfg")['controller']; + if ($got !== $want) $failures[] = "A /sys/class controller saved as its identity ($name) must be pinned too, got $got"; +} +// A label already saved for the device wins over the class one. +if (file("$legacy/pwm_labels.cfg", FILE_IGNORE_NEW_LINES) !== ["$classPwm=Front intake", "$class/hwmon7/pwm2=Absent", "$classPwm3=Saved", "$classPwm4=Blank", "$class/hwmon2/pwm5=Mixed"]) { + $failures[] = "A label must follow its pinned class controller, unless the device has one; others kept. Got:\n" . file_get_contents("$legacy/pwm_labels.cfg"); } if ($cfgLegacy['cpu_sensor'] !== $temp) { $failures[] = 'A /sys/class CPU sensor must be pinned to the device holding that index, got ' . $cfgLegacy['cpu_sensor']; @@ -150,8 +221,13 @@ if ($cfgLegacy['aux_sensor'] !== "storcli:c0:roc,$temp,$class/hwmon7/temp1_input") { $failures[] = 'Aux hwmon sensors must be pinned, other sources and absent ones kept, got ' . $cfgLegacy['aux_sensor']; } -// A class controller older saves recorded as its own identity is kept too. -file_put_contents("$legacy/fanctrlplus2_ided.cfg", "custom=\"ided\"\ncontroller=\"$class/hwmon2/pwm2\"\ncontroller_identity=\"$class/hwmon2/pwm2\"\n"); +$got = parse_ini_file("$legacy/fanctrlplus2_twin.cfg")['controller']; +if ($got !== "$class/hwmon2/pwm2") { + $failures[] = "A /sys/class controller must not be pinned to a device another fan already drives, got $got"; +} +// Later runs: an absent number that appears on another chip is not resolved. +$chip('nct6775.656', 'hwmon7', 'nct6798'); +touch("$sys/devices/platform/nct6775.656/hwmon/hwmon7/temp1_input"); // NVMe hwmon sits directly under its class device, without a hwmon/ directory. $nvme = "$sys/devices/pci0000:00/0000:01:00.0/nvme/nvme0"; @mkdir("$nvme/hwmon4", 0777, true); @@ -160,13 +236,57 @@ // A device-path sensor follows its chip to a new hwmon number. file_put_contents("$legacy/fanctrlplus2_moved.cfg", "custom=\"moved\"\ncpu_sensor=\"$sys/devices/platform/it87.2624/hwmon/hwmon9/temp1_input\"\n"); migrate_cfg_and_labels('fanctrlplus2', $legacy, "$class/hwmon*"); -if (parse_ini_file("$legacy/fanctrlplus2_ided.cfg")['controller'] !== "$class/hwmon2/pwm2") { - $failures[] = 'A /sys/class controller saved with itself as identity must be kept, not blanked or resolved.'; +$got = parse_ini_file("$legacy/fanctrlplus2_absent.cfg"); +if ($got['controller'] !== "$class/hwmon7/pwm2" || isset($got['controller_identity'])) { + $failures[] = "A /sys/class controller absent on the first run must be kept as saved, got {$got['controller']}"; +} +if (file("$legacy/pwm_labels.cfg", FILE_IGNORE_NEW_LINES)[1] !== "$class/hwmon7/pwm2=Absent") { + $failures[] = 'A label under an absent class controller must not move on later runs.'; } +if (parse_ini_file("$legacy/fanctrlplus2_old.cfg")['aux_sensor'] !== "storcli:c0:roc,$temp,$class/hwmon7/temp1_input") { + $failures[] = 'A /sys/class sensor absent on the first run must not be pinned later.'; +} +// A flag that cannot be written converts nothing, rather than converting again +// at a later start, when the numbers may name other chips. +$noflag = "$root/noflag"; +@mkdir($noflag); +symlink("$root/missing-dir/flag", "$noflag/.hwmon_numbers_converted"); +file_put_contents("$noflag/fanctrlplus2_x.cfg", "custom=\"x\"\ncontroller=\"$class/hwmon2/pwm2\"\n"); +migrate_cfg_and_labels('fanctrlplus2', $noflag, "$class/hwmon*"); +$got = parse_ini_file("$noflag/fanctrlplus2_x.cfg")['controller']; +if ($got !== "$class/hwmon2/pwm2") $failures[] = "Without a writable flag, nothing may be converted by number, got $got"; +// After the first run nothing is claimed ahead of migration: a config +// blanked on a device keeps resolving as before the update, even next to a +// class path naming that device, and a class path never resolves. +$later = "$root/later"; +@mkdir($later); +touch("$later/.hwmon_numbers_converted"); +file_put_contents("$later/fanctrlplus2_acls.cfg", "custom=\"acls\"\ncontroller=\"$class/hwmon2/pwm9\"\n"); +file_put_contents("$later/fanctrlplus2_bblank.cfg", "custom=\"bblank\"\ncontroller=\"\"\ncontroller_identity=\"$sys/devices/platform/it87.2624/hwmon/hwmon9/pwm9\"\n"); +migrate_cfg_and_labels('fanctrlplus2', $later, "$class/hwmon*"); +$got = parse_ini_file("$later/fanctrlplus2_bblank.cfg")['controller']; +if ($got !== "$dev/pwm9") $failures[] = "After the first run, a blanked device binding must resolve as before the update, got $got"; + +// A config blanked on a device or serial beats one blanked on a class path. +$tier = "$root/tier"; +@mkdir($tier); +file_put_contents("$tier/fanctrlplus2_acls.cfg", "custom=\"acls\"\ncontroller=\"\"\ncontroller_identity=\"$class/hwmon2/pwm9\"\n"); +file_put_contents("$tier/fanctrlplus2_bdev.cfg", "custom=\"bdev\"\ncontroller=\"\"\ncontroller_identity=\"$sys/devices/platform/it87.2624/hwmon/hwmon9/pwm9\"\n"); +migrate_cfg_and_labels('fanctrlplus2', $tier, "$class/hwmon*"); +$a = parse_ini_file("$tier/fanctrlplus2_acls.cfg")['controller']; $b = parse_ini_file("$tier/fanctrlplus2_bdev.cfg")['controller']; +if ($b !== "$dev/pwm9" || $a !== '') $failures[] = "A blanked device binding must beat a blanked class path, got bdev=$b acls=$a"; + +// Anything at the flag path, even a directory, means the conversion ran. +$dirflag = "$root/dirflag"; +@mkdir("$dirflag/.hwmon_numbers_converted", 0777, true); +file_put_contents("$dirflag/fanctrlplus2_x.cfg", "custom=\"x\"\ncontroller=\"$class/hwmon2/pwm2\"\n"); +migrate_cfg_and_labels('fanctrlplus2', $dirflag, "$class/hwmon*"); +$got = parse_ini_file("$dirflag/fanctrlplus2_x.cfg")['controller']; +if ($got !== "$class/hwmon2/pwm2") $failures[] = "A directory at the flag path must count as converted, got $got"; if (parse_ini_file("$legacy/fanctrlplus2_nvme.cfg")['aux_sensor'] !== "$nvme/hwmon4/temp1_input,,storcli:c0:roc") { $failures[] = 'An NVMe sensor must follow its hwmon number, keeping the other entries as saved.'; } -if (strpos(file_get_contents("$legacy/fanctrlplus2_moved.cfg"), 'controller') !== false) { +if (preg_match('/^controller/m', file_get_contents("$legacy/fanctrlplus2_moved.cfg"))) { $failures[] = 'Migration must not add controller keys a configuration does not have.'; } if (parse_ini_file("$legacy/fanctrlplus2_moved.cfg")['cpu_sensor'] !== $temp) { diff --git a/tests/usb_identity_test.php b/tests/usb_identity_test.php index f48061c..3ec74ac 100644 --- a/tests/usb_identity_test.php +++ b/tests/usb_identity_test.php @@ -138,6 +138,31 @@ $check(($map[$keyA] ?? null) === $nonHid[0] && ($map[$keyB] ?? null) === $nonHid[1], 'Different VID:PID pairs must not become an ambiguous serial.'); } +// First run after the update: a fan saved by its USB serial, whose HID +// instance changed, keeps the hub over a loser 2026.11.12 blanked on the +// hub's hwmon number, whatever the file order. +$hub = $device('9-9', 'hwmon77', 'HUBSERIAL'); +$stale = str_replace('F001.0008', 'F001.0003', $hub); +$hubCfg = "$root/hubcfg"; +mkdir($hubCfg); +file_put_contents("$hubCfg/fanctrlplus2_alose.cfg", "custom=\"alose\"\ncontroller=\"\"\ncontroller_identity=\"$hwmon/hwmon77/pwm10\"\n"); +file_put_contents("$hubCfg/fanctrlplus2_zwin.cfg", "custom=\"zwin\"\ncontroller=\"$stale\"\ncontroller_identity=\"".fcp_pwm_identity($hub)."\"\n"); +migrate_cfg_and_labels('fanctrlplus2', $hubCfg, "$hwmon/hwmon*"); +$check(parse_ini_file("$hubCfg/fanctrlplus2_zwin.cfg")['controller'] === $hub && parse_ini_file("$hubCfg/fanctrlplus2_alose.cfg")['controller'] === '', + 'A fan saved by USB serial must keep its hub over a blanked loser naming its hwmon number.'); + +// Devices no class path names migrate as before the update: an owner blanked +// while its serial was ambiguous, sorting first, recovers its hub from a loser +// still holding the hub's stale path. +$own = $device('9-8', 'hwmon78', 'OWNSERIAL'); +$ownCfg = "$root/owncfg"; +mkdir($ownCfg); +$ownId = fcp_pwm_identity($own); +file_put_contents("$ownCfg/fanctrlplus2_aown.cfg", "custom=\"aown\"\ncontroller=\"\"\ncontroller_identity=\"$ownId\"\n"); +file_put_contents("$ownCfg/fanctrlplus2_zlose.cfg", "custom=\"zlose\"\ncontroller=\"".str_replace('F001.0008', 'F001.0003', $own)."\"\ncontroller_identity=\"$ownId\"\n"); +migrate_cfg_and_labels('fanctrlplus2', $ownCfg, "$hwmon/hwmon*"); +$check(parse_ini_file("$ownCfg/fanctrlplus2_aown.cfg")['controller'] === $own, 'An owner blanked by an ambiguous serial must recover its hub as before the update.'); + exec('rm -rf '.escapeshellarg($root)); if ($failures) { fwrite(STDERR, implode("\n", $failures)."\n"); exit(1); } echo "USB serial identity tests passed\n";