Skip to content

Conversation

@devurandom
Copy link

@devurandom devurandom commented Nov 18, 2025

Describe your changes

Fixes #4808 by extracting the full username by:

  1. Get PID using pgrep
  2. Get UID from PID using /proc/${PID}/loginuid
  3. Get user name from UID using id

Also replaces "complex" pipe from ps to sed with a (hopefully) "simpler" (as in requiring less knowledge about the arguments of ps and regexps) invocation of cat and id.

Issue ticket number and link

#4808

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

This fixes a post-install script used in Linux packages, which should be invisible to regular users.

Docs PR URL (required if "docs added" is checked)

N/A


Closes: #4808

Summary by CodeRabbit

  • Bug Fixes
    • Improved robustness of the UI post-install behavior: installation now reliably detects running UI processes and only attempts a restart when appropriate.
    • Strengthened error handling and execution safeguards during installation to prevent unexpected failures and ensure the UI is relaunched under the correct user context when needed.

@CLAassistant
Copy link

CLAassistant commented Nov 18, 2025

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 18, 2025

Walkthrough

The post-install script release_files/ui-post-install.sh was changed to enable strict shell options, detect a running netbird-ui PID via pgrep, read /proc/[PID]/loginuid to map to a username with id -nu, and only restart netbird-ui under that username when a valid PID is found.

Changes

Cohort / File(s) Summary
Post-install script
release_files/ui-post-install.sh
Added set -e and set -u; switched process detection to pgrep and gated logic on a non-empty PID; read /proc/[PID]/loginuid to obtain UID and resolved username with id -nu; perform pkill netbird-ui and restart using nohup under the resolved user when applicable.

Sequence Diagram(s)

sequenceDiagram
  participant RPM as RPM %post
  participant Script as ui-post-install.sh
  participant Proc as /proc
  participant Sys as System

  RPM->>Script: execute post-install script
  Script->>Script: set -e, set -u
  Script->>Script: pgrep netbird-ui => PID?
  alt PID found
    Script->>Proc: read /proc/[PID]/loginuid => UID
    Script->>Script: id -nu UID => username
    Script->>Sys: pkill netbird-ui
    Script->>Sys: su - username -c "nohup /opt/netbird-ui/netbird-ui &"
  else no PID
    Script-->>RPM: skip restart (no running process)
  end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Areas to inspect:
    • Handling of /proc/[PID]/loginuid edge values (e.g., -1) and missing PID file.
    • Safety and quoting of su/nohup invocation for usernames with special characters.
    • Availability and behavior of pgrep and id on target distributions.

Poem

🐰
I hopped through /proc to find your name,
No more truncation, no more shame.
Loginuid led me to who to wake,
Netbird UI restarts — that’s no mistake. 🎉

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing ui-post-install.sh to use the full username, which addresses the core issue of username truncation.
Description check ✅ Passed The PR description is comprehensive, follows the template structure, includes the linked issue, proper classification as bug fix, and explains the solution clearly with implementation steps.
Linked Issues check ✅ Passed The changes directly address issue #4808 by replacing the flawed ps/sed username extraction with a reliable method using pgrep, /proc/loginuid, and id command to resolve full usernames.
Out of Scope Changes check ✅ Passed All changes are focused on fixing the ui-post-install.sh script's username extraction mechanism, directly addressing the linked issue without unrelated modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 059408e and 81e7ce5.

📒 Files selected for processing (1)
  • release_files/ui-post-install.sh (1 hunks)
🔇 Additional comments (1)
release_files/ui-post-install.sh (1)

1-15: Fix correctly uses UID-based user resolution to eliminate truncated username errors.

The implementation properly addresses issue #4808 by replacing the previous (and likely flawed) user extraction method with a robust UID→username mapping:

  1. Process detection (line 7): pgrep -x -f with || true safely finds the running process or gracefully continues if not found.
  2. UID extraction (line 10): Reads the login UID from /proc/[pid]/loginuid, which is the canonical source for the user who started the session.
  3. Username resolution (line 11): id -nu "${uid}" maps the UID to the full username (avoiding truncation issues like "TRUNCAT+").
  4. Error safety (lines 3-4): set -e and set -u ensure the script exits immediately if any command fails, preventing fallthrough with invalid state.
  5. Restart under correct user (line 14): Uses the resolved username to restart netbird-ui in the correct user context via su - "${username}".

Previous issues have been successfully addressed:

  • ✅ POSIX shell compliance: Line 8 uses [ -n "${pid}" ] (not [[ ]]).
  • ✅ Error handling: set -e catches failures in cat /proc/... or id -nu commands.
  • su syntax: Line 14 correctly uses single-dash form su - "${username}" (not redundant su -l -).

The script is functionally correct and robust for the target use case.


Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 05cbead and fc4f436.

📒 Files selected for processing (1)
  • release_files/ui-post-install.sh (1 hunks)
🧰 Additional context used
🪛 Shellcheck (0.11.0)
release_files/ui-post-install.sh

[warning] 5-5: In POSIX sh, [[ ]] is undefined.

(SC3010)

🔇 Additional comments (1)
release_files/ui-post-install.sh (1)

11-11: Verify su -l - syntax correctness on Fedora 43.

The su invocation uses the syntax su -l - "${username}", which is non-standard. Standard forms are:

  • su - "${username}" (login shell, implied by -)
  • su -l "${username}" (explicit -l flag)

The sequence -l - has the second - in an unusual position after the first flag. This may fail or behave unexpectedly on strict su implementations. The intent is unclear: is the second - a typo, or is it meant to request a login shell while preserving a different behavior?

Please verify that su -l - "${username}" works correctly on Fedora 43 and other target systems. If the syntax is non-standard, consider simplifying to su - "${username}" or su -l "${username}".

Additionally, test the script end-to-end with a long username to confirm it resolves and restarts correctly.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fc4f436 and 059408e.

📒 Files selected for processing (1)
  • release_files/ui-post-install.sh (1 hunks)
🔇 Additional comments (1)
release_files/ui-post-install.sh (1)

3-8: POSIX shell compatibility issue resolved. The previous SC3010 warning about Bash-specific [[ ]] syntax has been fixed. Line 8 now correctly uses the POSIX-compliant [ -n "${pid}" ] test, which will work across dash, ash, and other POSIX shells.

@sonarqubecloud
Copy link

@devurandom
Copy link
Author

Darwin tests failed at:

time="2025-11-20T23:23:49Z" level=error msg="token could not be parsed: token has invalid claims: token is missing required claim: iss claim is required"

Since my change only can affect Debian and Fedora/RHEL packages, not Darwin/macOS, I assume this is an unrelated issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-critical error in %post scriptlet

2 participants