Skip to content

Commit

Permalink
Make the server run as a daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
FenrirWolf committed Jul 1, 2021
1 parent f1a21e4 commit 3441b69
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"

[dependencies]
anyhow = "1.0"
daemonize = "0.4"
inotify = "0.9"
local_ipaddress = "0.1"
nix = "0.20"
Expand Down
18 changes: 18 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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<TcpStream> {
let mut ip_addr = local_ipaddress::get().context("Unable to retrieve local IP address")?;
ip_addr.push_str(":56709");
Expand Down

0 comments on commit 3441b69

Please sign in to comment.