1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
set test "sysroot_sysenv"
if {![installtest_p]} { untested "$test"; return }
if {[utrace_p] && [uprobes_p]} {
spawn stap --sysroot=/ --sysenv=PATH=$env(PATH) $srcdir/$subdir/$test.stp ls -c "ls > /dev/null"
expect {
-timeout 180
-re {^process begin\r\n} { pass "$test" }
timeout { fail "$test (timeout)" }
eof { fail "$test" }
}
catch {close}
wait
} else {
untested "$test"
}
# Functions to handle creating, deleting, and using the fake sysroot
# directory we're creating. By using bind mounts, we're "moving" the
# /lib/modules and /usr/lib/debug directories of this system into our
# fake sysroot.
# Clean up the sysroot_sysenv namespace configuration.
proc server_ns_ns_cleanup {} {
global sysroot_base_dir new_modules_dir new_debug_dir
if {[info exists sysroot_base_dir]
&& [string length $sysroot_base_dir]
&& [file isdirectory $sysroot_base_dir]} {
catch {exec rm -rf $sysroot_base_dir}
unset sysroot_base_dir
}
if {[info exists new_modules_dir]
&& [string length $new_modules_dir]
&& [file isdirectory $new_modules_dir]} {
catch {exec rm -rf $new_modules_dir}
unset new_modules_dir
}
if {[info exists new_debug_dir]
&& [string length $new_debug_dir]
&& [file isdirectory $new_debug_dir]} {
catch {exec rm -rf $new_debug_dir}
unset new_debug_dir
}
}
# Set up the sysroot_sysenv namespace configuration.
proc sysroot_sysenv_ns_setup {} {
global sysroot_base_dir new_modules_dir new_debug_dir
# If we've already created the directories, just return.
if {[info exists sysroot_base_dir]
&& [string length $sysroot_base_dir]
&& [info exists new_modules_dir]
&& [string length $new_modules_dir]
&& [info exists new_debug_dir]
&& [string length $new_debug_dir]} {
return 0
}
# Create the directories. We delete them in
# sysroot_sysenv_ns_cleanups().
if {[catch {exec mktemp -d} sysroot_base_dir]} {
verbose -log "sysroot_sysenv_setup - Failed to create sysroot directory: $sysroot_base_dir"
unset sysroot_base_dir
return 1
}
if {[catch {exec mktemp -d} new_modules_dir]} {
verbose -log "sysroot_sysenv_setup - Failed to create modules directory: $new_modules_dir"
unset new_modules_dir
sysroot_sysenv_ns_cleanup
return 1
}
if {[catch {exec mktemp -d} new_debug_dir]} {
verbose -log "sysroot_sysenv_setup - Failed to create debug directory: $new_debug_dir"
unset new_debug_dir
sysroot_sysenv_ns_cleanup
return 1
}
# OK, we've got our sysroot. Populate it with a couple of
# directories.
exec mkdir -p ${sysroot_base_dir}/lib/modules
exec mkdir -p ${sysroot_base_dir}/usr/lib/debug
exec mkdir -p ${sysroot_base_dir}/usr/src/kernels
exec mkdir -p ${sysroot_base_dir}/home
return 0
}
# sysroot_sysenv_ns_run: run 'command' in a custom mount
# namespace. Returns a list of [ RETURN_STATUS OUTPUT ].
proc sysroot_sysenv_ns_run { command } {
global sysroot_base_dir new_modules_dir new_debug_dir
if {[sysroot_sysenv_ns_setup]} { return [list 1 "server_ns_setup failure"] }
# We're going to call "unshare" to create a new mount
# namespace. Then we'll bind mount several host directories down
# into the fake sysroot directory, and bind mount empty
# directories on top of the host directories (to "replace" them).
#
# Note we're not "replacing" /usr/src/kernels, but leaving it on
# the host and putting in the sysroot. This is because there are
# symlinks to it from /usr/lib/debug.
set modules_dir "/lib/modules"
set debug_dir "/usr/lib/debug"
set src_dir "/usr/src/kernels"
set cmd {sudo unshare --mount bash -c\
"mount --bind ${modules_dir} ${sysroot_base_dir}${modules_dir}\
&& mount --bind ${new_modules_dir} ${modules_dir}\
&& mount --bind ${debug_dir} ${sysroot_base_dir}${debug_dir}\
&& mount --bind ${new_debug_dir} ${debug_dir}\
&& mount --bind ${src_dir} ${sysroot_base_dir}${src_dir}\
&& $command"}
verbose -log "running: [subst $cmd]"
set rc 0
if {[catch {eval exec $cmd} output]} {
# non-zero exit status, get it:
if {[lindex $::errorCode 0] eq "CHILDSTATUS"} {
set rc [lindex $::errorCode 2]
}
}
verbose -log "RC: $rc"
verbose -log "$output"
return [list $rc $output]
}
if {[sysroot_sysenv_ns_setup]} { fail "$test - namespace setup" }
# Make sure a basic kernel function probe works with our "sysroot".
set subtest "kernel function probe"
set script "probe kernel.function(\"sys_read\") { println(\"called\") }"
set stap_path $env(SYSTEMTAP_PATH)/stap
lassign [sysroot_sysenv_ns_run "${stap_path} -p4 --sysroot=${sysroot_base_dir} -e '$script'"] rc output
if {$rc == 0} {
pass "$test - $subtest"
} else {
fail "$test - $subtest"
}
# Compile a test executable.
set exepath "${sysroot_base_dir}/home/towers.x"
set res [target_compile $srcdir/$subdir/towers.c $exepath executable \
"additional_flags=-O2 additional_flags=-g"]
if { $res != "" } {
verbose "target_compile failed: $res" 2
fail "$test - compiling towers.c"
} else {
pass "$test compiling towers.c"
}
# Probe our test executable (listed with a full path relative to the
# sysroot).
set subtest "process function list"
set script "probe process(\"/home/towers.x\").function(\"*\") { println(ppfunc()) }"
lassign [sysroot_sysenv_ns_run "${stap_path} --sysroot=${sysroot_base_dir} -p4 -e '$script'"] rc output
if {$rc == 0} {
pass "$test - $subtest"
} else {
fail "$test - $subtest"
}
# Probe our test executable (listed with a wildcard path relative to
# the sysroot).
set subtest "process wildcard function"
set script "probe process(\"/home/tow*\").function(\"*\") {println(ppfunc()) }"
lassign [sysroot_sysenv_ns_run "${stap_path} --sysroot=${sysroot_base_dir}\
-p4 -e '$script'"] rc output
if {$rc == 0} {
pass "$test - $subtest"
} else {
fail "$test - $subtest"
}
# Here we're not using an absolute path, so stap will search the
# path. We'll use the --sysenv option to put the sysroot's /home
# directory in PATH.
set subtest "process path search"
set script "probe process(\"towers.x\").function(\"*\") { println(ppfunc()) }"
lassign [sysroot_sysenv_ns_run "${stap_path} --sysroot=${sysroot_base_dir}\
--sysenv=PATH=/usr/bin:/bin:/usr/sbin:/sbin:/home -p4\
-e '$script'"] rc output
if {$rc == 0} {
pass "$test - $subtest"
} else {
fail "$test - $subtest"
}
# Make sure that all the paths in the generated module source are
# relative to the target sysroot - there should be no host paths
# present.
set subtest "process host path"
set script "probe process(\"/home/towers.x\").function(\"push\").call { printf(\"%s: %d %d\\n\", ppfunc(), \$i, \$s) }"
lassign [sysroot_sysenv_ns_run "${stap_path} --sysroot=${sysroot_base_dir} -p3 -e '$script' | grep '/home/towers.x'"] rc output
if {$rc == 0} {
pass "$test - $subtest - output"
# We've got any mention of the target exe from the stap module C
# source in $output. Search it for any mention of the host path of
# the target executable ("${sysroot_base_dir}/home/towers.x"),
# instead of the target path ("/home/towers.x").
set matches 0
foreach line [split $output "\n"] {
if {[regexp {[^\"]/home/towers.x\"} $line]} {
incr matches
if {$matches == 1} {
verbose -log "the following line(s) have a host path:"
}
verbose -log "$line"
}
}
if {$matches == 0} {
pass "$test - $subtest - no host paths found"
} else {
fail "$test - $subtest - host paths found ($matches)"
}
} else {
fail "$test - $subtest - output"
}
# Now we're going to set up some symbolic links that are only valid
# inside the sysroot:
# /home/towers.lnk2 -> /home/towers.lnk -> /home/towers.x
set subtest "link creation"
lassign [sysroot_sysenv_ns_run "mount --bind ${sysroot_base_dir}/home /home\
&& ln -s /home/towers.x /home/towers.lnk\
&& ln -s /home/towers.lnk /home/towers.lnk2"] rc output
if {$rc == 0} {
pass "$test - $subtest"
} else {
fail "$test - $subtest"
}
# Make sure we can handle the symbolic link relative to the sysroot.
set subtest "symbolic link process function list"
set script "probe process(\"/home/towers.lnk\").function(\"*\") { println(ppfunc()) }"
lassign [sysroot_sysenv_ns_run "${stap_path} --sysroot=${sysroot_base_dir} -p4 -e '$script'"] rc output
if {$rc == 0} {
pass "$test - $subtest"
} else {
fail "$test - $subtest"
}
# Make sure we can handle an indirect symbolic link relative to the
# sysroot.
set subtest "double symbolic link process function list"
set script "probe process(\"/home/towers.lnk2\").function(\"*\") { println(ppfunc()) }"
lassign [sysroot_sysenv_ns_run "${stap_path} --sysroot=${sysroot_base_dir} -p4 -e '$script'"] rc output
if {$rc == 0} {
pass "$test - $subtest"
} else {
fail "$test - $subtest"
}
# Clean everything up.
server_ns_ns_cleanup
|