diff --git a/Cargo.lock b/Cargo.lock index 90e550c..20fa719 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,6 +18,12 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "boxfnonce" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5988cb1d626264ac94100be357308f29ff7cbdd3b36bda27f450a4ee3f713426" + [[package]] name = "cc" version = "1.0.67" @@ -30,6 +36,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "daemonize" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70c24513e34f53b640819f0ac9f705b673fcf4006d7aab8778bee72ebfc89815" +dependencies = [ + "boxfnonce", + "libc", +] + [[package]] name = "futures-core" version = "0.3.14" @@ -112,6 +128,7 @@ name = "mister_ojd_server" version = "0.1.0" dependencies = [ "anyhow", + "daemonize", "inotify", "local_ipaddress", "nix", diff --git a/Cargo.toml b/Cargo.toml index b40c3a4..0da15fe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" [dependencies] anyhow = "1.0" +daemonize = "0.4" inotify = "0.9" local_ipaddress = "0.1" nix = "0.20" diff --git a/src/main.rs b/src/main.rs index 3040392..e410684 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use anyhow::{anyhow, Context, Result}; +use daemonize::Daemonize; use inotify::{Inotify, EventMask, WatchMask}; use nix::{ioctl_read, ioctl_read_buf}; use serde_json::{json, Value}; @@ -38,6 +39,10 @@ ioctl_read!(get_num_buttons, JSIOCG_MAGIC, JSIOCGBUTTONS, u8); ioctl_read_buf!(get_gamepad_name, JSIOCG_MAGIC, JSIOCGNAME, u8); fn main() { + if let Err(e) = daemonize_me() { + panic!("Unable to daemonize application: {}", e); + } + loop { println!("Waiting for Open Joystick Display"); @@ -78,6 +83,19 @@ fn main() { } } +fn daemonize_me() -> Result<()> { + let stdout = File::create("/tmp/ojd_server.out")?; + let stderr = File::create("/tmp/ojd_server.err")?; + + let daemon = Daemonize::new() + .stdout(stdout) + .stderr(stderr); + + let _ = daemon.start()?; + + Ok(()) +} + fn connect_to_ojd() -> Result { let mut ip_addr = local_ipaddress::get().context("Unable to retrieve local IP address")?; ip_addr.push_str(":56709");