1#!/bin/sh 2#- 3# Copyright (c) 2022 Klara, Inc. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 9# 1. Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# 2. Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# 15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25# SUCH DAMAGE. 26# 27 28# Name of user to use for -u tests when running as root. Beware that 29# processes owned by that user will be affected by the test. 30TEST_USER=nobody 31 32_renice() { 33 atf_check -o empty -e ignore -s exit:0 renice "$@" 34} 35 36# Set a process's nice number to an absolute value 37atf_test_case renice_abs_pid 38renice_abs_pid_body() { 39 local pid nice incr 40 sleep 60 & 41 pid=$! 42 nice="$(ps -o nice= -p $pid)" 43 incr=3 44 _renice $((nice+incr)) $pid 45 atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)" 46 kill $pid 47} 48 49# Change a process's nice number by a relative value 50atf_test_case renice_rel_pid 51renice_rel_pid_body() { 52 local pid nice incr 53 sleep 60 & 54 pid=$! 55 nice="$(ps -o nice= -p $pid)" 56 incr=3 57 _renice -n $incr $pid 58 _renice -p -n $incr $pid 59 _renice -n $incr -p $pid 60 atf_check_equal $((nice+incr+incr+incr)) "$(ps -o nice= -p $pid)" 61 kill $pid 62} 63 64# Set a process group's nice number to an absolute value 65atf_test_case renice_abs_pgid 66renice_abs_pgid_body() { 67 local pid pgid nice incr 68 # make sure target runs in a different pgrp than ours 69 pid=$(sh -mc "sleep 60 >/dev/null & echo \$!") 70 pgid="$(ps -o pgid= -p $pid)" 71 nice="$(ps -o nice= -p $pid)" 72 incr=3 73 _renice $((nice+incr)) -g $pgid 74 atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)" 75 kill $pid 76} 77 78# Change a process group's nice number by a relative value 79atf_test_case renice_rel_pgid 80renice_rel_pgid_body() { 81 local pid pgid nice incr 82 # make sure target runs in a different pgrp than ours 83 pid=$(sh -mc "sleep 60 >/dev/null & echo \$!") 84 pgid="$(ps -o pgid= -p $pid)" 85 nice="$(ps -o nice= -p $pid)" 86 incr=3 87 _renice -g -n $incr $pgid 88 _renice -n $incr -g $pgid 89 atf_check_equal $((nice+incr+incr)) "$(ps -o nice= -p $pid)" 90 kill $pid 91} 92 93# Set a user's processes' nice numbers to an absolute value 94atf_test_case renice_abs_user 95renice_abs_user_head() { 96 atf_set "require.user" "root" 97} 98renice_abs_user_body() { 99 local user pid nice incr 100 pid=$(su -m $TEST_USER -c "sleep 60 >/dev/null & echo \$!") 101 nice="$(ps -o nice= -p $pid)" 102 incr=3 103 _renice $((nice+incr)) -u $TEST_USER 104 atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)" 105 kill $pid 106} 107 108# Change a user's processes' nice numbers by a relative value 109atf_test_case renice_rel_user 110renice_rel_user_head() { 111 atf_set "require.user" "root" 112} 113renice_rel_user_body() { 114 local user pid nice incr 115 pid=$(su -m $TEST_USER -c "sleep 60 >/dev/null & echo \$!") 116 nice="$(ps -o nice= -p $pid)" 117 incr=3 118 _renice -u -n $incr $TEST_USER 119 _renice -n $incr -u $TEST_USER 120 atf_check_equal $((nice+incr+incr)) "$(ps -o nice= -p $pid)" 121 kill $pid 122} 123 124# Test various delimiter positions 125atf_test_case renice_delim 126renice_delim_body() { 127 local pid nice incr 128 sleep 60 & 129 pid=$! 130 nice="$(ps -o nice= -p $pid)" 131 incr=0 132 # without -p 133 : $((incr=incr+1)) 134 _renice -- $((nice+incr)) $pid 135 atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)" 136 : $((incr=incr+1)) 137 _renice $((nice+incr)) -- $pid 138 atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)" 139 : $((incr=incr+1)) 140 _renice $((nice+incr)) $pid -- 141 atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)" 142 # with -p 143 : $((incr=incr+1)) 144 _renice -p -- $((nice+incr)) $pid 145 atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)" 146 : $((incr=incr+1)) 147 _renice -p $((nice+incr)) -- $pid 148 atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)" 149 : $((incr=incr+1)) 150 _renice -p $((nice+incr)) $pid -- 151 atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)" 152 : $((incr=incr+1)) 153 _renice $((nice+incr)) -p -- $pid 154 atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)" 155 : $((incr=incr+1)) 156 _renice $((nice+incr)) -p $pid -- 157 atf_check_equal $((nice+incr)) "$(ps -o nice= -p $pid)" 158 kill $pid 159} 160 161atf_init_test_cases() { 162 atf_add_test_case renice_abs_pid 163 atf_add_test_case renice_rel_pid 164 atf_add_test_case renice_abs_pgid 165 atf_add_test_case renice_rel_pgid 166 atf_add_test_case renice_abs_user 167 atf_add_test_case renice_rel_user 168 atf_add_test_case renice_delim 169} 170