//! Windows process, console, library and time-zone operations. #![allow(unsafe_code)] use std::ffi::{CStr, OsStr, OsString, c_void}; use std::io; use std::os::windows::ffi::{OsStrExt, OsStringExt}; use std::os::windows::io::{ AsHandle, AsRawHandle, BorrowedHandle, FromRawHandle, IntoRawHandle, OwnedHandle, RawHandle, }; use std::path::{Path, PathBuf}; use std::ptr::NonNull; use std::time::{Duration, SystemTime}; use windows_sys::Wdk::System::SystemServices::RtlGetVersion; use windows_sys::Win32::Foundation::{ ERROR_ACCESS_DENIED, FILETIME, FreeLibrary, STILL_ACTIVE, WAIT_FAILED, WAIT_OBJECT_0, }; use windows_sys::Win32::Globalization::GetUserDefaultLocaleName; use windows_sys::Win32::Storage::FileSystem::{ BY_HANDLE_FILE_INFORMATION, FILE_FLAG_BACKUP_SEMANTICS, FILE_READ_ATTRIBUTES, FILE_WRITE_ATTRIBUTES, GetFileInformationByHandle, }; use windows_sys::Win32::System::Console::{ CONSOLE_SCREEN_BUFFER_INFO, CTRL_BREAK_EVENT, CTRL_C_EVENT, ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_MOUSE_INPUT, ENABLE_PROCESSED_INPUT, ENABLE_PROCESSED_OUTPUT, ENABLE_VIRTUAL_TERMINAL_INPUT, ENABLE_VIRTUAL_TERMINAL_PROCESSING, ENABLE_WINDOW_INPUT, FlushConsoleInputBuffer, GetConsoleMode, GetConsoleScreenBufferInfo, GetStdHandle, STD_ERROR_HANDLE, STD_INPUT_HANDLE, SetConsoleCtrlHandler, SetConsoleMode, SetStdHandle, }; use windows_sys::Win32::System::LibraryLoader::{ GetProcAddress, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR, LOAD_LIBRARY_SEARCH_SYSTEM32, LoadLibraryExW, }; use windows_sys::Win32::System::Pipes::{CreatePipe, PeekNamedPipe}; use windows_sys::Win32::System::ProcessStatus::{K32GetProcessMemoryInfo, PROCESS_MEMORY_COUNTERS}; use windows_sys::Win32::System::SystemInformation::{GetSystemTimeAsFileTime, OSVERSIONINFOW}; use windows_sys::Win32::System::Threading::{ GetCurrentProcess, GetCurrentThread, GetExitCodeProcess, GetProcessTimes, GetThreadTimes, INFINITE, OpenProcess, PROCESS_ACCESS_RIGHTS, PROCESS_NAME_WIN32, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_TERMINATE, QueryFullProcessImageNameW, TerminateProcess, WaitForSingleObject, }; use windows_sys::core::BOOL; /// Whether process `pid` exists and has not exited. A process this user may not open exists, so it counts as alive. pub fn process_alive(pid: u32) -> bool { let process = match open(pid, PROCESS_QUERY_LIMITED_INFORMATION) { Ok(process) => process, Err(error) => return error.raw_os_error() == Some(ERROR_ACCESS_DENIED as i32), }; let mut code = 0; // SAFETY: `process` is an open handle with query access, and `code` outlives the call. let ok = unsafe { GetExitCodeProcess(process.as_raw_handle(), &mut code) }; // An exited process keeps its handle until the last one closes; its exit code tells it apart. ok == 0 || code == STILL_ACTIVE as u32 } /// Ends process `pid` with exit code 1 (`TerminateProcess`, what libuv does for every signal but 0). pub fn terminate(pid: u32) -> io::Result<()> { let process = open(pid, PROCESS_TERMINATE)?; // SAFETY: `process` is an open handle with terminate access. if unsafe { TerminateProcess(process.as_raw_handle(), 1) } == 0 { return Err(io::Error::last_os_error()); } Ok(()) } /// Makes the console (`CONIN$`) this process's standard input, for a process whose stdin was a pipe. The handle stays /// open for the life of the process; std and OpenTUI look the standard handle up on each read. pub fn set_stdin_console() -> io::Result<()> { let console = std::fs::OpenOptions::new().read(true).write(true).open("CONIN$")?.into_raw_handle(); // SAFETY: `console` is an open handle that is never closed. if unsafe { SetStdHandle(STD_INPUT_HANDLE, console) } == 0 { let error = io::Error::last_os_error(); // SAFETY: `SetStdHandle` failed, so nothing else holds `console`. drop(unsafe { OwnedHandle::from_raw_handle(console) }); return Err(error); } Ok(()) } /// The executable process `pid` runs, as a Win32 path (`QueryFullProcessImageNameW`). pub fn process_image(pid: u32) -> io::Result { let process = open(pid, PROCESS_QUERY_LIMITED_INFORMATION)?; // The longest path Win32 allows. let mut buffer = vec![0u16; 32_768]; let mut size = buffer.len() as u32; // SAFETY: `process` has query access, and `size` is the length of `buffer`, which outlives the call. let ok = unsafe { QueryFullProcessImageNameW(process.as_raw_handle(), PROCESS_NAME_WIN32, buffer.as_mut_ptr(), &mut size) }; if ok == 0 { return Err(io::Error::last_os_error()); } Ok(PathBuf::from(OsString::from_wide(&buffer[..size as usize]))) } /// This process's CPU time and age, from `GetProcessTimes`. pub struct ProcessTimes { /// User plus kernel time in seconds, as libuv's `uv_getrusage` adds them for `process.cpuUsage()`. pub cpu_seconds: f64, /// Milliseconds since the process was created. pub age_ms: f64, } pub fn process_times() -> Option { let [mut created, mut exited, mut kernel, mut user, mut now] = [FILETIME::default(); 5]; // SAFETY: the current process's pseudo handle needs no closing; the out-parameters outlive the call. let ok = unsafe { GetProcessTimes(GetCurrentProcess(), &mut created, &mut exited, &mut kernel, &mut user) }; if ok == 0 { return None; } // SAFETY: `now` outlives the call. unsafe { GetSystemTimeAsFileTime(&mut now) }; // A FILETIME counts 100 ns intervals. let ticks = |time: FILETIME| ((u64::from(time.dwHighDateTime) << 32) | u64::from(time.dwLowDateTime)) as f64; Some(ProcessTimes { cpu_seconds: (ticks(kernel) + ticks(user)) / 1e7, age_ms: (ticks(now) - ticks(created)) / 1e4 }) } /// The CPU time, user plus kernel, the calling thread has used, from `GetThreadTimes`. It advances by the scheduler's /// tick, not continuously. pub fn thread_cpu_time() -> Option { let [mut created, mut exited, mut kernel, mut user] = [FILETIME::default(); 4]; // SAFETY: the current thread's pseudo handle needs no closing; the out-parameters outlive the call. let ok = unsafe { GetThreadTimes(GetCurrentThread(), &mut created, &mut exited, &mut kernel, &mut user) }; if ok == 0 { return None; } // A FILETIME counts 100 ns intervals. let ticks = |time: FILETIME| (u64::from(time.dwHighDateTime) << 32) | u64::from(time.dwLowDateTime); Some(Duration::from_nanos((ticks(kernel) + ticks(user)).saturating_mul(100))) } /// This process's working set in bytes, which libuv's `uv_resident_set_memory` reports as the RSS. pub fn resident_memory() -> Option { let mut counters = PROCESS_MEMORY_COUNTERS { cb: size_of::() as u32, ..Default::default() }; // SAFETY: the pseudo handle needs no closing; `counters` is as large as `cb` says and outlives the call. let ok = unsafe { K32GetProcessMemoryInfo(GetCurrentProcess(), &mut counters, counters.cb) }; (ok != 0).then_some(counters.WorkingSetSize) } /// The working set of `child`, in bytes, while it runs. pub fn process_resident(child: &std::process::Child) -> Option { let mut counters = PROCESS_MEMORY_COUNTERS { cb: size_of::() as u32, ..Default::default() }; // SAFETY: the child's handle is open while `child` lives; `counters` is as large as `cb` says. let ok = unsafe { K32GetProcessMemoryInfo(child.as_raw_handle(), &mut counters, counters.cb) }; (ok != 0).then_some(counters.WorkingSetSize) } /// `os.release()`: `major.minor.build` from `RtlGetVersion`, as libuv's `uv_os_uname` reads it. Unlike /// `GetVersionExW`, it is not capped at 6.2 for an executable without a compatibility manifest. pub fn os_release() -> Option { let mut info = OSVERSIONINFOW { dwOSVersionInfoSize: size_of::() as u32, ..Default::default() }; // SAFETY: `info` is as large as its size field says and outlives the call. let status = unsafe { RtlGetVersion(&mut info) }; (status >= 0).then(|| format!("{}.{}.{}", info.dwMajorVersion, info.dwMinorVersion, info.dwBuildNumber)) } /// The user's default locale as a BCP 47 tag (`GetUserDefaultLocaleName`, the regional format the user set): `de-DE`, /// `sr-Latn-RS`. An alternate sort (`de-DE_phoneb`) is left out; `None` when Windows names none. pub fn user_locale() -> Option { // `LOCALE_NAME_MAX_LENGTH`, which windows-sys keeps under `Win32_System_SystemServices`. const MAX: usize = 85; let mut name = [0u16; MAX]; // SAFETY: `name` holds the `MAX` characters the call is told it may write. let written = unsafe { GetUserDefaultLocaleName(name.as_mut_ptr(), MAX as i32) }; let name = String::from_utf16(name.get(..(written as usize).checked_sub(1)?)?).ok()?; let tag = name.split('_').next().unwrap_or_default(); (!tag.is_empty()).then(|| tag.to_owned()) } /// Loads the library at `path`, or by name when it is not absolute (`LoadLibraryExW`). Each `/` becomes `\`, as the /// loader needs. Errors name the path. /// /// The library and its dependencies are searched for only in the application's directory, System32 and the /// directories added with `AddDllDirectory` (`LOAD_LIBRARY_SEARCH_DEFAULT_DIRS`), and for a library given by its /// absolute path also in that library's own directory (`LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR`, which needs a full path). /// The current directory and `PATH` are not searched: the current directory is the project, which must not supply a /// dependency of a plugin. /// /// # Safety /// /// Loading runs the library's initializers, which may do anything. pub unsafe fn load_library(path: &OsStr) -> io::Result> { let wide: Vec = path .encode_wide() .map(|unit| if unit == u16::from(b'/') { u16::from(b'\\') } else { unit }) .chain([0]) .collect(); let flags = match Path::new(path).is_absolute() { true => LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS, false => LOAD_LIBRARY_SEARCH_DEFAULT_DIRS, }; // SAFETY: `wide` is NUL-terminated and the reserved handle is null. The caller vouches for the initializers. let module = unsafe { LoadLibraryExW(wide.as_ptr(), std::ptr::null_mut(), flags) }; NonNull::new(module).ok_or_else(|| named(path.display(), io::Error::last_os_error())) } /// The address of `name` in `module` (`GetProcAddress`). Errors name the symbol. /// /// # Safety /// /// `module` must come from [`load_library`] and not be freed yet. pub unsafe fn library_symbol(module: NonNull, name: &CStr) -> io::Result> { // SAFETY: `module` is loaded and `name` is a C string. let address = unsafe { GetProcAddress(module.as_ptr(), name.as_ptr().cast()) }; address .and_then(|address| NonNull::new(address as *mut c_void)) .ok_or_else(|| named(name.to_string_lossy(), io::Error::last_os_error())) } /// Unloads `module` (`FreeLibrary`). /// /// # Safety /// /// `module` must come from [`load_library`], and nothing may use a symbol from it afterwards. pub unsafe fn free_library(module: NonNull) { // SAFETY: as the caller vouches. unsafe { FreeLibrary(module.as_ptr()) }; } fn named(name: impl std::fmt::Display, error: io::Error) -> io::Error { io::Error::new(error.kind(), format!("{name}: {error}")) } /// An anonymous pipe: the read end, then the write end. Child processes inherit neither. pub fn pipe() -> io::Result<(OwnedHandle, OwnedHandle)> { let (mut read, mut write) = (std::ptr::null_mut(), std::ptr::null_mut()); // SAFETY: the out-parameters outlive the call; without attributes the handles are not inheritable. if unsafe { CreatePipe(&mut read, &mut write, std::ptr::null(), 0) } == 0 { return Err(io::Error::last_os_error()); } // SAFETY: both handles are open and owned by nothing else. Ok(unsafe { (OwnedHandle::from_raw_handle(read), OwnedHandle::from_raw_handle(write)) }) } /// A handle put in place of the standard error by [`redirect_stderr`], until [`StderrRedirect::restore`]. pub struct StderrRedirect { /// The standard error before, open and owned by nobody here. previous: RawHandle, /// The handle in its place, open while it is the standard error. _handle: OwnedHandle, } // SAFETY: `previous` is only passed back to `SetStdHandle`, which accepts it from any thread. unsafe impl Send for StderrRedirect {} /// Makes `handle` this process's standard error. std looks the standard handle up on each write, so `eprintln!` /// follows it. pub fn redirect_stderr(handle: OwnedHandle) -> io::Result { // SAFETY: plain calls; the redirect keeps `handle` open while it is the standard error. unsafe { let previous = GetStdHandle(STD_ERROR_HANDLE); if SetStdHandle(STD_ERROR_HANDLE, handle.as_raw_handle()) == 0 { return Err(io::Error::last_os_error()); } Ok(StderrRedirect { previous, _handle: handle }) } } impl StderrRedirect { /// Gives the previous standard error back, then closes the handle that stood in for it. pub fn restore(self) { // SAFETY: `previous` was the standard error and is still open; it is not ours to close. unsafe { SetStdHandle(STD_ERROR_HANDLE, self.previous) }; } } /// How many bytes wait in the pipe `read`, without reading them (`PeekNamedPipe`). An error once every write end is /// closed and nothing waits (`ERROR_BROKEN_PIPE`). pub fn pipe_available(read: BorrowedHandle<'_>) -> io::Result { let mut available = 0; let null = std::ptr::null_mut(); // SAFETY: `read` is open, no bytes are copied, and `available` outlives the call. if unsafe { PeekNamedPipe(read.as_raw_handle(), null, 0, null.cast(), &mut available, null.cast()) } == 0 { return Err(io::Error::last_os_error()); } Ok(available) } /// From now on Ctrl+C and Ctrl+Break do nothing in this process, for a parent that waits for a child on the same /// console: the child gets them too, and the parent exits with the child's code. Other console events reach the /// handlers registered before. Unlike `SetConsoleCtrlHandler(NULL, TRUE)`, children do not inherit it. pub fn ignore_interrupts() -> io::Result<()> { // SAFETY: `interrupted` is a valid handler for the life of the process. if unsafe { SetConsoleCtrlHandler(Some(interrupted), 1) } == 0 { return Err(io::Error::last_os_error()); } Ok(()) } // The last handler registered runs first; `TRUE` stops the event there. unsafe extern "system" fn interrupted(event: u32) -> BOOL { BOOL::from(event == CTRL_C_EVENT || event == CTRL_BREAK_EVENT) } /// The console mode of `handle`, or `None` when it is not a console (libuv's test for a TTY). pub fn console_mode(handle: impl AsHandle) -> Option { let mut mode = 0; // SAFETY: plain call; an invalid or non-console handle makes it fail. (unsafe { GetConsoleMode(handle.as_handle().as_raw_handle(), &mut mode) } != 0).then_some(mode) } /// The visible columns and rows of the console `handle` draws on, or `None` when it is not a console. OpenTUI's /// `term::size` asks stdout only. pub fn console_size(handle: impl AsHandle) -> Option<(u16, u16)> { // SAFETY: an all-zero `CONSOLE_SCREEN_BUFFER_INFO` is valid plain data. let mut info: CONSOLE_SCREEN_BUFFER_INFO = unsafe { std::mem::zeroed() }; // SAFETY: plain call; an invalid or non-console handle makes it fail. if unsafe { GetConsoleScreenBufferInfo(handle.as_handle().as_raw_handle(), &mut info) } == 0 { return None; } let window = info.srWindow; let width = u16::try_from(i32::from(window.Right) - i32::from(window.Left) + 1).unwrap_or(0); let height = u16::try_from(i32::from(window.Bottom) - i32::from(window.Top) + 1).unwrap_or(0); Some((if width == 0 { 80 } else { width }, if height == 0 { 24 } else { height })) } pub fn set_console_mode(handle: impl AsHandle, mode: u32) -> io::Result<()> { // SAFETY: plain call; an invalid or non-console handle makes it fail. if unsafe { SetConsoleMode(handle.as_handle().as_raw_handle(), mode) } == 0 { return Err(io::Error::last_os_error()); } Ok(()) } /// Whether a child this process spawns inherits `file`'s handle; its value then names the same file there. pub fn set_inheritable(file: &std::fs::File, inherit: bool) -> io::Result<()> { use windows_sys::Win32::Foundation::{HANDLE_FLAG_INHERIT, SetHandleInformation}; let flags = if inherit { HANDLE_FLAG_INHERIT } else { 0 }; // SAFETY: a plain call on an open handle. if unsafe { SetHandleInformation(file.as_raw_handle(), HANDLE_FLAG_INHERIT, flags) } == 0 { return Err(io::Error::last_os_error()); } Ok(()) } /// A job whose processes end when its last handle closes: a parent that dies, however, takes its client along. pub struct KillOnCloseJob(OwnedHandle); impl KillOnCloseJob { pub fn new() -> io::Result { use windows_sys::Win32::System::JobObjects::{ CreateJobObjectW, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, JOBOBJECT_EXTENDED_LIMIT_INFORMATION, JobObjectExtendedLimitInformation, SetInformationJobObject, }; // SAFETY: an anonymous job; the handle is owned below. let job = unsafe { CreateJobObjectW(std::ptr::null(), std::ptr::null()) }; if job.is_null() { return Err(io::Error::last_os_error()); } // SAFETY: `CreateJobObjectW` returned a handle this function owns. let job = unsafe { OwnedHandle::from_raw_handle(job) }; // SAFETY: all-zero is a valid value of this plain struct. let mut limits: JOBOBJECT_EXTENDED_LIMIT_INFORMATION = unsafe { std::mem::zeroed() }; limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE; // SAFETY: the struct and its size match the information class. let set = unsafe { SetInformationJobObject( job.as_raw_handle(), JobObjectExtendedLimitInformation, std::ptr::from_ref(&limits).cast(), size_of::() as u32, ) }; if set == 0 { return Err(io::Error::last_os_error()); } Ok(KillOnCloseJob(job)) } /// Puts `child` in the job. pub fn assign(&self, child: &std::process::Child) -> io::Result<()> { use windows_sys::Win32::System::JobObjects::AssignProcessToJobObject; // SAFETY: both handles are open. if unsafe { AssignProcessToJobObject(self.0.as_raw_handle(), child.as_raw_handle()) } == 0 { return Err(io::Error::last_os_error()); } Ok(()) } } /// The server end of a duplex named pipe whose one client is a child's standard input ([`duplex`]): both directions of /// a protocol on one handle. Reads and writes are overlapped, each waited for, so a read pending on one thread never /// holds back a write on another, as a synchronous handle's would. pub struct Duplex(OwnedHandle); /// A duplex named pipe: this process's end, and the child's end to hand over as its standard input /// (`Stdio::from(OwnedHandle)`). It belongs to the child by construction: the name is random, its first instance is /// this one (`FILE_FLAG_FIRST_PIPE_INSTANCE`), it has one instance, only this user may open it, no remote client may, /// and its one client is connected here before anything else can be. Both ends are overlapped: Bun reads its stdin /// through libuv's overlapped path, and a synchronous end would serialize its pending read with its writes, which then /// wait for input (seen on Windows 11). pub fn duplex() -> io::Result<(Duplex, OwnedHandle)> { use std::hash::{BuildHasher, Hasher}; use windows_sys::Win32::Foundation::{ ERROR_PIPE_CONNECTED, GENERIC_READ, GENERIC_WRITE, INVALID_HANDLE_VALUE, LocalFree, }; use windows_sys::Win32::Security::Authorization::{ ConvertStringSecurityDescriptorToSecurityDescriptorW, SDDL_REVISION_1, }; use windows_sys::Win32::Security::{PSECURITY_DESCRIPTOR, SECURITY_ATTRIBUTES}; use windows_sys::Win32::Storage::FileSystem::{ CreateFileW, FILE_FLAG_FIRST_PIPE_INSTANCE, FILE_FLAG_OVERLAPPED, OPEN_EXISTING, PIPE_ACCESS_DUPLEX, }; use windows_sys::Win32::System::Pipes::{ ConnectNamedPipe, CreateNamedPipeW, PIPE_READMODE_BYTE, PIPE_REJECT_REMOTE_CLIENTS, PIPE_TYPE_BYTE, PIPE_WAIT, }; let random = || std::collections::hash_map::RandomState::new().build_hasher().finish(); let name = format!(r"\\.\pipe\opencode-js-host-{}-{:016x}{:016x}", std::process::id(), random(), random()); let name: Vec = OsStr::new(&name).encode_wide().chain([0]).collect(); // Full access for the object's owner (this user) alone, and no inherited entries. let sddl: Vec = OsStr::new("D:P(A;;GA;;;OW)").encode_wide().chain([0]).collect(); let mut descriptor: PSECURITY_DESCRIPTOR = std::ptr::null_mut(); // SAFETY: a NUL-terminated string and an out-parameter that outlives the call. let made = unsafe { ConvertStringSecurityDescriptorToSecurityDescriptorW( sddl.as_ptr(), SDDL_REVISION_1, &mut descriptor, std::ptr::null_mut(), ) }; if made == 0 { return Err(io::Error::last_os_error()); } let attributes = SECURITY_ATTRIBUTES { nLength: size_of::() as u32, lpSecurityDescriptor: descriptor, bInheritHandle: 0, }; // SAFETY: the name is NUL-terminated and the attributes outlive the call. let server = unsafe { CreateNamedPipeW( name.as_ptr(), PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS, 1, 64 << 10, 64 << 10, 0, &attributes, ) }; let error = io::Error::last_os_error(); // SAFETY: the descriptor came from the conversion above, and the pipe keeps its own copy. unsafe { LocalFree(descriptor.cast()) }; if server == INVALID_HANDLE_VALUE { return Err(error); } // SAFETY: an open handle owned by nothing else. let server = unsafe { OwnedHandle::from_raw_handle(server) }; // SAFETY: the name is NUL-terminated; without attributes the handle is not inheritable until std hands it over. let client = unsafe { CreateFileW( name.as_ptr(), GENERIC_READ | GENERIC_WRITE, 0, std::ptr::null(), OPEN_EXISTING, FILE_FLAG_OVERLAPPED, std::ptr::null_mut(), ) }; if client == INVALID_HANDLE_VALUE { return Err(io::Error::last_os_error()); } // SAFETY: an open handle owned by nothing else. let client = unsafe { OwnedHandle::from_raw_handle(client) }; // The client opened it already: the connection is made, and the one instance takes nobody else. let duplex = Duplex(server); let mut overlapped = Overlapped::new()?; // SAFETY: the handle is open and the OVERLAPPED outlives the call, which completes at once here. let connected = unsafe { ConnectNamedPipe(duplex.0.as_raw_handle(), &mut overlapped.0) }; if connected == 0 && io::Error::last_os_error().raw_os_error() != Some(ERROR_PIPE_CONNECTED as i32) { return Err(io::Error::last_os_error()); } Ok((duplex, client)) } /// An `OVERLAPPED` with its own manual-reset event, which it keeps open. struct Overlapped(windows_sys::Win32::System::IO::OVERLAPPED, #[allow(dead_code)] OwnedHandle); impl Overlapped { fn new() -> io::Result { use windows_sys::Win32::System::Threading::CreateEventW; // SAFETY: an anonymous manual-reset event, owned below. let event = unsafe { CreateEventW(std::ptr::null(), 1, 0, std::ptr::null()) }; if event.is_null() { return Err(io::Error::last_os_error()); } // SAFETY: all-zero is a valid OVERLAPPED; the event is open and owned by nothing else. let mut overlapped: windows_sys::Win32::System::IO::OVERLAPPED = unsafe { std::mem::zeroed() }; overlapped.hEvent = event; // SAFETY: `event` is open and nothing else owns it; the OVERLAPPED only borrows it. Ok(Self(overlapped, unsafe { OwnedHandle::from_raw_handle(event) })) } } impl Duplex { /// Reads what the child wrote, waiting for some; 0 once it closed its end or [`Duplex::disconnect`] ran. pub fn read(&self, buffer: &mut [u8]) -> io::Result { self.io(buffer.as_mut_ptr(), buffer.len(), false) } /// Writes some of `bytes`, waiting until the pipe takes them. pub fn write(&self, bytes: &[u8]) -> io::Result { self.io(bytes.as_ptr().cast_mut(), bytes.len(), true) } /// Ends the connection both ways: the child reads the end of its input, and a read pending here ends. pub fn disconnect(&self) { use windows_sys::Win32::System::IO::CancelIoEx; use windows_sys::Win32::System::Pipes::DisconnectNamedPipe; // SAFETY: plain calls on an open handle. unsafe { CancelIoEx(self.0.as_raw_handle(), std::ptr::null()); DisconnectNamedPipe(self.0.as_raw_handle()); } } fn io(&self, buffer: *mut u8, len: usize, write: bool) -> io::Result { use windows_sys::Win32::Foundation::{ ERROR_BROKEN_PIPE, ERROR_IO_PENDING, ERROR_OPERATION_ABORTED, ERROR_PIPE_NOT_CONNECTED, }; use windows_sys::Win32::Storage::FileSystem::{ReadFile, WriteFile}; use windows_sys::Win32::System::IO::GetOverlappedResult; let handle = self.0.as_raw_handle(); let len = len.min(u32::MAX as usize) as u32; let mut overlapped = Overlapped::new()?; let mut done = 0u32; // SAFETY: the buffer is valid for `len` bytes, and it and the OVERLAPPED outlive the operation, whose result is // waited for below before either goes. let started = unsafe { match write { true => WriteFile(handle, buffer, len, &mut done, &mut overlapped.0), false => ReadFile(handle, buffer, len, &mut done, &mut overlapped.0), } }; let mut error = (started == 0).then(io::Error::last_os_error); if error.as_ref().and_then(io::Error::raw_os_error) == Some(ERROR_IO_PENDING as i32) { // SAFETY: as above; the call waits for the operation's end. let waited = unsafe { GetOverlappedResult(handle, &overlapped.0, &mut done, 1) }; error = (waited == 0).then(io::Error::last_os_error); } match error.as_ref().and_then(io::Error::raw_os_error).map(|code| code as u32) { None => Ok(done as usize), // The other end closed, or this one disconnected: the end of the stream. Some(ERROR_BROKEN_PIPE | ERROR_PIPE_NOT_CONNECTED | ERROR_OPERATION_ABORTED) if !write => Ok(0), Some(_) => Err(error.unwrap_or_else(io::Error::last_os_error)), } } } /// A console control event that ends the session: the console closes, the user logs off, the system shuts down. pub fn ends_session(event: u32) -> bool { use windows_sys::Win32::System::Console::{CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, CTRL_SHUTDOWN_EVENT}; matches!(event, CTRL_CLOSE_EVENT | CTRL_LOGOFF_EVENT | CTRL_SHUTDOWN_EVENT) } /// Calls `handler` with each console control event, before the handlers registered earlier; it returns whether it /// handled the event. Once a process; later calls do nothing. pub fn on_console_control(handler: fn(u32) -> bool) -> io::Result<()> { static HANDLER: std::sync::OnceLock bool> = std::sync::OnceLock::new(); unsafe extern "system" fn route(event: u32) -> BOOL { BOOL::from(HANDLER.get().is_some_and(|handler| handler(event))) } if HANDLER.set(handler).is_err() { return Ok(()); } // SAFETY: `route` is a valid handler for the life of the process. if unsafe { SetConsoleCtrlHandler(Some(route), 1) } == 0 { return Err(io::Error::last_os_error()); } Ok(()) } /// Waits up to `timeout` for process `pid` to end, without taking its status. False when it still runs, or when it /// cannot be opened. pub fn wait_pid(pid: u32, timeout: Duration) -> bool { use windows_sys::Win32::System::Threading::PROCESS_SYNCHRONIZE; open(pid, PROCESS_SYNCHRONIZE).and_then(|process| wait_handle(process, Some(timeout))).unwrap_or(false) } /// The console modes of standard input and output, saved so a parent can put them back after a child that changed /// them died. pub struct ConsoleModes { input: Option, output: Option, } impl ConsoleModes { pub fn save() -> ConsoleModes { ConsoleModes { input: console_mode(io::stdin()), output: console_mode(io::stdout()) } } /// Turns on escape sequences on the console output, so a reset written next is interpreted, not printed. pub fn interpret_escapes(&self) { if let Some(mode) = self.output { let _ = set_console_mode(io::stdout(), vt_output_mode(mode)); } } /// Puts both modes back and drops the input the console holds: keys typed for a client that is gone. pub fn restore(&self) { if let Some(mode) = self.input { let _ = set_console_mode(io::stdin(), mode); // SAFETY: plain call on the standard input handle; a non-console handle makes it fail. unsafe { FlushConsoleInputBuffer(io::stdin().as_handle().as_raw_handle()) }; } if let Some(mode) = self.output { let _ = set_console_mode(io::stdout(), mode); } } } /// The raw input mode from a saved one, as libuv's `UV_TTY_MODE_RAW` takes it but with the console turning keys /// into escape sequences: no line editing, echo, Ctrl+C handling or mouse records; window size records on. Quick /// Edit and insert mode stay as they were. pub fn raw_input_mode(saved: u32) -> u32 { (saved & !(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT | ENABLE_MOUSE_INPUT)) | ENABLE_WINDOW_INPUT | ENABLE_VIRTUAL_TERMINAL_INPUT } /// The output mode with escape sequences interpreted, as libuv sets it on a console it writes to. `\n` keeps its /// carriage return: there is no `DISABLE_NEWLINE_AUTO_RETURN`. pub fn vt_output_mode(saved: u32) -> u32 { saved | ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING } /// Waits up to `timeout` (forever with `None`) for `handle` to be signaled, as console input is while it holds /// records. Consumes nothing. pub fn wait_handle(handle: impl AsHandle, timeout: Option) -> io::Result { let millis = timeout.map_or(INFINITE, |timeout| timeout.as_nanos().div_ceil(1_000_000).min(u128::from(INFINITE - 1)) as u32); // SAFETY: plain call; an invalid handle makes it fail. match unsafe { WaitForSingleObject(handle.as_handle().as_raw_handle(), millis) } { WAIT_FAILED => Err(io::Error::last_os_error()), result => Ok(result == WAIT_OBJECT_0), } } /// A time zone of the ICU that Windows ships (`icu.dll` since Windows 10 1903, `icuin.dll` since 1703), where Node /// and Bun take local times from: `TZ` as an IANA name, else the Windows zone with its whole rule history. The /// library is loaded from the system directory at first use, not imported, so a Windows without it still starts. pub struct IcuZone { calendar: NonNull, set_millis: SetMillis, get: Get, } // The entries of `ucal.h` it calls, and what `GetProcAddress` returns. type Open = unsafe extern "C" fn(*const u16, i32, *const std::ffi::c_char, i32, *mut i32) -> *mut c_void; type SetMillis = unsafe extern "C" fn(*mut c_void, f64, *mut i32); type Get = unsafe extern "C" fn(*const c_void, i32, *mut i32) -> i32; type Proc = unsafe extern "system" fn() -> isize; // SAFETY: an ICU calendar may move between threads; `&mut self` keeps it to one at a time. unsafe impl Send for IcuZone {} impl IcuZone { /// The zone named `name`, or ICU's default zone with `None`; `None` without ICU. ICU reads an unknown name as /// `Etc/Unknown`, which is UTC. pub fn open(name: Option<&str>) -> Option { const GREGORIAN: i32 = 1; let module = [c"icu.dll", c"icuin.dll"].iter().find_map(|library| { let wide: Vec = library.to_bytes().iter().map(|&byte| u16::from(byte)).chain([0]).collect(); // SAFETY: `wide` is NUL-terminated; Windows' own ICU initializes only itself. NonNull::new(unsafe { LoadLibraryExW(wide.as_ptr(), std::ptr::null_mut(), LOAD_LIBRARY_SEARCH_SYSTEM32) }) })?; // SAFETY: `module` is loaded and `name` is a C string. let symbol = |name: &CStr| unsafe { GetProcAddress(module.as_ptr(), name.as_ptr().cast()) }; // SAFETY: `module` stays loaded (it is never freed), and these are the signatures of `ucal.h`. let (open, set_millis, get) = unsafe { ( std::mem::transmute::(symbol(c"ucal_open")?), std::mem::transmute::(symbol(c"ucal_setMillis")?), std::mem::transmute::(symbol(c"ucal_get")?), ) }; let zone: Vec = name.map_or_else(Vec::new, |name| name.encode_utf16().collect()); let (id, len) = if name.is_some() { (zone.as_ptr(), zone.len() as i32) } else { (std::ptr::null(), 0) }; let mut status = 0; // SAFETY: `id` holds `len` units, or is null for the default zone; a null locale is the default one. let calendar = unsafe { open(id, len, std::ptr::null(), GREGORIAN, &mut status) }; NonNull::new(calendar).filter(|_| status <= 0).map(|calendar| Self { calendar, set_millis, get }) } /// Milliseconds east of UTC at `ms` since the epoch: the zone's raw offset plus its daylight saving. pub fn offset_ms(&mut self, ms: i64) -> Option { const ZONE_OFFSET: i32 = 15; const DST_OFFSET: i32 = 16; let mut status = 0; let calendar = self.calendar.as_ptr(); // SAFETY: `calendar` is open and only this `&mut self` uses it; `status` outlives each call. let offset = unsafe { (self.set_millis)(calendar, ms as f64, &mut status); (self.get)(calendar, ZONE_OFFSET, &mut status) + (self.get)(calendar, DST_OFFSET, &mut status) }; (status <= 0).then_some(offset) } } /// The file index of `path`, a file or a directory, which stands in for the inode: it tells a directory from one /// made again at its path, where the creation time does not (NTFS gives a name made again within 15 s its old /// creation time). pub fn file_index(path: &Path) -> io::Result { let file = open_path(path, FILE_READ_ATTRIBUTES)?; // SAFETY: an all-zero `BY_HANDLE_FILE_INFORMATION` is valid plain data. let mut info: BY_HANDLE_FILE_INFORMATION = unsafe { std::mem::zeroed() }; // SAFETY: `file` is open with attribute access, and `info` outlives the call. if unsafe { GetFileInformationByHandle(file.as_raw_handle(), &mut info) } == 0 { return Err(io::Error::last_os_error()); } Ok((u64::from(info.nFileIndexHigh) << 32) | u64::from(info.nFileIndexLow)) } /// Sets the modification time of `path`, a file or a directory. `File::set_modified` needs a handle with write /// access, which `File::open` does not give and a directory does not open with. pub fn set_modified(path: &Path, time: SystemTime) -> io::Result<()> { open_path(path, FILE_WRITE_ATTRIBUTES)?.set_modified(time) } // Opens `path` with `access` alone; `FILE_FLAG_BACKUP_SEMANTICS` lets it be a directory. fn open_path(path: &Path, access: u32) -> io::Result { use std::os::windows::fs::OpenOptionsExt; std::fs::OpenOptions::new().access_mode(access).custom_flags(FILE_FLAG_BACKUP_SEMANTICS).open(path) } fn open(pid: u32, access: PROCESS_ACCESS_RIGHTS) -> io::Result { // SAFETY: plain call; a null result is an error. let handle = unsafe { OpenProcess(access, 0, pid) }; if handle.is_null() { return Err(io::Error::last_os_error()); } // SAFETY: `handle` is open and owned by nothing else. Ok(unsafe { OwnedHandle::from_raw_handle(handle) }) } #[cfg(test)] mod tests { use super::*; use std::process::{Command, Stdio}; #[test] fn terminate_ends_a_live_process() { // `ping` waits a second between echoes; `timeout` needs a console on stdin. let mut child = Command::new("ping").args(["-n", "60", "127.0.0.1"]).stdout(Stdio::null()).spawn().unwrap(); assert!(process_alive(child.id())); terminate(child.id()).unwrap(); let status = child.wait().unwrap(); assert_eq!(status.code(), Some(1)); assert!(!process_alive(child.id())); } #[test] fn only_interrupts_are_ignored() { use windows_sys::Win32::System::Console::CTRL_CLOSE_EVENT; // SAFETY: the handler only compares its argument. let handled = |event| unsafe { interrupted(event) }; assert_eq!([CTRL_C_EVENT, CTRL_BREAK_EVENT, CTRL_CLOSE_EVENT].map(handled), [1, 1, 0]); ignore_interrupts().unwrap(); } #[test] fn console_modes() { let cooked = ENABLE_PROCESSED_INPUT | ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_MOUSE_INPUT | 0x40 | 0x80; // Quick Edit (0x40) and extended flags (0x80) stay as they were. assert_eq!(raw_input_mode(cooked), ENABLE_WINDOW_INPUT | ENABLE_VIRTUAL_TERMINAL_INPUT | 0x40 | 0x80); assert_eq!(vt_output_mode(0), ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING); let file = std::fs::File::open(std::env::current_exe().unwrap()).unwrap(); assert_eq!(console_mode(&file), None); assert!(set_console_mode(&file, 0).is_err()); } #[test] fn wait_handle_times_out_and_sees_a_signal() { let mut child = Command::new("ping").args(["-n", "60", "127.0.0.1"]).stdout(Stdio::null()).spawn().unwrap(); // A process handle is signaled once the process exits. assert!(!wait_handle(&child, Some(Duration::from_millis(10))).unwrap()); child.kill().unwrap(); assert!(wait_handle(&child, None).unwrap()); child.wait().unwrap(); } #[test] fn a_missing_process_is_gone() { // Process IDs are multiples of 4, so this one never exists; 0 is the idle process, which cannot be opened. assert!(!process_alive(u32::MAX - 2)); assert!(!process_alive(0)); assert!(terminate(u32::MAX - 2).is_err()); } #[test] fn process_image_is_this_executable() { let image = process_image(std::process::id()).unwrap(); assert_eq!( std::fs::canonicalize(image).unwrap(), std::fs::canonicalize(std::env::current_exe().unwrap()).unwrap() ); } #[test] fn this_process_has_times_memory_and_a_release() { // Both clocks advance in ticks of about 15.6 ms, but not in phase, so wait for each to tick. let times = process_times().unwrap(); assert!(times.cpu_seconds >= 0.0 && times.age_ms >= 0.0); let started = std::time::Instant::now(); let later = std::iter::repeat_with(|| process_times().unwrap()) .find(|later| { (later.cpu_seconds > times.cpu_seconds && later.age_ms > times.age_ms) || started.elapsed() > Duration::from_secs(5) }) .unwrap(); assert!(later.cpu_seconds > times.cpu_seconds && later.age_ms > times.age_ms); assert!(resident_memory().unwrap() > 0); let release = os_release().unwrap(); let parts: Vec = release.split('.').map(|part| part.parse().unwrap()).collect(); assert!(parts.len() == 3 && parts[0] >= 10, "{release}"); } // Expected (win-locale): the user's default locale is a BCP 47 tag, a language and its subtags, without a sort. #[test] fn the_user_locale_is_a_bcp_47_tag() { let locale = user_locale().unwrap(); let mut subtags = locale.split('-'); let language = subtags.next().unwrap(); assert!((2..=3).contains(&language.len()) && language.chars().all(|c| c.is_ascii_lowercase()), "{locale}"); assert!( subtags.all(|subtag| !subtag.is_empty() && subtag.chars().all(|c| c.is_ascii_alphanumeric())), "{locale}" ); } #[test] fn a_console_size_is_asked_of_the_handle_given() { // A redirected stdout does not hide the size of a stderr console. let (read, _write) = pipe().unwrap(); assert_eq!(console_size(read.as_handle()), None); if console_mode(std::io::stderr()).is_some() { assert!(console_size(std::io::stderr()).is_some_and(|(columns, rows)| columns > 0 && rows > 0)); } } #[test] fn icu_zones_read_iana_names_with_their_history() { let offset = |name: &str, ms: i64| IcuZone::open(Some(name)).unwrap().offset_ms(ms).unwrap(); assert_eq!(offset("America/New_York", 0), -18_000_000); // Summer time, and the 1986 change of Kathmandu's offset. assert_eq!(offset("America/New_York", 1_719_792_000_000), -14_400_000); assert_eq!(offset("Asia/Kathmandu", 0), 19_800_000); assert_eq!(offset("Asia/Kathmandu", 1_719_792_000_000), 20_700_000); assert_eq!(offset("No/Such_Zone", 0), 0); assert!(IcuZone::open(None).is_some()); } #[test] fn a_directory_made_again_has_another_index_and_takes_a_time() { let root = crate::temp::tempdir().unwrap(); let dir = root.path().join("d"); std::fs::create_dir(&dir).unwrap(); let first = file_index(&dir).unwrap(); assert_eq!(file_index(&dir).unwrap(), first); std::fs::remove_dir(&dir).unwrap(); std::fs::create_dir(&dir).unwrap(); assert_ne!(file_index(&dir).unwrap(), first); let old = SystemTime::now() - Duration::from_secs(3600); set_modified(&dir, old).unwrap(); assert_eq!(std::fs::metadata(&dir).unwrap().modified().unwrap(), old); } // Expected (js-stdio): the duplex pipe carries both ways at once, a read pending at either end holding back no write // at the other or its own (the client's end is overlapped, as the server's), and 4 MiB each way go without a // deadlock; a disconnect ends the client's input. #[test] fn a_duplex_pipe_carries_both_ways_at_once() { let (server, client) = duplex().unwrap(); let client = std::sync::Arc::new(Duplex(client)); let server = std::sync::Arc::new(server); // A read pending on the client's end, then a write on its end too: it goes through at once. let pending = { let client = client.clone(); std::thread::spawn(move || { let mut buffer = [0; 3]; client.read(&mut buffer).unwrap() }) }; std::thread::sleep(Duration::from_millis(100)); assert_eq!(client.write(b"up").unwrap(), 2); let mut buffer = [0; 2]; assert_eq!(server.read(&mut buffer).unwrap(), 2); assert_eq!(&buffer, b"up"); server.write(b"one").unwrap(); assert_eq!(pending.join().unwrap(), 3); // 4 MiB each way at once. let big = vec![b'x'; 4 << 20]; let pump = |from: std::sync::Arc, bytes: Vec| { std::thread::spawn(move || { let mut at = 0; while at < bytes.len() { at += from.write(&bytes[at..]).unwrap(); } }) }; let drain = |into: std::sync::Arc| { std::thread::spawn(move || { let (mut total, mut buffer) = (0, vec![0; 64 << 10]); while total < 4 << 20 { total += into.read(&mut buffer).unwrap(); } total }) }; let threads = [pump(server.clone(), big.clone()), pump(client.clone(), big)]; let read = [drain(server.clone()), drain(client.clone())]; threads.into_iter().for_each(|thread| thread.join().unwrap()); assert!(read.into_iter().all(|thread| thread.join().unwrap() == 4 << 20)); // The disconnect ends the client's input. server.disconnect(); let mut buffer = [0; 1]; assert!(client.read(&mut buffer).map_or(true, |read| read == 0)); } #[test] fn a_pipe_reports_what_waits_and_its_closed_writer() { use std::io::{Read, Write}; use std::os::windows::io::AsHandle; let (read, write) = pipe().unwrap(); assert_eq!(pipe_available(read.as_handle()).unwrap(), 0); std::fs::File::from(write).write_all(b"line\n").unwrap(); assert_eq!(pipe_available(read.as_handle()).unwrap(), 5); let mut text = String::new(); std::fs::File::from(read.try_clone().unwrap()).read_to_string(&mut text).unwrap(); assert_eq!(text, "line\n"); assert!(pipe_available(read.as_handle()).is_err()); } }