From 17219285754968205a8ecc76fc1ebb0db9836a37 Mon Sep 17 00:00:00 2001 From: bmcpt Date: Wed, 4 Jan 2023 11:30:54 +0530 Subject: [PATCH 1/9] watchdog todo --- module_template/service.sh | 2 + utilities/Cargo.lock | 278 ++++----------------------- utilities/Cargo.toml | 3 +- utilities/src/bin/uplink_watchdog.rs | 95 +++++++++ 4 files changed, 138 insertions(+), 240 deletions(-) create mode 100644 utilities/src/bin/uplink_watchdog.rs diff --git a/module_template/service.sh b/module_template/service.sh index 89e4fe1..004156a 100755 --- a/module_template/service.sh +++ b/module_template/service.sh @@ -3,8 +3,10 @@ MODULE_DIR=/data/local/uplinkmodule DATA_DIR=/data/local/uplink +pkill -x uplink_watchdog pkill -x uplink mkdir -p $DATA_DIR cd $DATA_DIR|| exit ("$MODULE_DIR"/bin/uplink -a $DATA_DIR/device.json -c "$MODULE_DIR"/etc/uplink.config.toml -v 2>&1 | "$MODULE_DIR"/bin/logrotate --output $DATA_DIR/out.log) & +"$MODULE_DIR"/bin/uplink_watchdog& \ No newline at end of file diff --git a/utilities/Cargo.lock b/utilities/Cargo.lock index e26f416..d99d999 100644 --- a/utilities/Cargo.lock +++ b/utilities/Cargo.lock @@ -3,59 +3,26 @@ version = 3 [[package]] -name = "android_system_properties" -version = "0.1.5" +name = "aho-corasick" +version = "0.7.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" dependencies = [ - "libc", + "memchr", ] -[[package]] -name = "autocfg" -version = "1.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" - [[package]] name = "bitflags" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" -[[package]] -name = "bumpalo" -version = "3.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "572f695136211188308f16ad2ca5c851a712c464060ae6974944458eb83880ba" - [[package]] name = "cc" version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d" -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "chrono" -version = "0.4.23" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "16b0a3d9ed01224b22057780a37bb8c5dbfe1be8ba48678e7bf57ec4b385411f" -dependencies = [ - "iana-time-zone", - "js-sys", - "num-integer", - "num-traits", - "time", - "wasm-bindgen", - "winapi", -] - [[package]] name = "clap" version = "4.0.32" @@ -93,66 +60,6 @@ dependencies = [ "os_str_bytes", ] -[[package]] -name = "codespan-reporting" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3538270d33cc669650c4b093848450d380def10c331d38c768e34cac80576e6e" -dependencies = [ - "termcolor", - "unicode-width", -] - -[[package]] -name = "core-foundation-sys" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" - -[[package]] -name = "cxx" -version = "1.0.85" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5add3fc1717409d029b20c5b6903fc0c0b02fa6741d820054f4a2efa5e5816fd" -dependencies = [ - "cc", - "cxxbridge-flags", - "cxxbridge-macro", - "link-cplusplus", -] - -[[package]] -name = "cxx-build" -version = "1.0.85" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4c87959ba14bc6fbc61df77c3fcfe180fc32b93538c4f1031dd802ccb5f2ff0" -dependencies = [ - "cc", - "codespan-reporting", - "once_cell", - "proc-macro2", - "quote", - "scratch", - "syn", -] - -[[package]] -name = "cxxbridge-flags" -version = "1.0.85" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "69a3e162fde4e594ed2b07d0f83c6c67b745e7f28ce58c6df5e6b6bef99dfb59" - -[[package]] -name = "cxxbridge-macro" -version = "1.0.85" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e7e2adeb6a0d4a282e581096b06e1791532b7d576dcde5ccd9382acf55db8e6" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "errno" version = "0.2.8" @@ -189,30 +96,6 @@ dependencies = [ "libc", ] -[[package]] -name = "iana-time-zone" -version = "0.1.53" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64c122667b287044802d6ce17ee2ddf13207ed924c712de9a66a5814d5b64765" -dependencies = [ - "android_system_properties", - "core-foundation-sys", - "iana-time-zone-haiku", - "js-sys", - "wasm-bindgen", - "winapi", -] - -[[package]] -name = "iana-time-zone-haiku" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0703ae284fc167426161c2e3f1da3ea71d94b21bedbcc9494e92b28e334e3dca" -dependencies = [ - "cxx", - "cxx-build", -] - [[package]] name = "io-lifetimes" version = "1.0.3" @@ -235,30 +118,12 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "js-sys" -version = "0.3.60" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47" -dependencies = [ - "wasm-bindgen", -] - [[package]] name = "libc" version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" -[[package]] -name = "link-cplusplus" -version = "1.0.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecd207c9c713c34f95a097a5b029ac2ce6010530c7b49d7fea24d977dede04f5" -dependencies = [ - "cc", -] - [[package]] name = "linux-raw-sys" version = "0.1.4" @@ -266,32 +131,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" [[package]] -name = "log" -version = "0.4.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" -dependencies = [ - "cfg-if", -] - -[[package]] -name = "num-integer" -version = "0.1.45" +name = "memchr" +version = "2.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9" -dependencies = [ - "autocfg", - "num-traits", -] - -[[package]] -name = "num-traits" -version = "0.2.15" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" -dependencies = [ - "autocfg", -] +checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" [[package]] name = "once_cell" @@ -347,6 +190,23 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "regex" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + [[package]] name = "rustix" version = "0.36.6" @@ -362,10 +222,10 @@ dependencies = [ ] [[package]] -name = "scratch" -version = "1.0.3" +name = "serde" +version = "1.0.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ddccb15bcce173023b3fedd9436f882a0739b8dfb45e4f6b6002bee5929f61b2" +checksum = "bb7d1f0d3021d347a83e556fc4683dea2ea09d87bccdf88ff5c12545d89d5efb" [[package]] name = "strsim" @@ -395,33 +255,33 @@ dependencies = [ [[package]] name = "time" -version = "0.1.45" +version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1b797afad3f312d1c66a56d11d0316f916356d11bd158fbc6ca6389ff6bf805a" +checksum = "a561bf4617eebd33bca6434b988f39ed798e527f51a1e797d0ee4f61c0a38376" dependencies = [ - "libc", - "wasi", - "winapi", + "serde", + "time-core", ] [[package]] -name = "unicode-ident" -version = "1.0.6" +name = "time-core" +version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" +checksum = "2e153e1f1acaef8acc537e68b44906d2db6436e2b35ac2c6b42640fff91f00fd" [[package]] -name = "unicode-width" -version = "0.1.10" +name = "unicode-ident" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" +checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" [[package]] name = "utilities" version = "0.1.0" dependencies = [ - "chrono", "clap", + "regex", + "time", ] [[package]] @@ -430,66 +290,6 @@ version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" -[[package]] -name = "wasi" -version = "0.10.0+wasi-snapshot-preview1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" - -[[package]] -name = "wasm-bindgen" -version = "0.2.83" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268" -dependencies = [ - "cfg-if", - "wasm-bindgen-macro", -] - -[[package]] -name = "wasm-bindgen-backend" -version = "0.2.83" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142" -dependencies = [ - "bumpalo", - "log", - "once_cell", - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-macro" -version = "0.2.83" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810" -dependencies = [ - "quote", - "wasm-bindgen-macro-support", -] - -[[package]] -name = "wasm-bindgen-macro-support" -version = "0.2.83" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c" -dependencies = [ - "proc-macro2", - "quote", - "syn", - "wasm-bindgen-backend", - "wasm-bindgen-shared", -] - -[[package]] -name = "wasm-bindgen-shared" -version = "0.2.83" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f" - [[package]] name = "winapi" version = "0.3.9" diff --git a/utilities/Cargo.toml b/utilities/Cargo.toml index afc4fcd..65a9740 100644 --- a/utilities/Cargo.toml +++ b/utilities/Cargo.toml @@ -7,4 +7,5 @@ edition = "2021" [dependencies] clap = { version = "4.0.32", features = ["derive"] } -chrono = "0.4.23" \ No newline at end of file +time = "0.3.17" +regex = "1.7.0" \ No newline at end of file diff --git a/utilities/src/bin/uplink_watchdog.rs b/utilities/src/bin/uplink_watchdog.rs new file mode 100644 index 0000000..0eccec1 --- /dev/null +++ b/utilities/src/bin/uplink_watchdog.rs @@ -0,0 +1,95 @@ +use std::alloc::System; +use std::cmp::PartialEq; +use std::fs::File; +use std::io::{BufRead, Write}; +use std::process::Command; +use std::thread::sleep; +use std::time::{Duration, SystemTime}; +use regex::{Captures, Regex}; +use time::{OffsetDateTime, PrimitiveDateTime, Time}; + +const TOMORROW_3AM: PrimitiveDateTime = OffsetDateTime::now_utc().date() + .next_day().unwrap() + .with_hms(22, 15, 0) + .unwrap(); + +const UPLINK_MODE_REGEX: Regex = Regex::new("^.+Switching to (.+) mode!!$").unwrap(); + +fn uplink_running() -> bool { + let output = Command::new("pgrep") + .arg("-x") + .arg("uplink") + .output() + .expect("failed to execute process"); + return output.status.success(); +} + +fn uplink_mode() -> Option { + let file = File::open("/var/log/uplink.log").unwrap(); + for line in std::io::BufReader::new(file).lines().rev() { + match line { + Err(_) => break, + Ok(line) => { + if let Some(mode) = UPLINK_MODE_REGEX.captures(line.as_str()) + .and_then(|matches| matches.get(1)) { + return Some(mode.as_str().to_string()) + } + } + } + } + + return None; +} + +fn internet_working() -> bool { + let output = Command::new("ping") + .arg("-c") + .arg("1") + .arg("google.com") + .output() + .expect("failed to execute process"); + return output.status.success(); +} + +fn main() { + let current_exe_path = std::env::current_exe().unwrap(); + let uplink_module_dir = current_exe_path + .parent() + .and_then(|d| d.parent()) + .unwrap_or_else(|| panic!("uplink module not installed properly")); + let restart = || { + Command::new(format!(uplink_module_dir.join("bin").join("daemonize"))) + .arg(uplink_module_dir.join("service.sh")) + .output() + .unwrap(); + }; + + println!("waiting till: {TOMORROW_3AM}"); + let mut tics = 0; + loop { + let _ = std::io::stdout().flush(); + let _ = std::io::stderr().flush(); + sleep(Duration::from_secs(1)); + tics += 1; + let now = OffsetDateTime::now_utc(); + + if now > TOMORROW_3AM { + println!("{now}: restarting uplink"); + restart() + } else if !uplink_running() { + println!("{now}: uplink crashed, restarting"); + restart + } + + let ev_time = 5 * 60; + if tics % ev_time == ev_time / 2 { + if internet_working() { + let curr_mode = uplink_mode(); + if curr_mode == "slow eventloop" { + println!("{now}: uplink stuck in slow eventloop mode, restarting"); + restart(); + } + } + } + } +} \ No newline at end of file From 86b6363c7834850aeec5c83d3ed2513de6b94fd5 Mon Sep 17 00:00:00 2001 From: bmcpt Date: Thu, 5 Jan 2023 13:05:43 +0530 Subject: [PATCH 2/9] got utilities to compile --- build.gradle.kts | 2 +- utilities/Cargo.lock | 7 ++++++ utilities/Cargo.toml | 3 ++- utilities/src/bin/logrotate.rs | 6 ++--- utilities/src/bin/uplink_watchdog.rs | 36 +++++++++++++++------------- 5 files changed, 32 insertions(+), 22 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 5153998..b0100a9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -112,7 +112,7 @@ archs.forEach { arch -> } task("copy-utilities-$arch", type = Copy::class) { - arrayOf("logrotate").forEach { utility -> + arrayOf("logrotate", "uplink_watchdog").forEach { utility -> from(root.resolve(root.resolve(Paths.get("utilities", "target", arch, "release", utility)))) } into(stage.resolve(arch).resolve("bin")) diff --git a/utilities/Cargo.lock b/utilities/Cargo.lock index d99d999..25d6009 100644 --- a/utilities/Cargo.lock +++ b/utilities/Cargo.lock @@ -118,6 +118,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + [[package]] name = "libc" version = "0.2.139" @@ -280,6 +286,7 @@ name = "utilities" version = "0.1.0" dependencies = [ "clap", + "lazy_static", "regex", "time", ] diff --git a/utilities/Cargo.toml b/utilities/Cargo.toml index 65a9740..1a070fa 100644 --- a/utilities/Cargo.toml +++ b/utilities/Cargo.toml @@ -8,4 +8,5 @@ edition = "2021" [dependencies] clap = { version = "4.0.32", features = ["derive"] } time = "0.3.17" -regex = "1.7.0" \ No newline at end of file +regex = "1.7.0" +lazy_static = "1.4.0" \ No newline at end of file diff --git a/utilities/src/bin/logrotate.rs b/utilities/src/bin/logrotate.rs index 749220c..2b7e4b4 100644 --- a/utilities/src/bin/logrotate.rs +++ b/utilities/src/bin/logrotate.rs @@ -1,6 +1,5 @@ -use std::time::SystemTime; -use chrono::{DateTime, Utc}; use clap::{arg, Parser}; +use time::{OffsetDateTime, UtcOffset}; use utilities::LazyFile; #[derive(Parser, Debug)] @@ -29,7 +28,8 @@ fn main() { if !output_file.exists() { output_file.create(); output_file.append_line( - DateTime::::from(SystemTime::now()).format("%d/%m/%Y %T").to_string().as_str() + OffsetDateTime::now_utc().to_offset(UtcOffset::from_hms(5, 30, 0).unwrap()) + .to_string().as_str() ); } if line.is_err() { diff --git a/utilities/src/bin/uplink_watchdog.rs b/utilities/src/bin/uplink_watchdog.rs index 0eccec1..f02c8ac 100644 --- a/utilities/src/bin/uplink_watchdog.rs +++ b/utilities/src/bin/uplink_watchdog.rs @@ -1,19 +1,14 @@ -use std::alloc::System; -use std::cmp::PartialEq; use std::fs::File; use std::io::{BufRead, Write}; use std::process::Command; use std::thread::sleep; -use std::time::{Duration, SystemTime}; -use regex::{Captures, Regex}; -use time::{OffsetDateTime, PrimitiveDateTime, Time}; +use std::time::Duration; +use regex::Regex; +use time::{OffsetDateTime, PrimitiveDateTime, UtcOffset}; -const TOMORROW_3AM: PrimitiveDateTime = OffsetDateTime::now_utc().date() - .next_day().unwrap() - .with_hms(22, 15, 0) - .unwrap(); - -const UPLINK_MODE_REGEX: Regex = Regex::new("^.+Switching to (.+) mode!!$").unwrap(); +lazy_static::lazy_static! { + static ref UPLINK_MODE_REGEX: Regex = Regex::new("^.+Switching to (.+) mode!!$").unwrap(); +} fn uplink_running() -> bool { let output = Command::new("pgrep") @@ -26,7 +21,8 @@ fn uplink_running() -> bool { fn uplink_mode() -> Option { let file = File::open("/var/log/uplink.log").unwrap(); - for line in std::io::BufReader::new(file).lines().rev() { + // TODO: iterate without collect + for line in std::io::BufReader::new(file).lines().collect::>().iter().rev() { match line { Err(_) => break, Ok(line) => { @@ -52,19 +48,25 @@ fn internet_working() -> bool { } fn main() { + let tomorrow_3am: PrimitiveDateTime = OffsetDateTime::now_utc() + .date() + .next_day().unwrap() + .with_hms(22, 15, 0) + .unwrap(); + let current_exe_path = std::env::current_exe().unwrap(); let uplink_module_dir = current_exe_path .parent() .and_then(|d| d.parent()) .unwrap_or_else(|| panic!("uplink module not installed properly")); let restart = || { - Command::new(format!(uplink_module_dir.join("bin").join("daemonize"))) + Command::new(uplink_module_dir.join("bin").join("daemonize")) .arg(uplink_module_dir.join("service.sh")) .output() .unwrap(); }; - println!("waiting till: {TOMORROW_3AM}"); + println!("waiting till: {tomorrow_3am}"); let mut tics = 0; loop { let _ = std::io::stdout().flush(); @@ -73,19 +75,19 @@ fn main() { tics += 1; let now = OffsetDateTime::now_utc(); - if now > TOMORROW_3AM { + if now > tomorrow_3am.assume_offset(UtcOffset::UTC) { println!("{now}: restarting uplink"); restart() } else if !uplink_running() { println!("{now}: uplink crashed, restarting"); - restart + restart() } let ev_time = 5 * 60; if tics % ev_time == ev_time / 2 { if internet_working() { let curr_mode = uplink_mode(); - if curr_mode == "slow eventloop" { + if curr_mode == Some("slow eventloop".to_string()) { println!("{now}: uplink stuck in slow eventloop mode, restarting"); restart(); } From 15b9da7c3331dae5755124913d52d52076f28810 Mon Sep 17 00:00:00 2001 From: bmcpt Date: Thu, 5 Jan 2023 15:24:36 +0530 Subject: [PATCH 3/9] daemonize.rs --- build.gradle.kts | 2 +- module_template/bin/daemonize | 2 -- module_template/etc/uplink.config.toml | 4 ++-- module_template/service.sh | 2 +- utilities/src/bin/daemonize.rs | 10 ++++++++++ 5 files changed, 14 insertions(+), 6 deletions(-) delete mode 100755 module_template/bin/daemonize create mode 100644 utilities/src/bin/daemonize.rs diff --git a/build.gradle.kts b/build.gradle.kts index b0100a9..8d3415f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -112,7 +112,7 @@ archs.forEach { arch -> } task("copy-utilities-$arch", type = Copy::class) { - arrayOf("logrotate", "uplink_watchdog").forEach { utility -> + arrayOf("logrotate", "uplink_watchdog", "daemonize").forEach { utility -> from(root.resolve(root.resolve(Paths.get("utilities", "target", arch, "release", utility)))) } into(stage.resolve(arch).resolve("bin")) diff --git a/module_template/bin/daemonize b/module_template/bin/daemonize deleted file mode 100755 index 3061416..0000000 --- a/module_template/bin/daemonize +++ /dev/null @@ -1,2 +0,0 @@ -#!/system/bin/sh -nohup "$@" > /dev/null & \ No newline at end of file diff --git a/module_template/etc/uplink.config.toml b/module_template/etc/uplink.config.toml index 663af9d..9799b77 100644 --- a/module_template/etc/uplink.config.toml +++ b/module_template/etc/uplink.config.toml @@ -5,8 +5,8 @@ path = "/data/local/uplink/persistence" max_file_size = 104857600 max_file_count = 3 -[ota] -enabled = true +[downloader] +actions = ["update_firmware"] path = "/data/local/uplink/ota" [stats] diff --git a/module_template/service.sh b/module_template/service.sh index 004156a..aa7641d 100755 --- a/module_template/service.sh +++ b/module_template/service.sh @@ -9,4 +9,4 @@ mkdir -p $DATA_DIR cd $DATA_DIR|| exit ("$MODULE_DIR"/bin/uplink -a $DATA_DIR/device.json -c "$MODULE_DIR"/etc/uplink.config.toml -v 2>&1 | "$MODULE_DIR"/bin/logrotate --output $DATA_DIR/out.log) & -"$MODULE_DIR"/bin/uplink_watchdog& \ No newline at end of file +"$MODULE_DIR"/bin/uplink_watchdog >> $DATA_DIR/restart.log 2>&1 \ No newline at end of file diff --git a/utilities/src/bin/daemonize.rs b/utilities/src/bin/daemonize.rs new file mode 100644 index 0000000..c28d3fe --- /dev/null +++ b/utilities/src/bin/daemonize.rs @@ -0,0 +1,10 @@ +use std::process::{Command, Stdio}; + +fn main() { + let args: Vec = std::env::args().collect(); + let command = args.get(1).unwrap_or_else(|| panic!("no command provided")); + let args = args.get(2..).unwrap_or(&[]); + Command::new(command) + .args(args) + .spawn().unwrap(); +} \ No newline at end of file From df45e5a8063cef96f65f02ecf5592dbd7fe9c4be Mon Sep 17 00:00:00 2001 From: bmcpt Date: Thu, 5 Jan 2023 15:50:33 +0530 Subject: [PATCH 4/9] uplink_watchdog.rs working --- build.gradle.kts | 13 ++++++++-- utilities/src/bin/daemonize.rs | 2 +- utilities/src/bin/uplink_watchdog.rs | 39 ++++++++++++++++------------ 3 files changed, 34 insertions(+), 20 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 8d3415f..88e88a6 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -68,7 +68,9 @@ archs.forEach { arch -> println(String(process.inputStream.readAllBytes())) println(String(process.errorStream.readAllBytes())) - assert(process.waitFor() == 0) + if (process.waitFor() != 0) { + throw Exception("cargo build failed") + } } } @@ -95,7 +97,9 @@ archs.forEach { arch -> println(String(process.inputStream.readAllBytes())) println(String(process.errorStream.readAllBytes())) - assert(process.waitFor() == 0) + if (process.waitFor() != 0) { + throw Exception("cargo build failed") + } } } @@ -168,3 +172,8 @@ tasks.create("buildArtifacts") { } dependsOn("copy-lib") } + +fun trace(value: T) : T { + println(value) + return value +} \ No newline at end of file diff --git a/utilities/src/bin/daemonize.rs b/utilities/src/bin/daemonize.rs index c28d3fe..5669636 100644 --- a/utilities/src/bin/daemonize.rs +++ b/utilities/src/bin/daemonize.rs @@ -1,4 +1,4 @@ -use std::process::{Command, Stdio}; +use std::process::Command; fn main() { let args: Vec = std::env::args().collect(); diff --git a/utilities/src/bin/uplink_watchdog.rs b/utilities/src/bin/uplink_watchdog.rs index f02c8ac..88d9c4f 100644 --- a/utilities/src/bin/uplink_watchdog.rs +++ b/utilities/src/bin/uplink_watchdog.rs @@ -1,5 +1,4 @@ -use std::fs::File; -use std::io::{BufRead, Write}; +use std::io::Write; use std::process::Command; use std::thread::sleep; use std::time::Duration; @@ -20,20 +19,17 @@ fn uplink_running() -> bool { } fn uplink_mode() -> Option { - let file = File::open("/var/log/uplink.log").unwrap(); - // TODO: iterate without collect - for line in std::io::BufReader::new(file).lines().collect::>().iter().rev() { - match line { - Err(_) => break, - Ok(line) => { - if let Some(mode) = UPLINK_MODE_REGEX.captures(line.as_str()) - .and_then(|matches| matches.get(1)) { - return Some(mode.as_str().to_string()) - } - } + let logs = format!( + "{}\n{}", + std::fs::read_to_string(("/data/local/uplink/out.log.1")).unwrap_or(String::new()), + std::fs::read_to_string(("/data/local/uplink/out.log")).unwrap_or(String::new()) + ); + for line in logs.lines().rev() { + if let Some(mode) = UPLINK_MODE_REGEX.captures(line) + .and_then(|matches| matches.get(1)) { + return Some(mode.as_str().to_string()); } } - return None; } @@ -59,6 +55,7 @@ fn main() { .parent() .and_then(|d| d.parent()) .unwrap_or_else(|| panic!("uplink module not installed properly")); + let restart = || { Command::new(uplink_module_dir.join("bin").join("daemonize")) .arg(uplink_module_dir.join("service.sh")) @@ -87,9 +84,17 @@ fn main() { if tics % ev_time == ev_time / 2 { if internet_working() { let curr_mode = uplink_mode(); - if curr_mode == Some("slow eventloop".to_string()) { - println!("{now}: uplink stuck in slow eventloop mode, restarting"); - restart(); + match curr_mode { + None => { + println!("mode not found"); + } + Some(mode) => { + println!("mode is {mode}"); + if mode == "slow eventloop".to_string() { + println!("{now}: uplink stuck in slow eventloop mode, restarting"); + restart(); + } + } } } } From c695a5d6912fb09f2dcaf2bfb886e37b897496e9 Mon Sep 17 00:00:00 2001 From: bmcpt Date: Thu, 5 Jan 2023 17:56:02 +0530 Subject: [PATCH 5/9] cleanup --- utilities/src/bin/uplink_watchdog.rs | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/utilities/src/bin/uplink_watchdog.rs b/utilities/src/bin/uplink_watchdog.rs index 88d9c4f..06f6af2 100644 --- a/utilities/src/bin/uplink_watchdog.rs +++ b/utilities/src/bin/uplink_watchdog.rs @@ -21,8 +21,8 @@ fn uplink_running() -> bool { fn uplink_mode() -> Option { let logs = format!( "{}\n{}", - std::fs::read_to_string(("/data/local/uplink/out.log.1")).unwrap_or(String::new()), - std::fs::read_to_string(("/data/local/uplink/out.log")).unwrap_or(String::new()) + std::fs::read_to_string("/data/local/uplink/out.log.1").unwrap_or(String::new()), + std::fs::read_to_string("/data/local/uplink/out.log").unwrap_or(String::new()) ); for line in logs.lines().rev() { if let Some(mode) = UPLINK_MODE_REGEX.captures(line) @@ -83,17 +83,11 @@ fn main() { let ev_time = 5 * 60; if tics % ev_time == ev_time / 2 { if internet_working() { - let curr_mode = uplink_mode(); - match curr_mode { - None => { - println!("mode not found"); - } - Some(mode) => { - println!("mode is {mode}"); - if mode == "slow eventloop".to_string() { - println!("{now}: uplink stuck in slow eventloop mode, restarting"); - restart(); - } + if let Some(mode) = uplink_mode() { + println!("mode is {mode}"); + if mode == "slow eventloop".to_string() { + println!("{now}: uplink stuck in slow eventloop mode, restarting"); + restart(); } } } From 940db39194482f3c43dfa5435db88ddd4d1c323a Mon Sep 17 00:00:00 2001 From: bmcpt Date: Thu, 5 Jan 2023 17:56:51 +0530 Subject: [PATCH 6/9] uplink fix --- uplink | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uplink b/uplink index 8808ad2..52f3ebc 160000 --- a/uplink +++ b/uplink @@ -1 +1 @@ -Subproject commit 8808ad2e165a12ed93663a54e8b5819c9890abc2 +Subproject commit 52f3ebcfd5a45707b49cd636630e1d80793f968c From 3a8b92b8c3885f0cdf123c9790cdac4ceaa48f2e Mon Sep 17 00:00:00 2001 From: bmcpt Date: Thu, 5 Jan 2023 17:57:49 +0530 Subject: [PATCH 7/9] uplink fix --- uplink | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uplink b/uplink index 52f3ebc..9e45018 160000 --- a/uplink +++ b/uplink @@ -1 +1 @@ -Subproject commit 52f3ebcfd5a45707b49cd636630e1d80793f968c +Subproject commit 9e45018e8a6f1813b543194b5c1e9d48e2bb3358 From a06fad0aa9f21db6b4d897dc2cb28f263df4873f Mon Sep 17 00:00:00 2001 From: bmcpt Date: Thu, 5 Jan 2023 18:08:35 +0530 Subject: [PATCH 8/9] use daemonize shell script --- build.gradle.kts | 2 +- module_template/bin/daemonize | 2 ++ utilities/src/bin/daemonize.rs | 10 ---------- 3 files changed, 3 insertions(+), 11 deletions(-) create mode 100755 module_template/bin/daemonize delete mode 100644 utilities/src/bin/daemonize.rs diff --git a/build.gradle.kts b/build.gradle.kts index 88e88a6..e66a1bb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -116,7 +116,7 @@ archs.forEach { arch -> } task("copy-utilities-$arch", type = Copy::class) { - arrayOf("logrotate", "uplink_watchdog", "daemonize").forEach { utility -> + arrayOf("logrotate", "uplink_watchdog").forEach { utility -> from(root.resolve(root.resolve(Paths.get("utilities", "target", arch, "release", utility)))) } into(stage.resolve(arch).resolve("bin")) diff --git a/module_template/bin/daemonize b/module_template/bin/daemonize new file mode 100755 index 0000000..1442347 --- /dev/null +++ b/module_template/bin/daemonize @@ -0,0 +1,2 @@ +#!/system/bin/sh +nohup "$@" > /dev/null 2>&1 & \ No newline at end of file diff --git a/utilities/src/bin/daemonize.rs b/utilities/src/bin/daemonize.rs deleted file mode 100644 index 5669636..0000000 --- a/utilities/src/bin/daemonize.rs +++ /dev/null @@ -1,10 +0,0 @@ -use std::process::Command; - -fn main() { - let args: Vec = std::env::args().collect(); - let command = args.get(1).unwrap_or_else(|| panic!("no command provided")); - let args = args.get(2..).unwrap_or(&[]); - Command::new(command) - .args(args) - .spawn().unwrap(); -} \ No newline at end of file From 0c3f89481af92aa04961d06bfbe4d6984c1672c0 Mon Sep 17 00:00:00 2001 From: bmcpt Date: Fri, 6 Jan 2023 11:42:03 +0530 Subject: [PATCH 9/9] use correct MODULE_DIR --- module_template/service.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/module_template/service.sh b/module_template/service.sh index aa7641d..4785904 100755 --- a/module_template/service.sh +++ b/module_template/service.sh @@ -1,6 +1,6 @@ #!/system/bin/sh -MODULE_DIR=/data/local/uplinkmodule +MODULE_DIR=$(dirname "$0") DATA_DIR=/data/local/uplink pkill -x uplink_watchdog