fix service status

This commit is contained in:
CherretGit
2025-11-01 19:56:08 +07:00
parent 8caa7ff696
commit 4f6a80a8ee
6 changed files with 229 additions and 34 deletions

View File

@@ -20,4 +20,5 @@ pretty_env_logger = { workspace = true }
log = { workspace = true }
nix = { workspace = true, features = ["user"] }
iptables = { workspace = true }
getset = { workspace = true }
getset = { workspace = true }
sysinfo = { workspace = true }

View File

@@ -1,7 +1,7 @@
use crate::service::{restart_service, service_status, start_service, stop_service};
use crate::{bin_version, get_autostart, module_version, set_autostart};
use clap::Subcommand;
use log::error;
use crate::service::{restart_service, service_status, start_service, stop_service};
#[derive(Subcommand)]
pub enum Command {
@@ -40,23 +40,23 @@ impl Command {
Command::Start => return start_service().await,
Command::Stop => {
let _ = stop_service().await;
},
}
Command::Restart => return restart_service().await,
Command::Status => {
println!(
"zaprett is {}",
if service_status().await {
if service_status().await? {
"working"
} else {
"stopped"
}
);
},
}
Command::SetAutostart { autostart } => {
if let Err(err) = set_autostart(autostart).await {
error!("Failed to set auto start: {err}")
}
},
}
Command::GetAutostart => get_autostart(),
Command::ModuleVersion => module_version(),
Command::BinaryVersion => bin_version(),
@@ -64,4 +64,4 @@ impl Command {
Ok(())
}
}
}

View File

@@ -1,20 +1,20 @@
pub mod cli;
pub mod config;
mod daemon;
pub mod iptables_rust;
mod service;
mod daemon;
use anyhow::bail;
use ini::Ini;
use libnfqws::nfqws_main;
use std::error;
use std::ffi::CString;
use std::os::raw::c_char;
use std::path::Path;
use std::sync::LazyLock;
use anyhow::bail;
use ini::Ini;
use tokio::{fs, task};
use tokio::fs::File;
use tokio::io::{copy, AsyncWriteExt};
use libnfqws::nfqws_main;
use tokio::io::{AsyncWriteExt, copy};
use tokio::{fs, task};
pub static MODULE_PATH: LazyLock<&Path> = LazyLock::new(|| Path::new("/data/adb/modules/zaprett"));
pub static ZAPRETT_DIR_PATH: LazyLock<&Path> =
@@ -72,15 +72,13 @@ pub async fn merge_files(
.await
.map_err(|e| format!("Failed to open {}: {}", input_path.display(), e))?;
copy(&mut input_file, &mut output_file)
.await
.map_err(|e| {
format!(
"Failed to write contents of {}: {}",
input_path.display(),
e
)
})?;
copy(&mut input_file, &mut output_file).await.map_err(|e| {
format!(
"Failed to write contents of {}: {}",
input_path.display(),
e
)
})?;
}
output_file.flush().await?;
@@ -88,7 +86,7 @@ pub async fn merge_files(
}
async fn run_nfqws(args_str: &str) -> anyhow::Result<()> {
if service::service_status().await {
if service::service_status().await? {
bail!("nfqws already started!");
}
@@ -112,7 +110,7 @@ async fn run_nfqws(args_str: &str) -> anyhow::Result<()> {
nfqws_main(c_args.len() as libc::c_int, ptrs.as_mut_ptr() as *mut _);
}
})
.await?;
.await?;
Ok(())
}

View File

@@ -8,7 +8,9 @@ use nix::sys::signal::{Signal, kill};
use nix::unistd::{Pid, Uid};
use regex::Regex;
use std::borrow::Cow;
use std::path::Path;
use sysctl::Sysctl;
use sysinfo::{Pid as SysPid, System};
use tokio::fs;
use tokio::fs::File;
use tokio::io::AsyncReadExt;
@@ -82,16 +84,33 @@ pub async fn stop_service() -> anyhow::Result<()> {
}
pub async fn restart_service() -> anyhow::Result<()> {
if !Uid::effective().is_root() {
bail!("Running not from root, exiting");
};
stop_service().await?;
start_service().await?;
info!("zaprett service restarted!");
Ok(())
}
pub async fn service_status() -> bool {
fs::read_to_string(MODULE_PATH.join("tmp/pid.lock"))
.await
.ok()
.and_then(|pid_str| pid_str.trim().parse::<i32>().ok())
.is_some()
pub async fn service_status() -> anyhow::Result<bool> {
if !Uid::effective().is_root() {
bail!("Running not from root, exiting");
};
let pid_i32 = match fs::read_to_string(Path::new(*MODULE_PATH).join("tmp/pid.lock")).await {
Ok(s) => match s.trim().parse::<i32>() {
Ok(pid) => pid,
Err(_) => return Ok(false),
},
Err(_) => return Ok(false),
};
let pid = SysPid::from(pid_i32 as usize);
let system = System::new_all();
if let Some(process) = system.process(pid) {
if process.name() == "zaprett" {
return Ok(true);
}
}
Ok(false)
}