functions.sh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. debug () {
  6. ${DEBUG:-:} "$@"
  7. }
  8. # newline
  9. N="
  10. "
  11. _C=0
  12. NO_EXPORT=1
  13. LOAD_STATE=1
  14. LIST_SEP=" "
  15. append() {
  16. local var="$1"
  17. local value="$2"
  18. local sep="${3:- }"
  19. eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
  20. }
  21. list_contains() {
  22. local var="$1"
  23. local str="$2"
  24. local val
  25. eval "val=\" \${$var} \""
  26. [ "${val%% $str *}" != "$val" ]
  27. }
  28. config_load() {
  29. [ -n "$IPKG_INSTROOT" ] && return 0
  30. uci_load "$@"
  31. }
  32. reset_cb() {
  33. config_cb() { return 0; }
  34. option_cb() { return 0; }
  35. list_cb() { return 0; }
  36. }
  37. reset_cb
  38. package() {
  39. return 0
  40. }
  41. config () {
  42. local cfgtype="$1"
  43. local name="$2"
  44. export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
  45. name="${name:-cfg$CONFIG_NUM_SECTIONS}"
  46. append CONFIG_SECTIONS "$name"
  47. [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
  48. export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
  49. export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
  50. }
  51. option () {
  52. local varname="$1"; shift
  53. local value="$*"
  54. export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
  55. [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
  56. }
  57. list() {
  58. local varname="$1"; shift
  59. local value="$*"
  60. local len
  61. config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0
  62. [ $len = 0 ] && append CONFIG_LIST_STATE "${CONFIG_SECTION}_${varname}"
  63. len=$(($len + 1))
  64. config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"
  65. config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"
  66. append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"
  67. list_cb "$varname" "$*"
  68. }
  69. config_unset() {
  70. config_set "$1" "$2" ""
  71. }
  72. # config_get <variable> <section> <option> [<default>]
  73. # config_get <section> <option>
  74. config_get() {
  75. case "$3" in
  76. "") eval echo "\${CONFIG_${1}_${2}:-\${4}}";;
  77. *) eval export ${NO_EXPORT:+-n} -- "${1}=\${CONFIG_${2}_${3}:-\${4}}";;
  78. esac
  79. }
  80. # config_get_bool <variable> <section> <option> [<default>]
  81. config_get_bool() {
  82. local _tmp
  83. config_get _tmp "$2" "$3" "$4"
  84. case "$_tmp" in
  85. 1|on|true|yes|enabled) _tmp=1;;
  86. 0|off|false|no|disabled) _tmp=0;;
  87. *) _tmp="$4";;
  88. esac
  89. export ${NO_EXPORT:+-n} "$1=$_tmp"
  90. }
  91. config_set() {
  92. local section="$1"
  93. local option="$2"
  94. local value="$3"
  95. local old_section="$CONFIG_SECTION"
  96. CONFIG_SECTION="$section"
  97. option "$option" "$value"
  98. CONFIG_SECTION="$old_section"
  99. }
  100. config_foreach() {
  101. local ___function="$1"
  102. [ "$#" -ge 1 ] && shift
  103. local ___type="$1"
  104. [ "$#" -ge 1 ] && shift
  105. local section cfgtype
  106. [ -z "$CONFIG_SECTIONS" ] && return 0
  107. for section in ${CONFIG_SECTIONS}; do
  108. config_get cfgtype "$section" TYPE
  109. [ -n "$___type" -a "x$cfgtype" != "x$___type" ] && continue
  110. eval "$___function \"\$section\" \"\$@\""
  111. done
  112. }
  113. config_list_foreach() {
  114. [ "$#" -ge 3 ] || return 0
  115. local section="$1"; shift
  116. local option="$1"; shift
  117. local function="$1"; shift
  118. local val
  119. local len
  120. local c=1
  121. config_get len "${section}" "${option}_LENGTH"
  122. [ -z "$len" ] && return 0
  123. while [ $c -le "$len" ]; do
  124. config_get val "${section}" "${option}_ITEM$c"
  125. eval "$function \"\$val\" \"\$@\""
  126. c="$(($c + 1))"
  127. done
  128. }
  129. insert_modules() {
  130. for m in $*; do
  131. if [ -f /etc/modules.d/$m ]; then
  132. sed 's/^[^#]/insmod &/' /etc/modules.d/$m | ash 2>&- || :
  133. else
  134. modprobe $m || :
  135. fi
  136. done
  137. }
  138. default_prerm() {
  139. local root="${IPKG_INSTROOT}"
  140. local pkgname="$(basename ${1%.*})"
  141. local ret=0
  142. if [ -f "$root/usr/lib/opkg/info/${pkgname}.prerm-pkg" ]; then
  143. ( . "$root/usr/lib/opkg/info/${pkgname}.prerm-pkg" )
  144. ret=$?
  145. fi
  146. local shell="$(which bash)"
  147. for i in $(grep -s "^/etc/init.d/" "$root/usr/lib/opkg/info/${pkgname}.list"); do
  148. if [ -n "$root" ]; then
  149. ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" disable
  150. else
  151. if [ "$PKG_UPGRADE" != "1" ]; then
  152. "$i" disable
  153. fi
  154. "$i" stop
  155. fi
  156. done
  157. return $ret
  158. }
  159. add_group_and_user() {
  160. local pkgname="$1"
  161. local rusers="$(sed -ne 's/^Require-User: *//p' $root/usr/lib/opkg/info/${pkgname}.control 2>/dev/null)"
  162. if [ -n "$rusers" ]; then
  163. local tuple oIFS="$IFS"
  164. for tuple in $rusers; do
  165. local uid gid uname gname
  166. IFS=":"
  167. set -- $tuple; uname="$1"; gname="$2"
  168. IFS="="
  169. set -- $uname; uname="$1"; uid="$2"
  170. set -- $gname; gname="$1"; gid="$2"
  171. IFS="$oIFS"
  172. if [ -n "$gname" ] && [ -n "$gid" ]; then
  173. group_exists "$gname" || group_add "$gname" "$gid"
  174. elif [ -n "$gname" ]; then
  175. group_add_next "$gname"; gid=$?
  176. fi
  177. if [ -n "$uname" ]; then
  178. user_exists "$uname" || user_add "$uname" "$uid" "$gid"
  179. fi
  180. if [ -n "$uname" ] && [ -n "$gname" ]; then
  181. group_add_user "$gname" "$uname"
  182. fi
  183. unset uid gid uname gname
  184. done
  185. fi
  186. }
  187. default_postinst() {
  188. local root="${IPKG_INSTROOT}"
  189. local pkgname="$(basename ${1%.*})"
  190. local ret=0
  191. add_group_and_user "${pkgname}"
  192. if [ -f "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" ]; then
  193. ( . "$root/usr/lib/opkg/info/${pkgname}.postinst-pkg" )
  194. ret=$?
  195. fi
  196. if [ -d "$root/rootfs-overlay" ]; then
  197. cp -R $root/rootfs-overlay/. $root/
  198. rm -fR $root/rootfs-overlay/
  199. fi
  200. if [ -z "$root" ] && grep -q -s "^/etc/uci-defaults/" "/usr/lib/opkg/info/${pkgname}.list"; then
  201. . /lib/functions/system.sh
  202. [ -d /tmp/.uci ] || mkdir -p /tmp/.uci
  203. for i in $(grep -s "^/etc/uci-defaults/" "/usr/lib/opkg/info/${pkgname}.list"); do
  204. ( [ -f "$i" ] && cd "$(dirname $i)" && . "$i" ) && rm -f "$i"
  205. done
  206. uci commit
  207. fi
  208. [ -n "$root" ] || rm -f /tmp/luci-indexcache 2>/dev/null
  209. local shell="$(which bash)"
  210. for i in $(grep -s "^/etc/init.d/" "$root/usr/lib/opkg/info/${pkgname}.list"); do
  211. if [ -n "$root" ]; then
  212. ${shell:-/bin/sh} "$root/etc/rc.common" "$root$i" enable
  213. else
  214. if [ "$PKG_UPGRADE" != "1" ]; then
  215. "$i" enable
  216. fi
  217. "$i" start
  218. fi
  219. done
  220. return $ret
  221. }
  222. include() {
  223. local file
  224. for file in $(ls $1/*.sh 2>/dev/null); do
  225. . $file
  226. done
  227. }
  228. find_mtd_index() {
  229. local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
  230. local INDEX="${PART##mtd}"
  231. echo ${INDEX}
  232. }
  233. find_mtd_part() {
  234. local INDEX=$(find_mtd_index "$1")
  235. local PREFIX=/dev/mtdblock
  236. [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
  237. echo "${INDEX:+$PREFIX$INDEX}"
  238. }
  239. group_add() {
  240. local name="$1"
  241. local gid="$2"
  242. local rc
  243. [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1
  244. [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group
  245. echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group
  246. rc=$?
  247. [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
  248. return $rc
  249. }
  250. group_exists() {
  251. grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
  252. }
  253. group_add_next() {
  254. local gid gids
  255. gid=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
  256. [ -n "$gid" ] && return $gid
  257. gids=$(cat ${IPKG_INSTROOT}/etc/group | cut -d: -f3)
  258. gid=100
  259. while [ -n "$(echo $gids | grep $gid)" ] ; do
  260. gid=$((gid + 1))
  261. done
  262. group_add $1 $gid
  263. return $gid
  264. }
  265. group_add_user() {
  266. local grp delim=","
  267. grp=$(grep -s "^${1}:" ${IPKG_INSTROOT}/etc/group)
  268. [ -z "$(echo $grp | cut -d: -f4 | grep $2)" ] || return
  269. [ -n "$(echo $grp | grep ":$")" ] && delim=""
  270. [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
  271. sed -i "s/$grp/$grp$delim$2/g" ${IPKG_INSTROOT}/etc/group
  272. [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
  273. }
  274. user_add() {
  275. local name="${1}"
  276. local uid="${2}"
  277. local gid="${3}"
  278. local desc="${4:-$1}"
  279. local home="${5:-/var/run/$1}"
  280. local shell="${6:-/bin/false}"
  281. local rc
  282. [ -z "$uid" ] && {
  283. uids=$(cat ${IPKG_INSTROOT}/etc/passwd | cut -d: -f3)
  284. uid=100
  285. while [ -n "$(echo $uids | grep $uid)" ] ; do
  286. uid=$((uid + 1))
  287. done
  288. }
  289. [ -z "$gid" ] && gid=$uid
  290. [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1
  291. [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
  292. echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd
  293. echo "${name}:x:0:0:99999:7:::" >> ${IPKG_INSTROOT}/etc/shadow
  294. rc=$?
  295. [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
  296. return $rc
  297. }
  298. user_exists() {
  299. grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
  300. }
  301. board_name() {
  302. [ -e /tmp/sysinfo/board_name ] && cat /tmp/sysinfo/board_name || echo "generic"
  303. }
  304. [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh