b3bf2213f4122c044632d0d835f0bc92c2d61491
[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 [ -z "$root" ] && grep -q -s "^/etc/uci-defaults/" "/usr/lib/opkg/info/${pkgname}.list"; then
234 . /lib/functions/system.sh
235 [ -d /tmp/.uci ] || mkdir -p /tmp/.uci
236 for i in $(sed -ne 's!^/etc/uci-defaults/!!p' "/usr/lib/opkg/info/${pkgname}.list"); do (
237 cd /etc/uci-defaults
238 [ -f "$i" ] && . "$i" && rm -f "$i"
239 ) done
240 uci commit
241 fi
242
243 [ -n "$root" ] || rm -f /tmp/luci-indexcache 2>/dev/null
244
245 local shell="$(which bash)"
246 for i in $(grep -s "^/etc/init.d/" "$root/usr/lib/opkg/info/${pkgname}.list"); do
247 if [ -n "$root" ]; then
248 ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" enable
249 else
250 if [ "$PKG_UPGRADE" != "1" ]; then
251 "$i" enable
252 fi
253 "$i" start
254 fi
255 done
256
257 return $ret
258 }
259
260 include() {
261 local file
262
263 for file in $(ls $1/*.sh 2>/dev/null); do
264 . $file
265 done
266 }
267
268 find_mtd_index() {
269 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
270 local INDEX="${PART##mtd}"
271
272 echo ${INDEX}
273 }
274
275 find_mtd_part() {
276 local INDEX=$(find_mtd_index "$1")
277 local PREFIX=/dev/mtdblock
278
279 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
280 echo "${INDEX:+$PREFIX$INDEX}"
281 }
282
283 group_add() {
284 local name="$1"
285 local gid="$2"
286 local rc
287 [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1
288 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group
289 echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group
290 rc=$?
291 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
292 return $rc
293 }
294
295 group_exists() {
296 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
297 }
298
299 group_add_next() {
300 local gid gids
301 gid=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
302 [ -n "$gid" ] && return $gid
303 gids=$(cat ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
304 gid=100
305 while [ -n "$(echo $gids | grep $gid)" ] ; do
306 gid=$((gid + 1))
307 done
308 group_add $1 $gid
309 return $gid
310 }
311
312 group_add_user() {
313 local grp delim=","
314 grp=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group)
315 [ -z "$(echo $grp | cut -d: -f4 | grep $2)" ] || return
316 [ -n "$(echo $grp | grep ":$")" ] && delim=""
317 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
318 sed -i "s/$grp/$grp$delim$2/g" ${IPKG_INSTROOT}/etc/group
319 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
320 }
321
322 user_add() {
323 local name="${1}"
324 local uid="${2}"
325 local gid="${3}"
326 local desc="${4:-$1}"
327 local home="${5:-/var/run/$1}"
328 local shell="${6:-/bin/false}"
329 local rc
330 [ -z "$uid" ] && {
331 uids=$(cat ${IPKG_INSTROOT}/etc/passwd | cut -d: -f3)
332 uid=100
333 while [ -n "$(echo $uids | grep $uid)" ] ; do
334 uid=$((uid + 1))
335 done
336 }
337 [ -z "$gid" ] && gid=$uid
338 [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1
339 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
340 echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd
341 echo "${name}:x:0:0:99999:7:::" >> ${IPKG_INSTROOT}/etc/shadow
342 rc=$?
343 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
344 return $rc
345 }
346
347 user_exists() {
348 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
349 }
350
351 [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh