Skip to content

Commit df06b0f

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 hardware or kernel problem. 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 4415063 commit df06b0f

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

scripts/latency-histogram

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,31 @@ proc which_exe {name} {
111111
return -code error "$name: executable not found"
112112
} ;# which_exe
113113

114+
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
121+
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
126+
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,"
128+
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"
131+
puts stderr $msg
132+
if {$::LH(use_x)} {
133+
package require Tk
134+
tk_messageBox -parent . -icon warning -type ok \
135+
-title "Latency Warning" -message $msg
136+
}
137+
} ;# check_rt_privileges
138+
114139
proc program_check {plist} {
115140
foreach prog $plist {
116141
if [catch {
@@ -944,6 +969,7 @@ proc windowToFile { win } {
944969
if ![info exists ::LH(start)] {
945970
set_defaults
946971
config
972+
check_rt_privileges
947973
progress "Loading packages"
948974
load_packages
949975
signal trap SIGINT finish

scripts/latency-test

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,31 @@ 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
24+
check_rt_privileges () {
25+
# root already has every privilege rtapi_app needs
26+
[ "$(id -u)" = 0 ] && return 0
27+
rtapi_app=$(command -v rtapi_app) || return 0
28+
[ -n "$rtapi_app" ] || return 0
29+
# setuid-root bit ('make setuid')
30+
[ -u "$rtapi_app" ] && return 0
31+
# file capabilities ('make setcap')
32+
if command -v getcap >/dev/null 2>&1; then
33+
case $(getcap "$rtapi_app" 2>/dev/null) in
34+
*cap_sys_nice*) return 0 ;;
35+
esac
36+
fi
37+
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
42+
}
43+
1944
parse_time () {
2045
case $1 in
2146
-) echo "0" ;;
@@ -170,4 +195,6 @@ L=\$(($SIGNALS
170195
echo \$L > "$HOME"/.latency
171196
EOF
172197

198+
check_rt_privileges
199+
173200
halrun lat.hal

0 commit comments

Comments
 (0)