Skip to content

Commit b60fcf8

Browse files
committed
feat(ctl): make netpulsed into netpulsectl, a program that starts and stops the daemon
1 parent af55dc0 commit b60fcf8

File tree

4 files changed

+122
-65
lines changed

4 files changed

+122
-65
lines changed

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ zstd = { version = "0.13.2", optional = true }
2828
name = "netpulse"
2929
path = "src/bins/netpulse.rs"
3030

31-
[[bin]] # daemon
32-
name = "netpulsed"
33-
path = "src/bins/netpulsed.rs"
31+
[[bin]] # daemon and controlling it
32+
name = "netpulsectl"
33+
path = "src/bins/netpulsectl.rs"

src/bins/daemon.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::time::{self, Duration, UNIX_EPOCH};
2+
3+
use netpulse::records::{Check, CheckFlag};
4+
use netpulse::store::Store;
5+
6+
// TODO: better error handling, keep going even if everything goes boom
7+
pub(crate) fn daemon() {
8+
println!("starting daemon...");
9+
let mut store = Store::load_or_create().expect("boom");
10+
loop {
11+
let time = time::SystemTime::now();
12+
println!("making a check...");
13+
store.add_check(Check::new(
14+
time,
15+
if time.duration_since(UNIX_EPOCH).unwrap().as_secs() % 10 == 0 {
16+
CheckFlag::Timeout | CheckFlag::TypePing
17+
} else {
18+
CheckFlag::Success.into()
19+
},
20+
None,
21+
));
22+
println!("saving...");
23+
store.save().expect("could not save");
24+
println!("done! sleeping...");
25+
std::thread::sleep(Duration::from_secs(5));
26+
}
27+
}

src/bins/netpulsectl.rs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
use core::panic;
2+
use std::fs::{self, File};
3+
4+
use daemonize::Daemonize;
5+
use getopts::Options;
6+
use netpulse::store::Store;
7+
use netpulse::{DAEMON_LOG_ERR, DAEMON_LOG_INF};
8+
9+
mod daemon;
10+
use daemon::daemon;
11+
12+
fn main() {
13+
let args: Vec<String> = std::env::args().collect();
14+
let program = &args[0];
15+
let mut opts = Options::new();
16+
opts.optflag("h", "help", "print this help menu");
17+
opts.optflag("s", "start", "start the netpulse daemon");
18+
opts.optflag("i", "info", "info about the running netpulse daemon");
19+
opts.optflag("e", "end", "stop the running netpulse daemon");
20+
let matches = match opts.parse(&args[1..]) {
21+
Ok(m) => m,
22+
Err(f) => {
23+
panic!("{}", f.to_string())
24+
}
25+
};
26+
27+
if matches.opt_present("help") {
28+
print_usage(&program, opts);
29+
} else if matches.opt_present("start") {
30+
startd();
31+
} else if matches.opt_present("info") {
32+
infod();
33+
} else if matches.opt_present("end") {
34+
endd();
35+
} else {
36+
print_usage(&program, opts);
37+
}
38+
}
39+
40+
fn infod() {
41+
todo!()
42+
}
43+
44+
fn endd() {
45+
todo!()
46+
}
47+
48+
fn print_usage(program: &str, opts: Options) {
49+
let brief = format!("Usage: {} [options]", program);
50+
print!("{}", opts.usage(&brief));
51+
}
52+
53+
fn startd() {
54+
let path = Store::path();
55+
let parent_path = path.parent().expect("store file has no parent directory");
56+
println!("Parent: {parent_path:?}");
57+
58+
let logfile = File::create(DAEMON_LOG_INF).expect("could not open info logfile");
59+
let errfile = File::create(DAEMON_LOG_ERR).expect("could not open error logfile");
60+
61+
fs::create_dir_all(parent_path).expect("could not create the store directory");
62+
63+
let daemonize = Daemonize::new()
64+
.pid_file("/run/netpulse.pid")
65+
.chown_pid_file(true)
66+
.working_directory(parent_path)
67+
.user("netpulse")
68+
.group("netpulse")
69+
.stdout(logfile)
70+
.stderr(errfile)
71+
.umask(0o027); // rwxr-x---
72+
73+
println!("daemon setup done");
74+
75+
let outcome = daemonize.execute();
76+
match outcome {
77+
daemonize::Outcome::Parent(result) => {
78+
if result.is_ok() {
79+
println!("netpulsed was started");
80+
} else {
81+
eprintln!("error while starting netpulsed: {result:#?}");
82+
}
83+
}
84+
daemonize::Outcome::Child(result) => {
85+
if result.is_ok() {
86+
daemon();
87+
} else {
88+
panic!("error while starting the daemon: {result:#?}")
89+
}
90+
}
91+
}
92+
}

src/bins/netpulsed.rs

Lines changed: 0 additions & 62 deletions
This file was deleted.

0 commit comments

Comments
 (0)