Files
orca/native/windows-cli-launcher/src/administrator_status.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

50 lines
1.4 KiB
Rust

use std::ffi::c_void;
#[link(name = "kernel32")]
extern "system" {
fn GetCurrentProcess() -> *mut c_void;
fn CloseHandle(handle: *mut c_void) -> i32;
}
#[link(name = "advapi32")]
extern "system" {
fn OpenProcessToken(process: *mut c_void, access: u32, token: *mut *mut c_void) -> i32;
fn GetTokenInformation(
token: *mut c_void,
class: u32,
buffer: *mut c_void,
size: u32,
returned: *mut u32,
) -> i32;
}
pub fn run_if_requested() {
if std::env::args_os().nth(1).as_deref()
!= Some(std::ffi::OsStr::new("--orca-administrator-status"))
{
return;
}
let mut token = std::ptr::null_mut();
let mut elevated = 0u32;
let mut returned = 0;
// SAFETY: TOKEN_QUERY reads this asInvoker child's token, inherited from the calling desktop.
unsafe {
if OpenProcessToken(GetCurrentProcess(), 8, &mut token) == 0 {
crate::fail("Unable to query Windows administrator status");
}
let ok = GetTokenInformation(
token,
20, // TokenElevation
&mut elevated as *mut u32 as *mut c_void,
std::mem::size_of::<u32>() as u32,
&mut returned,
);
CloseHandle(token);
if ok == 0 || returned != std::mem::size_of::<u32>() as u32 {
crate::fail("Unable to query Windows administrator status");
}
}
println!("{}", elevated != 0);
std::process::exit(0);
}