use std::io;
use std::path::Path;
#[cfg(unix)]
pub type Connection = std::os::unix::net::UnixStream;
#[cfg(windows)]
pub type Connection = std::fs::File;
pub fn connect(path: &Path) -> io::Result<Connection> {
#[cfg(unix)]
{
Connection::connect(path)
}
#[cfg(windows)]
{
std::fs::OpenOptions::new()
.read(true)
.write(true)
.open(path)
}
}
#[cfg(unix)]
pub fn set_timeouts(connection: &Connection, timeout: std::time::Duration) {
let _ = connection.set_read_timeout(Some(timeout));
let _ = connection.set_write_timeout(Some(timeout));
}
#[cfg(windows)]
pub fn set_timeouts(_connection: &Connection, _timeout: std::time::Duration) {}
#[cfg(unix)]
pub fn clear_timeouts(connection: &Connection) {
let _ = connection.set_read_timeout(None);
let _ = connection.set_write_timeout(None);
}
#[cfg(windows)]
pub fn clear_timeouts(_connection: &Connection) {}
#[cfg(unix)]
pub fn detach(command: &mut std::process::Command) {
use std::os::unix::process::CommandExt;
unsafe {
command.pre_exec(|| {
if libc::setsid() == -1 {
return Err(io::Error::last_os_error());
}
Ok(())
});
}
}
#[cfg(windows)]
pub fn detach(command: &mut std::process::Command) {
use std::os::windows::process::CommandExt;
const DETACHED_PROCESS: u32 = 0x0000_0008;
const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
command.creation_flags(DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP);
}
pub fn hide_window(command: &mut std::process::Command) {
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
const CREATE_NO_WINDOW: u32 = 0x0800_0000;
const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
command.creation_flags(CREATE_NO_WINDOW | CREATE_NEW_PROCESS_GROUP);
}
#[cfg(unix)]
{
detach(command);
}
}
#[cfg(target_os = "linux")]
pub fn die_with_parent(command: &mut std::process::Command) {
use std::os::unix::process::CommandExt;
unsafe {
command.pre_exec(|| {
if libc::prctl(libc::PR_SET_PDEATHSIG, libc::SIGTERM) == -1 {
return Err(io::Error::last_os_error());
}
Ok(())
});
}
}
#[cfg(not(target_os = "linux"))]
pub fn die_with_parent(_command: &mut std::process::Command) {}
#[cfg(target_os = "linux")]
fn process_name_matches(pid: u32) -> bool {
std::fs::read_to_string(format!("/proc/{pid}/comm"))
.map(|comm| comm.trim().contains("mfe"))
.unwrap_or(false)
}
#[cfg(target_os = "macos")]
fn process_name_matches(pid: u32) -> bool {
std::process::Command::new("ps")
.args(["-p", &pid.to_string(), "-o", "comm="])
.output()
.map(|out| String::from_utf8_lossy(&out.stdout).contains("mfe"))
.unwrap_or(false)
}
#[cfg(windows)]
fn process_name_matches(pid: u32) -> bool {
std::process::Command::new("tasklist")
.args(["/FI", &format!("PID eq {pid}"), "/NH"])
.output()
.map(|out| {
let text = String::from_utf8_lossy(&out.stdout).to_lowercase();
text.contains("mfe-player") && !text.contains("no tasks")
})
.unwrap_or(false)
}
#[cfg(not(any(target_os = "linux", target_os = "macos", windows)))]
fn process_name_matches(_pid: u32) -> bool {
true
}
pub fn is_player_pid(pid: u32) -> bool {
if pid == 0 || pid == std::process::id() || !pid_alive(pid) {
return false;
}
process_name_matches(pid)
}
pub fn release_memory() {
#[cfg(all(target_os = "linux", target_env = "gnu"))]
{
unsafe {
libc::malloc_trim(0);
}
}
}
#[cfg(unix)]
pub fn pid_alive(pid: u32) -> bool {
if pid == 0 || pid == std::process::id() {
return false;
}
unsafe { libc::kill(pid as libc::pid_t, 0) == 0 }
}
#[cfg(windows)]
pub fn pid_alive(pid: u32) -> bool {
if pid == 0 || pid == std::process::id() {
return false;
}
let output = std::process::Command::new("tasklist")
.args(["/FI", &format!("PID eq {pid}"), "/NH"])
.output();
match output {
Ok(output) => String::from_utf8_lossy(&output.stdout).contains(&pid.to_string()),
Err(_) => false,
}
}
pub fn terminate(pid: u32) {
#[cfg(unix)]
unsafe {
libc::kill(pid as libc::pid_t, libc::SIGTERM);
}
#[cfg(windows)]
{
let _ = std::process::Command::new("taskkill")
.args(["/PID", &pid.to_string()])
.output();
}
}
pub fn kill(pid: u32) {
#[cfg(unix)]
unsafe {
libc::kill(pid as libc::pid_t, libc::SIGKILL);
}
#[cfg(windows)]
{
let _ = std::process::Command::new("taskkill")
.args(["/PID", &pid.to_string(), "/F"])
.output();
}
}