Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daemon Hooking #353

Merged
merged 29 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
59d8452
Bind to socket
Camerooooon Jun 21, 2022
e0249e4
Hello world broadcast!
Camerooooon Jun 24, 2022
26df080
Retreive data from running daemon through UnixSocket
Camerooooon Jun 26, 2022
4dd14e4
Cargo format
Camerooooon Jun 26, 2022
3058a56
Send formatted packet
Camerooooon Jun 26, 2022
2c2ee16
Add tests for network parsing and other tests
Camerooooon Jun 27, 2022
72cde24
Add more tests for packet parsing
Camerooooon Jun 27, 2022
321ccf8
Remove obvious comments
Camerooooon Jun 27, 2022
c06ab48
Packet response working
Camerooooon Jun 28, 2022
6a3c674
Echo hello in HelloResponse packet
Camerooooon Jun 28, 2022
5a384ea
Cargo fmt
Camerooooon Jun 28, 2022
7087334
Fix up tests
Camerooooon Jun 28, 2022
6f5f893
Move network listening logic out of daemon file
Camerooooon Jun 28, 2022
9fef9cf
Allow multiple concurrent users to connect to the listener
Camerooooon Jun 28, 2022
3c2c65b
Add more error handling
Camerooooon Jun 28, 2022
393c14e
Send out hello packet upon monit init
Camerooooon Jun 30, 2022
d8f3473
Fix up wrnings
Camerooooon Jun 30, 2022
447f3be
Add missing import for test
Camerooooon Jun 30, 2022
39d09d7
Cargo fomat
Camerooooon Jun 30, 2022
8d78b25
Talking and responding packets!
Camerooooon Jul 2, 2022
ec78fe9
Cargo fmt
Camerooooon Jul 2, 2022
d3de994
Fix up warnings
Camerooooon Jul 2, 2022
a600a29
Remove commented code
Camerooooon Jul 2, 2022
38997ea
Remove old comment
Camerooooon Jul 2, 2022
ada0042
Add more error handling for connection issues
Camerooooon Jul 2, 2022
d76799e
Fix syntax
Camerooooon Jul 2, 2022
3144c74
Fix unused import warnings
Camerooooon Jul 2, 2022
c1b52b3
Move handling individual streams to its own thread
Camerooooon Jul 2, 2022
522f8d5
Remove debug messages
Camerooooon Jul 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 77 additions & 48 deletions src/daemon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::convert::TryInto;
use std::sync::{Arc, Mutex};
use std::time::SystemTime;
use std::{thread, time};

Expand All @@ -11,6 +12,7 @@ use super::cpu::{Speed, CPU};
use super::graph::{Graph, Grapher};
use super::logger;
use super::logger::Interface;
use super::network::{hook, listen};
use super::power::{
get_battery_status, has_battery, read_battery_charge, read_lid_state, read_power_source,
LidState,
Expand Down Expand Up @@ -57,7 +59,6 @@ pub trait Checker {
operation: &dyn Fn(&mut CPU) -> Result<(), Error>,
) -> Result<(), Error>;

fn run(&mut self) -> Result<(), Error>;
fn init(&mut self);

fn start_loop(&mut self) -> Result<(), Error>;
Expand Down Expand Up @@ -183,40 +184,6 @@ impl Checker for Daemon {
state
}

fn run(&mut self) -> Result<(), Error> {
self.init();

if self.settings.testing {
// Choose which mode acs runs in
if self.settings.edit {
let mut reps = 4;
while reps > 0 {
self.single_edit()?;
reps -= 1;
}
} else {
let mut reps = 4;
while reps > 0 {
self.single_monit()?;
reps -= 1;
}
}
} else {
// Choose which mode acs runs in
if self.settings.edit {
loop {
self.single_edit()?;
}
} else {
loop {
self.single_monit()?;
}
}
}

Ok(())
}

fn init(&mut self) {
// Get the commit hash from the compile time env variable
if self.settings.commit {
Expand Down Expand Up @@ -245,12 +212,6 @@ impl Checker for Daemon {
if self.settings.verbose {
self.print();
}

if self.charging {
thread::sleep(self.timeout);
} else {
thread::sleep(self.timeout_battery);
}
}

fn single_edit(&mut self) -> Result<(), Error> {
Expand Down Expand Up @@ -456,7 +417,7 @@ fn format_message(
)
}

pub fn daemon_init(settings: Settings, config: Config) -> Result<Daemon, Error> {
pub fn daemon_init(settings: Settings, config: Config) -> Result<Arc<Mutex<Daemon>>, Error> {
Camerooooon marked this conversation as resolved.
Show resolved Hide resolved
let started_as_edit: bool = settings.edit;
let mut edit = settings.edit;
let mut forced_reason: String = String::new();
Expand Down Expand Up @@ -542,7 +503,72 @@ pub fn daemon_init(settings: Settings, config: Config) -> Result<Daemon, Error>
daemon.cpus.push(cpu);
}

Ok(daemon)
let daemon_mutex = Arc::new(Mutex::new(daemon));

let c_daemon_mutex = Arc::clone(&daemon_mutex);
if settings.edit {
// Listen for acs clients
listen("/tmp/acs.sock", c_daemon_mutex);
} else {
// Broadcast hello message
hook("/tmp/acs.sock", c_daemon_mutex);
}

Ok(daemon_mutex)
}

pub fn run(daemon_mutex: Arc<Mutex<Daemon>>) -> Result<(), Error> {
// Aquire the lock for a bit
let mut daemon = daemon_mutex.lock().unwrap();

daemon.init();

if daemon.settings.testing {
// Choose which mode acs runs in
if daemon.settings.edit {
let mut reps = 4;
while reps > 0 {
daemon.single_edit()?;
reps -= 1;
}
} else {
let mut reps = 4;
while reps > 0 {
daemon.single_monit()?;
reps -= 1;
}
}
} else {
// Before runnig the loop drop the lock and aquire it again later within the loop
let mode = daemon.settings.edit.clone();

let effective_timeout = if daemon.charging {
daemon.timeout.clone()
} else {
daemon.timeout_battery.clone()
};

drop(daemon);

// Choose which mode acs runs in
if mode {
loop {
let mut daemon = daemon_mutex.lock().unwrap();
Camerooooon marked this conversation as resolved.
Show resolved Hide resolved
daemon.single_edit()?;
drop(daemon);
thread::sleep(effective_timeout);
}
} else {
loop {
let mut daemon = daemon_mutex.lock().unwrap();
daemon.single_monit()?;
drop(daemon);
thread::sleep(effective_timeout);
}
}
}

Ok(())
}

#[cfg(test)]
Expand All @@ -565,7 +591,8 @@ mod tests {

let config = default_config();

let daemon = daemon_init(settings, config).unwrap();
let daemon_mutex = daemon_init(settings, config).unwrap();
let daemon = daemon_mutex.lock().unwrap();

if Uid::effective().is_root() {
assert_eq!(daemon.settings.edit, true);
Expand All @@ -591,8 +618,9 @@ mod tests {

let config = default_config();

let mut daemon = daemon_init(settings, config).unwrap();
let preprint = Checker::preprint_render(&mut daemon);
let daemon_mutex = daemon_init(settings, config).unwrap();
let mut daemon = daemon_mutex.lock().unwrap();
let preprint = daemon.preprint_render();
if Uid::effective().is_root() {
assert!(preprint.contains("Auto Clock Speed daemon has been initialized in \u{1b}[31medit\u{1b}[0m mode with a delay of 1ms normally and 2ms when on battery"));
} else {
Expand Down Expand Up @@ -620,8 +648,9 @@ mod tests {

let config = default_config();

let mut daemon = daemon_init(settings, config).unwrap();
let preprint = Checker::preprint_render(&mut daemon);
let daemon_mutex = daemon_init(settings, config).unwrap();
let mut daemon = daemon_mutex.lock().unwrap();
let preprint = daemon.preprint_render();
assert!(preprint.contains("Auto Clock Speed daemon has been initialized in \u{1b}[33mmonitor\u{1b}[0m mode with a delay of 1ms normally and 2ms when on battery\n"));
assert!(preprint.contains("Name Max\tMin\tFreq\tTemp\tUsage\tGovernor\n"));
assert!(preprint.contains("Hz"));
Expand Down
2 changes: 1 addition & 1 deletion src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Setter for Set {
fn gov(&self, value: String, config: Config, settings: Settings) {
// Create the daemon to set the gov
match daemon_init(settings, config) {
Ok(mut d) => match d.set_govs(value.clone()) {
Ok(d) => match d.lock().unwrap().set_govs(value.clone()) {
Ok(_) => {}
Err(e) => eprint!("Could not set gov, {:?}", e),
},
Expand Down
11 changes: 4 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use std::{thread, time};
use structopt::StructOpt;

use config::{config_dir_exists, get_config, init_config};
use daemon::{daemon_init, Checker};
use daemon::daemon_init;
use display::show_config;
use error::Error;
use interactive::interactive;
Expand All @@ -43,6 +43,7 @@ pub mod graph;
pub mod interactive;
pub mod interface;
pub mod logger;
pub mod network;
pub mod power;
pub mod settings;
pub mod system;
Expand Down Expand Up @@ -223,8 +224,6 @@ enum ACSCommand {
}

fn parse_args(config: config::Config) {
let mut daemon: daemon::Daemon;

let set_settings = Settings {
verbose: true,
delay_battery: 0,
Expand Down Expand Up @@ -339,8 +338,7 @@ fn parse_args(config: config::Config) {

match daemon_init(settings, config) {
Ok(d) => {
daemon = d;
daemon.run().unwrap_err();
daemon::run(d).unwrap_err();
}
Err(_) => eprint!("Could not run daemon in edit mode"),
}
Expand Down Expand Up @@ -396,8 +394,7 @@ fn parse_args(config: config::Config) {

match daemon_init(settings, config) {
Ok(d) => {
daemon = d;
daemon.run().unwrap_err();
daemon::run(d).unwrap_err();
}
Err(_) => eprint!("Could not run daemon in monitor mode"),
}
Expand Down
Loading