Skip to content

Commit

Permalink
Update bessd
Browse files Browse the repository at this point in the history
  • Loading branch information
eshikafe committed Oct 7, 2023
2 parents ae55e1e + 7b7c8f0 commit 49ae2b8
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
9 changes: 4 additions & 5 deletions src/bin/bessd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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;
Expand All @@ -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.
Expand Down
47 changes: 42 additions & 5 deletions src/core/bessd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,28 @@ 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
// that has not yet been loaded by the BESS daemon. kInheritanceLimit is
// 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() {
Expand Down Expand Up @@ -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::<u32>().unwrap() > 0 {
pid_result = (true, pid.parse::<u32>().unwrap());
}
}
pid_result
}

// Tries to acquire the daemon pidfile lock for the file open at the given fd.
Expand All @@ -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.
Expand Down Expand Up @@ -167,3 +198,9 @@ pub fn list_plugins() -> Vec<String> {
pub fn get_current_directory() -> String {
"".to_string()
}

// Read Files
fn read_file_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>> where P: AsRef<Path>, {
let file = File::open(filename)?;
Ok(io::BufReader::new(file).lines())
}

0 comments on commit 49ae2b8

Please sign in to comment.