2 # Copyright (C) 2006-2014 OpenWrt.org
3 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
4 # Copyright (C) 2010 Vertical Communications
25 eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
33 eval "val=\" \${$var} \""
34 [ "${val%% $str *}" != "$val" ]
38 [ -n "$IPKG_INSTROOT" ] && return 0
43 config_cb() { return 0; }
44 option_cb() { return 0; }
45 list_cb() { return 0; }
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"
66 local varname="$1"; shift
69 export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
70 [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
74 local varname="$1"; shift
78 config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0
79 [ $len = 0 ] && append CONFIG_LIST_STATE "${CONFIG_SECTION}_${varname}"
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" "$*"
88 config_set "$1" "$2" ""
91 # config_get <variable> <section> <option> [<default>]
92 # config_get <section> <option>
95 "") eval echo "\${CONFIG_${1}_${2}:-\${4}}";;
96 *) eval export ${NO_EXPORT:+-n} -- "${1}=\${CONFIG_${2}_${3}:-\${4}}";;
100 # config_get_bool <variable> <section> <option> [<default>]
103 config_get _tmp "$2" "$3" "$4"
105 1|on|true|yes|enabled) _tmp=1;;
106 0|off|false|no|disabled) _tmp=0;;
109 export ${NO_EXPORT:+-n} "$1=$_tmp"
116 local old_section="$CONFIG_SECTION"
118 CONFIG_SECTION="$section"
119 option "$option" "$value"
120 CONFIG_SECTION="$old_section"
124 local ___function="$1"
125 [ "$#" -ge 1 ] && shift
127 [ "$#" -ge 1 ] && shift
128 local section cfgtype
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\" \"\$@\""
138 config_list_foreach() {
139 [ "$#" -ge 3 ] || return 0
140 local section="$1"; shift
141 local option="$1"; shift
142 local function="$1"; shift
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\" \"\$@\""
158 if [ -f /etc/modules.d/$m ]; then
159 sed 's/^[^#]/insmod &/' /etc/modules.d/$m | ash 2>&- || :
167 local root="${IPKG_INSTROOT}"
170 name=$(basename ${1%.*})
171 [ -f "$root/usr/lib/opkg/info/${name}.prerm-pkg" ] && . "$root/usr/lib/opkg/info/${name}.prerm-pkg"
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
178 if [ "$PKG_UPGRADE" != "1" ]; then
181 "$i" stop || /bin/true
186 add_group_and_user() {
188 local rusers="$(sed -ne 's/^Require-User: *//p' $root/usr/lib/opkg/info/${pkgname}.control 2>/dev/null)"
190 if [ -n "$rusers" ]; then
191 local tuple oIFS="$IFS"
192 for tuple in $rusers; do
193 local uid gid uname gname
196 set -- $tuple; uname="$1"; gname="$2"
198 set -- $uname; uname="$1"; uid="$2"
199 set -- $gname; gname="$1"; gid="$2"
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=$?
208 if [ -n "$uname" ]; then
209 user_exists "$uname" || user_add "$uname" "$uid" "$gid"
212 if [ -n "$uname" ] && [ -n "$gname" ]; then
213 group_add_user "$gname" "$uname"
216 unset uid gid uname gname
222 local root="${IPKG_INSTROOT}"
223 local pkgname="$(basename ${1%.*})"
226 add_group_and_user "${pkgname}"
228 if [ -f "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" ]; then
229 ( . "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" )
233 if [ -d "$root/rootfs-overlay" ]; then
234 cp -R $root/rootfs-overlay/. $root/
235 rm -fR $root/rootfs-overlay/
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 (
243 [ -f "$i" ] && . "$i" && rm -f "$i"
248 [ -n "$root" ] || rm -f /tmp/luci-indexcache 2>/dev/null
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
255 if [ "$PKG_UPGRADE" != "1" ]; then
268 for file in $(ls $1/*.sh 2>/dev/null); do
274 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
275 local INDEX="${PART##mtd}"
281 local INDEX=$(find_mtd_index "$1")
282 local PREFIX=/dev/mtdblock
284 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
285 echo "${INDEX:+$PREFIX$INDEX}"
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
296 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
301 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
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)
310 while [ -n "$(echo $gids | grep $gid)" ] ; do
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
331 local desc="${4:-$1}"
332 local home="${5:-/var/run/$1}"
333 local shell="${6:-/bin/false}"
336 uids=$(cat ${IPKG_INSTROOT}/etc/passwd | cut -d: -f3)
338 while [ -n "$(echo $uids | grep $uid)" ] ; do
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
348 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
353 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
357 [ -e /tmp/sysinfo/board_name ] && cat /tmp/sysinfo/board_name || echo "generic"
360 [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh