6f23e0b8b2ea57fedc0cf3ae8d1f300e843a1516
[openwrt.git] / package / base-files / files / lib / functions.sh
1 #!/bin/sh
2 # Copyright (C) 2006-2013 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 hotplug_dev() {
21         env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug-call net
22 }
23
24 append() {
25         local var="$1"
26         local value="$2"
27         local sep="${3:- }"
28
29         eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
30 }
31
32 list_contains() {
33         local var="$1"
34         local str="$2"
35         local val
36
37         eval "val=\" \${$var} \""
38         [ "${val%% $str *}" != "$val" ]
39 }
40
41 list_remove() {
42         local var="$1"
43         local remove="$2"
44         local val
45
46         eval "val=\" \${$var} \""
47         val1="${val%% $remove *}"
48         [ "$val1" = "$val" ] && return
49         val2="${val##* $remove }"
50         [ "$val2" = "$val" ] && return
51         val="${val1## } ${val2%% }"
52         val="${val%% }"
53         eval "export ${NO_EXPORT:+-n} -- \"$var=\$val\""
54 }
55
56 config_load() {
57         [ -n "$IPKG_INSTROOT" ] && return 0
58         uci_load "$@"
59 }
60
61 reset_cb() {
62         config_cb() { return 0; }
63         option_cb() { return 0; }
64         list_cb() { return 0; }
65 }
66 reset_cb
67
68 package() {
69         return 0
70 }
71
72 config () {
73         local cfgtype="$1"
74         local name="$2"
75
76         export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
77         name="${name:-cfg$CONFIG_NUM_SECTIONS}"
78         append CONFIG_SECTIONS "$name"
79         [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
80         export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
81         export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
82 }
83
84 option () {
85         local varname="$1"; shift
86         local value="$*"
87
88         export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
89         [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
90 }
91
92 list() {
93         local varname="$1"; shift
94         local value="$*"
95         local len
96
97         config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0
98         [ $len = 0 ] && append CONFIG_LIST_STATE "${CONFIG_SECTION}_${varname}"
99         len=$(($len + 1))
100         config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"
101         config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"
102         append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"
103         list_cb "$varname" "$*"
104 }
105
106 config_rename() {
107         local OLD="$1"
108         local NEW="$2"
109         local oldvar
110         local newvar
111
112         [ -n "$OLD" -a -n "$NEW" ] || return
113         for oldvar in `set | grep ^CONFIG_${OLD}_ | \
114                 sed -e 's/\(.*\)=.*$/\1/'` ; do
115                 newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
116                 eval "export ${NO_EXPORT:+-n} \"$newvar=\${$oldvar}\""
117                 unset "$oldvar"
118         done
119         export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
120
121         [ "$CONFIG_SECTION" = "$OLD" ] && export ${NO_EXPORT:+-n} CONFIG_SECTION="$NEW"
122 }
123
124 config_unset() {
125         config_set "$1" "$2" ""
126 }
127
128 config_clear() {
129         local SECTION="$1"
130         local oldvar
131
132         list_remove CONFIG_SECTIONS "$SECTION"
133         export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
134
135         for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
136                 sed -e 's/\(.*\)=.*$/\1/'` ; do
137                 unset $oldvar
138         done
139 }
140
141 # config_get <variable> <section> <option> [<default>]
142 # config_get <section> <option>
143 config_get() {
144         case "$3" in
145                 "") eval echo "\${CONFIG_${1}_${2}:-\${4}}";;
146                 *)  eval export ${NO_EXPORT:+-n} -- "${1}=\${CONFIG_${2}_${3}:-\${4}}";;
147         esac
148 }
149
150 # config_get_bool <variable> <section> <option> [<default>]
151 config_get_bool() {
152         local _tmp
153         config_get _tmp "$2" "$3" "$4"
154         case "$_tmp" in
155                 1|on|true|enabled) _tmp=1;;
156                 0|off|false|disabled) _tmp=0;;
157                 *) _tmp="$4";;
158         esac
159         export ${NO_EXPORT:+-n} "$1=$_tmp"
160 }
161
162 config_set() {
163         local section="$1"
164         local option="$2"
165         local value="$3"
166         local old_section="$CONFIG_SECTION"
167
168         CONFIG_SECTION="$section"
169         option "$option" "$value"
170         CONFIG_SECTION="$old_section"
171 }
172
173 config_foreach() {
174         local ___function="$1"
175         [ "$#" -ge 1 ] && shift
176         local ___type="$1"
177         [ "$#" -ge 1 ] && shift
178         local section cfgtype
179
180         [ -z "$CONFIG_SECTIONS" ] && return 0
181         for section in ${CONFIG_SECTIONS}; do
182                 config_get cfgtype "$section" TYPE
183                 [ -n "$___type" -a "x$cfgtype" != "x$___type" ] && continue
184                 eval "$___function \"\$section\" \"\$@\""
185         done
186 }
187
188 config_list_foreach() {
189         [ "$#" -ge 3 ] || return 0
190         local section="$1"; shift
191         local option="$1"; shift
192         local function="$1"; shift
193         local val
194         local len
195         local c=1
196
197         config_get len "${section}" "${option}_LENGTH"
198         [ -z "$len" ] && return 0
199         while [ $c -le "$len" ]; do
200                 config_get val "${section}" "${option}_ITEM$c"
201                 eval "$function \"\$val\" \"\$@\""
202                 c="$(($c + 1))"
203         done
204 }
205
206 insert_modules() {
207         [ -d /etc/modules.d ] && {
208                 cd /etc/modules.d
209                 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
210         }
211 }
212
213 include() {
214         local file
215
216         for file in $(ls $1/*.sh 2>/dev/null); do
217                 . $file
218         done
219 }
220
221 find_mtd_index() {
222         local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
223         local INDEX="${PART##mtd}"
224
225         echo ${INDEX}
226 }
227
228 find_mtd_part() {
229         local INDEX=$(find_mtd_index "$1")
230         local PREFIX=/dev/mtdblock
231
232         [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
233         echo "${INDEX:+$PREFIX$INDEX}"
234 }
235
236 find_mtd_chardev() {
237         local INDEX=$(find_mtd_index "$1")
238         local PREFIX=/dev/mtd
239
240         [ -d /dev/mtd ] && PREFIX=/dev/mtd/
241         echo "${INDEX:+$PREFIX$INDEX}"
242 }
243
244 mtd_get_mac_ascii()
245 {
246         local mtdname="$1"
247         local key="$2"
248         local part
249         local mac_dirty
250
251         part=$(find_mtd_part "$mtdname")
252         if [ -z "$part" ]; then
253                 echo "mtd_get_mac_ascii: partition $mtdname not found!" >&2
254                 return
255         fi
256
257         mac_dirty=$(strings "$part" | sed -n 's/^'"$key"'=//p')
258
259         # "canonicalize" mac
260         [ -n "$mac_dirty" ] && macaddr_canonicalize "$mac_dirty"
261 }
262
263 mtd_get_blob()
264 {
265         local mtdname="$1"
266         local offset="$2"
267         local count="$3"
268         local firmware="$4"
269         local part
270
271         part=$(find_mtd_part "$mtdname")
272         if [ -z "$part" ]; then
273                 echo "mtd_get_blob: partition $mtdname not found!" >&2
274                 return 1
275         fi
276
277         dd if=$part of=$firmware bs=1 skip=$offset count=$count 2>/dev/null || {
278                 echo "mtd_get_blob: failed to extract $firmware from $part" >&2
279                 return 1
280         }
281 }
282
283 mtd_get_mac_binary() {
284         local mtdname="$1"
285         local offset="$2"
286         local part
287
288         part=$(find_mtd_part "$mtdname")
289         if [ -z "$part" ]; then
290                 echo "mtd_get_mac_binary: partition $mtdname not found!" >&2
291                 return
292         fi
293
294         dd bs=1 skip=$offset count=6 if=$part 2>/dev/null | hexdump -v -n 6 -e '5/1 "%02x:" 1/1 "%02x"'
295 }
296
297 mtd_get_part_size() {
298         local part_name=$1
299         local first dev size erasesize name
300         while read dev size erasesize name; do
301                 name=${name#'"'}; name=${name%'"'}
302                 if [ "$name" = "$part_name" ]; then
303                         echo $((0x$size))
304                         break
305                 fi
306         done < /proc/mtd
307 }
308
309 macaddr_add() {
310         local mac=$1
311         local val=$2
312         local oui=${mac%:*:*:*}
313         local nic=${mac#*:*:*:}
314
315         nic=$(printf "%06x" $((0x${nic//:/} + $val & 0xffffff)) | sed 's/^\(.\{2\}\)\(.\{2\}\)\(.\{2\}\)/\1:\2:\3/')
316         echo $oui:$nic
317 }
318
319 macaddr_setbit_la()
320 {
321         local mac=$1
322
323         printf "%02x:%s" $((0x${mac%%:*} | 0x02)) ${mac#*:}
324 }
325
326 macaddr_2bin()
327 {
328         local mac=$1
329
330         echo -ne \\x${mac//:/\\x}
331 }
332
333 macaddr_canonicalize()
334 {
335         local mac="$1"
336         local canon=""
337
338         [ ${#mac} -gt 17 ] && return
339         [ -n "${mac//[a-fA-F0-9\.: -]/}" ] && return
340
341         for octet in ${mac//[\.:-]/ }; do
342                 case "${#octet}" in
343                 1)
344                         octet="0${octet}"
345                         ;;
346                 2)
347                         ;;
348                 4)
349                         octet="${octet:0:2} ${octet:2:2}"
350                         ;;
351                 12)
352                         octet="${octet:0:2} ${octet:2:2} ${octet:4:2} ${octet:6:2} ${octet:8:2} ${octet:10:2}"
353                         ;;
354                 *)
355                         return
356                         ;;
357                 esac
358                 canon=${canon}${canon:+ }${octet}
359         done
360
361         [ ${#canon} -ne 17 ] && return
362
363         printf "%02x:%02x:%02x:%02x:%02x:%02x" 0x${canon// / 0x} 2>/dev/null
364 }
365
366 strtok() { # <string> { <variable> [<separator>] ... }
367         local tmp
368         local val="$1"
369         local count=0
370
371         shift
372
373         while [ $# -gt 1 ]; do
374                 tmp="${val%%$2*}"
375
376                 [ "$tmp" = "$val" ] && break
377
378                 val="${val#$tmp$2}"
379
380                 export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1))
381                 shift 2
382         done
383
384         if [ $# -gt 0 -a -n "$val" ]; then
385                 export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1))
386         fi
387
388         return $count
389 }
390
391
392 jffs2_mark_erase() {
393         local part="$(find_mtd_part "$1")"
394         [ -z "$part" ] && {
395                 echo Partition not found.
396                 return 1
397         }
398         echo -e "\xde\xad\xc0\xde" | mtd -qq write - "$1"
399 }
400
401 uci_apply_defaults() {
402         cd /etc/uci-defaults || return 0
403         files="$(ls)"
404         [ -z "$files" ] && return 0
405         mkdir -p /tmp/.uci
406         for file in $files; do
407                 ( . "./$(basename $file)" ) && rm -f "$file"
408         done
409         uci commit
410 }
411
412 group_add() {
413         local name="$1"
414         local gid="$2"
415         local rc
416         [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1
417         [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group
418         echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group
419         rc=$?
420         [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
421         return $rc
422 }
423
424 group_exists() {
425         grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
426 }
427
428 user_add() {
429         local name="${1}"
430         local uid="${2}"
431         local gid="${3:-$2}"
432         local desc="${4:-$1}"
433         local home="${5:-/var/run/$1}"
434         local shell="${6:-/bin/false}"
435         local rc
436         [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1
437         [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
438         echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd
439         echo "${name}:x:0:0:99999:7:::" >> ${IPKG_INSTROOT}/etc/shadow
440         rc=$?
441         [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
442         return $rc
443 }
444
445 user_exists() {
446         grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
447 }
448
449
450 pi_include() {
451         if [ -f "/tmp/overlay/$1" ]; then
452                 . "/tmp/overlay/$1"
453         elif [ -f "$1" ]; then
454                 . "$1"
455         elif [ -d "/tmp/overlay/$1" ]; then
456                 if [ -n "$(ls /tmp/overlay/$1/*.sh 2>/dev/null)" ]; then
457                         for src_script in /tmp/overlay/$1/*.sh; do
458                                 . "$src_script"
459                         done
460                 fi
461         elif [ -d "$1" ]; then
462                 if [ -n "$(ls $1/*.sh 2>/dev/null)" ]; then
463                         for src_script in $1/*.sh; do
464                                 . "$src_script"
465                         done
466                 fi
467         else
468                 echo "WARNING: $1 not found"
469                 return 1
470         fi
471         return 0
472 }
473
474 boot_hook_splice_start() {
475         export -n PI_HOOK_SPLICE=1
476 }
477
478 boot_hook_splice_finish() {
479         local hook
480         for hook in $PI_STACK_LIST; do
481                 local v; eval "v=\${${hook}_splice:+\$${hook}_splice }$hook"
482                 export -n "${hook}=${v% }"
483                 export -n "${hook}_splice="
484         done
485         export -n PI_HOOK_SPLICE=
486 }
487
488 boot_hook_init() {
489         local hook="${1}_hook"
490         export -n "PI_STACK_LIST=${PI_STACK_LIST:+$PI_STACK_LIST }$hook"
491         export -n "$hook="
492 }
493
494 boot_hook_add() {
495         local hook="${1}_hook${PI_HOOK_SPLICE:+_splice}"
496         local func="${2}"
497
498         [ -n "$func" ] && {
499                 local v; eval "v=\$$hook"
500                 export -n "$hook=${v:+$v }$func"
501         }
502 }
503
504 boot_hook_shift() {
505         local hook="${1}_hook"
506         local rvar="${2}"
507
508         local v; eval "v=\$$hook"
509         [ -n "$v" ] && {
510                 local first="${v%% *}"
511
512                 [ "$v" != "${v#* }" ] && \
513                         export -n "$hook=${v#* }" || \
514                         export -n "$hook="
515
516                 export -n "$rvar=$first"
517                 return 0
518         }
519
520         return 1
521 }
522
523 boot_run_hook() {
524         local hook="$1"
525         local func
526
527         while boot_hook_shift "$hook" func; do
528                 local ran; eval "ran=\$PI_RAN_$func"
529                 [ -n "$ran" ] || {
530                         export -n "PI_RAN_$func=1"
531                         $func "$1" "$2"
532                 }
533         done
534 }
535
536 jffs2_ready() {
537         mtdpart="$(find_mtd_part rootfs_data)"
538         [ -z "$mtdpart" ] && return 1
539         magic=$(hexdump $mtdpart -n 4 -e '4/1 "%02x"')
540         [ "$magic" != "deadc0de" ]
541 }
542
543 dupe() { # <new_root> <old_root>
544         cd $1
545         echo -n "creating directories... "
546         {
547                 cd $2
548                 find . -xdev -type d
549                 echo "./dev ./overlay ./mnt ./proc ./tmp"
550                 # xdev skips mounted directories
551                 cd $1
552         } | xargs mkdir -p
553         echo "done"
554
555         echo -n "setting up symlinks... "
556         for file in $(cd $2; find . -xdev -type f;); do
557                 case "$file" in
558                 ./rom/note) ;; #nothing
559                 ./etc/config*|\
560                 ./usr/lib/opkg/info/*) cp -af $2/$file $file;;
561                 *) ln -sf /rom/${file#./*} $file;;
562                 esac
563         done
564         for file in $(cd $2; find . -xdev -type l;); do
565                 cp -af $2/${file#./*} $file
566         done
567         echo "done"
568 }
569
570 pivot() { # <new_root> <old_root>
571         mount -o noatime,move /proc $1/proc && \
572         pivot_root $1 $1$2 && {
573                 mount -o noatime,move $2/dev /dev
574                 mount -o noatime,move $2/tmp /tmp
575                 mount -o noatime,move $2/sys /sys 2>&-
576                 mount -o noatime,move $2/overlay /overlay 2>&-
577                 return 0
578         }
579 }
580
581 fopivot() { # <rw_root> <ro_root> <dupe?>
582         root=$1
583         {
584                 if grep -q overlay /proc/filesystems; then
585                         mount -o noatime,lowerdir=/,upperdir=$1 -t overlayfs "overlayfs:$1" /mnt && root=/mnt
586                 elif grep -q mini_fo /proc/filesystems; then
587                         mount -t mini_fo -o noatime,base=/,sto=$1 "mini_fo:$1" /mnt 2>&- && root=/mnt
588                 else
589                         mount --bind -o noatime / /mnt
590                         mount --bind -o noatime,union "$1" /mnt && root=/mnt
591                 fi
592         } || {
593                 [ "$3" = "1" ] && {
594                 mount | grep "on $1 type" 2>&- 1>&- || mount -o noatime,bind $1 $1
595                 dupe $1 $rom
596                 }
597         }
598         pivot $root $2
599 }
600
601 ramoverlay() {
602         mkdir -p /tmp/root
603         mount -t tmpfs -o noatime,mode=0755 root /tmp/root
604         fopivot /tmp/root /rom 1
605 }
606
607 [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh