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