hal-9001 0.2.0

HAL-9001 — Central TUI de Controle do Sistema & Assistente de Sistema (Rust/ratatui)
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};

use tokio::sync::broadcast;

use crate::events::{Action, EventTx};
use crate::i18n::{Language, SharedLang};

pub async fn run(
    lang: SharedLang,
    tx: EventTx,
    _actions: broadcast::Receiver<Action>,
) -> anyhow::Result<()> {
    super::pending_stub("power", &lang, lang.messages().pending_module_power, tx).await
}

/// How a probed sysfs knob expresses "bypass on" vs. "bypass off".
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BypassKind {
    /// A numeric charge-limit threshold: a low value caps charging
    /// (bypass/conservation on), `off` restores unrestricted charging.
    Threshold { on: &'static str, off: &'static str },
    /// A plain `0`/`1` conservation-mode flag.
    Binary,
}

/// An agnostic handle to whatever battery-bypass / conservation-mode knob
/// [`BatteryBypass::probe`] found on this host, if any.
#[derive(Debug, Clone)]
pub struct BatteryBypass {
    path: PathBuf,
    kind: BypassKind,
}

impl BatteryBypass {
    /// Probes the real filesystem for a supported knob. Returns `None` on
    /// desktops or laptops whose vendor driver exposes none of the known
    /// paths, so callers can skip rendering the feature entirely.
    pub fn probe() -> Option<Self> {
        Self::probe_at(Path::new("/"))
    }

    /// Cascade used by [`Self::probe`], parameterized over a root so tests
    /// can point it at a tempdir standing in for `/`.
    pub fn probe_at(root: &Path) -> Option<Self> {
        // 1. Universal kernel sysfs (ThinkPad, modern ASUS, Framework, Dell,
        // Huawei): /sys/class/power_supply/BAT*/charge_control_end_threshold
        // (or charge_control_limit_max).
        let power_supply = root.join("sys/class/power_supply");
        if let Some(path) = find_in_dir_with_prefix(
            &power_supply,
            "BAT",
            &["charge_control_end_threshold", "charge_control_limit_max"],
        ) {
            return Some(Self {
                path,
                kind: BypassKind::Threshold {
                    on: "60",
                    off: "100",
                },
            });
        }

        // 2. Lenovo IdeaPad / Yoga / Legion ACPI conservation mode.
        let ideapad_drivers = root.join("sys/bus/platform/drivers/ideapad_acpi");
        if let Some(path) = find_in_dir_with_prefix(&ideapad_drivers, "", &["conservation_mode"]) {
            return Some(Self {
                path,
                kind: BypassKind::Binary,
            });
        }
        let ideapad_direct = root.join("sys/devices/platform/ideapad_acpi/conservation_mode");
        if ideapad_direct.exists() {
            return Some(Self {
                path: ideapad_direct,
                kind: BypassKind::Binary,
            });
        }

        // 3. LG Gram battery care limit.
        let lg = root.join("sys/devices/platform/lg-laptop/battery_care_limit");
        if lg.exists() {
            return Some(Self {
                path: lg,
                kind: BypassKind::Threshold {
                    on: "80",
                    off: "100",
                },
            });
        }

        // 4. Samsung battery life extender.
        let samsung = root.join("sys/devices/platform/samsung-laptop/battery_life_extender");
        if samsung.exists() {
            return Some(Self {
                path: samsung,
                kind: BypassKind::Binary,
            });
        }

        // 5. Legacy ASUS WMI charge threshold.
        let asus = root.join("sys/devices/platform/asus-nb-wmi/charge_control_end_threshold");
        if asus.exists() {
            return Some(Self {
                path: asus,
                kind: BypassKind::Threshold {
                    on: "60",
                    off: "100",
                },
            });
        }

        None
    }

    /// Reads the current state straight from sysfs.
    pub fn is_enabled(&self) -> bool {
        let content = std::fs::read_to_string(&self.path).unwrap_or_default();
        let value = content.trim();
        match self.kind {
            BypassKind::Threshold { off, .. } => !value.is_empty() && value != off,
            BypassKind::Binary => value == "1",
        }
    }

    /// Flips the knob with a direct write and returns the new state. When
    /// the write is rejected (HAL-9001 normally runs unprivileged) this
    /// returns [`BypassError::PermissionDenied`] instead of elevating on its
    /// own: spawning `pkexec`/`sudo` without a TTY wedges the raw-mode TUI,
    /// so the caller asks for the password through the in-app sudo modal and
    /// finishes with [`apply_value_with_sudo`].
    pub fn toggle(&self) -> Result<bool, BypassError> {
        let enabling = !self.is_enabled();
        let value = self.target_value(enabling);
        match std::fs::write(&self.path, value) {
            Ok(()) => Ok(enabling),
            Err(e) if e.kind() == std::io::ErrorKind::PermissionDenied => {
                Err(BypassError::PermissionDenied {
                    path: self.path.clone(),
                    target_value: value,
                    enabling,
                })
            }
            Err(e) => Err(BypassError::Failed(e.to_string())),
        }
    }

