Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 25 additions & 4 deletions crates/openlogi-permissions/src/udev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ use std::process::{Command, Stdio};
const RULES: &str = include_str!("../../../packaging/linux/udev/70-openlogi.rules");

/// Where the packages and `install.sh` put [`RULES`].
const PACKAGED_RULES_DIR: &str = "/usr/lib/udev/rules.d";

/// Where the root half and the Flatpak host command put [`RULES`]: the
/// administrator's directory, since `/usr` may be read-only. udev prefers it
/// over [`PACKAGED_RULES_DIR`], so a copy here outlives package upgrades.
const RULES_DIR: &str = "/etc/udev/rules.d";
const RULES_FILE: &str = "70-openlogi.rules";

Expand Down Expand Up @@ -224,13 +229,14 @@ pub enum InstallError {
/// `/etc/udev/rules.d`, reload udev, and re-run the rules for the nodes they
/// cover. Needs root.
///
/// Writes nothing when the installed file already matches, so a package's
/// copy is left untouched and the run only re-applies it.
/// Writes nothing when a package already installed the same rules, or when
/// the file in `/etc/udev/rules.d` already matches. The run then only
/// re-applies them, which is what an install missing its udev reload needs.
pub fn install_rules() -> Result<(), InstallError> {
if let Some(manager) = rules_manager() {
return Err(InstallError::Managed(manager));
}
write_rules(Path::new(RULES_DIR))?;
place_rules(Path::new(PACKAGED_RULES_DIR), Path::new(RULES_DIR))?;
for args in UDEVADM_STEPS {
udevadm(args)?;
}
Expand All @@ -240,10 +246,25 @@ pub fn install_rules() -> Result<(), InstallError> {
Ok(())
}

/// Write [`RULES`] into `admin_dir` unless `packaged_dir` already holds them
/// and `admin_dir` has no copy to shadow them. An existing copy there is
/// refreshed instead, since udev reads it in place of the packaged one.
fn place_rules(packaged_dir: &Path, admin_dir: &Path) -> Result<(), InstallError> {
if holds_rules(packaged_dir) && !admin_dir.join(RULES_FILE).exists() {
return Ok(());
}
write_rules(admin_dir)
}

/// Whether `dir` holds a `70-openlogi.rules` identical to [`RULES`].
fn holds_rules(dir: &Path) -> bool {
fs::read(dir.join(RULES_FILE)).is_ok_and(|current| current == RULES.as_bytes())
}

/// Write [`RULES`] into `dir` unless an identical file is already there.
fn write_rules(dir: &Path) -> Result<(), InstallError> {
let path = dir.join(RULES_FILE);
if fs::read(&path).is_ok_and(|current| current == RULES.as_bytes()) {
if holds_rules(dir) {
return Ok(());
}
let write_error = |error| InstallError::Write {
Expand Down
26 changes: 26 additions & 0 deletions crates/openlogi-permissions/src/udev/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,32 @@ fn writing_leaves_an_identical_file_in_place() {
assert_eq!(fs::read_to_string(&destination).expect("rules"), RULES);
}

/// When a package already installed the same rules under `/usr/lib`, the root
/// half writes no copy into `/etc`: udev would let that copy shadow the
/// package's rules after every later upgrade. A missing or different packaged
/// file still gets the `/etc` copy.
#[test]
fn placing_skips_the_admin_copy_when_the_package_ships_the_rules() {
let packaged = tempfile::tempdir().expect("packaged dir");
let admin = tempfile::tempdir().expect("admin dir");
let admin_rules = admin.path().join(RULES_FILE);

fs::write(packaged.path().join(RULES_FILE), RULES).expect("packaged rules");
super::place_rules(packaged.path(), admin.path()).expect("nothing to write");
assert!(!admin_rules.exists(), "no shadowing copy in the admin dir");

fs::write(packaged.path().join(RULES_FILE), "# older\n").expect("older rules");
super::place_rules(packaged.path(), admin.path()).expect("admin copy written");
assert_eq!(fs::read_to_string(&admin_rules).expect("rules"), RULES);

// A stale copy left in the admin dir would shadow the packaged rules, so
// it is refreshed even though the package ships identical ones.
fs::write(packaged.path().join(RULES_FILE), RULES).expect("packaged rules");
fs::write(&admin_rules, "# stale\n").expect("stale admin rules");
super::place_rules(packaged.path(), admin.path()).expect("admin copy refreshed");
assert_eq!(fs::read_to_string(&admin_rules).expect("rules"), RULES);
}

/// pkexec reserves 126 for a dismissed dialog and 127 for refusing; every
/// other status is the root half's. A failure carries the first stderr line,
/// without the prefix pkexec or `main` leads it with.
Expand Down
2 changes: 1 addition & 1 deletion docs/INSTALL-linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ Install the bundled udev rules to grant access to the active-seat user without
requiring `sudo` or group membership (requires `systemd-logind`):

```sh
sudo cp packaging/linux/udev/70-openlogi.rules /etc/udev/rules.d/
sudo cp packaging/linux/udev/70-openlogi.rules /usr/lib/udev/rules.d/

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Required guides omit the change

Blocking. The command at docs/INSTALL-linux.md:238 changes where users install the rule, but none of docs/CONFIGURATION.md, docs/USAGE.md, or docs/DECISIONS.md changes. The repository requires user-visible behavior changes to be documented in one of those guides. Add a short entry explaining the new location and how an existing /etc copy takes precedence.

Rule Used: Severity: medium. A user-visible behaviour change or a known limitation left by a fix (state lost on restart, a race window not covered, Ctrl-C skipping a hand-back) is stated in docs/CONFIGURATION.md, docs/USAGE.md, or docs/DECISIONS.md in the... (source)

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

sudo udevadm control --reload-rules
sudo udevadm trigger
```
Expand Down
39 changes: 38 additions & 1 deletion packaging/linux/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,23 @@ need_command() {
command -v "$1" >/dev/null 2>&1 || die "required command not found: $1"
}

# Whether an installed system package (not this script) owns $1. Checked
# before touching a file at a path both a package and this installer can
# write to, so a migration or removal here never strands a package-managed
# file (dpkg/rpm reinstall, or pacman on an AUR build of the .deb).
owned_by_package() {
if command -v dpkg >/dev/null 2>&1 && dpkg -S "$1" >/dev/null 2>&1; then
return 0
fi
if command -v rpm >/dev/null 2>&1 && rpm -qf "$1" >/dev/null 2>&1; then
return 0
fi
if command -v pacman >/dev/null 2>&1 && pacman -Qo "$1" >/dev/null 2>&1; then
return 0
fi
return 1
}

require_linux() {
need_command uname
[ "$(uname -s)" = Linux ] || die "this installer supports Linux only"
Expand Down Expand Up @@ -374,6 +391,25 @@ install_source_file() {
fi
}

# Earlier installers wrote the rule to /etc/udev/rules.d, where udev lets it
# shadow the copy under /usr/lib. Remove that copy only after the /usr/lib rule
# is in place, and only when it is byte-identical to the bundled rule and no
# package owns it. Any other file at that path is an administrator's override:
# keep it and say that it takes precedence.
LEGACY_UDEV_RULE=/etc/udev/rules.d/70-openlogi.rules
migrate_legacy_udev_rule() {
bundled_rule=$1
[ -e "$LEGACY_UDEV_RULE" ] || return 0
owned_by_package "$LEGACY_UDEV_RULE" && return 0
if cmp -s "$bundled_rule" "$LEGACY_UDEV_RULE"; then
printf 'Removing the old copy of the udev rule from /etc/udev/rules.d...\n'
run_source_privileged rm -f "$LEGACY_UDEV_RULE"
else
printf '%s\n' "Keeping $LEGACY_UDEV_RULE: it differs from the bundled rule and" \
"takes precedence over /usr/lib/udev/rules.d. Remove it to use the bundled rule." >&2
fi
}

run_source_privileged() {
if [ "$DRY_RUN" -eq 1 ]; then
print_command sudo "$@"
Expand Down Expand Up @@ -421,7 +457,8 @@ install_source() {
done

install_source_file 644 "${SCRIPT_DIR}/udev/70-openlogi.rules" \
/etc/udev/rules.d/70-openlogi.rules
/usr/lib/udev/rules.d/70-openlogi.rules
migrate_legacy_udev_rule "${SCRIPT_DIR}/udev/70-openlogi.rules"
install_source_file 644 "${TEMP_DIR}/openlogi-agent.service" \
/usr/lib/systemd/user/openlogi-agent.service
install_source_file 644 "${SCRIPT_DIR}/desktop/openlogi.desktop" \
Expand Down
2 changes: 1 addition & 1 deletion packaging/linux/nfpm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contents:

# ── udev rules ──────────────────────────────────────────────────────────────
- src: packaging/linux/udev/70-openlogi.rules
dst: /etc/udev/rules.d/70-openlogi.rules
dst: /usr/lib/udev/rules.d/70-openlogi.rules
file_info:
mode: 0644

Expand Down
2 changes: 1 addition & 1 deletion packaging/linux/tests/install-smoke.sh
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ dash "${SOURCE_REPO}/packaging/linux/install.sh" \
for binary in openlogi openlogi-desktop openlogi-overlay openlogi-agent; do
assert_contains "$MOCK_LOG" "/target/release/${binary} ${SOURCE_PREFIX}/bin/${binary}"
done
assert_contains "$MOCK_LOG" '/etc/udev/rules.d/70-openlogi.rules'
assert_contains "$MOCK_LOG" '/usr/lib/udev/rules.d/70-openlogi.rules'
assert_contains "$MOCK_LOG" '/usr/lib/systemd/user/openlogi-agent.service'
assert_contains "$MOCK_LOG" '/usr/share/applications/openlogi.desktop'
assert_contains "$MOCK_LOG" '/usr/share/icons/hicolor/1024x1024/apps/openlogi.png'
Expand Down
2 changes: 1 addition & 1 deletion packaging/linux/udev/70-openlogi.rules
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# on systems running systemd-logind (uaccess tag).
#
# Install:
# sudo cp 70-openlogi.rules /etc/udev/rules.d/
# sudo cp 70-openlogi.rules /usr/lib/udev/rules.d/
# sudo udevadm control --reload-rules
# sudo udevadm trigger
#
Expand Down
29 changes: 28 additions & 1 deletion packaging/linux/uninstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ done

BINDIR="${PREFIX}/bin"

# Whether an installed system package (not this script) owns $1 — so removing
# a path both a package and this installer can write to never strands a
# package-managed file still in place (dpkg/rpm, or pacman on an AUR build
# of the .deb).
owned_by_package() {
if command -v dpkg >/dev/null 2>&1 && dpkg -S "$1" >/dev/null 2>&1; then
return 0
fi
if command -v rpm >/dev/null 2>&1 && rpm -qf "$1" >/dev/null 2>&1; then
return 0
fi
if command -v pacman >/dev/null 2>&1 && pacman -Qo "$1" >/dev/null 2>&1; then
return 0
fi
return 1
}

# ── stop and disable the agent ────────────────────────────────────────────────

# systemctl --user targets the session of whichever user is running this script.
Expand All @@ -51,7 +68,17 @@ sudo rm -f "${BINDIR}/openlogi" "${BINDIR}/openlogi-desktop" \
# ── udev rules ────────────────────────────────────────────────────────────────

echo "Removing udev rules …"
sudo rm -f /etc/udev/rules.d/70-openlogi.rules
# Both the current location and the pre-migration one (#1545): an installed
# package still owns either path independently and keeps it on uninstall.
for rule_path in /usr/lib/udev/rules.d/70-openlogi.rules /etc/udev/rules.d/70-openlogi.rules; do
if [ -e "$rule_path" ]; then
if owned_by_package "$rule_path"; then
echo "Keeping $rule_path — owned by an installed package" >&2
else
sudo rm -f "$rule_path"
fi
fi
done
if command -v udevadm >/dev/null 2>&1; then
sudo udevadm control --reload-rules
sudo udevadm trigger --subsystem-match=hidraw
Expand Down
Loading