base-files: make postinst script run inside a subshell
[openwrt/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 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 [ -d /etc/modules.d ] && {
158 cd /etc/modules.d
159 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
160 }
161 }
162
163 default_prerm() {
164 local name
165 name=$(echo $(basename $1) | cut -d. -f1)
166 [ -f /usr/lib/opkg/info/${name}.prerm-pkg ] && . /usr/lib/opkg/info/${name}.prerm-pkg
167 for i in `cat /usr/lib/opkg/info/${name}.list | grep "^/etc/init.d/"`; do
168 $i disable
169 $i stop
170 done
171 }
172
173 default_postinst() {
174 local name rusers
175 name=$(echo $(basename $1) | cut -d. -f1)
176 [ -f ${IPKG_INSTROOT}/usr/lib/opkg/info/${name}.postinst-pkg ] && ( . ${IPKG_INSTROOT}/usr/lib/opkg/info/${name}.postinst-pkg )
177 rusers=$(grep "Require-User:" ${IPKG_INSTROOT}/usr/lib/opkg/info/${name}.control)
178 [ -n "$rusers" ] && {
179 local user group
180 for a in $(echo $rusers | sed "s/Require-User://g"); do
181 user=""
182 group=""
183 for b in $(echo $a | sed "s/:/ /g"); do
184 [ -z "$user" ] && {
185 user=$b
186 continue
187 }
188 [ -z "$group" ] && {
189 group=$b
190 group_add_next $b
191 gid=$?
192 user_exists $user || user_add $user "" $gid
193 continue
194 }
195 group_add_next $b
196 group_add_user $b $user
197 done
198 done
199 }
200 [ "$PKG_UPGRADE" = "1" ] || for i in `cat ${IPKG_INSTROOT}/usr/lib/opkg/info/${name}.list | grep "^/etc/init.d/"`; do
201 [ -n "${IPKG_INSTROOT}" ] && $(which bash) ${IPKG_INSTROOT}/etc/rc.common ${IPKG_INSTROOT}$i enable; \
202 [ -n "${IPKG_INSTROOT}" ] || {
203 $i enable
204 $i start
205 }
206 done
207 return 0
208 }
209
210 include() {
211 local file
212
213 for file in $(ls $1/*.sh 2>/dev/null); do
214 . $file
215 done
216 }
217
218 find_mtd_index() {
219 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
220 local INDEX="${PART##mtd}"
221
222 echo ${INDEX}
223 }
224
225 find_mtd_part() {
226 local INDEX=$(find_mtd_index "$1")
227 local PREFIX=/dev/mtdblock
228
229 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
230 echo "${INDEX:+$PREFIX$INDEX}"
231 }
232
233 group_add() {
234 local name="$1"
235 local gid="$2"
236 local rc
237 [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1
238 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group
239 echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group
240 rc=$?
241 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
242 return $rc
243 }
244
245 group_exists() {
246 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
247 }
248
249 group_add_next() {
250 local gid gids
251 gid=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
252 [ -n "$gid" ] && return $gid
253 gids=$(cat ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
254 gid=100
255 while [ -n "$(echo $gids | grep $gid)" ] ; do
256 gid=$((gid + 1))
257 done
258 group_add $1 $gid
259 return $gid
260 }
261
262 group_add_user() {
263 local grp delim=","
264 grp=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group)
265 [ -z "$(echo $grp | cut -d: -f4 | grep $2)" ] || return
266 [ -n "$(echo $grp | grep ":$")" ] && delim=""
267 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
268 sed -i "s/$grp/$grp$delim$2/g" ${IPKG_INSTROOT}/etc/group
269 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
270 }
271
272 user_add() {
273 local name="${1}"
274 local uid="${2}"
275 local gid="${3}"
276 local desc="${4:-$1}"
277 local home="${5:-/var/run/$1}"
278 local shell="${6:-/bin/false}"
279 local rc
280 [ -z "$uid" ] && {
281 uids=$(cat ${IPKG_INSTROOT}/etc/passwd | cut -d: -f3)
282 uid=100
283 while [ -n "$(echo $uids | grep $uid)" ] ; do
284 uid=$((uid + 1))
285 done
286 }
287 [ -z "$gid" ] && gid=$uid
288 [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1
289 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
290 echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd
291 echo "${name}:x:0:0:99999:7:::" >> ${IPKG_INSTROOT}/etc/shadow
292 rc=$?
293 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
294 return $rc
295 }
296
297 user_exists() {
298 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
299 }
300
301 [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh