# rlimit tests

# many of these tests use as_root or as_non_root
if {! [installtest_p]} { return }

# To make sure "sudo stap" works correctly, use the full path.
set installed_stap "$env(SYSTEMTAP_PATH)/stap"

# We use -w in the stap invocations so that warnings re. blocklisted functions
# are not emitted (due to rlimit.stp using a broad wildcard).

# Unlimited
set test "rlimit unlimited"
if {[catch { exec $installed_stap -wp4 $srcdir/$subdir/rlimit.stp} res ]} {
     fail $test
} else {
     pass $test
}

# Increase AS limit (as non root)
set test "rlimit AS increase (as non root)"
if {[as_non_root "$installed_stap --rlimit-as=1 --rlimit-as=999999999999 -wp4 $srcdir/$subdir/rlimit.stp" ]} {
     pass $test
} else {
     fail $test
}

# Increase AS limit (as root)
set test "rlimit AS increase (as root)"
if {[as_root "$installed_stap --rlimit-as=1 --rlimit-as=999999999999 -wp4 $srcdir/$subdir/rlimit.stp" ]} {
     fail $test
} else {
     pass $test
}

# Realistic AS limit
set test "rlimit AS realistic"
if {[catch { exec $installed_stap --rlimit-as=614400000 -wp4 $srcdir/$subdir/rlimit.stp} res ]} {
     fail $test
} else {
     pass $test
}

# Small AS limit
set test "rlimit AS small"
if {[catch { exec $installed_stap --rlimit-as=1 -wp4 $srcdir/$subdir/rlimit.stp} res ]} {
     pass $test
} else {
     fail $test
}

# Increase CPU limit (as non root)
set test "rlimit CPU increase (as non root)"
if {[as_non_root "$installed_stap --rlimit-cpu=1 --rlimit-cpu=999999999999 -wp4 $srcdir/$subdir/rlimit.stp" ]} {
     pass $test
} else {
     fail $test
}

# Increase CPU limit (as root)
set test "rlimit CPU increase (as root)"
if {[as_root "$installed_stap --rlimit-cpu=1 --rlimit-cpu=999999999999 -wp4 $srcdir/$subdir/rlimit.stp" ]} {
     fail $test
} else {
     pass $test
}

# Realistic CPU limit
set test "rlimit CPU realistic"
if {[catch { exec $installed_stap --rlimit-cpu=60 -wp4 $srcdir/$subdir/rlimit.stp} res ]} {
     fail $test
} else {
     pass $test
}

# Small CPU limit
set test "rlimit CPU small"
if {[catch { exec $installed_stap --disable-cache --rlimit-cpu=1 -wp4 $srcdir/$subdir/rlimit.stp} res ]} {
     pass $test
} else {
     fail $test
}

# Increase NPROC limit (as non root)
set test "rlimit NPROC increase (as non root)"
if {[as_non_root "$installed_stap --rlimit-nproc=1 --rlimit-nproc=999999999999 -wp4 $srcdir/$subdir/rlimit.stp" ]} {
     pass $test
} else {
     fail $test
}

# Increase NPROC limit (as root)
set test "rlimit NPROC increase (as root)"
if {[as_root "$installed_stap --rlimit-nproc=1 --rlimit-nproc=999999999999 -wp4 $srcdir/$subdir/rlimit.stp" ]} {
     fail $test
} else {
     pass $test
}
# Realistic NPROC limit
set test "rlimit NPROC realistic"
if {[catch { exec $installed_stap --rlimit-nproc=20 -wp4 $srcdir/$subdir/rlimit.stp} res ]} {
     fail $test
} else {
     pass $test
}

# Small NPROC limit
# Did not seem to change functionality

# Increase STACK limit (as non root)
set test "rlimit STACK increase (as non root)"
if {[as_non_root "$installed_stap --rlimit-stack=1 --rlimit-stack=999999999999 -wp4 $srcdir/$subdir/rlimit.stp" ]} {
     pass $test
} else {
     fail $test
}

# Increase STACK limit (as root)
set test "rlimit STACK increase (as root)"
if {[as_root "$installed_stap --rlimit-stack=1 --rlimit-stack=999999999999 -wp4 $srcdir/$subdir/rlimit.stp" ]} {
     fail $test
} else {
     pass $test
}

# Realistic STACK limit
set test "rlimit STACK realistic"
if {[catch { exec $installed_stap --rlimit-stack=1024000 -wp4 $srcdir/$subdir/rlimit.stp} res ]} {
     fail $test
} else {
     pass $test
}

# Small STACK limit
# PR13977. The setrlimit() call in stap succeeds, but some kernels ignore
# the small limit.

# Increase FSIZE limit (as non root)
set test "rlimit FSIZE increase (as non root)"
if {[as_non_root "$installed_stap --rlimit-fsize=1 --rlimit-fsize=999999999999 -wp4 $srcdir/$subdir/rlimit.stp" ]} {
     pass $test
} else {
     fail $test
}

# Increase FSIZE limit (as root)
set test "rlimit FSIZE increase (as root)"
if {[as_root "$installed_stap --rlimit-fsize=1 --rlimit-fsize=999999999999 -wp4 $srcdir/$subdir/rlimit.stp" ]} {
     fail $test
} else {
     pass $test
}

# Realistic FSIZE limit
set test "rlimit FSIZE realistic"
if {[catch { exec $installed_stap --rlimit-fsize=51200000 -wp4 $srcdir/$subdir/rlimit.stp} res ]} {
     fail $test
} else {
     pass $test
}

# Small FSIZE limit
# Did not seem to change functionality
