|
| 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 | +} |
0 commit comments