#![allow(unsafe_code)]
use std::ffi::OsString;
use std::io;
use std::os::windows::ffi::{OsStrExt, OsStringExt};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::{ptr, thread};
use windows_sys::Win32::Foundation::{
CloseHandle, ERROR_ALREADY_EXISTS, ERROR_PIPE_BUSY, ERROR_PIPE_CONNECTED, GENERIC_WRITE,
GetLastError, HANDLE, INVALID_HANDLE_VALUE,
};
use windows_sys::Win32::Storage::FileSystem::{
CreateFileW, FILE_ATTRIBUTE_NORMAL, FILE_FLAG_FIRST_PIPE_INSTANCE, OPEN_EXISTING,
PIPE_ACCESS_INBOUND, ReadFile, WriteFile,
};
use windows_sys::Win32::System::Pipes::{
ConnectNamedPipe, CreateNamedPipeW, DisconnectNamedPipe, PIPE_READMODE_BYTE,
PIPE_REJECT_REMOTE_CLIENTS, PIPE_TYPE_BYTE, PIPE_UNLIMITED_INSTANCES, PIPE_WAIT,
WaitNamedPipeW,
};
use windows_sys::Win32::System::RemoteDesktop::ProcessIdToSessionId;
use windows_sys::Win32::System::Threading::{CreateMutexW, GetCurrentProcessId};
use crate::{Arrivals, FirstInstance, SingleInstance, this_launch};
const PATIENCE: Duration = Duration::from_secs(3);
pub(crate) fn claim(id: &str) -> io::Result<SingleInstance> {
let name = format!("waft-single-instance-{}-{}", sanitized(id), session());
let mutex_name = wide(&format!("Local\\{name}"));
let mutex = unsafe { CreateMutexW(ptr::null(), 0, mutex_name.as_ptr()) };
if mutex.is_null() {
return Err(io::Error::last_os_error());
}
let existed = unsafe { GetLastError() } == ERROR_ALREADY_EXISTS;
let pipe = wide(&format!(r"\\.\pipe\{name}"));
if existed {
unsafe { CloseHandle(mutex) };
hand_over(&pipe)?;
return Ok(SingleInstance::Second);
}
let arrivals = Arc::new(Arrivals::default());
let first = FirstInstance::new(Arc::clone(&arrivals), Box::new(Owned(mutex)));
thread::Builder::new()
.name("waft-single-instance".to_owned())
.spawn(move || listen(&pipe, &arrivals))?;
Ok(SingleInstance::First(first))
}
struct Owned(HANDLE);
unsafe impl Send for Owned {}
impl Drop for Owned {
fn drop(&mut self) {
unsafe { CloseHandle(self.0) };
}
}
fn sanitized(id: &str) -> String {
id.chars()
.map(|letter| {
if letter.is_ascii_alphanumeric() || matches!(letter, '.' | '-') {
letter
} else {
'_'
}
})
.collect()
}
fn session() -> u32 {
let mut session = 0;
unsafe { ProcessIdToSessionId(GetCurrentProcessId(), &mut session) };
session
}
fn wide(text: &str) -> Vec<u16> {
text.encode_utf16().chain([0]).collect()
}
fn hand_over(pipe: &[u16]) -> io::Result<()> {
let (arguments, current_dir) = this_launch();
let message = encode(&arguments, ¤t_dir.into_os_string());
let deadline = Instant::now() + PATIENCE;
let handle = loop {
let handle = unsafe {
CreateFileW(
pipe.as_ptr(),
GENERIC_WRITE,
0,
ptr::null(),
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
ptr::null_mut(),
)
};
if handle != INVALID_HANDLE_VALUE {
break Owned(handle);
}
let error = unsafe { GetLastError() };
if Instant::now() >= deadline {
return Err(io::Error::from_raw_os_error(error as i32));
}
if error == ERROR_PIPE_BUSY {
unsafe { WaitNamedPipeW(pipe.as_ptr(), 500) };
} else {
thread::sleep(Duration::from_millis(50));
}
};
let mut written = 0;
while written < message.len() {
let mut wrote = 0;
let rest = &message[written..];
let ok = unsafe {
WriteFile(
handle.0,
rest.as_ptr(),
rest.len() as u32,
&mut wrote,
ptr::null_mut(),
)
};
if ok == 0 {
return Err(io::Error::last_os_error());
}
written += wrote as usize;
}
Ok(())
}
fn listen(pipe: &[u16], arrivals: &Arc<Arrivals>) {
loop {
let instance = unsafe {
CreateNamedPipeW(
pipe.as_ptr(),
PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS,
PIPE_UNLIMITED_INSTANCES,
0,
64 * 1024,
0,
ptr::null(),
)
};
if instance == INVALID_HANDLE_VALUE {
return;
}
let instance = Owned(instance);
let connected = unsafe { ConnectNamedPipe(instance.0, ptr::null_mut()) } != 0
|| unsafe { GetLastError() } == ERROR_PIPE_CONNECTED;
if connected {
if let Some((arguments, current_dir)) = decode(&read_all(&instance)) {
arrivals.arrive(arguments, PathBuf::from(current_dir));
}
unsafe { DisconnectNamedPipe(instance.0) };
}
}
}
fn read_all(instance: &Owned) -> Vec<u8> {
let mut message = Vec::new();
let mut chunk = [0u8; 4096];
loop {
let mut read = 0;
let ok = unsafe {
ReadFile(
instance.0,
chunk.as_mut_ptr(),
chunk.len() as u32,
&mut read,
ptr::null_mut(),
)
};
if ok == 0 || read == 0 {
return message;
}
message.extend_from_slice(&chunk[..read as usize]);
}
}
fn encode(arguments: &[OsString], current_dir: &OsString) -> Vec<u8> {
let mut message = Vec::new();
message.extend_from_slice(&(arguments.len() as u32).to_le_bytes());
for text in arguments.iter().chain([current_dir]) {
let units: Vec<u16> = text.encode_wide().collect();
message.extend_from_slice(&(units.len() as u32).to_le_bytes());
for unit in units {
message.extend_from_slice(&unit.to_le_bytes());
}
}
message
}
fn decode(message: &[u8]) -> Option<(Vec<OsString>, OsString)> {
let mut rest = message;
let number = |rest: &mut &[u8]| -> Option<u32> {
let (bytes, after) = rest.split_first_chunk::<4>()?;
*rest = after;
Some(u32::from_le_bytes(*bytes))
};
let count = number(&mut rest)?;
let mut texts = Vec::new();
for _ in 0..=count {
let units = number(&mut rest)? as usize;
let (bytes, after) = rest.split_at_checked(units * 2)?;
rest = after;
let units: Vec<u16> = bytes
.as_chunks::<2>()
.0
.iter()
.map(|pair| u16::from_le_bytes(*pair))
.collect();
texts.push(OsString::from_wide(&units));
}
let current_dir = texts.pop()?;
Some((texts, current_dir))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_message_carries_the_arguments_and_the_folder() {
let arguments = vec![
OsString::from("notes.txt"),
OsString::from("https://example.org"),
];
let folder = OsString::from(r"C:\Users\me");
let message = encode(&arguments, &folder);
assert_eq!(decode(&message), Some((arguments, folder)));
assert_eq!(decode(&message[..message.len() - 1]), None);
}
}