commit 0a5d7f83f6a30637cb39d539142e59f8dde985b6
parent 4370b6ea999223409870a161cc0c6c28bd50cdfc
Author: Daniel Barlow <dan@telent.net>
Date: Wed, 13 May 2026 19:02:48 +0100
remove fork and nix crates, use libc directly
doesn't seem to make any difference to binary size?
Diffstat:
3 files changed, 54 insertions(+), 40 deletions(-)
diff --git a/Cargo.lock b/Cargo.lock
@@ -41,12 +41,6 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -59,15 +53,6 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -86,10 +71,8 @@ dependencies = [
name = "luad"
version = "0.1.0"
dependencies = [
- "fork",
"libc",
"mlua",
- "nix",
]
[[package]]
@@ -127,18 +110,6 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
@@ -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"] }
diff --git a/src/main.rs b/src/main.rs
@@ -23,11 +23,57 @@ use libc::{
pollfd,
poll,
POLLIN,
- c_void
+ c_void,
+
+ // fork,
+ pid_t,
+
+ // wait,
+ c_int,
+ c_uint,
+
+ WIFEXITED,
+ WIFSIGNALED,
+ WEXITSTATUS,
+ WTERMSIG
};
-use nix::sys::wait::{wait,WaitStatus::{Exited,Signaled}};
-use fork::{Fork, fork};
+
+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);
}
}