4b1b838572a0cd6060856d44fcf9e5da4d14c697
[openwrt/openwrt.git] / package / base-files / files / lib / functions.sh
1 # Copyright (C) 2006-2014 OpenWrt.org
2 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
3 # Copyright (C) 2010 Vertical Communications
4
5
6 debug () {
7 ${DEBUG:-:} "$@"
8 }
9
10 # newline
11 N="
12 "
13
14 _C=0
15 NO_EXPORT=1
16 LOAD_STATE=1
17 LIST_SEP=" "
18
19 # xor multiple hex values of the same length
20 xor() {
21 local val
22 local ret="0x$1"
23 local retlen=${#1}
24
25 shift
26 while [ -n "$1" ]; do
27 val="0x$1"
28 ret=$((ret ^ val))
29 shift
30 done
31
32 printf "%0${retlen}x" "$ret"
33 }
34
35 append() {
36 local var="$1"
37 local value="$2"
38 local sep="${3:- }"
39
40 eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
41 }
42
43 list_contains() {
44 local var="$1"
45 local str="$2"
46 local val
47
48 eval "val=\" \${$var} \""
49 [ "${val%% $str *}" != "$val" ]
50 }
51
52 config_load() {
53 [ -n "$IPKG_INSTROOT" ] && return 0
54 uci_load "$@"
55 }
56
57 reset_cb() {
58 config_cb() { return 0; }
59 option_cb() { return 0; }
60 list_cb() { return 0; }
61 }
62 reset_cb
63
64 package() {
65 return 0
66 }
67
68 config () {
69 local cfgtype="$1"
70 local name="$2"
71
72 export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$((CONFIG_NUM_SECTIONS + 1))
73 name="${name:-cfg$CONFIG_NUM_SECTIONS}"
74 append CONFIG_SECTIONS "$name"
75 export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
76 config_set "$CONFIG_SECTION" "TYPE" "${cfgtype}"
77 [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
78 }
79
80 option () {
81 local varname="$1"; shift
82 local value="$*"
83
84 config_set "$CONFIG_SECTION" "${varname}" "${value}"
85 [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
86 }
87
88 list() {
89 local varname="$1"; shift
90 local value="$*"
91 local len
92
93 config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0
94 [ $len = 0 ] && append CONFIG_LIST_STATE "${CONFIG_SECTION}_${varname}"
95 len=$((len + 1))
96 config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"
97 config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"
98 append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"
99 [ -n "$NO_CALLBACK" ] || list_cb "$varname" "$*"
100 }
101
102 config_unset() {
103 config_set "$1" "$2" ""
104 }
105
106 # config_get <variable> <section> <option> [<default>]
107 # config_get <section> <option>
108 config_get() {
109 case "$2${3:-$1}" in
110 *[!A-Za-z0-9_]*) : ;;
111 *)
112 case "$3" in
113 "") eval echo "\"\${CONFIG_${1}_${2}:-\${4}}\"";;
114 *) eval export ${NO_EXPORT:+-n} -- "${1}=\${CONFIG_${2}_${3}:-\${4}}";;
115 esac
116 ;;
117 esac
118 }
119
120 # get_bool <value> [<default>]
121 get_bool() {
122 local _tmp="$1"
123 case "$_tmp" in
124 1|on|true|yes|enabled) _tmp=1;;
125 0|off|false|no|disabled) _tmp=0;;
126 *) _tmp="$2";;
127 esac
128 echo -n "$_tmp"
129 }
130
131 # config_get_bool <variable> <section> <option> [<default>]
132 config_get_bool() {
133 local _tmp
134 config_get _tmp "$2" "$3" "$4"
135 _tmp="$(get_bool "$_tmp" "$4")"
136 export ${NO_EXPORT:+-n} "$1=$_tmp"
137 }
138
139 config_set() {
140 local section="$1"
141 local option="$2"
142 local value="$3"
143
144 export ${NO_EXPORT:+-n} "CONFIG_${section}_${option}=${value}"
145 }
146
147 config_foreach() {
148 local ___function="$1"
149 [ "$#" -ge 1 ] && shift
150 local ___type="$1"
151 [ "$#" -ge 1 ] && shift
152 local section cfgtype
153
154 [ -z "$CONFIG_SECTIONS" ] && return 0
155 for section in ${CONFIG_SECTIONS}; do
156 config_get cfgtype "$section" TYPE
157 [ -n "$___type" ] && [ "x$cfgtype" != "x$___type" ] && continue
158 eval "$___function \"\$section\" \"\$@\""
159 done
160 }
161
162 config_list_foreach() {
163 [ "$#" -ge 3 ] || return 0
164 local section="$1"; shift
165 local option="$1"; shift
166 local function="$1"; shift
167 local val
168 local len
169 local c=1
170
171 config_get len "${section}" "${option}_LENGTH"
172 [ -z "$len" ] && return 0
173 while [ $c -le "$len" ]; do
174 config_get val "${section}" "${option}_ITEM$c"
175 eval "$function \"\$val\" \"\$@\""
176 c="$((c + 1))"
177 done
178 }
179
180 default_prerm() {
181 local root="${IPKG_INSTROOT}"
182 local pkgname="$(basename ${1%.*})"
183 local ret=0
184
185 if [ -f "$root/usr/lib/opkg/info/${pkgname}.prerm-pkg" ]; then
186 ( . "$root/usr/lib/opkg/info/${pkgname}.prerm-pkg" )
187 ret=$?
188 fi
189
190 local shell="$(command -v bash)"
191 for i in $(grep -s "^/etc/init.d/" "$root/usr/lib/opkg/info/${pkgname}.list"); do
192 if [ -n "$root" ]; then
193 ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" disable
194 else
195 if [ "$PKG_UPGRADE" != "1" ]; then
196 "$i" disable
197 fi
198 "$i" stop
199 fi
200 done
201
202 return $ret
203 }
204
205 add_group_and_user() {
206 local pkgname="$1"
207 local rusers="$(sed -ne 's/^Require-User: *//p' $root/usr/lib/opkg/info/${pkgname}.control 2>/dev/null)"
208
209 if [ -n "$rusers" ]; then
210 local tuple oIFS="$IFS"
211 for tuple in $rusers; do
212 local uid gid uname gname addngroups addngroup addngname addngid
213
214 IFS=":"
215 set -- $tuple; uname="$1"; gname="$2"; addngroups="$3"
216 IFS="="
217 set -- $uname; uname="$1"; uid="$2"
218 set -- $gname; gname="$1"; gid="$2"
219 IFS="$oIFS"
220
221 if [ -n "$gname" ] && [ -n "$gid" ]; then
222 group_exists "$gname" || group_add "$gname" "$gid"
223 elif [ -n "$gname" ]; then
224 gid="$(group_add_next "$gname")"
225 fi
226
227 if [ -n "$uname" ]; then
228 user_exists "$uname" || user_add "$uname" "$uid" "$gid"
229 fi
230
231 if [ -n "$uname" ] && [ -n "$gname" ]; then
232 group_add_user "$gname" "$uname"
233 fi
234
235 if [ -n "$uname" ] && [ -n "$addngroups" ]; then
236 oIFS="$IFS"
237 IFS=","
238 for addngroup in $addngroups ; do
239 IFS="="
240 set -- $addngroup; addngname="$1"; addngid="$2"
241 if [ -n "$addngid" ]; then
242 group_exists "$addngname" || group_add "$addngname" "$addngid"
243 else
244 group_add_next "$addngname"
245 fi
246
247 group_add_user "$addngname" "$uname"
248 done
249 IFS="$oIFS"
250 fi
251
252 unset uid gid uname gname addngroups addngroup addngname addngid
253 done
254 fi
255 }
256
257 default_postinst() {
258 local root="${IPKG_INSTROOT}"
259 local pkgname="$(basename ${1%.*})"
260 local filelist="/usr/lib/opkg/info/${pkgname}.list"
261 local ret=0
262
263 add_group_and_user "${pkgname}"
264
265 if [ -f "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" ]; then
266 ( . "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" )
267 ret=$?
268 fi
269
270 if [ -d "$root/rootfs-overlay" ]; then
271 cp -R $root/rootfs-overlay/. $root/
272 rm -fR $root/rootfs-overlay/
273 fi
274
275 if [ -z "$root" ]; then
276 if grep -m1 -q -s "^/etc/modules.d/" "$filelist"; then
277 kmodloader
278 fi
279
280 if grep -m1 -q -s "^/etc/sysctl.d/" "$filelist"; then
281 /etc/init.d/sysctl restart
282 fi
283
284 if grep -m1 -q -s "^/etc/uci-defaults/" "$filelist"; then
285 [ -d /tmp/.uci ] || mkdir -p /tmp/.uci
286 for i in $(grep -s "^/etc/uci-defaults/" "$filelist"); do
287 ( [ -f "$i" ] && cd "$(dirname $i)" && . "$i" ) && rm -f "$i"
288 done
289 uci commit
290 fi
291
292 rm -f /tmp/luci-indexcache
293 fi
294
295 local shell="$(command -v bash)"
296 for i in $(grep -s "^/etc/init.d/" "$root$filelist"); do
297 if [ -n "$root" ]; then
298 ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" enable
299 else
300 if [ "$PKG_UPGRADE" != "1" ]; then
301 "$i" enable
302 fi
303 "$i" start
304 fi
305 done
306
307 return $ret
308 }
309
310 include() {
311 local file
312
313 for file in $(ls $1/*.sh 2>/dev/null); do
314 . $file
315 done
316 }
317
318 find_mtd_index() {
319 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
320 local INDEX="${PART##mtd}"
321
322 echo ${INDEX}
323 }
324
325 find_mtd_part() {
326 local INDEX=$(find_mtd_index "$1")
327 local PREFIX=/dev/mtdblock
328
329 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
330 echo "${INDEX:+$PREFIX$INDEX}"
331 }
332
333 find_mmc_part() {
334 local DEVNAME PARTNAME ROOTDEV
335
336 if grep -q "$1" /proc/mtd; then
337 echo "" && return 0
338 fi
339
340 if [ -n "$2" ]; then
341 ROOTDEV="$2"
342 else
343 ROOTDEV="mmcblk*"
344 fi
345
346 for DEVNAME in /sys/block/$ROOTDEV/mmcblk*p*; do
347 PARTNAME="$(grep PARTNAME ${DEVNAME}/uevent | cut -f2 -d'=')"
348 [ "$PARTNAME" = "$1" ] && echo "/dev/$(basename $DEVNAME)" && return 0
349 done
350 }
351
352 group_add() {
353 local name="$1"
354 local gid="$2"
355 local rc
356 [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1
357 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group
358 echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group
359 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
360 }
361
362 group_exists() {
363 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
364 }
365
366 group_add_next() {
367 local gid gids
368 gid=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
369 if [ -n "$gid" ]; then
370 echo $gid
371 return
372 fi
373 gids=$(cut -d: -f3 ${IPKG_INSTROOT}/etc/group)
374 gid=65536
375 while echo "$gids" | grep -q "^$gid$"; do
376 gid=$((gid + 1))
377 done
378 group_add $1 $gid
379 echo $gid
380 }
381
382 group_add_user() {
383 local grp delim=","
384 grp=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group)
385 echo "$grp" | cut -d: -f4 | grep -q $2 && return
386 echo "$grp" | grep -q ":$" && delim=""
387 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
388 sed -i "s/$grp/$grp$delim$2/g" ${IPKG_INSTROOT}/etc/group
389 if [ -z "$IPKG_INSTROOT" ] && [ -x /usr/sbin/selinuxenabled ] && selinuxenabled; then
390 restorecon /etc/group
391 fi
392 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
393 }
394
395 user_add() {
396 local name="${1}"
397 local uid="${2}"
398 local gid="${3}"
399 local desc="${4:-$1}"
400 local home="${5:-/var/run/$1}"
401 local shell="${6:-/bin/false}"
402 local rc
403 [ -z "$uid" ] && {
404 uids=$(cut -d: -f3 ${IPKG_INSTROOT}/etc/passwd)
405 uid=65536
406 while echo "$uids" | grep -q "^$uid$"; do
407 uid=$((uid + 1))
408 done
409 }
410 [ -z "$gid" ] && gid=$uid
411 [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1
412 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
413 echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd
414 echo "${name}:x:0:0:99999:7:::" >> ${IPKG_INSTROOT}/etc/shadow
415 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
416 }
417
418 user_exists() {
419 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
420 }
421
422 board_name() {
423 [ -e /tmp/sysinfo/board_name ] && cat /tmp/sysinfo/board_name || echo "generic"
424 }
425
426 cmdline_get_var() {
427 local var=$1
428 local cmdlinevar tmp
429
430 for cmdlinevar in $(cat /proc/cmdline); do
431 tmp=${cmdlinevar##${var}}
432 [ "=" = "${tmp:0:1}" ] && echo ${tmp:1}
433 done
434 }
435
436 [ -z "$IPKG_INSTROOT" ] && [ -f /lib/config/uci.sh ] && . /lib/config/uci.sh