1# 2# Copyright (c) 2022-2023 Klara, Inc. 3# 4# SPDX-License-Identifier: BSD-2-Clause 5# 6 7# Name of user to use for -u tests when running as root. Beware that 8# processes owned by that user will be affected by the test. 9TEST_USER=nobody 10 11_renice() { 12 atf_check -o empty -e ignore -s exit:0 renice "$@" 13} 14 15atf_check_nice_value() { 16 local pid=$1 17 local nice=$2 18 atf_check test "$(ps -o nice= -p $pid)" -eq "$nice" 19} 20 21atf_test_case renice_abs_pid 22renice_abs_pid_head() { 23 atf_set "descr" "Set a process's nice number to an absolute value" 24} 25renice_abs_pid_body() { 26 local pid nice incr 27 sleep 60 & 28 pid=$! 29 nice="$(ps -o nice= -p $pid)" 30 incr=3 31 _renice $((nice+incr)) $pid 32 atf_check_nice_value $pid $((nice+incr)) 33 kill $pid 34} 35 36atf_test_case renice_rel_pid 37renice_rel_pid_head() { 38 atf_set "descr" "Change a process's nice number by a relative value" 39} 40renice_rel_pid_body() { 41 local pid nice incr 42 sleep 60 & 43 pid=$! 44 nice="$(ps -o nice= -p $pid)" 45 incr=3 46 _renice -n $incr $pid 47 _renice -p -n $incr $pid 48 _renice -n $incr -p $pid 49 atf_check_nice_value $pid $((nice+incr+incr+incr)) 50 kill $pid 51} 52 53atf_test_case renice_abs_pgid 54renice_abs_pgid_head() { 55 atf_set "descr" "Set a process group's nice number to an absolute value" 56} 57renice_abs_pgid_body() { 58 local pid pgid nice incr 59 # make sure target runs in a different pgrp than ours 60 pid=$(sh -mc "sleep 60 >/dev/null & echo \$!") 61 pgid="$(ps -o pgid= -p $pid)" 62 nice="$(ps -o nice= -p $pid)" 63 incr=3 64 _renice $((nice+incr)) -g $pgid 65 atf_check_nice_value $pid $((nice+incr)) 66 kill $pid 67} 68 69atf_test_case renice_rel_pgid 70renice_rel_pgid_head() { 71 atf_set "descr" "Change a process group's nice number by a relative value" 72} 73renice_rel_pgid_body() { 74 local pid pgid nice incr 75 # make sure target runs in a different pgrp than ours 76 pid=$(sh -mc "sleep 60 >/dev/null & echo \$!") 77 pgid="$(ps -o pgid= -p $pid)" 78 nice="$(ps -o nice= -p $pid)" 79 incr=3 80 _renice -g -n $incr $pgid 81 _renice -n $incr -g $pgid 82 atf_check_nice_value $pid $((nice+incr+incr)) 83 kill $pid 84} 85 86atf_test_case renice_abs_user 87renice_abs_user_head() { 88 atf_set "descr" "Set a user's processes' nice numbers to an absolute value" 89 atf_set "require.user" "root" 90} 91renice_abs_user_body() { 92 local user pid nice incr 93 pid=$(su -m $TEST_USER -c "/bin/sh -c 'sleep 60 >/dev/null & echo \$!'") 94 nice="$(ps -o nice= -p $pid)" 95 incr=3 96 _renice $((nice+incr)) -u $TEST_USER 97 atf_check_nice_value $pid $((nice+incr)) 98 kill $pid 99} 100 101atf_test_case renice_rel_user 102renice_rel_user_head() { 103 atf_set "descr" "Change a user's processes' nice numbers by a relative value" 104 atf_set "require.user" "root" 105} 106renice_rel_user_body() { 107 local user pid nice incr 108 pid=$(su -m $TEST_USER -c "/bin/sh -c 'sleep 60 >/dev/null & echo \$!'") 109 nice="$(ps -o nice= -p $pid)" 110 incr=3 111 _renice -u -n $incr $TEST_USER 112 _renice -n $incr -u $TEST_USER 113 atf_check_nice_value $pid $((nice+incr+incr)) 114 kill $pid 115} 116 117atf_test_case renice_delim 118renice_delim_head() { 119 atf_set "descr" "Test various delimiter positions" 120} 121renice_delim_body() { 122 local pid nice incr 123 sleep 60 & 124 pid=$! 125 nice="$(ps -o nice= -p $pid)" 126 incr=0 127 # without -p 128 : $((incr=incr+1)) 129 _renice -- $((nice+incr)) $pid 130 atf_check_nice_value $pid $((nice+incr)) 131 : $((incr=incr+1)) 132 _renice $((nice+incr)) -- $pid 133 atf_check_nice_value $pid $((nice+incr)) 134 : $((incr=incr+1)) 135 _renice $((nice+incr)) $pid -- 136 atf_check_nice_value $pid $((nice+incr)) 137 # with -p 138 : $((incr=incr+1)) 139 _renice -p -- $((nice+incr)) $pid 140 atf_check_nice_value $pid $((nice+incr)) 141 : $((incr=incr+1)) 142 _renice -p $((nice+incr)) -- $pid 143 atf_check_nice_value $pid $((nice+incr)) 144 : $((incr=incr+1)) 145 _renice -p $((nice+incr)) $pid -- 146 atf_check_nice_value $pid $((nice+incr)) 147 : $((incr=incr+1)) 148 _renice $((nice+incr)) -p -- $pid 149 atf_check_nice_value $pid $((nice+incr)) 150 : $((incr=incr+1)) 151 _renice $((nice+incr)) -p $pid -- 152 atf_check_nice_value $pid $((nice+incr)) 153 kill $pid 154} 155 156atf_test_case renice_incr_noarg 157renice_incr_noarg_head() { 158 atf_set "descr" "Do not segfault if -n is given without an argument" 159} 160renice_incr_noarg_body() { 161 atf_check -o empty -e ignore -s exit:1 renice -n 162} 163 164atf_init_test_cases() { 165 atf_add_test_case renice_abs_pid 166 atf_add_test_case renice_rel_pid 167 atf_add_test_case renice_abs_pgid 168 atf_add_test_case renice_rel_pgid 169 atf_add_test_case renice_abs_user 170 atf_add_test_case renice_rel_user 171 atf_add_test_case renice_delim 172 atf_add_test_case renice_incr_noarg 173} 174