aboutsummaryrefslogtreecommitdiffstats
path: root/main/openrc/sysfsconf.initd
blob: cefc3595ddb0b6862b9ea826ba422b0589be10f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/sbin/openrc-run

description="Set sysfs variables from /etc/sysfs.conf and /etc/sysfs.d/*.conf"
conffile=/etc/sysfs.conf
confdir=/etc/sysfs.d

depend() {
	need sysfs
}

setval() {
	local value="$1" attrib="$2"
	# Some fields need a terminating newline, others
	# need the terminating newline to be absent :-(
	echo -n "$value" > "$attrib" 2>/dev/null \
		|| echo "$value" > "$attrib"
}

load_conffile() {
	local file="$1"
	while read line; do
		local line=${line%%#*}
		local cmd= attrib= value=
		set -- $line
		if [ $# -eq 0 ]; then
			continue
		fi
		case "$1$3" in
			mode=)	cmd=chmod
				attrib="$2"
				value="$4"
				;;
			owner=) cmd=chown
				attrib="$2"
				value="$4"
				;;
			*)	if [ "$2" = "=" ]; then
					cmd=setval
					attrib="$1"
					value="$3"
				fi
				;; 
		esac
		if ! [ -e "/sys/$attrib" ]; then
			eerror "$attrib: unknown attribute"
			continue
		fi
		if [ -z "$attrib" ] || [ -z "$value" ]; then
			eerror "syntax error in $file: '$line'"
			continue
		fi
		$cmd "$value" "/sys/$attrib"
	done < "$file"
}

start() {
	[ -r "$conffile" -o -d "$confdir" ] || return 0
	ebegin "Setting sysfs variables"
	for file in $conffile $confdir/*.conf; do
		[ -r "$file" ] || continue
		load_conffile "$file" || return 1
	done
	eend 0

}