use std::time::Duration;
use crate::context::Context;
use crate::exec::all_lines;
use crate::install::{Step, StepFailed, run_step, step};
const JOIN_HYPERV_ADMINS: &str = "$a = [Security.Principal.WindowsIdentity]::GetCurrent().Name -replace '''', ''''''; \
$s = '$ErrorActionPreference = ''Stop''; try { Add-LocalGroupMember -SID ''S-1-5-32-578'' -Member ''' + $a + ''' } catch [Microsoft.PowerShell.Commands.MemberExistsException] { }'; \
$e = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($s)); \
try { $p = Start-Process -FilePath powershell.exe -ArgumentList '-NoProfile','-NonInteractive','-EncodedCommand',$e -Verb RunAs -WindowStyle Hidden -Wait -PassThru; exit $p.ExitCode } catch { exit 1223 }";
#[must_use]
pub fn join_hyperv_admins_step() -> Step {
step("powershell", &["-NoProfile", "-NonInteractive", "-Command", JOIN_HYPERV_ADMINS])
}
pub async fn join_hyperv_admins(cx: &Context, on_line: &mut (dyn FnMut(&str) + Send)) -> Result<(), StepFailed> {
run_step(cx, &join_hyperv_admins_step(), on_line).await.map(drop)
}
pub const RESTART_NOTE_MAX: usize = 200;
const ERROR_SHUTDOWN_IS_SCHEDULED: u32 = 1190;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Restart {
Scheduled,
AlreadyScheduled,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum RestartFailed {
Note,
Refused(String),
}
impl std::fmt::Display for RestartFailed {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RestartFailed::Note => write!(f, "a restart's note is one line of at most {RESTART_NOTE_MAX} characters"),
RestartFailed::Refused(why) => write!(f, "the restart couldn't be scheduled: {why}"),
}
}
}
impl std::error::Error for RestartFailed {}
pub async fn schedule_restart(cx: &Context, after: Duration, note: &str) -> Result<Restart, RestartFailed> {
let note = note.trim();
if note.is_empty() || note.chars().count() > RESTART_NOTE_MAX || note.chars().any(char::is_control) {
return Err(RestartFailed::Note);
}
let seconds = after.as_secs().to_string();
let captured = cx
.probe("shutdown", &["/r", "/t", &seconds, "/c", note])
.await
.map_err(|e| RestartFailed::Refused(e.to_string()))?;
if captured.success() {
Ok(Restart::Scheduled)
} else if win32_error(&all_lines(&captured)) == Some(ERROR_SHUTDOWN_IS_SCHEDULED) {
Ok(Restart::AlreadyScheduled)
} else {
Err(RestartFailed::Refused(captured.reason()))
}
}
fn win32_error(said: &str) -> Option<u32> {
let said = said.trim_end().trim_end_matches('.');
let (_, number) = said.strip_suffix(')')?.rsplit_once('(')?;
number.trim().parse().ok()
}
#[cfg(test)]
mod tests {
use isothrum_runner::fake::{FakeProcesses, Reply};
use super::*;
use crate::install::exit_codes::Known;
use crate::testing::answered_by;
#[test]
fn joining_hyperv_administrators_is_one_uac_prompt_by_the_groups_sid() {
let step = join_hyperv_admins_step();
assert_eq!(step.program, "powershell");
let script = step.args.last().unwrap();
for part in [
"Add-LocalGroupMember -SID ''S-1-5-32-578''",
"MemberExistsException",
"WindowsIdentity]::GetCurrent().Name",
"-Verb RunAs",
"-Wait -PassThru",
"catch { exit 1223 }",
] {
assert!(script.contains(part), "{part} in {script}");
}
assert!(!script.contains('"'), "{script}");
}
#[tokio::test]
async fn a_declined_prompt_to_join_reads_as_cancelled() {
let cx = answered_by(FakeProcesses::script().expect("powershell *", Reply::fail(1223, "")).build());
let err = join_hyperv_admins(&cx, &mut |_| {}).await.unwrap_err();
assert!(
matches!(
err,
StepFailed::Exited {
known: Some(Known::Cancelled),
..
}
),
"{err:?}"
);
let joined = answered_by(FakeProcesses::script().expect("powershell *", Reply::ok("")).build());
assert_eq!(join_hyperv_admins(&joined, &mut |_| {}).await, Ok(()));
}
#[tokio::test]
async fn a_restart_is_scheduled_with_its_note_or_found_scheduled_already() {
let fake = std::sync::Arc::new(FakeProcesses::script().expect("shutdown /r /t 5 /c", Reply::ok("")).build());
let cx = crate::testing::shared(std::sync::Arc::<FakeProcesses>::clone(&fake));
assert_eq!(
schedule_restart(&cx, Duration::from_secs(5), " Restarting to finish WSL 2. ").await,
Ok(Restart::Scheduled)
);
assert_eq!(fake.made("shutdown")[0].args.last().map(String::as_str), Some("Restarting to finish WSL 2."));
let french = "Un arrêt du système a déjà été planifié.(1190)\r";
let scheduled = answered_by(FakeProcesses::script().expect("shutdown *", Reply::fail(1, french)).build());
assert_eq!(
schedule_restart(&scheduled, Duration::from_secs(5), "note").await,
Ok(Restart::AlreadyScheduled)
);
let denied = answered_by(FakeProcesses::script().expect("shutdown *", Reply::fail(1, "Access is denied.(5)")).build());
assert!(
matches!(schedule_restart(&denied, Duration::from_secs(5), "note").await, Err(RestartFailed::Refused(why)) if why.contains("Access is denied"))
);
}
#[tokio::test]
async fn a_restart_note_is_one_short_line() {
let cx = answered_by(FakeProcesses::script().build());
for note in ["", "two\nlines", &"x".repeat(RESTART_NOTE_MAX + 1)] {
assert_eq!(schedule_restart(&cx, Duration::from_secs(5), note).await, Err(RestartFailed::Note), "{note:?}");
}
}
#[test]
fn the_win32_error_is_read_from_the_end_whatever_the_language() {
assert_eq!(win32_error("A system shutdown has already been scheduled.(1190)\r\n"), Some(1190));
assert_eq!(win32_error("listening on port 1190"), None);
assert_eq!(win32_error(""), None);
}
}