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