From 1c6995b9a4584db9928bb58ef5bb109b22ff6c66 Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 19 Nov 2026 07:47:24 +0000 Subject: [PATCH 01/11] Keep a missing controller instead of moving the fan to another chip Configuration migration resolved a saved controller whose device was absent by chip name: through whatever chip now held the saved hwmon index, then by any chip with a channel of the same name. At boot, before a driver plugin has bound its chip, that could move a fan onto another chip's PWM output. Migration now follows only the same device: a /sys/devices path whose hwmon index changed, or a USB serial identity. A saved path that no longer exists is kept, including one an older version blanked, so the settings page can show it. The page lists a saved controller, CPU sensor or auxiliary sensor that is not present as missing and keeps it selected instead of swapping in the first entry or dropping it, and asks for confirmation before saving one. Saving an unchanged missing controller keeps its USB identity. --- README.md | 7 ++ .../plugins/fanctrlplus2/fanctrlplus2.page | 13 +++ .../plugins/fanctrlplus2/include/Common.php | 80 ++++--------------- .../fanctrlplus2/include/FanBlockRender.php | 14 ++++ .../include/update.fanctrlplus2.php | 4 +- tests/missing_binding_ui_test.php | 35 ++++++++ tests/pwm_path_migration_test.php | 37 ++++++++- tests/usb_identity_test.php | 2 +- 8 files changed, 125 insertions(+), 67 deletions(-) create mode 100644 tests/missing_binding_ui_test.php diff --git a/README.md b/README.md index 5168d70..4cef63d 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,13 @@ Restart the service after a driver reload or port move. If multiple configurations resolve to one channel, the already-current assignment wins; blocked bindings remain saved for recovery when the conflicting assignment 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. +The settings page shows a saved controller, CPU sensor, or auxiliary sensor +that is not present as "Missing" and keeps it selected; saving asks for +confirmation first, and the fan cannot use it until it is back. Saving a configuration without a resolved controller, including a rename, retains its binding. Selecting a different controller replaces the binding. Clearing a label also removes its dormant USB identity entry, so it cannot diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/fanctrlplus2.page b/src/usr/local/emhttp/plugins/fanctrlplus2/fanctrlplus2.page index 6ace881..8ce6137 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/fanctrlplus2.page +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/fanctrlplus2.page @@ -1232,6 +1232,19 @@ $(function() { return; } + // Saved controllers and sensors that are not present are kept until + // changed, but the fan cannot use them meanwhile. + const missing = []; + $('.fan-block').each(function () { + const name = $(this).find('.custom-name-input').val(); + $(this).find('select:enabled option[data-missing]:selected').each(function () { + missing.push(`Fan ${name}: ${this.dataset.missing} ${this.value}`); + }); + }); + if (missing.length && !confirm(`These are not present on this system:\n\n${missing.join('\n')}\n\nThe fan cannot use them until they are back. Save anyway?`)) { + return; + } + $('.interval-input').each(function(){ this.value = stripUnit(this.value); }); diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php index a1d0adb..9efd48a 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/Common.php @@ -66,58 +66,6 @@ function fcp_parse_history_line(string $line): ?array { // ============================= // Migrate hwmonX configuration and label paths. // ============================= -function normalize_chip_name(string $chip): string { - // Remove a trailing numeric suffix. - $chip = preg_replace('/\.\d+$/', '', $chip); - // Remove ISA address fragments such as -isa-XXXX. - $chip = preg_replace('/-isa-[0-9a-fA-Fx]+$/', '', $chip); - return $chip; -} - -function extract_chip_and_pwm_from_path(string $old_path): ?array { - $old_path = trim($old_path, " \t\n\r\0\x0B\"'"); - - // Extract pwmN from the path first. - preg_match_all('/pwm(\d+)/', $old_path, $pm); - if (empty($pm[1])) return null; - $pwmN = 'pwm' . end($pm[1]); - - // Prefer a platform node such as nct6775.672 when available. - if (preg_match('#/platform/([^/]+)/#', $old_path, $m)) { - $platform = $m[1]; - // Find the matching pwmN under platform/$platform/hwmon/*. - foreach (glob("/sys/devices/platform/$platform/hwmon/hwmon*") as $dir) { - if (is_file("$dir/$pwmN") && is_file("$dir/name")) { - $chip = normalize_chip_name(trim(@file_get_contents("$dir/name"))); - if ($chip !== '') { - return [$chip, $pwmN]; - } - } - } - } - - // Fall back to hwmonX under /sys/class/hwmon when no platform node exists. - preg_match_all('/hwmon(\d+)/', $old_path, $hm); - if (!empty($hm[1])) { - $hwmon = 'hwmon' . end($hm[1]); - $name_file = "/sys/class/hwmon/$hwmon/name"; - if (is_file($name_file)) { - $chip = normalize_chip_name(trim(@file_get_contents($name_file))); - if ($chip !== '') return [$chip, $pwmN]; - } - } - - // Last resort: scan every hwmon* directory. - foreach (glob("/sys/class/hwmon/hwmon*") as $dir) { - if (is_file("$dir/$pwmN") && is_file("$dir/name")) { - $chip = normalize_chip_name(trim(@file_get_contents("$dir/name"))); - if ($chip !== '') return [$chip, $pwmN]; - } - } - - return null; -} - // Find where a saved /sys/devices pwm path lives now. The hwmonN index follows // probe order and a USB HID device gets a new instance suffix // (0003:VID:PID.NNNN) each time it enumerates, so both are wildcarded; the @@ -138,13 +86,15 @@ function find_moved_pwm_path(string $old_path): ?string { return count($matches) === 1 ? $matches[0] : null; } -// Path-only entries cannot recover a serial after the device has moved. -// Keep the same-port migration for those legacy/no-serial entries. -function current_pwm_path(string $old_path, array $pwm_map): ?string { - $moved = find_moved_pwm_path($old_path); - if ($moved !== null || preg_match(FCP_HID_INSTANCE, $old_path) || preg_match('#/usb\d+/#', $old_path)) return $moved; - $pair = extract_chip_and_pwm_from_path($old_path); - return $pair ? ($pwm_map[$pair[0].':'.$pair[1]] ?? null) : null; +// Where a saved path-only binding lives now: only the same device, never +// another chip with the same channel name, which a fan must not be moved to +// behind the user's back (a chip whose driver has not loaded yet, #41). A +// legacy /sys/class path names an index, so it is pinned to the device that +// holds that index now. +function current_pwm_path(string $old_path): ?string { + if (strpos($old_path, '/class/hwmon/') === false) return find_moved_pwm_path($old_path); + $real = realpath($old_path); + return $real !== false && is_file($real) ? $real : null; } // A fan is a USB controller plus its interface/hwmon channel, not a USB port. @@ -220,8 +170,6 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw $cfgpath = $cfgpath ?: "/boot/config/plugins/$plugin"; $labelFile = "$cfgpath/pwm_labels.cfg"; $pwms = list_pwm($hwmon_glob); - $pwm_map = []; - foreach ($pwms as $pwm) $pwm_map[normalize_chip_name($pwm['chip']).':'.$pwm['name']] = $pwm['sensor']; $identities = fcp_pwm_identity_map($pwms); // --- labels --- @@ -244,7 +192,7 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw } $stable = strpos($old_path, 'usb:') === 0; - $new_path = $stable ? ($identities[$old_path] ?? null) : current_pwm_path($old_path, $pwm_map); + $new_path = $stable ? ($identities[$old_path] ?? null) : current_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; @@ -295,7 +243,7 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw $identity = (string)($ini['controller_identity'] ?? ''); if ($identity === '' && !preg_match('#/pwm\d+$#', $old_path)) continue; $source = $identity !== '' ? $identity : $old_path; - $new_path = strpos($source, 'usb:') === 0 ? ($identities[$source] ?? null) : current_pwm_path($source, $pwm_map); + $new_path = strpos($source, 'usb:') === 0 ? ($identities[$source] ?? null) : current_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)) continue; @@ -310,7 +258,11 @@ function migrate_cfg_and_labels(string $plugin, string $cfgpath = '', string $hw // Never drive a replacement at the old path. Keep the identity // so a disconnected/ambiguous controller can recover later. if ($identity === '') continue; - $ini['controller'] = ''; + // A saved path that no longer exists stays, so the settings page + // shows it as missing until the user picks another controller. + // One that exists now belongs to another fan or device: blank it. + $saved = $old_path !== '' ? $old_path : (strpos($identity, 'usb:') === 0 ? '' : $identity); + $ini['controller'] = $saved !== '' && !file_exists($saved) ? $saved : ''; } else { $in_use[$new_path] = $cfgfile; $ini['controller'] = $new_path; diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/FanBlockRender.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/FanBlockRender.php index ed8c907..a0a4c21 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/FanBlockRender.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/FanBlockRender.php @@ -212,6 +212,9 @@ class="custom-name-input" + + + @@ -299,6 +302,9 @@ class="interval-input fcp-interval-input" $label): ?> + + + @@ -337,6 +343,14 @@ class="interval-input fcp-interval-input" + + + + + + + + diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/update.fanctrlplus2.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/update.fanctrlplus2.php index 9b2b5d7..3fcd23a 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/update.fanctrlplus2.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/update.fanctrlplus2.php @@ -172,7 +172,9 @@ // Read before rename/overwrite: an empty runtime controller means the // saved binding is unresolved, not that the user selected another device. $previous = $plan['previous']; - $controller_identity = $controller === '' + // An unchanged controller keeps its identity: a missing one (kept by + // migration until the user picks another) cannot be identified again. + $controller_identity = $controller === '' || $controller === (string)($previous['controller'] ?? '') ? (string)($previous['controller_identity'] ?? $previous['controller'] ?? '') : (fcp_unique_pwm_identity($controller) ?? $controller); diff --git a/tests/missing_binding_ui_test.php b/tests/missing_binding_ui_test.php new file mode 100644 index 0000000..92490d0 --- /dev/null +++ b/tests/missing_binding_ui_test.php @@ -0,0 +1,35 @@ + 'nct6798', 'name' => 'pwm2', 'sensor' => '/sys/devices/platform/nct6775.656/hwmon/hwmon2/pwm2']]; +$cpu = ['/sys/class/hwmon/hwmon1/temp1_input' => 'coretemp - Package']; +$aux = ['hwmon' => [['path' => 'hwmon:present', 'label' => 'Present']]]; +$render = fn(array $cfg) => render_fan_block($cfg + ['custom' => 'Array', 'file' => 'fanctrlplus2_Array.cfg'], 0, $pwms, [], [], $cpu, $aux); + +$gone = '/sys/devices/platform/it87.2624/hwmon/hwmon2/pwm2'; +$html = $render([ + 'controller' => $gone, + 'cpu_enable' => '1', 'cpu_sensor' => '/sys/class/hwmon/hwmon9/temp1_input', + 'aux_enable' => '1', 'aux_sensor' => 'hwmon:present,storcli:c0:roc', +]); +foreach ([ + "value=\"$gone\" data-missing=\"PWM controller\" selected" => 'A missing controller must stay selected and be marked missing.', + 'value="/sys/class/hwmon/hwmon9/temp1_input" data-missing="CPU sensor" selected' => 'A missing CPU sensor must stay selected instead of the first one listed.', + 'value="storcli:c0:roc" data-missing="auxiliary sensor" selected' => 'A missing aux sensor must stay selected instead of being dropped.', +] as $needle => $message) { + if (strpos($html, $needle) === false) $failures[] = $message; +} +if (strpos($html, 'value="hwmon:present" data-missing') !== false) { + $failures[] = 'A present aux sensor must not be marked missing.'; +} + +$html = $render(['controller' => $pwms[0]['sensor'], 'cpu_sensor' => array_key_first($cpu)]); +if (strpos($html, 'data-missing') !== false) { + $failures[] = 'Present selections must not be marked missing.'; +} + +if ($failures) { fwrite(STDERR, implode("\n", $failures) . "\n"); exit(1); } +echo "missing binding UI tests passed\n"; diff --git a/tests/pwm_path_migration_test.php b/tests/pwm_path_migration_test.php index b55d477..e887f07 100644 --- a/tests/pwm_path_migration_test.php +++ b/tests/pwm_path_migration_test.php @@ -45,7 +45,7 @@ $make("$root/class/hwmon/hwmon3/pwm2"); if (find_moved_pwm_path("$root/class/hwmon/hwmon5/pwm2") !== null) { - $failures[] = 'A /sys/class path carries no device identity and must be left to the chip-name fallback.'; + $failures[] = 'A /sys/class path carries no device identity, so it cannot be followed to another index.'; } if (find_moved_pwm_path("$root/devices/platform/nct6775.*/hwmon/hwmon4/pwm2") !== null) { $failures[] = 'A saved path must not be used as a glob pattern.'; @@ -89,6 +89,41 @@ $failures[] = 'Exactly one of two stale fans on the same PWM must be migrated to its current path.'; } +// #41: a chip whose driver has not loaded yet. Another chip now holds its +// hwmon index and has a channel of the same name; the fan must not move there. +$sys = "$root/chips"; +$class = "$sys/class/hwmon"; +@mkdir($class, 0777, true); +$chip = function (string $device, string $index, string $name) use ($sys, $class): string { + $dir = "$sys/devices/platform/$device/hwmon/$index"; + @mkdir($dir, 0777, true); + file_put_contents("$dir/name", "$name\n"); + touch("$dir/pwm2"); + symlink($dir, "$class/$index"); + return "$dir/pwm2"; +}; +$other = $chip('nct6775.656', 'hwmon2', 'nct6798'); +$it87 = "$sys/devices/platform/it87.2624/hwmon/hwmon2/pwm2"; +$boot = "$root/boot"; +@mkdir($boot); +file_put_contents("$boot/fanctrlplus2_array.cfg", "custom=\"array\"\ncontroller=\"$it87\"\ncontroller_identity=\"$it87\"\n"); +// Configurations an older version blanked keep their binding in the identity. +file_put_contents("$boot/fanctrlplus2_blanked.cfg", "custom=\"blanked\"\ncontroller=\"\"\ncontroller_identity=\"$it87\"\n"); +migrate_cfg_and_labels('fanctrlplus2', $boot, "$class/hwmon*"); +foreach (['array', 'blanked'] as $name) { + if (parse_ini_file("$boot/fanctrlplus2_$name.cfg")['controller'] !== $it87) { + $failures[] = "A missing chip's fan ($name) must keep its saved controller, not move to another chip's channel."; + } +} +unlink("$class/hwmon2"); +rename("$sys/devices/platform/nct6775.656/hwmon/hwmon2", "$sys/devices/platform/nct6775.656/hwmon/hwmon3"); +symlink("$sys/devices/platform/nct6775.656/hwmon/hwmon3", "$class/hwmon3"); +$loaded = $chip('it87.2624', 'hwmon2', 'it8686'); +migrate_cfg_and_labels('fanctrlplus2', $boot, "$class/hwmon*"); +if (parse_ini_file("$boot/fanctrlplus2_array.cfg")['controller'] !== $loaded) { + $failures[] = 'Once its chip loads, the fan must bind to it again.'; +} + exec('rm -rf ' . escapeshellarg($root)); if ($failures) { diff --git a/tests/usb_identity_test.php b/tests/usb_identity_test.php index 333a5fe..f48061c 100644 --- a/tests/usb_identity_test.php +++ b/tests/usb_identity_test.php @@ -98,7 +98,7 @@ file_put_contents($serialFile, "controller=\"$root/gone/hwmon1/pwm10\"\ncontroller_identity=\"$known\"\n"); file_put_contents($liveFile, "controller=\"$replacement\"\n"); migrate_cfg_and_labels('fanctrlplus2', $collision, "$hwmon/hwmon*"); - $check(parse_ini_file($liveFile)['controller'] === $replacement && parse_ini_file($serialFile)['controller'] === '', 'A current manual assignment must win without two loops, in either filename order.'); + $check(parse_ini_file($liveFile)['controller'] === $replacement && parse_ini_file($serialFile)['controller'] === "$root/gone/hwmon1/pwm10", 'A current manual assignment must win without two loops, in either filename order; the other keeps its missing path.'); unlink($liveFile); migrate_cfg_and_labels('fanctrlplus2', $collision, "$hwmon/hwmon*"); $check(parse_ini_file($serialFile)['controller'] === $replacement, 'A blocked identity must recover when the other assignment is removed.'); From 14c7ff5acd404dd48ab3f80f79b31d9460c9b94b Mon Sep 17 00:00:00 2001 From: Andre Brait Date: Thu, 19 Nov 2026 08:09:39 +0000 Subject: [PATCH 02/11] Refuse hwmon-number controllers, pin sensors to devices, keep bindings on save A controller saved as /sys/class/hwmon/hwmonN/pwmM names a number, not a chip, so migration no longer resolves it and rc.fanctrlplus2 and Run Now no longer drive it: the settings page asks for the controller to be selected again. CPU and auxiliary hwmon sensors are now saved by device path, and older /sys/class ones are pinned once by migration, which runs after every plugin has installed. Migration no longer copies a blanked controller's identity back into the controller, which let a fan that lost a conflict take the PWM back after its chip reloaded. The page shows such a binding by its identity instead, and saving it unchanged keeps it blank. A saved controller the list lacks survives a PWM label refresh, existing but unlisted selections are kept without a missing flag, an unchanged controller with no identity gets one on save, and a disabled CPU or auxiliary source keeps its saved sensor. --- README.md | 15 ++- .../plugins/fanctrlplus2/fanctrlplus2.page | 6 +- .../plugins/fanctrlplus2/include/Common.php | 102 +++++++++++------- .../fanctrlplus2/include/FanBlockRender.php | 43 +++++++- .../include/update.fanctrlplus2.php | 23 ++-- .../scripts/fanctrlplus2_refresh_single.sh | 4 + .../fanctrlplus2/scripts/rc.fanctrlplus2 | 4 +- tests/curve_config_test.php | 25 +++++ tests/missing_binding_ui_test.php | 50 ++++++--- tests/pwm_path_migration_test.php | 38 ++++++- tests/rc_boot_test.sh | 11 ++ 11 files changed, 243 insertions(+), 78 deletions(-) diff --git a/README.md b/README.md index 4cef63d..477f747 100644 --- a/README.md +++ b/README.md @@ -206,10 +206,17 @@ 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. -The settings page shows a saved controller, CPU sensor, or auxiliary sensor -that is not present as "Missing" and keeps it selected; saving asks for -confirmation first, and the fan cannot use it until it is back. +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. +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 +until another one is selected. Saving a configuration without a resolved controller, including a rename, retains its binding. Selecting a different controller replaces the binding. Clearing a label also removes its dormant USB identity entry, so it cannot diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/fanctrlplus2.page b/src/usr/local/emhttp/plugins/fanctrlplus2/fanctrlplus2.page index 8ce6137..195581b 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/fanctrlplus2.page +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/fanctrlplus2.page @@ -928,6 +928,9 @@ $(function() { $('.pwm-controller').each(function () { const selected = $(this).val(); + // The saved controller the list lacks (missing, or saved by hwmon + // number) stays, so a label save does not drop it. + const saved = $(this).find('option[data-saved]').detach(); $(this).empty().append(''); pwmList.forEach(pwm => { @@ -940,6 +943,7 @@ $(function() { $(this).append($(' - - + + + @@ -303,7 +335,8 @@ class="interval-input fcp-interval-input" - + + @@ -345,9 +378,9 @@ class="interval-input fcp-interval-input" - + - + diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/include/update.fanctrlplus2.php b/src/usr/local/emhttp/plugins/fanctrlplus2/include/update.fanctrlplus2.php index 3fcd23a..ef94cb5 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/update.fanctrlplus2.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/update.fanctrlplus2.php @@ -172,11 +172,17 @@ // Read before rename/overwrite: an empty runtime controller means the // saved binding is unresolved, not that the user selected another device. $previous = $plan['previous']; - // An unchanged controller keeps its identity: a missing one (kept by - // migration until the user picks another) cannot be identified again. - $controller_identity = $controller === '' || $controller === (string)($previous['controller'] ?? '') - ? (string)($previous['controller_identity'] ?? $previous['controller'] ?? '') - : (fcp_unique_pwm_identity($controller) ?? $controller); + // An unchanged controller keeps its binding: a missing one (kept by + // migration until the user picks another) cannot be identified again, and a + // blanked one is shown by its identity (fcp_saved_controller_option). + $previous_controller = (string)($previous['controller'] ?? ''); + $previous_identity = (string)($previous['controller_identity'] ?? ''); + if ($controller === $previous_controller || ($previous_controller === '' && $controller === $previous_identity)) { + $controller = $previous_controller; + } + $controller_identity = ($controller === '' || $controller === $previous_controller) && $previous_identity !== '' + ? $previous_identity + : ($controller === '' ? $previous_controller : (fcp_unique_pwm_identity($controller) ?? $controller)); // Rename the cfg file when Custom Name changes. if ($old_file !== $expected_file) { @@ -237,11 +243,14 @@ // CPU fallback $cpu_enable = $_POST['cpu_enable'][$i] ?? '0'; - $cpu_sensor = $_POST['cpu_sensor'][$i] ?? ''; + // A disabled select is not posted: keep the sensor saved for when the + // source is enabled again. + $cpu_sensor = $_POST['cpu_sensor'][$i] ?? (string)($plan['previous']['cpu_sensor'] ?? ''); // Aux sensor fallback $aux_enable = $_POST['aux_enable'][$i] ?? '0'; - $aux_sensor = isset($_POST['aux_sensor'][$i]) ? implode(',', $_POST['aux_sensor'][$i]) : ''; + $aux_sensor = isset($_POST['aux_sensor'][$i]) ? implode(',', $_POST['aux_sensor'][$i]) + : ($aux_enable === '1' ? '' : (string)($plan['previous']['aux_sensor'] ?? '')); // Build the configuration contents. $cfg = [ 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 0cb30ff..8030ac8 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 @@ -25,6 +25,10 @@ fail() { exit 1 } +# A /sys/class/hwmon path names an index, whatever chip holds it (see rc.fanctrlplus2). +[[ "$controller" == */class/hwmon/* ]] && + fail "The PWM controller is saved by its hwmon number ($controller), which may now belong to another chip. Select the controller again on the settings page. Nothing was written." + mkdir -p "/var/tmp/${plugin}" || fail "Could not create /var/tmp/${plugin}, so this run cannot coordinate with the fan loop. Nothing was written." diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/rc.fanctrlplus2 b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/rc.fanctrlplus2 index 84fbe91..baeb847 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/rc.fanctrlplus2 +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/rc.fanctrlplus2 @@ -55,8 +55,10 @@ fanctrlplus2.start_fans() { fi # Serial resolution can leave an absent/ambiguous controller unassigned. + # A /sys/class/hwmon path names an index, whatever chip holds it, so it + # is not driven until the user picks the controller again. controller=$(grep -Po '^controller="\K[^"]+' "$cfg") - if [[ -z "$controller" || ! -w "$controller" ]]; then + if [[ -z "$controller" || ! -w "$controller" || "$controller" == */class/hwmon/* ]]; then [[ -n "$custom_safe" ]] && missing=1 (( quiet )) || [[ -z "$raw_custom" ]] || fcp_warn_not_started "$cfg" "$raw_custom" "$controller" "/var/tmp/$plugin/faults_$raw_custom" diff --git a/tests/curve_config_test.php b/tests/curve_config_test.php index 533dd31..97ac6fd 100644 --- a/tests/curve_config_test.php +++ b/tests/curve_config_test.php @@ -255,6 +255,31 @@ $rawSaved = parse_ini_file("$cfgDir/fanctrlplus2_Raw.cfg"); $check($rawSaved['max'] === '100' && $rawSaved['idle'] === '76', 'Unedited raw PWM values must survive percentage display rounding.'); +// Saving keeps a saved binding the page showed unchanged: a missing controller +// keeps its identity, a blanked one stays blank, and a disabled source's +// sensor (its select is not posted) stays saved. +$bindingSave = function (array $saved, callable $edit) use ($write, $save, $request, $rawLegacy, $rawRules, $cfgDir) { + $write('fanctrlplus2_Raw.cfg', array_replace($rawLegacy, $saved)); + $post = $request([['file' => 'fanctrlplus2_Raw.cfg', 'custom' => 'Raw', 'pwm' => '30', 'max' => '39', 'idle' => '30', 'curve' => json_encode($rawRules)]]); + $edit($post); + $reply = $save($post); + return ($reply['status'] ?? '') === 'ok' ? parse_ini_file("$cfgDir/fanctrlplus2_Raw.cfg") : $reply; +}; +$gonePwm = '/sys/devices/platform/it87.2624/hwmon/hwmon2/pwm2'; +$usbKey = 'usb:' . bin2hex(json_encode(['3904', 'f001', 'ABC123', '/interface:1.0/hwmon/hwmon*/pwm1'])); +$saved = $bindingSave(['controller' => $gonePwm, 'controller_identity' => $usbKey], function (&$post) use ($gonePwm) { $post['controller'][0] = $gonePwm; }); +$check(($saved['controller'] ?? '') === $gonePwm && ($saved['controller_identity'] ?? '') === $usbKey, 'Saving an unchanged missing controller must keep it and its USB identity.'); +$saved = $bindingSave(['controller' => '', 'controller_identity' => $gonePwm], function (&$post) use ($gonePwm) { $post['controller'][0] = $gonePwm; }); +$check(($saved['controller'] ?? null) === '' && ($saved['controller_identity'] ?? '') === $gonePwm, 'Saving a blanked controller shown by its identity must keep it blank.'); +$cpuPath = '/sys/devices/platform/coretemp.0/hwmon/hwmon1/temp1_input'; +$saved = $bindingSave(['cpu_sensor' => $cpuPath, 'aux_sensor' => 'storcli:c0:roc'], function (&$post) { + unset($post['cpu_sensor'][0]); +}); +$check(($saved['cpu_sensor'] ?? '') === $cpuPath && ($saved['aux_sensor'] ?? '') === 'storcli:c0:roc', 'Disabled sources must keep their saved sensors.'); +$saved = $bindingSave(['aux_sensor' => 'storcli:c0:roc'], function (&$post) { $post['aux_enable'][0] = '1'; }); +$check(($saved['aux_sensor'] ?? null) === '', 'An enabled aux source with nothing selected must save no sensor.'); +$write('fanctrlplus2_Raw.cfg', $rawLegacy); + $write('fanctrlplus2_JBOD.cfg', $legacyFan); $cpuOnly = ['cpu' => $with(['points' => [[40, 120], [70, 255]]]), 'aux' => $contract]; $reply = $save($request([['file' => 'fanctrlplus2_JBOD.cfg', 'custom' => 'JBOD', 'cpu' => '1', 'idle' => '40', 'groups' => [], 'curve' => json_encode($cpuOnly)]])); diff --git a/tests/missing_binding_ui_test.php b/tests/missing_binding_ui_test.php index 92490d0..435782b 100644 --- a/tests/missing_binding_ui_test.php +++ b/tests/missing_binding_ui_test.php @@ -4,32 +4,50 @@ require_once __DIR__ . '/../src/usr/local/emhttp/plugins/fanctrlplus2/include/FanBlockRender.php'; $failures = []; +$check = function (bool $ok, string $message) use (&$failures) { if (!$ok) $failures[] = $message; }; +$root = sys_get_temp_dir() . '/fcp_missing_ui_' . getmypid(); +@mkdir($root, 0777, true); +$present = "$root/temp2_input"; +touch($present); + $pwms = [['chip' => 'nct6798', 'name' => 'pwm2', 'sensor' => '/sys/devices/platform/nct6775.656/hwmon/hwmon2/pwm2']]; -$cpu = ['/sys/class/hwmon/hwmon1/temp1_input' => 'coretemp - Package']; +$cpu = ['/sys/devices/platform/coretemp.0/hwmon/hwmon1/temp1_input' => 'coretemp - Package']; $aux = ['hwmon' => [['path' => 'hwmon:present', 'label' => 'Present']]]; $render = fn(array $cfg) => render_fan_block($cfg + ['custom' => 'Array', 'file' => 'fanctrlplus2_Array.cfg'], 0, $pwms, [], [], $cpu, $aux); +// The option element carrying $value, or '' when there is none. +$option = function (string $html, string $value): string { + return preg_match('#