c69feb33b8f73d3002d21a5e0f7b4d93fc936aef
[openwrt/staging/chunkeey.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 root="${IPKG_INSTROOT}"
168 local name
169
170 name=$(basename ${1%.*})
171 [ -f "$root/usr/lib/opkg/info/${name}.prerm-pkg" ] && . "$root/usr/lib/opkg/info/${name}.prerm-pkg"
172
173 local shell="$(which bash)"
174 for i in `cat "$root/usr/lib/opkg/info/${name}.list" | grep "^/etc/init.d/"`; do
175 if [ -n "$root" ]; then
176 ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" disable
177 else
178 if [ "$PKG_UPGRADE" != "1" ]; then
179 "$i" disable
180 fi
181 "$i" stop || /bin/true
182 fi
183 done
184 }
185
186 add_group_and_user() {
187 local pkgname="$1"
188 local rusers="$(sed -ne 's/^Require-User: *//p' $root/usr/lib/opkg/info/${pkgname}.control 2>/dev/null)"
189
190 if [ -n "$rusers" ]; then
191 local tuple oIFS="$IFS"
192 for tuple in $rusers; do
193 local uid gid uname gname
194
195 IFS=":"
196 set -- $tuple; uname="$1"; gname="$2"
197 IFS="="
198 set -- $uname; uname="$1"; uid="$2"
199 set -- $gname; gname="$1"; gid="$2"
200 IFS="$oIFS"
201
202 if [ -n "$gname" ] && [ -n "$gid" ]; then
203 group_exists "$gname" || group_add "$gname" "$gid"
204 elif [ -n "$gname" ]; then
205 group_add_next "$gname"; gid=$?
206 fi
207
208 if [ -n "$uname" ]; then
209 user_exists "$uname" || user_add "$uname" "$uid" "$gid"
210 fi
211
212 if [ -n "$uname" ] && [ -n "$gname" ]; then
213 group_add_user "$gname" "$uname"
214 fi
215
216 unset uid gid uname gname
217 done
218 fi
219 }
220
221 default_postinst() {
222 local root="${IPKG_INSTROOT}"
223 local pkgname="$(basename ${1%.*})"
224 local ret=0
225
226 add_group_and_user "${pkgname}"
227
228 if [ -f "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" ]; then
229 ( . "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" )
230 ret=$?
231 fi
232
233 if [ -d "$root/rootfs-overlay" ]; then
234 cp -R $root/rootfs-overlay/. $root/
235 rm -fR $root/rootfs-overlay/
236 fi
237
238 if [ -z "$root" ] && grep -q -s "^/etc/uci-defaults/" "/usr/lib/opkg/info/${pkgname}.list"; then
239 . /lib/functions/system.sh
240 [ -d /tmp/.uci ] || mkdir -p /tmp/.uci
241 for i in $(sed -ne 's!^/etc/uci-defaults/!!p' "/usr/lib/opkg/info/${pkgname}.list"); do (
242 cd /etc/uci-defaults
243 [ -f "$i" ] && . ./"$i" && rm -f "$i"
244 ) done
245 uci commit
246 fi
247
248 [ -n "$root" ] || rm -f /tmp/luci-indexcache 2>/dev/null
249
250 local shell="$(which bash)"
251 for i in $(grep -s "^/etc/init.d/" "$root/usr/lib/opkg/info/${pkgname}.list"); do
252 if [ -n "$root" ]; then
253 ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" enable
254 else
255 if [ "$PKG_UPGRADE" != "1" ]; then
256 "$i" enable
257 fi
258 "$i" start
259 fi
260 done
261
262 return $ret
263 }
264
265 include() {
266 local file
267
268 for file in $(ls $1/*.sh 2>/dev/null); do
269 . $file
270 done
271 }
272
273 find_mtd_index() {
274 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
275 local INDEX="${PART##mtd}"
276
277 echo ${INDEX}
278 }
279
280 find_mtd_part() {
281 local INDEX=$(find_mtd_index "$1")
282 local PREFIX=/dev/mtdblock
283
284 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
285 echo "${INDEX:+$PREFIX$INDEX}"
286 }
287
288 group_add() {
289 local name="$1"
290 local gid="$2"
291 local rc
292 [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1
293 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group
294 echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group
295 rc=$?
296 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
297 return $rc
298 }
299
300 group_exists() {
301 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
302 }
303
304 group_add_next() {
305 local gid gids
306 gid=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
307 [ -n "$gid" ] && return $gid
308 gids=$(cat ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
309 gid=100
310 while [ -n "$(echo $gids | grep $gid)" ] ; do
311 gid=$((gid + 1))
312 done
313 group_add $1 $gid
314 return $gid
315 }
316
317 group_add_user() {
318 local grp delim=","
319 grp=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group)
320 [ -z "$(echo $grp | cut -d: -f4 | grep $2)" ] || return
321 [ -n "$(echo $grp | grep ":$")" ] && delim=""
322 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
323 sed -i "s/$grp/$grp$delim$2/g" ${IPKG_INSTROOT}/etc/group
324 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
325 }
326
327 user_add() {
328 local name="${1}"
329 local uid="${2}"
330 local gid="${3}"
331 local desc="${4:-$1}"
332 local home="${5:-/var/run/$1}"
333 local shell="${6:-/bin/false}"
334 local rc
335 [ -z "$uid" ] && {
336 uids=$(cat ${IPKG_INSTROOT}/etc/passwd | cut -d: -f3)
337 uid=100
338 while [ -n "$(echo $uids | grep $uid)" ] ; do
339 uid=$((uid + 1))
340 done
341 }
342 [ -z "$gid" ] && gid=$uid
343 [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1
344 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
345 echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd
346 echo "${name}:x:0:0:99999:7:::" >> ${IPKG_INSTROOT}/etc/shadow
347 rc=$?
348 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
349 return $rc
350 }
351
352 user_exists() {
353 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
354 }
355
356 board_name() {
357 [ -e /tmp/sysinfo/board_name ] && cat /tmp/sysinfo/board_name || echo "generic"
358 }
359
360 [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh