| 1 |
// Copyright © 2017 Mozilla Foundation |
| 2 |
// |
| 3 |
// This program is made available under an ISC-style license. See the |
| 4 |
// accompanying file LICENSE for details |
| 5 |
|
| 6 |
#![warn(unused_extern_crates)] |
| 7 |
#![recursion_limit = "1024"] |
| 8 |
#[macro_use] |
| 9 |
extern crate error_chain; |
| 10 |
#[macro_use] |
| 11 |
extern crate log; |
| 12 |
|
| 13 |
pub mod codec; |
| 14 |
#[allow(deprecated)] |
| 15 |
pub mod errors; |
| 16 |
pub mod messages; |
| 17 |
pub mod shm; |
| 18 |
|
| 19 |
pub mod ipccore; |
| 20 |
pub mod rpccore; |
| 21 |
pub mod sys; |
| 22 |
|
| 23 |
pub use crate::messages::{ClientMessage, ServerMessage}; |
| 24 |
|
| 25 |
#[cfg(unix)] |
| 26 |
use std::os::unix::io::IntoRawFd; |
| 27 |
#[cfg(windows)] |
| 28 |
use std::os::windows::io::IntoRawHandle; |
| 29 |
|
| 30 |
use std::io::Result; |
| 31 |
|
| 32 |
// This must match the definition of |
| 33 |
// ipc::FileDescriptor::PlatformHandleType in Gecko. |
| 34 |
#[cfg(windows)] |
| 35 |
pub type PlatformHandleType = std::os::windows::raw::HANDLE; |
| 36 |
#[cfg(unix)] |
| 37 |
pub type PlatformHandleType = libc::c_int; |
| 38 |
|
| 39 |
// This stands in for RawFd/RawHandle. |
| 40 |
#[derive(Debug)] |
| 41 |
pub struct PlatformHandle(PlatformHandleType); |
| 42 |
|
| 43 |
pub const INVALID_HANDLE_VALUE: PlatformHandleType = -1isize as PlatformHandleType; |
| 44 |
|
| 45 |
#[cfg(unix)] |
| 46 |
fn valid_handle(handle: PlatformHandleType) -> bool { |
| 47 |
handle >= 0 |
| 48 |
} |
| 49 |
|
| 50 |
#[cfg(windows)] |
| 51 |
fn valid_handle(handle: PlatformHandleType) -> bool { |
| 52 |
handle != INVALID_HANDLE_VALUE && !handle.is_null() |
| 53 |
} |
| 54 |
|
| 55 |
impl PlatformHandle { |
| 56 |
pub fn new(raw: PlatformHandleType) -> PlatformHandle { |
| 57 |
assert!(valid_handle(raw)); |
| 58 |
PlatformHandle(raw) |
| 59 |
} |
| 60 |
|
| 61 |
#[cfg(windows)] |
| 62 |
pub fn from<T: IntoRawHandle>(from: T) -> PlatformHandle { |
| 63 |
PlatformHandle::new(from.into_raw_handle()) |
| 64 |
} |
| 65 |
|
| 66 |
#[cfg(unix)] |
| 67 |
pub fn from<T: IntoRawFd>(from: T) -> PlatformHandle { |
| 68 |
PlatformHandle::new(from.into_raw_fd()) |
| 69 |
} |
| 70 |
|
| 71 |
#[allow(clippy::missing_safety_doc)] |
| 72 |
pub unsafe fn into_raw(self) -> PlatformHandleType { |
| 73 |
let handle = self.0; |
| 74 |
std::mem::forget(self); |
| 75 |
handle |
| 76 |
} |
| 77 |
|
| 78 |
#[cfg(unix)] |
| 79 |
pub fn duplicate(h: PlatformHandleType) -> Result<PlatformHandle> { |
| 80 |
unsafe { |
| 81 |
let newfd = libc::dup(h); |
| 82 |
if !valid_handle(newfd) { |
| 83 |
return Err(std::io::Error::last_os_error()); |
| 84 |
} |
| 85 |
Ok(PlatformHandle::from(newfd)) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
#[allow(clippy::missing_safety_doc)] |
| 90 |
#[cfg(windows)] |
| 91 |
pub unsafe fn duplicate(h: PlatformHandleType) -> Result<PlatformHandle> { |
| 92 |
let dup = duplicate_platform_handle(h, None)?; |
| 93 |
Ok(PlatformHandle::new(dup)) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
impl Drop for PlatformHandle { |
| 98 |
fn drop(&mut self) { |
| 99 |
unsafe { close_platform_handle(self.0) } |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
#[cfg(unix)] |
| 104 |
unsafe fn close_platform_handle(handle: PlatformHandleType) { |
| 105 |
libc::close(handle); |
| 106 |
} |
| 107 |
|
| 108 |
#[cfg(windows)] |
| 109 |
use windows_sys::Win32::{ |
| 110 |
Foundation::{ |
| 111 |
CloseHandle, DuplicateHandle, DUPLICATE_CLOSE_SOURCE, DUPLICATE_SAME_ACCESS, FALSE, |
| 112 |
S_FALSE, S_OK, |
| 113 |
}, |
| 114 |
System::Com::{CoInitializeEx, COINIT_MULTITHREADED}, |
| 115 |
System::Threading::{GetCurrentProcess, OpenProcess, PROCESS_DUP_HANDLE}, |
| 116 |
}; |
| 117 |
|
| 118 |
#[cfg(windows)] |
| 119 |
unsafe fn close_platform_handle(handle: PlatformHandleType) { |
| 120 |
CloseHandle(handle as _); |
| 121 |
} |
| 122 |
|
| 123 |
// Duplicate `source_handle` to `target_pid`. Returns the value of the new handle inside the target process. |
| 124 |
// If `target_pid` is `None`, `source_handle` is duplicated in the current process. |
| 125 |
#[cfg(windows)] |
| 126 |
pub(crate) unsafe fn duplicate_platform_handle( |
| 127 |
source_handle: PlatformHandleType, |
| 128 |
target_pid: Option<u32>, |
| 129 |
) -> Result<PlatformHandleType> { |
| 130 |
let source_process = GetCurrentProcess(); |
| 131 |
let target_process = if let Some(pid) = target_pid { |
| 132 |
let target = OpenProcess(PROCESS_DUP_HANDLE, FALSE, pid); |
| 133 |
if !valid_handle(target as _) { |
| 134 |
return Err(std::io::Error::new( |
| 135 |
std::io::ErrorKind::Other, |
| 136 |
"invalid target process", |
| 137 |
)); |
| 138 |
} |
| 139 |
Some(target) |
| 140 |
} else { |
| 141 |
None |
| 142 |
}; |
| 143 |
|
| 144 |
let mut target_handle = windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE; |
| 145 |
let ok = DuplicateHandle( |
| 146 |
source_process, |
| 147 |
source_handle as _, |
| 148 |
target_process.unwrap_or(source_process), |
| 149 |
&mut target_handle, |
| 150 |
0, |
| 151 |
FALSE, |
| 152 |
DUPLICATE_SAME_ACCESS, |
| 153 |
); |
| 154 |
if let Some(target) = target_process { |
| 155 |
CloseHandle(target); |
| 156 |
}; |
| 157 |
if ok == FALSE { |
| 158 |
return Err(std::io::Error::new( |
| 159 |
std::io::ErrorKind::Other, |
| 160 |
"DuplicateHandle failed", |
| 161 |
)); |
| 162 |
} |
| 163 |
Ok(target_handle as _) |
| 164 |
} |
| 165 |
|
| 166 |
// Close `target_handle_to_close` inside target process `target_pid` using a |
| 167 |
// special invocation of `DuplicateHandle`. See |
| 168 |
// https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-duplicatehandle#:~:text=Normally%20the%20target,dwOptions%20to%20DUPLICATE_CLOSE_SOURCE. |
| 169 |
#[cfg(windows)] |
| 170 |
pub(crate) unsafe fn close_target_handle( |
| 171 |
target_handle_to_close: PlatformHandleType, |
| 172 |
target_pid: u32, |
| 173 |
) -> Result<()> { |
| 174 |
let target_process = OpenProcess(PROCESS_DUP_HANDLE, FALSE, target_pid); |
| 175 |
if !valid_handle(target_process as _) { |
| 176 |
return Err(std::io::Error::new( |
| 177 |
std::io::ErrorKind::Other, |
| 178 |
"invalid target process", |
| 179 |
)); |
| 180 |
} |
| 181 |
|
| 182 |
let ok = DuplicateHandle( |
| 183 |
target_process, |
| 184 |
target_handle_to_close as _, |
| 185 |
0, |
| 186 |
std::ptr::null_mut(), |
| 187 |
0, |
| 188 |
FALSE, |
| 189 |
DUPLICATE_CLOSE_SOURCE, |
| 190 |
); |
| 191 |
CloseHandle(target_process); |
| 192 |
if ok == FALSE { |
| 193 |
return Err(std::io::Error::new( |
| 194 |
std::io::ErrorKind::Other, |
| 195 |
"DuplicateHandle failed", |
| 196 |
)); |
| 197 |
} |
| 198 |
Ok(()) |
| 199 |
} |
| 200 |
|
| 201 |
#[cfg(windows)] |
| 202 |
pub fn server_platform_init() { |
| 203 |
unsafe { |
| 204 |
let r = CoInitializeEx(std::ptr::null_mut(), COINIT_MULTITHREADED as _); |
| 205 |
assert!(r == S_OK || r == S_FALSE); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
#[cfg(unix)] |
| 210 |
pub fn server_platform_init() {} |