From f4b0ff77ac552dd6f481b8f30f81e31d97023faf Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 19 Nov 2026 13:21:32 +0000 Subject: [PATCH 1/9] Convert controllers saved by hwmon number once to the chip holding it Stable releases resolved a /sys/class/hwmon/hwmonN controller to the chip holding that number at each start. #43 stopped driving such controllers until the user picked them again. Pin them instead, once, to the device behind hwmonN when fan control starts after every plugin has installed, as CPU and auxiliary sensors already are. A number that is absent, or a device another fan already drives, keeps the saved path and stays flagged. --- README.md | 14 +++++------ .../plugins/fanctrlplus2/include/Common.php | 23 +++++++++-------- tests/pwm_path_migration_test.php | 25 +++++++++++++------ 3 files changed, 37 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 477f747..b106489 100644 --- a/README.md +++ b/README.md @@ -206,13 +206,13 @@ 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. +Controllers and sensors saved in the older `/sys/class/hwmon/hwmonN` form +name a number rather than a chip. They are converted once, when fan control +starts after every plugin has installed, to the chip holding that number then. +A controller whose number is absent then is not driven: select that fan's +controller again once. 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..a1aff21 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php @@ -90,17 +90,18 @@ 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. Migration pins +// it, once, to the chip holding that number (fcp_pin_sensor) after every +// plugin has installed, the chip stable releases resolved it to; an absent +// number stays saved until the user picks the controller again (#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 +// A hwmon controller or 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; @@ -270,14 +271,16 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw } } // 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. +// exists, including 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 { $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); + if (strpos($source, 'usb:') === 0) $new_path = $identities[$source] ?? null; + elseif (fcp_class_hwmon_path($source)) $new_path = is_file($source) ? fcp_pin_sensor($source) : null; + else $new_path = find_moved_pwm_path($source); 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/tests/pwm_path_migration_test.php b/tests/pwm_path_migration_test.php index daed7f1..4f95845 100644 --- a/tests/pwm_path_migration_test.php +++ b/tests/pwm_path_migration_test.php @@ -126,11 +126,12 @@ $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. Controllers and +// sensors are pinned to the device holding it; an absent index is kept. $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); file_put_contents("$legacy/fanctrlplus2_old.cfg", implode("\n", [ 'custom="old"', @@ -141,8 +142,8 @@ ]) . "\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']; } 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 +151,12 @@ 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"); +// A class controller whose index is absent is kept, also when older saves +// recorded it as its own identity, and one whose device another fan already +// drives is kept rather than doubled. +file_put_contents("$legacy/fanctrlplus2_ided.cfg", "custom=\"ided\"\ncontroller=\"$class/hwmon7/pwm2\"\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"); // 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,8 +165,12 @@ // 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.'; +if (parse_ini_file("$legacy/fanctrlplus2_ided.cfg")['controller'] !== "$class/hwmon7/pwm2" + || parse_ini_file("$legacy/fanctrlplus2_absent.cfg")['controller'] !== "$class/hwmon7/pwm2") { + $failures[] = 'A /sys/class controller whose index is absent must be kept, not blanked or moved.'; +} +if (parse_ini_file("$legacy/fanctrlplus2_twin.cfg")['controller'] !== "$class/hwmon2/pwm2") { + $failures[] = 'A /sys/class controller must not be pinned to a device another fan already drives.'; } 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.'; From c4c9f20aec89874f29da3f0a0e97f648b010d0c7 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 19 Nov 2026 13:28:57 +0000 Subject: [PATCH 2/9] Convert a class controller only on its first migration, with its label Migration reruns at every start, so an absent hwmon number could be pinned later to whichever chip then took it. Resolve a /sys/class controller only while it has no identity. One that is absent, or loses a conflict, is saved as its own identity, so it is never resolved by number again. A label saved under a converted controller moves with it. --- .../plugins/fanctrlplus2/include/Common.php | 45 ++++++++++++++----- tests/pwm_path_migration_test.php | 20 +++++++-- 2 files changed, 52 insertions(+), 13 deletions(-) diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php index a1aff21..fba81de 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php @@ -91,9 +91,10 @@ 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: it is never driven or followed to another number. Migration pins -// it, once, to the chip holding that number (fcp_pin_sensor) after every -// plugin has installed, the chip stable releases resolved it to; an absent -// number stays saved until the user picks the controller again (#41). +// a controller saved that way to the chip holding that number on its first run +// after every plugin has installed, the chip stable releases resolved it to. +// A number absent then stays saved until the user picks the controller again, +// and 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; } @@ -101,8 +102,9 @@ function fcp_class_hwmon_path(string $path): bool { // A hwmon controller or 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. +// 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; return str_starts_with($path, '/') ? (find_moved_pwm_path($path) ?? $path) : $path; @@ -236,7 +238,7 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw // 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 = []; + $configs = []; $in_use = []; $pinned = []; foreach ($cfgfiles as $cfgfile) { $ini = @parse_ini_file($cfgfile); if (!$ini) continue; @@ -256,6 +258,8 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw if ($ini[$k] !== $original[$k]) log_migrate("migrate cfg: $cfgfile $k: {$original[$k]} → {$ini[$k]}"); } fcp_migrate_controller($ini, $cfgfile, $in_use, $identities); + $was = (string)($original['controller'] ?? ''); + if (fcp_class_hwmon_path($was) && !fcp_class_hwmon_path((string)$ini['controller'])) $pinned[$was] = $ini['controller_identity']; 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. @@ -269,18 +273,39 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw } safe_rewrite($cfgfile, $buf); } + + // A label saved under a class controller follows it, only when that + // controller was pinned; a label already saved for the device wins. + if ($pinned && is_file($labelFile)) { + $lines = file($labelFile, FILE_IGNORE_NEW_LINES) ?: []; + $keys = array_map(fn($l) => trim(explode('=', $l, 2)[0], " \t\n\r\0\x0B\"'"), $lines); + $out = []; + foreach ($lines as $i => $line) { + $to = $pinned[$keys[$i]] ?? null; + if ($to === null) { $out[] = $line; continue; } + if (in_array($to, $keys, true)) continue; + $out[] = $to.'='.explode('=', $line, 2)[1]; + log_migrate("migrate label: {$keys[$i]} → $to"); + } + if ($out !== $lines) safe_rewrite($labelFile, implode("\n", $out)); + } } // Resolves one configuration's controller in $ini. A saved path that no longer -// exists, including a /sys/class one, is kept for the settings page to show, -// until the user picks another controller; nothing is moved to another device. +// exists, or a /sys/class one that cannot be pinned (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): 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; if (strpos($source, 'usb:') === 0) $new_path = $identities[$source] ?? null; - elseif (fcp_class_hwmon_path($source)) $new_path = is_file($source) ? fcp_pin_sensor($source) : null; - else $new_path = find_moved_pwm_path($source); + elseif (fcp_class_hwmon_path($source)) { + // Only on the first run: a class path saved as its own identity was + // absent or lost a conflict then, and is never resolved by number again. + $new_path = $identity === '' ? (realpath($source) ?: null) : null; + if ($new_path === null) $ini['controller_identity'] = $identity = $source; + } else $new_path = find_moved_pwm_path($source); 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/tests/pwm_path_migration_test.php b/tests/pwm_path_migration_test.php index 4f95845..c46d74a 100644 --- a/tests/pwm_path_migration_test.php +++ b/tests/pwm_path_migration_test.php @@ -133,6 +133,7 @@ $temp = "$sys/devices/platform/it87.2624/hwmon/hwmon2/temp1_input"; $classPwm = "$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm2"; touch($temp); +file_put_contents("$legacy/pwm_labels.cfg", "$class/hwmon2/pwm2=Front intake\n$class/hwmon7/pwm2=Absent\n"); file_put_contents("$legacy/fanctrlplus2_old.cfg", implode("\n", [ 'custom="old"', "controller=\"$class/hwmon2/pwm2\"", @@ -145,6 +146,9 @@ if ($cfgLegacy['controller'] !== $classPwm) { $failures[] = 'A /sys/class controller must be pinned to the device holding that index, got ' . $cfgLegacy['controller']; } +if (file("$legacy/pwm_labels.cfg", FILE_IGNORE_NEW_LINES) !== ["$classPwm=Front intake", "$class/hwmon7/pwm2=Absent"]) { + $failures[] = "A label must follow its pinned class controller, 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']; } @@ -165,9 +169,19 @@ // 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/hwmon7/pwm2" - || parse_ini_file("$legacy/fanctrlplus2_absent.cfg")['controller'] !== "$class/hwmon7/pwm2") { - $failures[] = 'A /sys/class controller whose index is absent must be kept, not blanked or moved.'; +foreach (['ided', 'absent'] as $name) { + $got = parse_ini_file("$legacy/fanctrlplus2_$name.cfg"); + if ($got['controller'] !== "$class/hwmon7/pwm2" || $got['controller_identity'] !== "$class/hwmon7/pwm2") { + $failures[] = "A /sys/class controller whose index is absent must be kept and marked ($name), got {$got['controller']} / " . ($got['controller_identity'] ?? ''); + } +} +// A number absent on the first run is never resolved later, when another chip +// may hold it. +$chip('nct6775.656', 'hwmon7', 'nct6798'); +migrate_cfg_and_labels('fanctrlplus2', $legacy, "$class/hwmon*"); +$got = parse_ini_file("$legacy/fanctrlplus2_absent.cfg")['controller']; +if ($got !== "$class/hwmon7/pwm2") { + $failures[] = "A /sys/class controller absent on its first run must not be pinned when the number appears later, got $got"; } if (parse_ini_file("$legacy/fanctrlplus2_twin.cfg")['controller'] !== "$class/hwmon2/pwm2") { $failures[] = 'A /sys/class controller must not be pinned to a device another fan already drives.'; From 8dc6fcf9f71b6d4be3edf63f9f006293efc28113 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 19 Nov 2026 13:38:12 +0000 Subject: [PATCH 3/9] Convert hwmon-number bindings on the first migration after the update only A per-config marker could not tell a first run from a class controller 2026.11.12 saved as its own identity, and sensors had no marker at all. Record the first migration with a flag file in the plugin's config directory instead: only that run pins class controllers and sensors to the chip holding the number. The label move now runs inside the existing label pass, after the configurations. --- README.md | 12 +- .../plugins/fanctrlplus2/include/Common.php | 147 ++++++++---------- tests/pwm_path_migration_test.php | 61 ++++---- 3 files changed, 109 insertions(+), 111 deletions(-) diff --git a/README.md b/README.md index b106489..258b827 100644 --- a/README.md +++ b/README.md @@ -208,11 +208,13 @@ 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. CPU and auxiliary hwmon sensors are saved by their device as well. -Controllers and sensors saved in the older `/sys/class/hwmon/hwmonN` form -name a number rather than a chip. They are converted once, when fan control -starts after every plugin has installed, to the chip holding that number then. -A controller whose number is absent then is not driven: select that fan's -controller again once. +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 fba81de..68a71ae 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php @@ -90,23 +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: it is never driven or followed to another number. Migration pins -// a controller saved that way to the chip holding that number on its first run -// after every plugin has installed, the chip stable releases resolved it to. -// A number absent then stays saved until the user picks the controller again, -// and is never resolved later, when another chip may hold it (#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 controller or 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 (fcp_class_hwmon_path($path)) return $by_number ? (realpath($path) ?: $path) : $path; return str_starts_with($path, '/') ? (find_moved_pwm_path($path) ?? $path) : $path; } @@ -184,6 +187,48 @@ 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; + $by_number = !is_file($flag); + + // --- 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'] ?? ''); + $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(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 = (string)($original['controller'] ?? ''); + if (fcp_class_hwmon_path($was) && !fcp_class_hwmon_path((string)$ini['controller'])) $pinned[$was] = $ini['controller']; + 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)) { @@ -205,7 +250,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; @@ -233,79 +279,20 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw } if ($changed) safe_rewrite($labelFile, implode("\n", $out)); } - - // --- 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'] ?? ''); - $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); - $was = (string)($original['controller'] ?? ''); - if (fcp_class_hwmon_path($was) && !fcp_class_hwmon_path((string)$ini['controller'])) $pinned[$was] = $ini['controller_identity']; - 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); - } - - // A label saved under a class controller follows it, only when that - // controller was pinned; a label already saved for the device wins. - if ($pinned && is_file($labelFile)) { - $lines = file($labelFile, FILE_IGNORE_NEW_LINES) ?: []; - $keys = array_map(fn($l) => trim(explode('=', $l, 2)[0], " \t\n\r\0\x0B\"'"), $lines); - $out = []; - foreach ($lines as $i => $line) { - $to = $pinned[$keys[$i]] ?? null; - if ($to === null) { $out[] = $line; continue; } - if (in_array($to, $keys, true)) continue; - $out[] = $to.'='.explode('=', $line, 2)[1]; - log_migrate("migrate label: {$keys[$i]} → $to"); - } - if ($out !== $lines) safe_rewrite($labelFile, implode("\n", $out)); - } + if ($by_number) @touch($flag); } // Resolves one configuration's controller in $ini. A saved path that no longer -// exists, or a /sys/class one that cannot be pinned (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): 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 = false): 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; if (strpos($source, 'usb:') === 0) $new_path = $identities[$source] ?? null; - elseif (fcp_class_hwmon_path($source)) { - // Only on the first run: a class path saved as its own identity was - // absent or lost a conflict then, and is never resolved by number again. - $new_path = $identity === '' ? (realpath($source) ?: null) : null; - if ($new_path === null) $ini['controller_identity'] = $identity = $source; - } else $new_path = find_moved_pwm_path($source); + elseif (fcp_class_hwmon_path($source)) $new_path = $by_number ? (realpath($source) ?: null) : null; + else $new_path = find_moved_pwm_path($source); 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/tests/pwm_path_migration_test.php b/tests/pwm_path_migration_test.php index c46d74a..4761b59 100644 --- a/tests/pwm_path_migration_test.php +++ b/tests/pwm_path_migration_test.php @@ -126,14 +126,17 @@ $failures[] = 'Once its chip loads, the fan must bind to it again.'; } -// Legacy /sys/class bindings name an index, not a device. Controllers and -// sensors are pinned to the device holding it; an absent index is kept. +// 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); -file_put_contents("$legacy/pwm_labels.cfg", "$class/hwmon2/pwm2=Front intake\n$class/hwmon7/pwm2=Absent\n"); +touch("$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm3"); +$classPwm3 = "$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm3"; +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"); file_put_contents("$legacy/fanctrlplus2_old.cfg", implode("\n", [ 'custom="old"', "controller=\"$class/hwmon2/pwm2\"", @@ -141,13 +144,25 @@ "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"); +touch("$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm4"); +file_put_contents("$legacy/fanctrlplus2_blank.cfg", "custom=\"blank\"\ncontroller=\"\"\ncontroller_identity=\"$class/hwmon2/pwm4\"\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'] !== $classPwm) { $failures[] = 'A /sys/class controller must be pinned to the device holding that index, got ' . $cfgLegacy['controller']; } -if (file("$legacy/pwm_labels.cfg", FILE_IGNORE_NEW_LINES) !== ["$classPwm=Front intake", "$class/hwmon7/pwm2=Absent"]) { - $failures[] = "A label must follow its pinned class controller, others kept. Got:\n" . file_get_contents("$legacy/pwm_labels.cfg"); +foreach (['ided' => $classPwm3, 'blank' => "$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm4"] 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"]) { + $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']; @@ -155,12 +170,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 whose index is absent is kept, also when older saves -// recorded it as its own identity, and one whose device another fan already -// drives is kept rather than doubled. -file_put_contents("$legacy/fanctrlplus2_ided.cfg", "custom=\"ided\"\ncontroller=\"$class/hwmon7/pwm2\"\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"); +$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); @@ -169,22 +185,15 @@ // 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*"); -foreach (['ided', 'absent'] as $name) { - $got = parse_ini_file("$legacy/fanctrlplus2_$name.cfg"); - if ($got['controller'] !== "$class/hwmon7/pwm2" || $got['controller_identity'] !== "$class/hwmon7/pwm2") { - $failures[] = "A /sys/class controller whose index is absent must be kept and marked ($name), got {$got['controller']} / " . ($got['controller_identity'] ?? ''); - } -} -// A number absent on the first run is never resolved later, when another chip -// may hold it. -$chip('nct6775.656', 'hwmon7', 'nct6798'); -migrate_cfg_and_labels('fanctrlplus2', $legacy, "$class/hwmon*"); -$got = parse_ini_file("$legacy/fanctrlplus2_absent.cfg")['controller']; -if ($got !== "$class/hwmon7/pwm2") { - $failures[] = "A /sys/class controller absent on its first run must not be pinned when the number appears later, got $got"; +$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_twin.cfg")['controller'] !== "$class/hwmon2/pwm2") { - $failures[] = 'A /sys/class controller must not be pinned to a device another fan already drives.'; +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.'; } 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.'; From f966c0ef7c91b123785f4d9b1bc93c62f0e66922 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 19 Nov 2026 13:44:43 +0000 Subject: [PATCH 4/9] Never read a sensor by hwmon number; move a blank-form controller's label A sensor kept in the /sys/class/hwmon form, because its number was absent on the first migration, is now reported unread instead of read from whatever chip holds that number. The first run also registers class controllers by the device they name before resolving, so a conflict loser 2026.11.12 blanked cannot take the device. A controller pinned from its identity alone moves its label too, and a failed flag write is logged. --- README.md | 2 +- .../plugins/fanctrlplus2/include/Common.php | 16 +++++++++++----- .../plugins/fanctrlplus2/scripts/aux_sensors.sh | 4 +++- .../fanctrlplus2/scripts/fanctrlplus2_loop.sh | 3 ++- .../scripts/fanctrlplus2_refresh_single.sh | 3 ++- tests/aux_sensor_read_test.sh | 6 ++++++ tests/pwm_path_migration_test.php | 16 +++++++++++----- 7 files changed, 36 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 258b827..c17ed31 100644 --- a/README.md +++ b/README.md @@ -208,7 +208,7 @@ 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. CPU and auxiliary hwmon sensors are saved by their device as well. -Controllers, their labels and sensors saved in the older +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 diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php index 68a71ae..04fc5d1 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php @@ -109,7 +109,7 @@ function fcp_class_hwmon_path(string $path): bool { // (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 (fcp_class_hwmon_path($path)) return $by_number ? (realpath($path) ?: $path) : $path; + if ($by_number && fcp_class_hwmon_path($path)) return realpath($path) ?: $path; return str_starts_with($path, '/') ? (find_moved_pwm_path($path) ?? $path) : $path; } @@ -203,6 +203,12 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw $identity = (string)($ini['controller_identity'] ?? ''); $source = $identity !== '' ? $identity : $path; $resolved = strpos($source, 'usb:') === 0 ? ($identities[$source] ?? null) : (is_file($source) ? $source : null); + // The first run pins a class controller: it holds the device it names, + // before a blanked loser's identity can claim it. + if ($by_number && fcp_class_hwmon_path($path) && is_file($path) && !isset($in_use[realpath($path)])) { + $in_use[realpath($path)] = $cfgfile; + continue; + } // Already-current manual assignments win over stale migrated ones. if ($resolved === $path && $path !== '' && !isset($in_use[$path])) $in_use[$path] = $cfgfile; } @@ -214,7 +220,7 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw 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 = (string)($original['controller'] ?? ''); + $was = (string)(($original['controller'] ?? '') ?: ($original['controller_identity'] ?? '')); if (fcp_class_hwmon_path($was) && !fcp_class_hwmon_path((string)$ini['controller'])) $pinned[$was] = $ini['controller']; if ($ini === $original) continue; // Only these fields change. Re-serializing the entire INI would @@ -279,19 +285,19 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw } if ($changed) safe_rewrite($labelFile, implode("\n", $out)); } - if ($by_number) @touch($flag); + if ($by_number && !@touch($flag)) log_migrate("migrate: cannot write $flag; hwmon numbers will be converted again next start"); } // Resolves one configuration's controller in $ini. A saved path that no longer // 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 = false): void { +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; if (strpos($source, 'usb:') === 0) $new_path = $identities[$source] ?? null; - elseif (fcp_class_hwmon_path($source)) $new_path = $by_number ? (realpath($source) ?: null) : null; + elseif ($by_number && fcp_class_hwmon_path($source)) $new_path = realpath($source) ?: null; else $new_path = find_moved_pwm_path($source); 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"); 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/pwm_path_migration_test.php b/tests/pwm_path_migration_test.php index 4761b59..25e8b0e 100644 --- a/tests/pwm_path_migration_test.php +++ b/tests/pwm_path_migration_test.php @@ -134,9 +134,11 @@ $temp = "$sys/devices/platform/it87.2624/hwmon/hwmon2/temp1_input"; $classPwm = "$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm2"; touch($temp); -touch("$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm3"); $classPwm3 = "$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm3"; -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"); +$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"); file_put_contents("$legacy/fanctrlplus2_old.cfg", implode("\n", [ 'custom="old"', "controller=\"$class/hwmon2/pwm2\"", @@ -147,8 +149,10 @@ // 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"); -touch("$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm4"); 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_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*"); @@ -156,12 +160,14 @@ if ($cfgLegacy['controller'] !== $classPwm) { $failures[] = 'A /sys/class controller must be pinned to the device holding that index, got ' . $cfgLegacy['controller']; } -foreach (['ided' => $classPwm3, 'blank' => "$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm4"] as $name => $want) { +$got = parse_ini_file("$legacy/fanctrlplus2_aloser.cfg")['controller']; +if ($got !== '') $failures[] = "A blanked conflict loser must not take the device from the fan saved on it, got $got"; +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"]) { +if (file("$legacy/pwm_labels.cfg", FILE_IGNORE_NEW_LINES) !== ["$classPwm=Front intake", "$class/hwmon7/pwm2=Absent", "$classPwm3=Saved", "$classPwm4=Blank"]) { $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) { From f3ae666465f0c810fcaa0971f14206856775d64d Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 19 Nov 2026 14:00:22 +0000 Subject: [PATCH 5/9] Let current device claims win first; never pin a blank label key The first run's class claims now come after current device-path claims, and use the source migration resolves (the identity first), so a stale class path cannot take a device from the fan that holds it. A blank-form config that stays blank no longer records an empty pin that rewrote its label as '=Label'. The flag is written before converting, so a flag that cannot be written converts nothing. Sensors saved by hwmon number now ask to be selected again on the settings page. --- .../plugins/fanctrlplus2/include/Common.php | 23 ++++++----- .../fanctrlplus2/include/FanBlockRender.php | 11 ++++- tests/curve_manual_test.sh | 11 +++++ tests/fan_response_loop_test.sh | 6 +++ tests/missing_binding_ui_test.php | 3 ++ tests/pwm_path_migration_test.php | 40 ++++++++++++++++++- 6 files changed, 81 insertions(+), 13 deletions(-) diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php index 04fc5d1..58ecb80 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php @@ -188,7 +188,10 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw $pwms = list_pwm($hwmon_glob); $identities = fcp_pwm_identity_map($pwms); $flag = "$cfgpath/".FCP_CLASS_FLAG; - $by_number = !is_file($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. + $by_number = !is_file($flag) && @touch($flag); + if (!$by_number && !is_file($flag)) log_migrate("migrate: cannot write $flag; hwmon numbers are not converted"); // --- cfgs --- // A fan assigned to the current path while an old one still pointed at @@ -203,15 +206,17 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw $identity = (string)($ini['controller_identity'] ?? ''); $source = $identity !== '' ? $identity : $path; $resolved = strpos($source, 'usb:') === 0 ? ($identities[$source] ?? null) : (is_file($source) ? $source : null); - // The first run pins a class controller: it holds the device it names, - // before a blanked loser's identity can claim it. - if ($by_number && fcp_class_hwmon_path($path) && is_file($path) && !isset($in_use[realpath($path)])) { - $in_use[realpath($path)] = $cfgfile; - continue; - } // 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 fan still saved on a class path holds the + // device that path names, before a loser 2026.11.12 blanked can claim it. + if ($by_number) foreach ($configs as $cfgfile => $ini) { + $path = (string)($ini['controller'] ?? ''); + $source = (string)($ini['controller_identity'] ?? '') ?: $path; + if ($path === '' || !fcp_class_hwmon_path($source) || !is_file($source)) continue; + $in_use[realpath($source)] ??= $cfgfile; + } foreach ($configs as $cfgfile => $ini) { $original = $ini; foreach (['cpu_sensor', 'aux_sensor'] as $k) { @@ -221,7 +226,8 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw } fcp_migrate_controller($ini, $cfgfile, $in_use, $identities, $by_number); $was = (string)(($original['controller'] ?? '') ?: ($original['controller_identity'] ?? '')); - if (fcp_class_hwmon_path($was) && !fcp_class_hwmon_path((string)$ini['controller'])) $pinned[$was] = $ini['controller']; + $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. @@ -285,7 +291,6 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw } if ($changed) safe_rewrite($labelFile, implode("\n", $out)); } - if ($by_number && !@touch($flag)) log_migrate("migrate: cannot write $flag; hwmon numbers will be converted again next start"); } // Resolves one configuration's controller in $ini. A saved path that no longer // exists, or a /sys/class one not pinned on the first run ($by_number, 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/tests/curve_manual_test.sh b/tests/curve_manual_test.sh index dca9300..2d54ce6 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 0 +run_manual 60000 +[[ "$(cat "$tmp/pwm1")" != 255 ]] && read_by_number=no || read_by_number=yes +expect_equal no "$read_by_number" "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..24ee522 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") != 255 ]] || { echo 'The loop read a CPU sensor saved by its hwmon number.' >&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 25e8b0e..937ce92 100644 --- a/tests/pwm_path_migration_test.php +++ b/tests/pwm_path_migration_test.php @@ -153,6 +153,20 @@ // 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 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*"); @@ -160,8 +174,21 @@ if ($cfgLegacy['controller'] !== $classPwm) { $failures[] = 'A /sys/class controller must be pinned to the device holding that index, got ' . $cfgLegacy['controller']; } -$got = parse_ini_file("$legacy/fanctrlplus2_aloser.cfg")['controller']; -if ($got !== '') $failures[] = "A blanked conflict loser must not take the device from the fan saved on it, got $got"; +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')}"; +} +$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"; @@ -201,6 +228,15 @@ 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"; 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.'; } From efd1d78d2dccd4cf02315c66fd1c15549dcba8f4 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 19 Nov 2026 14:08:35 +0000 Subject: [PATCH 6/9] Let moved device bindings claim first; count any flag path as converted On the first run, fans whose saved device path or USB identity moved now claim their device before class paths do, and losers 2026.11.12 blanked claim nothing, so file order cannot hand a loser another fan's device. A label follows the class path migration resolved, the identity first. Anything at the flag path, even a directory, counts as converted. --- .../plugins/fanctrlplus2/include/Common.php | 29 +++++++++++++------ tests/curve_manual_test.sh | 6 ++-- tests/fan_response_loop_test.sh | 2 +- tests/pwm_path_migration_test.php | 21 ++++++++++++-- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php index 58ecb80..c72f190 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php @@ -190,8 +190,12 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw $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. - $by_number = !is_file($flag) && @touch($flag); - if (!$by_number && !is_file($flag)) log_migrate("migrate: cannot write $flag; hwmon numbers are not converted"); + // 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 @@ -209,13 +213,18 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw // 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 fan still saved on a class path holds the - // device that path names, before a loser 2026.11.12 blanked can claim it. - if ($by_number) foreach ($configs as $cfgfile => $ini) { + // Then, on the first run, fans still saved on a device claim it: first + // those whose saved device moved (it names a device), then those saved on + // a class path (it names a number). Losers 2026.11.12 blanked claim + // nothing here, so file order cannot hand them a fan's device. + if ($by_number) foreach ([false, true] as $by_class) foreach ($configs as $cfgfile => $ini) { $path = (string)($ini['controller'] ?? ''); - $source = (string)($ini['controller_identity'] ?? '') ?: $path; - if ($path === '' || !fcp_class_hwmon_path($source) || !is_file($source)) continue; - $in_use[realpath($source)] ??= $cfgfile; + $identity = (string)($ini['controller_identity'] ?? ''); + $source = $identity !== '' ? $identity : $path; + if ($path === '' || fcp_class_hwmon_path($source) !== $by_class) continue; + if ($by_class) $device = is_file($source) ? realpath($source) : null; + else $device = strpos($source, 'usb:') === 0 ? ($identities[$source] ?? null) : find_moved_pwm_path($source); + if ($device !== null) $in_use[$device] ??= $cfgfile; } foreach ($configs as $cfgfile => $ini) { $original = $ini; @@ -225,7 +234,9 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw 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 = (string)(($original['controller'] ?? '') ?: ($original['controller_identity'] ?? '')); + // The class path migration resolved: the identity first, as it does. + $was = (string)($original['controller_identity'] ?? ''); + if ($was === '') $was = (string)($original['controller'] ?? ''); $now = (string)($ini['controller'] ?? ''); if (fcp_class_hwmon_path($was) && $now !== '' && !fcp_class_hwmon_path($now)) $pinned[$was] = $now; if ($ini === $original) continue; diff --git a/tests/curve_manual_test.sh b/tests/curve_manual_test.sh index 2d54ce6..ea87434 100755 --- a/tests/curve_manual_test.sh +++ b/tests/curve_manual_test.sh @@ -364,10 +364,10 @@ 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 0 +fresh 100 run_manual 60000 -[[ "$(cat "$tmp/pwm1")" != 255 ]] && read_by_number=no || read_by_number=yes -expect_equal no "$read_by_number" "Run Now must not read a CPU sensor saved by its hwmon number." +# 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 diff --git a/tests/fan_response_loop_test.sh b/tests/fan_response_loop_test.sh index 24ee522..013dab6 100755 --- a/tests/fan_response_loop_test.sh +++ b/tests/fan_response_loop_test.sh @@ -94,5 +94,5 @@ 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") != 255 ]] || { echo 'The loop read a CPU sensor saved by its hwmon number.' >&2; exit 1; } +[[ $(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/pwm_path_migration_test.php b/tests/pwm_path_migration_test.php index 937ce92..fe05ffd 100644 --- a/tests/pwm_path_migration_test.php +++ b/tests/pwm_path_migration_test.php @@ -138,7 +138,7 @@ $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"); +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\"", @@ -165,6 +165,13 @@ // 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 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"); @@ -185,6 +192,9 @@ 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')}"; +} $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']}"; @@ -194,7 +204,7 @@ 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"]) { +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) { @@ -237,6 +247,13 @@ 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"; +// 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.'; } From 64ca7eae5717e7b31017e1c0a0d80dbc809f9624 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 19 Nov 2026 14:17:35 +0000 Subject: [PATCH 7/9] Claim moved device bindings on every run, through one shared resolver Fans whose saved device path or USB identity moved now claim their device before any blanked loser on every run, not only the first, so a renumbered chip cannot hand a loser another fan's device. The claim and the migration share one resolver (fcp_controller_source, fcp_controller_target), so they read the same trimmed binding, the identity first. --- .../plugins/fanctrlplus2/include/Common.php | 44 +++++++++++-------- tests/pwm_path_migration_test.php | 21 +++++++++ tests/usb_identity_test.php | 13 ++++++ 3 files changed, 60 insertions(+), 18 deletions(-) diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php index c72f190..6a3f3d5 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php @@ -213,17 +213,15 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw // 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, fans still saved on a device claim it: first - // those whose saved device moved (it names a device), then those saved on - // a class path (it names a number). Losers 2026.11.12 blanked claim - // nothing here, so file order cannot hand them a fan's device. - if ($by_number) foreach ([false, true] as $by_class) foreach ($configs as $cfgfile => $ini) { - $path = (string)($ini['controller'] ?? ''); - $identity = (string)($ini['controller_identity'] ?? ''); - $source = $identity !== '' ? $identity : $path; - if ($path === '' || fcp_class_hwmon_path($source) !== $by_class) continue; - if ($by_class) $device = is_file($source) ? realpath($source) : null; - else $device = strpos($source, 'usb:') === 0 ? ($identities[$source] ?? null) : find_moved_pwm_path($source); + // Then fans still saved on a device claim it: first those whose saved + // device moved (it names a device), then, on the first run, those saved on + // a class path (it names a number). Blanked losers claim nothing here, so + // file order cannot hand them a fan's device. + foreach ([false, true] as $by_class) foreach ($configs as $cfgfile => $ini) { + if ((string)($ini['controller'] ?? '') === '') continue; + $source = fcp_controller_source($ini); + if (fcp_class_hwmon_path($source) !== $by_class || !preg_match('#^usb:|/pwm\d+$#', $source)) continue; + $device = fcp_controller_target($source, $identities, $by_number); if ($device !== null) $in_use[$device] ??= $cfgfile; } foreach ($configs as $cfgfile => $ini) { @@ -234,9 +232,7 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw 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); - // The class path migration resolved: the identity first, as it does. - $was = (string)($original['controller_identity'] ?? ''); - if ($was === '') $was = (string)($original['controller'] ?? ''); + $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; @@ -303,6 +299,20 @@ 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\"'"); +} + +// 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 not pinned on the first run ($by_number, // fcp_class_hwmon_path), is kept for the settings page to show, until the user @@ -311,10 +321,8 @@ function fcp_migrate_controller(array &$ini, string $cfgfile, array &$in_use, ar $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; - if (strpos($source, 'usb:') === 0) $new_path = $identities[$source] ?? null; - elseif ($by_number && fcp_class_hwmon_path($source)) $new_path = realpath($source) ?: null; - else $new_path = 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/tests/pwm_path_migration_test.php b/tests/pwm_path_migration_test.php index fe05ffd..2633224 100644 --- a/tests/pwm_path_migration_test.php +++ b/tests/pwm_path_migration_test.php @@ -172,6 +172,11 @@ 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"); @@ -195,6 +200,9 @@ 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']}"; @@ -247,6 +255,19 @@ 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 too, a fan whose chip moved keeps it over a loser +// 2026.11.12 blanked on the same device, whatever the file order. +$later = "$root/later"; +@mkdir($later); +touch("$later/.hwmon_numbers_converted"); +@mkdir("$sys/devices/platform/it87.9999/hwmon/hwmon5", 0777, true); +touch("$sys/devices/platform/it87.9999/hwmon/hwmon5/pwm1"); +$oldDev = "$sys/devices/platform/it87.9999/hwmon/hwmon4/pwm1"; +file_put_contents("$later/fanctrlplus2_alose.cfg", "custom=\"alose\"\ncontroller=\"\"\ncontroller_identity=\"$oldDev\"\n"); +file_put_contents("$later/fanctrlplus2_zwin.cfg", "custom=\"zwin\"\ncontroller=\"$oldDev\"\n"); +migrate_cfg_and_labels('fanctrlplus2', $later, "$class/hwmon*"); +$w = parse_ini_file("$later/fanctrlplus2_zwin.cfg")['controller']; $l = parse_ini_file("$later/fanctrlplus2_alose.cfg")['controller']; +if ($w !== "$sys/devices/platform/it87.9999/hwmon/hwmon5/pwm1" || $l !== '') $failures[] = "A moved fan must keep its device over a blanked loser on later runs, got zwin=$w alose=$l"; // Anything at the flag path, even a directory, means the conversion ran. $dirflag = "$root/dirflag"; @mkdir("$dirflag/.hwmon_numbers_converted", 0777, true); diff --git a/tests/usb_identity_test.php b/tests/usb_identity_test.php index f48061c..1ebe498 100644 --- a/tests/usb_identity_test.php +++ b/tests/usb_identity_test.php @@ -138,6 +138,19 @@ $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.'); + exec('rm -rf '.escapeshellarg($root)); if ($failures) { fwrite(STDERR, implode("\n", $failures)."\n"); exit(1); } echo "USB serial identity tests passed\n"; From 1b628e525a34eb7db557ea081c9e881c417f14cb Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 19 Nov 2026 14:26:39 +0000 Subject: [PATCH 8/9] Claim only devices a class path names, on the first run only Running the moved-binding claim on every start changed how configurations not saved by hwmon number migrate: an owner blanked while its USB serial was ambiguous could no longer recover its device. The first run now claims only devices some /sys/class path names, so every other configuration migrates exactly as before the update. --- .../plugins/fanctrlplus2/include/Common.php | 33 ++++++++++++------- tests/pwm_path_migration_test.php | 13 -------- tests/usb_identity_test.php | 12 +++++++ 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php index 6a3f3d5..61327a6 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php @@ -207,22 +207,31 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw if (!$ini) continue; $configs[$cfgfile] = $ini; $path = (string)($ini['controller'] ?? ''); - $identity = (string)($ini['controller_identity'] ?? ''); - $source = $identity !== '' ? $identity : $path; + $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 fans still saved on a device claim it: first those whose saved - // device moved (it names a device), then, on the first run, those saved on - // a class path (it names a number). Blanked losers claim nothing here, so - // file order cannot hand them a fan's device. - foreach ([false, true] as $by_class) foreach ($configs as $cfgfile => $ini) { - if ((string)($ini['controller'] ?? '') === '') continue; - $source = fcp_controller_source($ini); - if (fcp_class_hwmon_path($source) !== $by_class || !preg_match('#^usb:|/pwm\d+$#', $source)) continue; - $device = fcp_controller_target($source, $identities, $by_number); - if ($device !== null) $in_use[$device] ??= $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. Blanked losers claim + // nothing, so file order cannot hand them a fan's device. Devices no class + // path names are left to the migration below, as on later runs. + if ($by_number) { + $target = []; + foreach ($configs as $cfgfile => $ini) { + $source = fcp_controller_source($ini); + if (preg_match('#^usb:|/pwm\d+$#', $source)) $target[$cfgfile] = fcp_controller_target($source, $identities, true); + } + $named = []; + foreach ($target as $cfgfile => $device) { + if ($device !== null && fcp_class_hwmon_path(fcp_controller_source($configs[$cfgfile]))) $named[$device] = true; + } + foreach ([false, true] as $by_class) foreach ($target as $cfgfile => $device) { + $ini = $configs[$cfgfile]; + if ($device === null || !isset($named[$device]) || (string)($ini['controller'] ?? '') === '') continue; + if (fcp_class_hwmon_path(fcp_controller_source($ini)) === $by_class) $in_use[$device] ??= $cfgfile; + } } foreach ($configs as $cfgfile => $ini) { $original = $ini; diff --git a/tests/pwm_path_migration_test.php b/tests/pwm_path_migration_test.php index 2633224..bb6e583 100644 --- a/tests/pwm_path_migration_test.php +++ b/tests/pwm_path_migration_test.php @@ -255,19 +255,6 @@ 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 too, a fan whose chip moved keeps it over a loser -// 2026.11.12 blanked on the same device, whatever the file order. -$later = "$root/later"; -@mkdir($later); -touch("$later/.hwmon_numbers_converted"); -@mkdir("$sys/devices/platform/it87.9999/hwmon/hwmon5", 0777, true); -touch("$sys/devices/platform/it87.9999/hwmon/hwmon5/pwm1"); -$oldDev = "$sys/devices/platform/it87.9999/hwmon/hwmon4/pwm1"; -file_put_contents("$later/fanctrlplus2_alose.cfg", "custom=\"alose\"\ncontroller=\"\"\ncontroller_identity=\"$oldDev\"\n"); -file_put_contents("$later/fanctrlplus2_zwin.cfg", "custom=\"zwin\"\ncontroller=\"$oldDev\"\n"); -migrate_cfg_and_labels('fanctrlplus2', $later, "$class/hwmon*"); -$w = parse_ini_file("$later/fanctrlplus2_zwin.cfg")['controller']; $l = parse_ini_file("$later/fanctrlplus2_alose.cfg")['controller']; -if ($w !== "$sys/devices/platform/it87.9999/hwmon/hwmon5/pwm1" || $l !== '') $failures[] = "A moved fan must keep its device over a blanked loser on later runs, got zwin=$w alose=$l"; // Anything at the flag path, even a directory, means the conversion ran. $dirflag = "$root/dirflag"; @mkdir("$dirflag/.hwmon_numbers_converted", 0777, true); diff --git a/tests/usb_identity_test.php b/tests/usb_identity_test.php index 1ebe498..3ec74ac 100644 --- a/tests/usb_identity_test.php +++ b/tests/usb_identity_test.php @@ -151,6 +151,18 @@ $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"; From 47c1f27ae128573224f92c6ee6ba73860766b76e Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 19 Nov 2026 14:47:13 +0000 Subject: [PATCH 9/9] Let blanked device bindings outrank blanked class paths on the first run A config blanked on a device or serial now claims a class-named device before one blanked on a class path, which never claims. Tests pin the first-run-only claim and the tier order. --- .../plugins/fanctrlplus2/include/Common.php | 27 ++++++++++--------- tests/pwm_path_migration_test.php | 23 +++++++++++++++- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php index 61327a6..dca6dab 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php @@ -214,23 +214,24 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw } // 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. Blanked losers claim - // nothing, so file order cannot hand them a fan's device. Devices no class - // path names are left to the migration below, as on later runs. + // 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 = []; + $target = []; $is_class = []; $named = []; foreach ($configs as $cfgfile => $ini) { $source = fcp_controller_source($ini); - if (preg_match('#^usb:|/pwm\d+$#', $source)) $target[$cfgfile] = fcp_controller_target($source, $identities, true); + 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; } - $named = []; - foreach ($target as $cfgfile => $device) { - if ($device !== null && fcp_class_hwmon_path(fcp_controller_source($configs[$cfgfile]))) $named[$device] = true; - } - foreach ([false, true] as $by_class) foreach ($target as $cfgfile => $device) { - $ini = $configs[$cfgfile]; - if ($device === null || !isset($named[$device]) || (string)($ini['controller'] ?? '') === '') continue; - if (fcp_class_hwmon_path(fcp_controller_source($ini)) === $by_class) $in_use[$device] ??= $cfgfile; + // 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) { diff --git a/tests/pwm_path_migration_test.php b/tests/pwm_path_migration_test.php index bb6e583..57b145e 100644 --- a/tests/pwm_path_migration_test.php +++ b/tests/pwm_path_migration_test.php @@ -255,6 +255,27 @@ 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); @@ -265,7 +286,7 @@ 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) {