    fn target_value(&self, enabling: bool) -> &'static str {
        match self.kind {
            BypassKind::Threshold { on, off } => {
                if enabling {
                    on
                } else {
                    off
                }
            }
            BypassKind::Binary => {
                if enabling {
                    "1"
                } else {
                    "0"
                }
            }
        }
    }
}

/// Why [`BatteryBypass::toggle`] could not flip the knob.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BypassError {
    /// The direct write needs root: retry with [`apply_value_with_sudo`]
    /// writing `target_value` to `path`, which yields the `enabling` state.
    PermissionDenied {
        path: PathBuf,
        target_value: &'static str,
        enabling: bool,
    },
    Failed(String),
}

impl std::fmt::Display for BypassError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::PermissionDenied { path, .. } => {
                write!(f, "permission denied: {}", path.display())
            }
            Self::Failed(e) => f.write_str(e),
        }
    }
}

/// Outcome of an elevated write attempt.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SudoWriteError {
    /// sudo rejected the password (or needs one and none was given).
    AuthFailed,
    Failed(String),
}

/// Writes `value` to `path` as root via `sudo -S`, feeding `password` on
/// stdin (the same pattern `storage.rs` uses). `-k` ignores any cached
/// credential so sudo always consumes the password line instead of handing
/// it to the command. The value travels as an argument rather than on
/// stdin so a wrong password is never followed by a second stdin line that
/// sudo would count as another failed attempt (pam_faillock).
pub fn apply_value_with_sudo(
    path: &Path,
    value: &str,
    password: &str,
) -> Result<(), SudoWriteError> {
    run_sudo_write(&["-k", "-S", "-p", ""], path, value, Some(password))
}

/// Same as [`apply_value_with_sudo`] but only succeeds when sudo already
/// holds a cached credential (`sudo -n`), so the password modal can be
/// skipped entirely.
pub fn apply_value_with_cached_sudo(path: &Path, value: &str) -> Result<(), SudoWriteError> {
    run_sudo_write(&["-n"], path, value, None)
}

fn run_sudo_write(
    sudo_flags: &[&str],
    path: &Path,
    value: &str,
    password: Option<&str>,
) -> Result<(), SudoWriteError> {
    let mut child = Command::new("sudo")
        .args(sudo_flags)
        .arg("--")
        .args(["sh", "-c", "printf '%s' \"$1\" > \"$2\"", "hal9001"])
        .arg(value)
        .arg(path)
        .stdin(Stdio::piped())
        .stdout(Stdio::null())
        .stderr(Stdio::piped())
        .spawn()
        .map_err(|e| SudoWriteError::Failed(format!("sudo: {e}")))?;

    if let Some(mut stdin) = child.stdin.take() {
        if let Some(pw) = password {
            // A write error here means sudo already exited; its status and
            // stderr below carry the real reason.
            let _ = stdin.write_all(pw.as_bytes());
            let _ = stdin.write_all(b"\n");
        }
    }

    let output = child
        .wait_with_output()
        .map_err(|e| SudoWriteError::Failed(format!("sudo: {e}")))?;
    if output.status.success() {
        return Ok(());
    }
    let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
    if crate::backend::storage::is_sudo_auth_failure(&stderr) {
        Err(SudoWriteError::AuthFailed)
    } else {
        Err(SudoWriteError::Failed(stderr))
    }
}

/// Toggles whatever battery-bypass knob is present on this host. Probes
/// fresh on every call since the check is a handful of cheap path lookups.
pub async fn toggle_bypass(lang: Language) -> Result<bool, BypassError> {
    let bypass = BatteryBypass::probe().ok_or_else(|| {
        BypassError::Failed(lang.messages().err_battery_bypass_unavailable.to_string())
    })?;
    tokio::task::spawn_blocking(move || bypass.toggle())
        .await
        .map_err(|e| BypassError::Failed(e.to_string()))?
}

