refactor(linux): replace users crate with libc::geteuid for root detection

Closes #5333
This commit is contained in:
Slinetrac
2025-11-07 18:58:13 +08:00
parent 9cc2f44379
commit 5759bed687
3 changed files with 17 additions and 24 deletions

11
src-tauri/Cargo.lock generated
View File

@@ -1158,7 +1158,6 @@ dependencies = [
"tauri-plugin-window-state",
"tokio",
"tokio-stream",
"users",
"warp",
"winapi",
"windows-sys 0.61.2",
@@ -8699,16 +8698,6 @@ dependencies = [
"url",
]
[[package]]
name = "users"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032"
dependencies = [
"libc",
"log",
]
[[package]]
name = "utf-8"
version = "0.7.6"

View File

@@ -112,9 +112,6 @@ windows-sys = { version = "0.61.2", features = [
"Win32_UI_WindowsAndMessaging",
] }
[target.'cfg(target_os = "linux")'.dependencies]
users = "0.11.0"
[target.'cfg(unix)'.dependencies]
signal-hook = "0.3.18"

View File

@@ -122,7 +122,6 @@ async fn reinstall_service() -> Result<()> {
#[cfg(target_os = "linux")]
async fn uninstall_service() -> Result<()> {
logging!(info, Type::Service, "uninstall service");
use users::get_effective_uid;
let uninstall_path =
tauri::utils::platform::current_exe()?.with_file_name("clash-verge-service-uninstall");
@@ -134,13 +133,14 @@ async fn uninstall_service() -> Result<()> {
let uninstall_shell: String = uninstall_path.to_string_lossy().replace(" ", "\\ ");
let elevator = crate::utils::help::linux_elevator();
let status = match get_effective_uid() {
0 => StdCommand::new(uninstall_shell).status()?,
_ => StdCommand::new(elevator)
let status = if linux_running_as_root() {
StdCommand::new(&uninstall_path).status()?
} else {
StdCommand::new(elevator)
.arg("sh")
.arg("-c")
.arg(uninstall_shell)
.status()?,
.status()?
};
logging!(
info,
@@ -163,7 +163,6 @@ async fn uninstall_service() -> Result<()> {
#[allow(clippy::unused_async)]
async fn install_service() -> Result<()> {
logging!(info, Type::Service, "install service");
use users::get_effective_uid;
let install_path =
tauri::utils::platform::current_exe()?.with_file_name("clash-verge-service-install");
@@ -175,13 +174,14 @@ async fn install_service() -> Result<()> {
let install_shell: String = install_path.to_string_lossy().replace(" ", "\\ ");
let elevator = crate::utils::help::linux_elevator();
let status = match get_effective_uid() {
0 => StdCommand::new(install_shell).status()?,
_ => StdCommand::new(elevator)
let status = if linux_running_as_root() {
StdCommand::new(&install_path).status()?
} else {
StdCommand::new(elevator)
.arg("sh")
.arg("-c")
.arg(install_shell)
.status()?,
.status()?
};
logging!(
info,
@@ -218,6 +218,13 @@ async fn reinstall_service() -> Result<()> {
}
}
#[cfg(target_os = "linux")]
fn linux_running_as_root() -> bool {
const ROOT_UID: u32 = 0;
unsafe { libc::geteuid() == ROOT_UID }
}
#[cfg(target_os = "macos")]
async fn uninstall_service() -> Result<()> {
use crate::utils::i18n::t;