base-files: fix error message during boot
[openwrt/openwrt.git] / package / base-files / files / lib / functions.sh
1 #!/bin/sh
2 # Copyright (C) 2006-2014 OpenWrt.org
3 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
4 # Copyright (C) 2010 Vertical Communications
5
6
7 debug () {
8 ${DEBUG:-:} "$@"
9 }
10
11 # newline
12 N="
13 "
14
15 _C=0
16 NO_EXPORT=1
17 LOAD_STATE=1
18 LIST_SEP=" "
19
20 append() {
21 local var="$1"
22 local value="$2"
23 local sep="${3:- }"
24
25 eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
26 }
27
28 list_contains() {
29 local var="$1"
30 local str="$2"
31 local val
32
33 eval "val=\" \${$var} \""
34 [ "${val%% $str *}" != "$val" ]
35 }
36
37 config_load() {
38 [ -n "$IPKG_INSTROOT" ] && return 0
39 uci_load "$@"
40 }
41
42 reset_cb() {
43 config_cb() { return 0; }
44 option_cb() { return 0; }
45 list_cb() { return 0; }
46 }
47 reset_cb
48
49 package() {
50 return 0
51 }
52
53 config () {
54 local cfgtype="$1"
55 local name="$2"
56
57 export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
58 name="${name:-cfg$CONFIG_NUM_SECTIONS}"
59 append CONFIG_SECTIONS "$name"
60 [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
61 export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
62 export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
63 }
64
65 option () {
66 local varname="$1"; shift
67 local value="$*"
68
69 export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
70 [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
71 }
72
73 list() {
74 local varname="$1"; shift
75 local value="$*"
76 local len
77
78 config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0
79 [ $len = 0 ] && append CONFIG_LIST_STATE "${CONFIG_SECTION}_${varname}"
80 len=$(($len + 1))
81 config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"
82 config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"
83 append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"
84 list_cb "$varname" "$*"
85 }
86
87 config_unset() {
88 config_set "$1" "$2" ""
89 }
90
91 # config_get <variable> <section> <option> [<default>]
92 # config_get <section> <option>
93 config_get() {
94 case "$3" in
95 "") eval echo "\${CONFIG_${1}_${2}:-\${4}}";;
96 *) eval export ${NO_EXPORT:+-n} -- "${1}=\${CONFIG_${2}_${3}:-\${4}}";;
97 esac
98 }
99
100 # config_get_bool <variable> <section> <option> [<default>]
101 config_get_bool() {
102 local _tmp
103 config_get _tmp "$2" "$3" "$4"
104 case "$_tmp" in
105 1|on|true|yes|enabled) _tmp=1;;
106 0|off|false|no|disabled) _tmp=0;;
107 *) _tmp="$4";;
108 esac
109 export ${NO_EXPORT:+-n} "$1=$_tmp"
110 }
111
112 config_set() {
113 local section="$1"
114 local option="$2"
115 local value="$3"
116 local old_section="$CONFIG_SECTION"
117
118 CONFIG_SECTION="$section"
119 option "$option" "$value"
120 CONFIG_SECTION="$old_section"
121 }
122
123 config_foreach() {
124 local ___function="$1"
125 [ "$#" -ge 1 ] && shift
126 local ___type="$1"
127 [ "$#" -ge 1 ] && shift
128 local section cfgtype
129
130 [ -z "$CONFIG_SECTIONS" ] && return 0
131 for section in ${CONFIG_SECTIONS}; do
132 config_get cfgtype "$section" TYPE
133 [ -n "$___type" -a "x$cfgtype" != "x$___type" ] && continue
134 eval "$___function \"\$section\" \"\$@\""
135 done
136 }
137
138 config_list_foreach() {
139 [ "$#" -ge 3 ] || return 0
140 local section="$1"; shift
141 local option="$1"; shift
142 local function="$1"; shift
143 local val
144 local len
145 local c=1
146
147 config_get len "${section}" "${option}_LENGTH"
148 [ -z "$len" ] && return 0
149 while [ $c -le "$len" ]; do
150 config_get val "${section}" "${option}_ITEM$c"
151 eval "$function \"\$val\" \"\$@\""
152 c="$(($c + 1))"
153 done
154 }
155
156 insert_modules() {
157 for m in $*; do
158 if [ -f /etc/modules.d/$m ]; then
159 sed 's/^[^#]/insmod &/' /etc/modules.d/$m | ash 2>&- || :
160 else
161 modprobe $m
162 fi
163 done
164 }
165
166 default_prerm() {
167 local name
168 name=$(basename ${1%.*})
169 [ -f /usr/lib/opkg/info/${name}.prerm-pkg ] && . /usr/lib/opkg/info/${name}.prerm-pkg
170 for i in `cat /usr/lib/opkg/info/${name}.list | grep "^/etc/init.d/"`; do
171 $i disable
172 $i stop
173 done
174 }
175
176 default_postinst() {
177 local root="${IPKG_INSTROOT}"
178 local pkgname="$(basename ${1%.*})"
179 local rusers="$(sed -ne 's/^Require-User: *//p' $root/usr/lib/opkg/info/${pkgname}.control 2>/dev/null)"
180 local ret=0
181
182 if [ -n "$rusers" ]; then
183 local tuple oIFS="$IFS"
184 for tuple in $rusers; do
185 local uid gid uname gname
186
187 IFS=":"
188 set -- $tuple; uname="$1"; gname="$2"
189 IFS="="
190 set -- $uname; uname="$1"; uid="$2"
191 set -- $gname; gname="$1"; gid="$2"
192 IFS="$oIFS"
193
194 if [ -n "$gname" ] && [ -n "$gid" ]; then
195 group_exists "$gname" || group_add "$gname" "$gid"
196 elif [ -n "$gname" ]; then
197 group_add_next "$gname"; gid=$?
198 fi
199
200 if [ -n "$uname" ]; then
201 user_exists "$uname" || user_add "$uname" "$uid" "$gid"
202 fi
203
204 if [ -n "$uname" ] && [ -n "$gname" ]; then
205 group_add_user "$gname" "$uname"
206 fi
207
208 unset uid gid uname gname
209 done
210 fi
211
212 if [ -f "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" ]; then
213 ( . "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" )
214 ret=$?
215 fi
216
217 [ -n "$root" ] || rm -f /tmp/luci-indexcache 2>/dev/null
218
219 if [ "$PKG_UPGRADE" != "1" ]; then
220 local shell="$(which bash)"
221 for i in $(grep -s "^/etc/init.d/" "$root/usr/lib/opkg/info/${pkgname}.list"); do
222 if [ -n "$root" ]; then
223 ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" enable
224 else
225 "$i" enable
226 "$i" start
227 fi
228 done
229 fi
230
231 return $ret
232 }
233
234 include() {
235 local file
236
237 for file in $(ls $1/*.sh 2>/dev/null); do
238 . $file
239 done
240 }
241
242 find_mtd_index() {
243 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
244 local INDEX="${PART##mtd}"
245
246 echo ${INDEX}
247 }
248
249 find_mtd_part() {
250 local INDEX=$(find_mtd_index "$1")
251 local PREFIX=/dev/mtdblock
252
253 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
254 echo "${INDEX:+$PREFIX$INDEX}"
255 }
256
257 group_add() {
258 local name="$1"
259 local gid="$2"
260 local rc
261 [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1
262 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group
263 echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group
264 rc=$?
265 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
266 return $rc
267 }
268
269 group_exists() {
270 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
271 }
272
273 group_add_next() {
274 local gid gids
275 gid=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
276 [ -n "$gid" ] && return $gid
277 gids=$(cat ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
278 gid=100
279 while [ -n "$(echo $gids | grep $gid)" ] ; do
280 gid=$((gid + 1))
281 done
282 group_add $1 $gid
283 return $gid
284 }
285
286 group_add_user() {
287 local grp delim=","
288 grp=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group)
289 [ -z "$(echo $grp | cut -d: -f4 | grep $2)" ] || return
290 [ -n "$(echo $grp | grep ":$")" ] && delim=""
291 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
292 sed -i "s/$grp/$grp$delim$2/g" ${IPKG_INSTROOT}/etc/group
293 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
294 }
295
296 user_add() {
297 local name="${1}"
298 local uid="${2}"
299 local gid="${3}"
300 local desc="${4:-$1}"
301 local home="${5:-/var/run/$1}"
302 local shell="${6:-/bin/false}"
303 local rc
304 [ -z "$uid" ] && {
305 uids=$(cat ${IPKG_INSTROOT}/etc/passwd | cut -d: -f3)
306 uid=100
307 while [ -n "$(echo $uids | grep $uid)" ] ; do
308 uid=$((uid + 1))
309 done
310 }
311 [ -z "$gid" ] && gid=$uid
312 [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1
313 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
314 echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd
315 echo "${name}:x:0:0:99999:7:::" >> ${IPKG_INSTROOT}/etc/shadow
316 rc=$?
317 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
318 return $rc
319 }
320
321 user_exists() {
322 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
323 }
324
325 [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh