Skip to content

Commit 77c1e3e

Browse files
committed
latency-test, latency-histogram: warn when rtapi_app lacks RT privileges
When a run-in-place build is used without 'sudo make setuid' (or 'sudo make setcap'), rtapi_app runs unprivileged: realtime threads get neither SCHED_FIFO scheduling nor locked memory. The resulting latency readings are wildly inflated and do not reflect the real hardware, which is easy to mistake for a code regression. Add a check in both tools that, for a non-root user, locates rtapi_app and verifies it is either setuid root or carries the cap_sys_nice file capability. If neither holds, print a warning that the numbers may be skewed and point at the missing setuid/setcap step. The check stays silent when run as root or when rtapi_app cannot be located. Closes #4044
1 parent df06b0f commit 77c1e3e

2 files changed

Lines changed: 17 additions & 28 deletions

File tree

scripts/latency-histogram

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,17 @@ proc which_exe {name} {
112112
} ;# which_exe
113113

114114
proc check_rt_privileges {} {
115-
# Realtime threads need SCHED_FIFO scheduling and locked memory, which
116-
# rtapi_app can only obtain when it is setuid root ('sudo make setuid') or
117-
# carries file capabilities ('sudo make setcap'). Without them rtapi_app
118-
# runs unprivileged and the measured latency is wildly inflated and
119-
# meaningless. See https://github.com/LinuxCNC/linuxcnc/issues/4044
120-
if {![catch {exec id -u} uid] && $uid == 0} return ;# root has all privileges
115+
# Without 'sudo make setuid' (or 'sudo make setcap') rtapi_app runs
116+
# unprivileged: realtime threads get no SCHED_FIFO and no locked memory, so
117+
# the latency readings are wildly inflated. Warn rather than mislead.
118+
if {![catch {exec id -u} uid] && $uid == 0} return ;# root has every privilege
121119
if {[catch {which_exe rtapi_app} rtapi]} return ;# cannot locate, stay quiet
122-
# setuid-root bit ('make setuid')
123-
if {![catch {file attributes $rtapi -permissions} mode] && ($mode & 04000)} return
124-
# file capabilities ('make setcap')
125-
if {![catch {exec getcap $rtapi} caps] && [string match *cap_sys_nice* $caps]} return
120+
if {![catch {file attributes $rtapi -permissions} mode] && ($mode & 04000)} return ;# setuid root
121+
if {![catch {exec getcap $rtapi} caps] && [string match *cap_sys_nice* $caps]} return ;# file caps
126122
set msg "Warning: rtapi_app is not setuid root and has no realtime capabilities."
127-
append msg "\nRealtime threads cannot get SCHED_FIFO priority or locked memory,"
123+
append msg "\nRealtime threads get no SCHED_FIFO priority or locked memory,"
128124
append msg "\nso the latency shown will be far worse than reality."
129-
append msg "\nOn a run-in-place build, run 'sudo make setuid' (or 'sudo make setcap') in src/."
130-
append msg "\nSee https://github.com/LinuxCNC/linuxcnc/issues/4044"
125+
append msg "\nRun 'sudo make setuid' (or 'sudo make setcap') in src/."
131126
puts stderr $msg
132127
if {$::LH(use_x)} {
133128
package require Tk

scripts/latency-test

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,23 @@ kversion=$(uname -v)
1616
calc() { awk "BEGIN { print ($1); }" < /dev/null; }
1717
icalc() { awk "BEGIN { printf \"%.0f\n\", ($1); }" < /dev/null; }
1818

19-
# Realtime threads need SCHED_FIFO scheduling and locked memory, which
20-
# rtapi_app can only obtain when it is setuid root ('sudo make setuid') or
21-
# carries file capabilities ('sudo make setcap'). Without them rtapi_app runs
22-
# unprivileged and the measured latency is wildly inflated and meaningless.
23-
# See https://github.com/LinuxCNC/linuxcnc/issues/4044
19+
# Without 'sudo make setuid' (or 'sudo make setcap') rtapi_app runs
20+
# unprivileged: realtime threads get no SCHED_FIFO and no locked memory, so the
21+
# latency readings are wildly inflated. Warn rather than mislead.
2422
check_rt_privileges () {
25-
# root already has every privilege rtapi_app needs
26-
[ "$(id -u)" = 0 ] && return 0
23+
[ "$(id -u)" = 0 ] && return 0 # root has every privilege
2724
rtapi_app=$(command -v rtapi_app) || return 0
2825
[ -n "$rtapi_app" ] || return 0
29-
# setuid-root bit ('make setuid')
30-
[ -u "$rtapi_app" ] && return 0
31-
# file capabilities ('make setcap')
26+
[ -u "$rtapi_app" ] && return 0 # setuid root
3227
if command -v getcap >/dev/null 2>&1; then
3328
case $(getcap "$rtapi_app" 2>/dev/null) in
34-
*cap_sys_nice*) return 0 ;;
29+
*cap_sys_nice*) return 0 ;; # file capabilities
3530
esac
3631
fi
3732
echo "Warning: rtapi_app is not setuid root and has no realtime capabilities." >&2
38-
echo " Realtime threads cannot get SCHED_FIFO priority or locked memory," >&2
39-
echo " so the latency numbers shown below will be far worse than reality." >&2
40-
echo " On a run-in-place build, run 'sudo make setuid' (or 'sudo make setcap')" >&2
41-
echo " in the src/ directory. See https://github.com/LinuxCNC/linuxcnc/issues/4044" >&2
33+
echo " Realtime threads get no SCHED_FIFO priority or locked memory," >&2
34+
echo " so the latency shown will be far worse than reality." >&2
35+
echo " Run 'sudo make setuid' (or 'sudo make setcap') in src/." >&2
4236
}
4337

4438
parse_time () {

0 commit comments

Comments
 (0)