remove fork and nix crates, use libc directly

doesn't seem to make any difference to binary size?
This commit is contained in:
2026-05-13 19:02:48 +01:00
parent 4370b6ea99
commit 0a5d7f83f6
3 changed files with 55 additions and 41 deletions
Generated
-29
View File
@@ -40,12 +40,6 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801"
[[package]]
name = "cfg_aliases"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "either"
version = "1.15.0"
@@ -58,15 +52,6 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
[[package]]
name = "fork"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bcc4b4161e53d499e41af904acb23950adf85682c772921ef3957cf1ecc98b3"
dependencies = [
"libc",
]
[[package]]
name = "libc"
version = "0.2.186"
@@ -86,10 +71,8 @@ dependencies = [
name = "luad"
version = "0.1.0"
dependencies = [
"fork",
"libc",
"mlua",
"nix",
]
[[package]]
@@ -126,18 +109,6 @@ dependencies = [
"pkg-config",
]
[[package]]
name = "nix"
version = "0.31.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d6d0705320c1e6ba1d912b5e37cf18071b6c2e9b7fa8215a1e8a7651966f5d3"
dependencies = [
"bitflags",
"cfg-if",
"cfg_aliases",
"libc",
]
[[package]]
name = "num-traits"
version = "0.2.19"
-2
View File
@@ -4,7 +4,5 @@ version = "0.1.0"
edition = "2024"
[dependencies]
fork = "0.7.0"
libc = "0.2.186"
mlua = { version = "0.11.6", features = ["lua53"] }
nix = { version = "0.31.2", features = ["process"] }
+55 -10
View File
@@ -23,11 +23,57 @@ use libc::{
pollfd,
poll,
POLLIN,
c_void
};
use nix::sys::wait::{wait,WaitStatus::{Exited,Signaled}};
c_void,
use fork::{Fork, fork};
// fork,
pid_t,
// wait,
c_int,
c_uint,
WIFEXITED,
WIFSIGNALED,
WEXITSTATUS,
WTERMSIG
};
enum WaitStatus {
Exited(pid_t, c_int),
Signaled(pid_t, c_int, bool),
}
use crate::WaitStatus::*;
enum Forked {
Parent(pid_t),
Child,
Err(std::io::Error)
}
fn fork() -> Forked {
let child_pid = unsafe { libc::fork() };
if child_pid > 0 {
Forked::Parent(child_pid)
} else if child_pid == 0 {
Forked::Child
} else {
Forked::Err(std::io::Error::last_os_error())
}
}
fn wait() -> Result<WaitStatus, std::io::Error> {
let mut wstatus : c_int = 0;
let pid = unsafe { libc::wait(&mut wstatus) };
if pid >= 0 {
if WIFEXITED(wstatus) {
return Ok(WaitStatus::Exited(pid, WEXITSTATUS(wstatus)))
} else if WIFSIGNALED(wstatus) {
return Ok(WaitStatus::Signaled(pid, WTERMSIG(wstatus), false))
}
}
Err(std::io::Error::last_os_error())
}
static mut sigchild_fd : i32 = -1;
@@ -47,8 +93,8 @@ fn fork_lua(lua : &Lua, args: Vec<String>, fds: Vec<i32>) -> Option<i32> {
let path = Path::new(pathname);
match fork() {
Ok(Fork::Parent(pid)) => Some(pid),
Ok(Fork::Child) => {
Forked::Parent(pid) => Some(pid),
Forked::Child => {
for (i, fd) in fds.iter().enumerate() {
unsafe { libc::dup2(*fd, i.try_into().unwrap()); }
};
@@ -69,7 +115,7 @@ fn fork_lua(lua : &Lua, args: Vec<String>, fds: Vec<i32>) -> Option<i32> {
},
};
},
Err(e) => {
Forked::Err(e) => {
println!("fork error {:?}", e);
None
}
@@ -177,10 +223,9 @@ fn main() -> std::io::Result<()> {
if let Some((pid, ret)) =
match wstatus {
Exited(pid, code) => Some((pid, code as u32)),
Signaled(pid, sig, _) => Some((pid, (sig as u32) + 128)),
_ => None
Signaled(pid, sig, _) => Some((pid, (sig as u32) + 128))
} {
if let Some(addr) = peers.get(&pid.as_raw()) {
if let Some(addr) = peers.get(&pid) {
sock.send_to_addr(&ret.to_be_bytes(), addr);
}
}