Last active
July 21, 2017 19:08
-
-
Save PeterFaiman/5b67c530b0ffa009ebef904ed0678e26 to your computer and use it in GitHub Desktop.
Emulate the sysctl -p flag from Linux on OpenBSD.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Emulate the sysctl -p flag from Linux. To use it as the default sysctl | |
# command, name it sysctl and place it in your path before /sbin. | |
# The functions stripcom, update_limit, and sysctl_conf are copied from /etc/rc | |
# from an OpenBSD 6.1-current snapshot. Replace them with the definitions from | |
# your own /etc/rc to be sure you get the correct behavior. You MUST then | |
# modify sysctl_conf in two ways: | |
# | |
# * change the reference to /etc/sysctl.conf to "$1" so the function can be | |
# used on arbitrary files | |
# * change the call to sysctl to /sbin/sysctl so this script won't call itself | |
# recursively | |
# BEGIN COPIED FROM /etc/rc | |
# Turn off Strict Bourne shell. | |
set +o sh | |
# Strip in- and whole-line comments from a file. | |
# Strip leading and trailing whitespace if IFS is set. | |
# Usage: stripcom /path/to/file | |
stripcom() { | |
local _file=$1 _line | |
[[ -s $_file ]] || return | |
while read _line ; do | |
_line=${_line%%#*} | |
[[ -n $_line ]] && print -r -- "$_line" | |
done <$_file | |
} | |
# Update resource limits based on login.conf settings. | |
# Usage: update_limit -flag capability | |
update_limit() { | |
local _flag=$1 # ulimit flag | |
local _cap=$2 _val # login.conf capability and its value | |
local _suffix | |
for _suffix in {,-max,-cur}; do | |
_val=$(getcap -f /etc/login.conf -s ${_cap}${_suffix} daemon 2>/dev/null) | |
[[ -n $_val ]] || continue | |
[[ $_val == infinity ]] && _val=unlimited | |
case $_suffix in | |
-cur) ulimit -S $_flag $_val | |
;; | |
-max) ulimit -H $_flag $_val | |
;; | |
*) ulimit $_flag $_val | |
return | |
;; | |
esac | |
done | |
} | |
# Apply sysctl.conf(5) settings. | |
sysctl_conf() { | |
stripcom "$1" | # was: stripcom /etc/sysctl.conf | |
while read _line; do | |
/sbin/sysctl "$_line" # was: sysctl "$_line" | |
case $_line in | |
kern.maxproc=*) | |
update_limit -p maxproc;; | |
kern.maxfiles=*) | |
update_limit -n openfiles;; | |
esac | |
done | |
} | |
# END COPIED FROM /etc/rc | |
# Pretend to be Linux sysctl by handling -p flag. | |
if [[ $# -gt 0 && "$1" = "-p" ]]; then | |
shift | |
if [[ $# -eq 0 ]]; then | |
sysctl_conf /etc/sysctl.conf | |
else | |
for sysctls_file in "$@"; do | |
if [[ -f "$sysctls_file" ]]; then | |
sysctl_conf "$sysctls_file" | |
else | |
echo "sysctl: cannot open \"$sysctls_file\": No such file or directory" >&2 | |
fi | |
done | |
fi | |
else | |
exec /sbin/sysctl "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment