Skip to content

Commit 8274d2d

Browse files
committed
latency-test, latency-histogram: warn when rtapi_app lacks RT privileges
Without 'sudo make setuid' (or 'sudo make setcap') rtapi_app runs unprivileged: no SCHED_FIFO, no locked memory, so latency readings are wildly inflated and easy to mistake for a code regression. Warn, for a non-root user, when rtapi_app is neither setuid root nor carries the cap_sys_nice capability. Closes #4044
1 parent 4415063 commit 8274d2d

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

scripts/latency-histogram

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

114+
proc check_rt_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
119+
if {[catch {which_exe rtapi_app} rtapi]} return ;# cannot locate, stay quiet
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
122+
set msg "Warning: rtapi_app is not setuid root and has no realtime capabilities."
123+
append msg "\nRealtime threads get no SCHED_FIFO priority or locked memory,"
124+
append msg "\nso the latency shown will be far worse than reality."
125+
append msg "\nRun 'sudo make setuid' (or 'sudo make setcap') in src/."
126+
puts stderr $msg
127+
if {$::LH(use_x)} {
128+
package require Tk
129+
tk_messageBox -parent . -icon warning -type ok \
130+
-title "Latency Warning" -message $msg
131+
}
132+
} ;# check_rt_privileges
133+
114134
proc program_check {plist} {
115135
foreach prog $plist {
116136
if [catch {
@@ -944,6 +964,7 @@ proc windowToFile { win } {
944964
if ![info exists ::LH(start)] {
945965
set_defaults
946966
config
967+
check_rt_privileges
947968
progress "Loading packages"
948969
load_packages
949970
signal trap SIGINT finish

scripts/latency-test

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

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.
22+
check_rt_privileges () {
23+
[ "$(id -u)" = 0 ] && return 0 # root has every privilege
24+
rtapi_app=$(command -v rtapi_app) || return 0
25+
[ -n "$rtapi_app" ] || return 0
26+
[ -u "$rtapi_app" ] && return 0 # setuid root
27+
if command -v getcap >/dev/null 2>&1; then
28+
case $(getcap "$rtapi_app" 2>/dev/null) in
29+
*cap_sys_nice*) return 0 ;; # file capabilities
30+
esac
31+
fi
32+
echo "Warning: rtapi_app is not setuid root and has no realtime capabilities." >&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
36+
}
37+
1938
parse_time () {
2039
case $1 in
2140
-) echo "0" ;;
@@ -170,4 +189,6 @@ L=\$(($SIGNALS
170189
echo \$L > "$HOME"/.latency
171190
EOF
172191

192+
check_rt_privileges
193+
173194
halrun lat.hal

0 commit comments

Comments
 (0)