package sysfsutils: add support for sysfs settings at boot
[openwrt/openwrt.git] / package / libs / sysfsutils / files / sysfsutils
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2017 Rodolfo Giometti <giometti@enneenne.com>
3 #
4 # Based on Debian's script /etc/init.d/sysfsutils by
5 # Martin Pitt <mpitt@debian.org>
6
7 load_conffile() {
8 FILE="$1"
9 sed 's/#.*$//; /^[[:space:]]*$/d;
10 s/^[[:space:]]*\([^=[:space:]]*\)[[:space:]]*\([^=[:space:]]*\)[[:space:]]*=[[:space:]]*\(.*\)/\1 \2 \3/' \
11 $FILE | {
12 while read f1 f2 f3; do
13 if [ "$f1" = "mode" -a -n "$f2" -a -n "$f3" ]; then
14 if [ -f "/sys/$f2" ] || [ -d "/sys/$f2" ]; then
15 chmod "$f3" "/sys/$f2"
16 else
17 echo "unknown attribute $f2"
18 fi
19 elif [ "$f1" = "owner" -a -n "$f2" -a -n "$f3" ]; then
20 if [ -f "/sys/$f2" ]; then
21 chown "$f3" "/sys/$f2"
22 else
23 echo "unknown attribute $f2"
24 fi
25 elif [ "$f1" -a -n "$f2" -a -z "$f3" ]; then
26 if [ -f "/sys/$f1" ]; then
27 # Some fields need a terminating newline, others
28 # need the terminating newline to be absent :-(
29 echo -n "$f2" > "/sys/$f1" 2>/dev/null ||
30 echo "$f2" > "/sys/$f1"
31 else
32 echo "unknown attribute $f1"
33 fi
34 else
35 echo "syntax error in $CONFFILE: '$f1' '$f2' '$f3'"
36 exit 1
37 fi
38 done
39 }
40 }
41
42 START=11
43 start() {
44 for file in /etc/sysfs.conf /etc/sysfs.d/*.conf; do
45 [ -r "$file" ] || continue
46 load_conffile "$file"
47 done
48 }