Page MenuHomePhorge

fork.rs
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None
use global_placeholders::global;
use std::{ffi::CString, process::exit};
pub enum Fork {
Parent(libc::pid_t),
Child,
}
pub fn chdir() -> Result<libc::c_int, i32> {
let dir = CString::new(global!("pmc.base")).expect("CString::new failed");
let res = unsafe { libc::chdir(dir.as_ptr()) };
match res {
-1 => Err(-1),
res => Ok(res),
}
}
pub fn fork() -> Result<Fork, i32> {
let res = unsafe { libc::fork() };
match res {
-1 => Err(-1),
0 => Ok(Fork::Child),
res => Ok(Fork::Parent(res)),
}
}
pub fn setsid() -> Result<libc::pid_t, i32> {
let res = unsafe { libc::setsid() };
match res {
-1 => Err(-1),
res => Ok(res),
}
}
pub fn close_fd() -> Result<(), i32> {
match unsafe { libc::close(0) } {
-1 => Err(-1),
_ => match unsafe { libc::close(1) } {
-1 => Err(-1),
_ => match unsafe { libc::close(2) } {
-1 => Err(-1),
_ => Ok(()),
},
},
}
}
pub fn daemon(nochdir: bool, noclose: bool) -> Result<Fork, i32> {
match fork() {
Ok(Fork::Parent(_)) => exit(0),
Ok(Fork::Child) => setsid().and_then(|_| {
if !nochdir {
chdir()?;
}
if !noclose {
close_fd()?;
}
fork()
}),
Err(n) => Err(n),
}
}

File Metadata

Mime Type
text/plain
Expires
Tue, Dec 16, 11:34 AM (22 h, 31 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
484779
Default Alt Text
fork.rs (1 KB)

Event Timeline