diff --git a/Cargo.toml b/Cargo.toml index a9952df..4411e07 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ prost-types = "0.11" tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] } chrono = "0.4.22" lazy_static = "1.4.0" +daemonize="0.4.1" [build-dependencies] # time = "0.1" diff --git a/README.md b/README.md index 3125187..fb85bd1 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,10 @@ A Rust implementation of [BESS(Berkeley Extensible Software Switch)](https://git - Python 3+ (for bessctl) - `sudo apt update && sudo apt upgrade -y` - `sudo apt install -y protobuf-compiler libprotobuf-dev` +<<<<<<< HEAD +======= +- Install python3 (Python 3.8.10 is used for development) +>>>>>>> 7b7c8f0af7cdd06bcfd03f85ab000bdcc2107df6 - `pip install --user protobuf==3.20.1 grpcio==1.46.0 grpcio-tools==1.46.0` - `pip install --user scapy` diff --git a/src/bin/bessd.rs b/src/bin/bessd.rs index 90f9374..411f8eb 100644 --- a/src/bin/bessd.rs +++ b/src/bin/bessd.rs @@ -16,10 +16,6 @@ async fn main() { // google::SetVersionString(VERSION); // google::SetUsageMessage("BESS Command Line Options:"); // google::ParseCommandLineFlags(&argc, &argv, true); - - bessd::process_command_line_args(); - bessd::check_running_as_root(); - // let pidfile_fd = bessd::CheckUniqueInstance(flag.i); // ignore_result(bessd::SetResourceLimit()); @@ -28,7 +24,7 @@ async fn main() { info!("Launching BESS daemon in process mode..."); } else { info!("Launching BESS daemon in background..."); - + bessd::daemonize(); // if flags.logtostderr == true || flags.alsologtostderr == true { // flags.logtostderr = false; // flags.alsologtostderr = false; @@ -37,6 +33,9 @@ async fn main() { // signal_fd = bessd::daemonize(); } + bessd::process_command_line_args(); + bessd::check_running_as_root(); + info!("bessd {}", option_env!("CARGO_PKG_VERSION").unwrap()); // Store our PID (child's, if daemonized) in the PID file. diff --git a/src/core/bessd.rs b/src/core/bessd.rs index eb4b1f0..2ea6654 100644 --- a/src/core/bessd.rs +++ b/src/core/bessd.rs @@ -35,14 +35,18 @@ use clap::Parser; use env_logger::fmt::Color; use env_logger::{Builder, Target, WriteStyle}; use exitcode; +use exitcode::OK; use log::*; -use std::io::Write; +use std::io::{self, BufRead,Write}; use std::process::exit; +use std::path::Path; use libc::*; use nix::*; // #include "port.h" +use std::{fs::File}; +use daemonize::Daemonize; // Utility routines for the main bess daemon. // When Modules extend other Modules, they may reference a shared object @@ -50,6 +54,9 @@ use nix::*; // the number of passes that will be made while loading Module shared objects, // and thus the maximum inheritance depth of any Module. pub const K_INHERITANCE_LIMIT: u32 = 10; +pub const STD_OUT_FILE_PATH:&str = "/tmp/bessd.out"; +pub const STD_ERR_FILE_PATH:&str = "/tmp/bessd.err"; +pub const PID_FILE_PATH:&str = "/tmp/bessd2.pid"; // Process command line arguments from gflags. pub fn process_command_line_args() { @@ -112,8 +119,14 @@ pub fn write_pid_file(fd: u32, pid: u32) {} // Read the pid value from the given file fd. Returns true and the read pid // value upon success. Returns false upon failure. -pub fn read_pid_file(fd: u32) -> (bool, u32) { - (false, 0) +pub fn read_pid_file() -> (bool, u32) { + let mut pid_result = (false, 0); + if let Ok(pid) = read_file_lines(PID_FILE_PATH).unwrap().into_iter().nth(0).unwrap(){ + if pid != "" || pid.parse::().unwrap() > 0 { + pid_result = (true, pid.parse::().unwrap()); + } + } + pid_result } // Tries to acquire the daemon pidfile lock for the file open at the given fd. @@ -132,8 +145,26 @@ pub fn check_unique_instance(pidfile_path: &str) -> u32 { } // Starts BESS as a daemon running in the background. -pub fn daemonize() -> i32 { - 0 +pub fn daemonize() -> u32 { + let std_out = File::create(STD_OUT_FILE_PATH).unwrap(); + let std_err = File::create(STD_ERR_FILE_PATH).unwrap(); + let daemonize = Daemonize::new() + .pid_file(PID_FILE_PATH) // Every method except `new` and `start` + .chown_pid_file(false) // is optional, see `Daemonize` documentation + .working_directory("/tmp") // for default behaviour. + .group("daemon") // Group name + .user("unknown") + .group(2) // or group id. + .umask(0o777) // Set umask, `0o027` by default. + .stdout(std_out) // Redirect stdout to `/tmp/daemon.out`. + .stderr(std_err) // Redirect stderr to `/tmp/daemon.err`. + .privileged_action(|| "Executed before drop privileges"); + + match daemonize.start() { + Ok(_) => println!("Success, started daemon successfully"), + Err(e) => eprintln!("Error, {}", e), + } + read_pid_file().1 } // Sets BESS's resource limit. Returns true upon success. @@ -167,3 +198,9 @@ pub fn list_plugins() -> Vec { pub fn get_current_directory() -> String { "".to_string() } + +// Read Files +fn read_file_lines

(filename: P) -> io::Result>> where P: AsRef, { + let file = File::open(filename)?; + Ok(io::BufReader::new(file).lines()) +} \ No newline at end of file