Skip to content

Commit

Permalink
Merge pull request #13 from rgwood/fix-sudo-user-units
Browse files Browse the repository at this point in the history
Fix failure to launch when running as root
  • Loading branch information
rgwood authored Jan 21, 2024
2 parents 28a4d3b + 0d474d6 commit afaade2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ indexmap = "2.0.0"
clipboard-anywhere = "0.2.2"
chrono = { version = "0.4.31", default-features = false }
lazy_static = "1.4.0"
nix = { version = "0.27.1", features = ["user"] }

# build with `cargo build --profile profiling`
# to analyze performance with tooling like perf / samply / superluminal
Expand Down
13 changes: 12 additions & 1 deletion src/systemd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use anyhow::Result;
use duct::cmd;
use log::error;
use tokio_util::sync::CancellationToken;
use tracing::info;
use zbus::Connection;
Expand Down Expand Up @@ -101,6 +102,8 @@ pub async fn get_all_services(scope: &Scope) -> Result<Vec<UnitWithStatus>> {

let mut units = vec![];

let is_root = nix::unistd::geteuid().is_root();

match scope {
Scope::Global => {
let system_units = get_services(UnitScope::Global).await?;
Expand All @@ -113,7 +116,15 @@ pub async fn get_all_services(scope: &Scope) -> Result<Vec<UnitWithStatus>> {
Scope::All => {
let (system_units, user_units) = tokio::join!(get_services(UnitScope::Global), get_services(UnitScope::User));
units.extend(system_units?);
units.extend(user_units?);

// Should always be able to get user units, but it may fail when running as root
if let Ok(user_units) = user_units {
units.extend(user_units);
} else if is_root {
error!("Failed to get user units, ignoring because we're running as root")
} else {
user_units?;
}
},
}

Expand Down

0 comments on commit afaade2

Please sign in to comment.