|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +# For a directory containeing the cgroup slice information, return the value of |
| 6 | +# pids.max, or 0 if set to "max". Return -1 exit code if the file doesn't exist. |
| 7 | +function read_max_pids() { |
| 8 | + if [ ! -f ${1}/pids.max ]; then |
| 9 | + return -1 |
| 10 | + fi |
| 11 | + local max_pids=$(<${1}/pids.max) |
| 12 | + if [ $max_pids == "max" ]; then |
| 13 | + echo 0 |
| 14 | + return |
| 15 | + fi |
| 16 | + echo $max_pids |
| 17 | +} |
| 18 | + |
| 19 | +default_max_pids_limit=999999999 |
| 20 | +max_pids_limit=$default_max_pids_limit |
| 21 | +dirprefix="/sys/fs/cgroup/pids" |
| 22 | + |
| 23 | +for cg in $(grep :pids: /proc/self/cgroup); do |
| 24 | + # Parse out the slice field from the cgroup output. |
| 25 | + # <cgroup_id>:<subystem>:<slice> |
| 26 | + dirsuffix=$(echo "$cg" | awk -F\: '{print $3}') |
| 27 | + |
| 28 | + # The slice field can have a prefix that is not part of the directory path. |
| 29 | + # This must be stripped iteratively until we find the valid slice directory. |
| 30 | + while [ ! -d "${dirprefix}/${dirsuffix}" ]; do |
| 31 | + dirsuffix=${dirsuffix#*/} |
| 32 | + done |
| 33 | + dir="${dirprefix}/${dirsuffix}" |
| 34 | + |
| 35 | + # Start at the current cgroup and traverse up the directory hierarchy |
| 36 | + # reading max.pids in each. The lowest value will be the effective max.pids |
| 37 | + # value. |
| 38 | + while [ -f "${dir}/pids.max" ]; do |
| 39 | + max_pids=$(read_max_pids "${dir}") |
| 40 | + if [[ $max_pids -gt 0 && $max_pids -lt $max_pids_limit ]]; then |
| 41 | + max_pids_limit=$max_pids |
| 42 | + fi |
| 43 | + dir="${dir}/.." |
| 44 | + done |
| 45 | +done |
| 46 | + |
| 47 | +# TBC: Don't fail if we can't determine limit. |
| 48 | +if [ $max_pids_limit -eq $default_max_pids_limit ]; then |
| 49 | + echo "WARNING: Unable to determine effective max.pids limit" |
| 50 | + exit 0 |
| 51 | +fi |
| 52 | + |
| 53 | +# Fail if MINIMUM_MAX_PIDS_LIMIT is set and is greater than current limit. |
| 54 | +if [[ -n "${MINIMUM_MAX_PIDS_LIMIT}" && $MINIMUM_MAX_PIDS_LIMIT -gt $max_pids_limit ]]; then |
| 55 | + echo "ERROR: Effective max.pids limit ($max_pids_limit) less than MINIMUM_MAX_PIDS_LIMIT ($MINIMUM_MAX_PIDS_LIMIT)" |
| 56 | + exit 1 |
| 57 | +fi |
| 58 | + |
| 59 | +if [ -n "${RECOMMENDED_MAX_PIDS_LIMIT}" ]; then |
| 60 | + if [ $RECOMMENDED_MAX_PIDS_LIMIT -gt $max_pids_limit ]; then |
| 61 | + echo "WARNING: Effective max.pids limit ($max_pids_limit) less than RECOMMENDED_MAX_PIDS_LIMIT ($RECOMMENDED_MAX_PIDS_LIMIT)" |
| 62 | + else |
| 63 | + echo "OK: Effective max.pids limit ($max_pids_limit) at least RECOMMENDED_MAX_PIDS_LIMIT ($RECOMMENDED_MAX_PIDS_LIMIT)" |
| 64 | + fi |
| 65 | + exit 0 |
| 66 | +fi |
| 67 | + |
| 68 | +# No requirements set, just output current limit. |
| 69 | +echo "Effective max.pids limit: $max_pids_limit" |
0 commit comments