S02sysctl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/bin/sh
  2. #
  3. # This script is used by busybox and procps-ng.
  4. #
  5. # With procps-ng, the "--system" option of sysctl also enables "--ignore", so
  6. # errors are not reported via syslog. Use the run_logger function to mimic the
  7. # --system behavior, still reporting errors via syslog. Users not interested
  8. # on error reports can add "-e" to SYSCTL_ARGS.
  9. #
  10. # busybox does not have a "--system" option neither reports errors via syslog,
  11. # so the scripting provides a consistent behavior between the implementations.
  12. # Testing the busybox sysctl exit code is fruitless, as at the moment, since
  13. # its exit status is zero even if errors happen. Hopefully this will be fixed
  14. # in a future busybox version.
  15. PROGRAM="sysctl"
  16. SYSCTL_ARGS=""
  17. # shellcheck source=/dev/null
  18. [ -r "/etc/default/$PROGRAM" ] && . "/etc/default/$PROGRAM"
  19. # Files are read from directories in the SYSCTL_SOURCES list, in the given
  20. # order. A file may be used more than once, since there can be multiple
  21. # symlinks to it. No attempt is made to prevent this.
  22. SYSCTL_SOURCES="/etc/sysctl.d/ /usr/local/lib/sysctl.d/ /usr/lib/sysctl.d/ /lib/sysctl.d/ /etc/sysctl.conf"
  23. # If the logger utility is available all messages are sent to syslog, except
  24. # for the final status. The file redirections do the following:
  25. #
  26. # - stdout is redirected to syslog with facility.level "kern.info"
  27. # - stderr is redirected to syslog with facility.level "kern.err"
  28. # - file dscriptor 4 is used to pass the result to the "start" function.
  29. #
  30. run_logger() {
  31. # shellcheck disable=SC2086 # we need the word splitting
  32. find $SYSCTL_SOURCES -maxdepth 1 -name '*.conf' -print0 2> /dev/null | \
  33. xargs -0 -r -n 1 readlink -f | {
  34. prog_status="OK"
  35. while :; do
  36. read -r file || {
  37. echo "$prog_status" >&4
  38. break
  39. }
  40. echo "* Applying $file ..."
  41. /sbin/sysctl $SYSCTL_ARGS -p "$file" || prog_status="FAIL"
  42. done 2>&1 >&3 | /usr/bin/logger -t sysctl -p kern.err
  43. } 3>&1 | /usr/bin/logger -t sysctl -p kern.info
  44. }
  45. # If logger is not available all messages are sent to stdout/stderr.
  46. run_std() {
  47. # shellcheck disable=SC2086 # we need the word splitting
  48. find $SYSCTL_SOURCES -maxdepth 1 -name '*.conf' -print0 2> /dev/null | \
  49. xargs -0 -r -n 1 readlink -f | {
  50. prog_status="OK"
  51. while :; do
  52. read -r file || {
  53. echo "$prog_status" >&4
  54. break
  55. }
  56. echo "* Applying $file ..."
  57. /sbin/sysctl $SYSCTL_ARGS -p "$file" || prog_status="FAIL"
  58. done
  59. }
  60. }
  61. if [ -x /usr/bin/logger ]; then
  62. run_program="run_logger"
  63. else
  64. run_program="run_std"
  65. fi
  66. start() {
  67. printf '%s %s: ' "$1" "$PROGRAM"
  68. status=$("$run_program" 4>&1)
  69. echo "$status"
  70. if [ "$status" = "OK" ]; then
  71. return 0
  72. fi
  73. return 1
  74. }
  75. case "$1" in
  76. start)
  77. start "Running";;
  78. restart|reload)
  79. start "Rerunning";;
  80. stop)
  81. :;;
  82. *)
  83. echo "Usage: $0 {start|stop|restart|reload}"
  84. exit 1
  85. esac