diff --git a/README.md b/README.md index 5168d70..477f747 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,20 @@ 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. 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 6ace881..9d6b0f3 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/fanctrlplus2.page +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/fanctrlplus2.page @@ -208,9 +208,10 @@ document.head.appendChild(link); function updatePwmDropdowns() { // Collect every non-empty PWM selection. const selected = []; + // A kept binding's option belongs to its own fan only. $('.pwm-controller').each(function() { const val = $(this).val(); - if (val) selected.push(val); + if (val && !$(this).find('option:selected').is('[data-saved]')) selected.push(val); }); // Update each select option. @@ -928,6 +929,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 +944,10 @@ $(function() { $(this).append($(' + + + + @@ -299,6 +356,10 @@ class="interval-input fcp-interval-input" $label): ?> + + + + @@ -337,6 +398,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..5614374 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/include/update.fanctrlplus2.php +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/include/update.fanctrlplus2.php @@ -172,9 +172,20 @@ // 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 === '' - ? (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 posted as '__fcp_keep__' (fcp_saved_controller_option). + $previous_controller = (string)($previous['controller'] ?? ''); + $previous_identity = (string)($previous['controller_identity'] ?? ''); + if ($controller === '__fcp_keep__') $controller = $previous_controller; + if (($controller === '' || $controller === $previous_controller) && $previous_identity !== '') { + $controller_identity = $previous_identity; + } elseif ($controller === '') { + $controller_identity = $previous_controller; + } else { + // A /sys/class path names a number, not a device: no identity from it. + $controller_identity = fcp_class_hwmon_path($controller) ? '' : (fcp_unique_pwm_identity($controller) ?? $controller); + } // Rename the cfg file when Custom Name changes. if ($old_file !== $expected_file) { @@ -235,11 +246,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/disk_group_control.sh b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/disk_group_control.sh index a2cd540..476c770 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/disk_group_control.sh +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/disk_group_control.sh @@ -487,17 +487,25 @@ fcp_track_faults() { done } +# A controller fan control may drive: writable, and not a /sys/class/hwmon +# path, which names a number whatever chip holds it (#41). +fcp_drivable_controller() { + [[ -n "$1" && -w "$1" && "$1" != */class/hwmon/* ]] +} + # Warns once that a fan's loop could not start because its controller is # missing. The flag directory is the loop's own, so the loop announces the # recovery once the controller is back and the fan is started again. fcp_warn_not_started() { local cfg_file="$1" name="$2" controller="$3" dir="$4" + local details="${controller:-Its PWM controller} is missing or cannot be written, so FanCtrl Plus 2 did not start controlling this fan. The controller may be unplugged, renumbered or ambiguous, or its driver not loaded. Check the controller on the FanCtrl Plus 2 settings page, then apply." + [[ "$controller" == */class/hwmon/* ]] && + details="$controller is saved by its hwmon number, which may now belong to another chip, so FanCtrl Plus 2 did not start controlling this fan. Select the controller again on the FanCtrl Plus 2 settings page, then apply." mkdir -p "$dir" 2>/dev/null [[ -e "$dir/controller" ]] && return 0 fcp_alerts_enabled || return 0 [[ -d "$dir" && -w "$dir" ]] || return 0 - fcp_notify -i warning -s "Fan $name $fcp_controller_subject" \ - -d "${controller:-Its PWM controller} is missing or cannot be written, so FanCtrl Plus 2 did not start controlling this fan. The controller may be unplugged, renumbered or ambiguous, or its driver not loaded. Check the controller on the FanCtrl Plus 2 settings page, then apply." && + fcp_notify -i warning -s "Fan $name $fcp_controller_subject" -d "$details" && printf '%s' "$fcp_controller_subject" > "$dir/controller" 2>/dev/null } diff --git a/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/fanctrlplus2_dashboard_update.sh b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/fanctrlplus2_dashboard_update.sh index ff4ca50..88ab3fa 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/fanctrlplus2_dashboard_update.sh +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/fanctrlplus2_dashboard_update.sh @@ -14,6 +14,10 @@ while true; do [[ "$service" != "1" ]] && continue [[ -z "$controller" || -z "$custom" ]] && continue + # A /sys/class/hwmon controller is not driven: the chip holding that + # number now is not necessarily this fan's (#41). + [[ "$controller" == */class/hwmon/* ]] && controller="${controller%/*}/.not-driven/pwm0" + # Derive the fanX_input path from pwmX. if [[ "$controller" =~ pwm([0-9]+)$ ]]; then fan_index="${BASH_REMATCH[1]}" 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..0832dae 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,23 @@ 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." + +# A saved controller that is not there (migration keeps it until the user +# picks another): nothing to write to. +[[ -n "$controller" && -e "$controller" ]] || + fail "The PWM controller ${controller:-for this fan} is not detected now. Select another on the settings page, or wait for it to return. Nothing was written." + +# A USB controller kept while unresolved (migration keeps its last path) may +# have another device at that path now: drive it only as the saved device. +if [[ "${controller_identity:-}" == usb:* ]] && + ! php -r 'require $argv[1]; exit(fcp_unique_pwm_identity($argv[2]) === $argv[3] ? 0 : 1);' \ + "${BASH_SOURCE%/*}/../include/Common.php" "$controller" "$controller_identity" 2>/dev/null; then + fail "The PWM controller at $controller is not the USB controller saved for this fan, which is not detected now. Nothing was written." +fi + 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..bc7a461 100644 --- a/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/rc.fanctrlplus2 +++ b/src/usr/local/emhttp/plugins/fanctrlplus2/scripts/rc.fanctrlplus2 @@ -24,6 +24,7 @@ rc_script="/usr/local/emhttp/plugins/$plugin/scripts/rc.fanctrlplus2" config_lock="/var/run/${plugin}.config.lock" boot_wait_seconds=300 boot_retry_seconds=10 +hwmon_dir=/sys/class/hwmon boot_pattern="$rc_script boot\$" user_stopped="/var/run/${plugin}.user_stopped" source "/usr/local/emhttp/plugins/$plugin/scripts/disk_group_control.sh" @@ -55,10 +56,14 @@ fanctrlplus2.start_fans() { fi # Serial resolution can leave an absent/ambiguous controller unassigned. + # A /sys/class/hwmon path waits for the user, not a driver: it warns now. controller=$(grep -Po '^controller="\K[^"]+' "$cfg") - if [[ -z "$controller" || ! -w "$controller" ]]; then - [[ -n "$custom_safe" ]] && missing=1 - (( quiet )) || [[ -z "$raw_custom" ]] || + if ! fcp_drivable_controller "$controller"; then + if [[ "$controller" != */class/hwmon/* ]]; then + [[ -n "$custom_safe" ]] && missing=1 + (( quiet )) && continue + fi + [[ -z "$raw_custom" ]] || fcp_warn_not_started "$cfg" "$raw_custom" "$controller" "/var/tmp/$plugin/faults_$raw_custom" continue fi @@ -90,11 +95,20 @@ fanctrlplus2.start() { fi return 0 fi + # A boot start in its grace period would race this start. + fanctrlplus2.end_boot # A fan without its controller is warned about, not a failed start. fanctrlplus2.start_now return 0 } +# Ends a boot start still waiting, and lets one of its steps already running +# finish, so it cannot start loops after a stop or alongside a start. +fanctrlplus2.end_boot() { + pkill -f "$boot_pattern" + while pgrep -f "$rc_script (start-now|resume)" >/dev/null; do sleep 0.2; done +} + # Runs one rc action under the settings lock, which the fan loops it starts # must not inherit (see array_monitor.sh). fanctrlplus2.locked() { @@ -104,20 +118,38 @@ fanctrlplus2.locked() { # Boot start, in the background. Waits for emhttpd, which the go file starts # once every plugin has installed, or 5 minutes should it never run. A fan # whose controller is still missing then gets 5 more minutes for a driver -# that loads late, retried every 10 seconds, before it warns. The fans with a +# that loads late, retried when the PWM channels change, before it warns. The fans with a # controller run meanwhile. fanctrlplus2.boot() { local deadline=$((SECONDS + boot_wait_seconds)) until pgrep -x emhttpd >/dev/null || (( SECONDS >= deadline )); do sleep 2; done + # Retry when the PWM channels change (a chip binds, a driver is swapped or + # adds channels), not every round: each retry runs the migration. Listed + # before the first attempt, so a change during it still counts. + local seen now + seen=$(fanctrlplus2.pwm_channels) fanctrlplus2.locked start-now quiet && return deadline=$((SECONDS + boot_wait_seconds)) while (( SECONDS < deadline )); do sleep "$boot_retry_seconds" + now=$(fanctrlplus2.pwm_channels) + [[ "$now" == "$seen" ]] && continue + seen=$now fanctrlplus2.locked resume quiet && return done fanctrlplus2.locked resume } +# The PWM channels present, resolved to their devices. Older drivers keep +# them on the parent device (see list_pwm). One file at a time: readlink may +# print nothing when any argument (an unmatched glob) fails. +fanctrlplus2.pwm_channels() { + local p + for p in "$hwmon_dir"/*/pwm* "$hwmon_dir"/*/device/pwm*; do + [[ -e "$p" ]] && readlink -f "$p" + done +} + fanctrlplus2.start_now() { local status=0 pkill -f "$loop_script" 2>/dev/null @@ -152,10 +184,7 @@ fanctrlplus2.start_now() { fanctrlplus2.stop() { # Record that the user requested a stop. touch "$user_stopped" - # A boot start still waiting must not start the fans again, and one of its - # steps already running finishes first, so it cannot start loops after this. - pkill -f "$boot_pattern" - while pgrep -f "$rc_script (start-now|resume)" >/dev/null; do sleep 0.2; done + fanctrlplus2.end_boot # The fans whose loop has run (a PID file): the BIOS keeps the others. local -A ran=() @@ -184,7 +213,7 @@ fanctrlplus2.stop() { [[ -n "$name" && -n "${ran[$name]:-}" ]] || continue pwm=$(grep -Po '^controller="\K[^"]+' "$cfg") - [[ -z "$pwm" || ! -w "$pwm" ]] && continue + fcp_drivable_controller "$pwm" || continue enable="${pwm}_enable" if [[ -w "$enable" ]]; then diff --git a/tests/curve_config_test.php b/tests/curve_config_test.php index 533dd31..f07420f 100644 --- a/tests/curve_config_test.php +++ b/tests/curve_config_test.php @@ -255,6 +255,36 @@ $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) { $post['controller'][0] = '__fcp_keep__'; }); +$check(($saved['controller'] ?? null) === '' && ($saved['controller_identity'] ?? '') === $gonePwm, 'Saving a blanked controller shown by its identity must keep it blank.'); +$saved = $bindingSave(['controller' => '', 'controller_identity' => $gonePwm], function (&$post) use ($gonePwm) { $post['controller'][0] = $gonePwm; }); +$check(($saved['controller'] ?? null) === $gonePwm, 'Picking a controller equal to a blanked identity is an explicit choice and must be saved.'); +$classPwm = '/sys/class/hwmon/hwmon3/pwm1'; +$saved = $bindingSave(['controller' => $classPwm, 'controller_identity' => ''], function (&$post) use ($classPwm) { $post['controller'][0] = $classPwm; }); +$check(($saved['controller'] ?? '') === $classPwm && ($saved['controller_identity'] ?? null) === '', 'Saving a controller kept by hwmon number must not give it the identity of whatever holds that number now.'); +$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/curve_manual_test.sh b/tests/curve_manual_test.sh index 820d6e3..dca9300 100755 --- a/tests/curve_manual_test.sh +++ b/tests/curve_manual_test.sh @@ -334,6 +334,31 @@ if await_barrier; then fi cfg_pwm=100 +# A controller saved by its /sys/class/hwmon number may belong to another chip +# now (#41): Run Now refuses it and writes nothing. +mkdir -p "$tmp/class/hwmon/hwmon2" +echo 77 > "$tmp/class/hwmon/hwmon2/pwm1" +write_cfg '' +sed -i "s#^controller=.*#controller=\"$tmp/class/hwmon/hwmon2/pwm1\"#" "$FCP_CONFIG_DIR/fanctrlplus2_$custom.cfg" +run_manual 50000 +expect_equal "1 77" "$rc $(cat "$tmp/class/hwmon/hwmon2/pwm1")" "Run Now refuses a controller saved by its hwmon number." +expect_equal yes "$(said 'saved by its hwmon number')" "The refusal says to select the controller again." +write_cfg '' + +# A saved controller that is not there is refused with a reason, not a write error. +write_cfg '' +sed -i "s#^controller=.*#controller=\"$tmp/gone/pwm1\"#" "$FCP_CONFIG_DIR/fanctrlplus2_$custom.cfg" +run_manual 50000 +expect_equal "1 yes" "$rc $(said 'is not detected now')" "Run Now refuses a missing controller with a reason." + +# A USB controller kept while unresolved: another device may sit at its path. +write_cfg 'controller_identity="usb:00"' +printf '%s\n' 77 > "$tmp/pwm1" +run_manual 50000 +expect_equal "1 77" "$rc $(cat "$tmp/pwm1")" "Run Now refuses a path that is not the saved USB controller." +expect_equal yes "$(said 'not the USB controller saved')" "The refusal names the missing USB controller." +write_cfg '' + if (( failures > 0 )); then exit 1 fi diff --git a/tests/missing_binding_ui_test.php b/tests/missing_binding_ui_test.php new file mode 100644 index 0000000..871749e --- /dev/null +++ b/tests/missing_binding_ui_test.php @@ -0,0 +1,63 @@ + 'nct6798', 'name' => 'pwm2', 'sensor' => '/sys/devices/platform/nct6775.656/hwmon/hwmon2/pwm2']]; +$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('#