fn find_in_dir_with_prefix(dir: &Path, prefix: &str, filenames: &[&str]) -> Option<PathBuf> {
    let entries = std::fs::read_dir(dir).ok()?;
    for entry in entries.flatten() {
        let name = entry.file_name();
        if !name.to_string_lossy().starts_with(prefix) {
            continue;
        }
        for filename in filenames {
            let candidate = entry.path().join(filename);
            if candidate.exists() {
                return Some(candidate);
            }
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    fn write_file(path: &Path, content: &str) {
        std::fs::create_dir_all(path.parent().unwrap()).unwrap();
        std::fs::write(path, content).unwrap();
    }

    #[test]
    fn probe_finds_universal_kernel_sysfs() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        write_file(
            &root.join("sys/class/power_supply/BAT0/charge_control_end_threshold"),
            "100\n",
        );

        let bypass = BatteryBypass::probe_at(root).expect("should find BAT0 threshold");
        assert!(!bypass.is_enabled());
        assert_eq!(
            bypass.kind,
            BypassKind::Threshold {
                on: "60",
                off: "100"
            }
        );
    }

    #[test]
    fn probe_finds_charge_control_limit_max_variant() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        write_file(
            &root.join("sys/class/power_supply/BAT1/charge_control_limit_max"),
            "60\n",
        );

        let bypass = BatteryBypass::probe_at(root).expect("should find BAT1 limit_max");
        assert!(bypass.is_enabled());
    }

    #[test]
    fn probe_finds_ideapad_conservation_mode_under_glob_dir() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        write_file(
            &root.join("sys/bus/platform/drivers/ideapad_acpi/VPC2004:00/conservation_mode"),
            "1\n",
        );

        let bypass = BatteryBypass::probe_at(root).expect("should find ideapad conservation_mode");
        assert!(bypass.is_enabled());
        assert_eq!(bypass.kind, BypassKind::Binary);
    }

    #[test]
    fn probe_finds_ideapad_conservation_mode_direct_path() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        write_file(
            &root.join("sys/devices/platform/ideapad_acpi/conservation_mode"),
            "0\n",
        );

        let bypass = BatteryBypass::probe_at(root).expect("should find direct conservation_mode");
        assert!(!bypass.is_enabled());
    }

    #[test]
    fn probe_finds_lg_gram_battery_care_limit() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        write_file(
            &root.join("sys/devices/platform/lg-laptop/battery_care_limit"),
            "80\n",
        );

        let bypass = BatteryBypass::probe_at(root).expect("should find LG Gram knob");
        assert!(bypass.is_enabled());
        assert_eq!(
            bypass.kind,
            BypassKind::Threshold {
                on: "80",
                off: "100"
            }
        );
    }

    #[test]
    fn probe_finds_samsung_battery_life_extender() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        write_file(
            &root.join("sys/devices/platform/samsung-laptop/battery_life_extender"),
            "1\n",
        );

        let bypass = BatteryBypass::probe_at(root).expect("should find Samsung knob");
        assert!(bypass.is_enabled());
    }

    #[test]
    fn probe_finds_legacy_asus_wmi_threshold() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        write_file(
            &root.join("sys/devices/platform/asus-nb-wmi/charge_control_end_threshold"),
            "100\n",
        );

        let bypass = BatteryBypass::probe_at(root).expect("should find legacy ASUS WMI knob");
        assert!(!bypass.is_enabled());
    }

    #[test]
    fn probe_prefers_universal_sysfs_over_vendor_specific() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        write_file(
            &root.join("sys/class/power_supply/BAT0/charge_control_end_threshold"),
            "60\n",
        );
        write_file(
            &root.join("sys/devices/platform/samsung-laptop/battery_life_extender"),
            "0\n",
        );

        let bypass = BatteryBypass::probe_at(root).expect("should find a knob");
        assert_eq!(
            bypass.kind,
            BypassKind::Threshold {
                on: "60",
                off: "100"
            }
        );
    }

    #[test]
    fn probe_returns_none_when_no_known_path_exists() {
        let dir = tempfile::tempdir().unwrap();
        assert!(BatteryBypass::probe_at(dir.path()).is_none());
    }

    #[test]
    fn toggle_flips_threshold_value_on_a_writable_file() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let path = root.join("sys/class/power_supply/BAT0/charge_control_end_threshold");
        write_file(&path, "100\n");

        let bypass = BatteryBypass::probe_at(root).unwrap();
        assert!(!bypass.is_enabled());

        let enabled = bypass.toggle().expect("direct write should succeed");
        assert!(enabled);
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "60");
        assert!(bypass.is_enabled());

        let enabled = bypass.toggle().expect("direct write should succeed");
        assert!(!enabled);
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "100");
    }

    #[test]
    fn toggle_reports_permission_denied_instead_of_elevating() {
        use std::os::unix::fs::PermissionsExt;

        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let path = root.join("sys/class/power_supply/BAT0/charge_control_end_threshold");
        write_file(&path, "100\n");
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o444)).unwrap();
        if std::fs::OpenOptions::new().write(true).open(&path).is_ok() {
            // Running as root: permission bits are not enforced.
            return;
        }

        let bypass = BatteryBypass::probe_at(root).unwrap();
        assert_eq!(
            bypass.toggle(),
            Err(BypassError::PermissionDenied {
                path: path.clone(),
                target_value: "60",
                enabling: true,
            })
        );
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "100\n");
    }

    #[test]
    fn toggle_flips_binary_value_on_a_writable_file() {
        let dir = tempfile::tempdir().unwrap();
        let root = dir.path();
        let path = root.join("sys/devices/platform/ideapad_acpi/conservation_mode");
        write_file(&path, "0\n");

        let bypass = BatteryBypass::probe_at(root).unwrap();
        let enabled = bypass.toggle().expect("direct write should succeed");
        assert!(enabled);
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "1");
    }
}