Files
orca/native/windows-cli-launcher/src/administrator_restart.rs
Neil 3f81541188 Support restarting Windows Orca as administrator (#28073)
* feat(windows): add administrator PowerShell terminal setting

* fix(settings): localize administrator terminal search keywords

* fix(windows): restrict elevated terminal host to Orca's runtime and requesting account

* fix(windows): harden administrator terminal routing, inventory and restore

* fix(build): build the Windows agent hook when electron-builder packages Windows

* fix(build): build the packaged Windows agent hook for the target architecture

* fix(windows): accept the development terminal runtime and test real elevation in CI

* feat(windows): show administrator status and restart instructions

* fix(settings): localize administrator mode search keywords

* test(settings): isolate the desktop permission section in shell picker tests

* Add Windows administrator restart and simplify Terminal settings

* Keep administrator restart silent and settings actions within narrow panes

* Keep Windows shell choices and permission guides usable at minimum width

* Prevent later restarts from replaying administrator terminal reset

* Fix CI route resolution and transient Windows fixture cleanup
2026-10-13 18:06:05 -07:00

85 lines
2.4 KiB
Rust

use std::env;
use std::ffi::{c_void, OsStr};
use std::os::windows::ffi::OsStrExt;
use std::os::windows::process::CommandExt;
use std::process::Command;
#[link(name = "shell32")]
extern "system" {
fn ShellExecuteW(
window: *mut c_void,
operation: *const u16,
file: *const u16,
parameters: *const u16,
directory: *const u16,
show: i32,
) -> *mut c_void;
}
#[link(name = "ole32")]
extern "system" {
fn CoInitializeEx(reserved: *mut c_void, mode: u32) -> i32;
fn CoUninitialize();
}
pub fn run_if_requested() {
let mut args = env::args_os().skip(1);
if args.next().as_deref() != Some(OsStr::new("--orca-restart-as-administrator")) {
return;
}
let (Some(executable), Some(elevated_parameters), Some(ordinary_parameters)) =
(args.next(), args.next(), args.next())
else {
crate::fail("Missing administrator restart arguments");
};
if args.next().is_some() {
crate::fail("Unexpected administrator restart arguments");
}
let operation = wide(OsStr::new("runas"));
let file = wide(&executable);
let parameters = wide(&elevated_parameters);
let show = if env::var("ORCA_BACKGROUND_LAUNCH").as_deref() == Ok("1") {
0
} else {
1
};
// SAFETY: buffers remain terminated and alive; COM is paired on this thread.
let launched = unsafe {
let initialized = CoInitializeEx(std::ptr::null_mut(), 6) >= 0;
let result = if initialized {
ShellExecuteW(
std::ptr::null_mut(),
operation.as_ptr(),
file.as_ptr(),
parameters.as_ptr(),
std::ptr::null(),
show,
) as isize
} else {
0
};
if initialized {
CoUninitialize();
}
result > 32
};
if launched {
std::process::exit(0);
}
// Cancellation reopens the ordinary desktop without ending its retained terminals.
match Command::new(executable)
.raw_arg(ordinary_parameters)
.creation_flags(0x08000000) // CREATE_NO_WINDOW only suppresses a console.
.status()
{
Ok(status) => std::process::exit(status.code().unwrap_or(1)),
Err(error) => crate::fail(&format!("Unable to reopen Orca: {error}")),
}
}
fn wide(value: &OsStr) -> Vec<u16> {
value.encode_wide().chain(std::iter::once(0)).collect()
}