ramips: add support for the Senao/EnGenius ESR-9753 board
[openwrt.git] / package / base-files / files / lib / functions.sh
1 #!/bin/sh
2 # Copyright (C) 2006-2011 OpenWrt.org
3 # Copyright (C) 2006 Fokus Fraunhofer <carsten.tittel@fokus.fraunhofer.de>
4
5
6 debug () {
7 ${DEBUG:-:} "$@"
8 }
9 mount() {
10 busybox mount "$@"
11 }
12
13 # newline
14 N="
15 "
16
17 _C=0
18 NO_EXPORT=1
19 LOAD_STATE=1
20 LIST_SEP=" "
21
22 hotplug_dev() {
23 env -i ACTION=$1 INTERFACE=$2 /sbin/hotplug-call net
24 }
25
26 append() {
27 local var="$1"
28 local value="$2"
29 local sep="${3:- }"
30
31 eval "export ${NO_EXPORT:+-n} -- \"$var=\${$var:+\${$var}\${value:+\$sep}}\$value\""
32 }
33
34 list_contains() {
35 local var="$1"
36 local str="$2"
37 local val
38
39 eval "val=\" \${$var} \""
40 [ "${val%% $str *}" != "$val" ]
41 }
42
43 list_remove() {
44 local var="$1"
45 local remove="$2"
46 local val
47
48 eval "val=\" \${$var} \""
49 val1="${val%% $remove *}"
50 [ "$val1" = "$val" ] && return
51 val2="${val##* $remove }"
52 [ "$val2" = "$val" ] && return
53 val="${val1## } ${val2%% }"
54 val="${val%% }"
55 eval "export ${NO_EXPORT:+-n} -- \"$var=\$val\""
56 }
57
58 config_load() {
59 [ -n "$IPKG_INSTROOT" ] && return 0
60 uci_load "$@"
61 }
62
63 reset_cb() {
64 config_cb() { return 0; }
65 option_cb() { return 0; }
66 list_cb() { return 0; }
67 }
68 reset_cb
69
70 package() {
71 return 0
72 }
73
74 config () {
75 local cfgtype="$1"
76 local name="$2"
77
78 export ${NO_EXPORT:+-n} CONFIG_NUM_SECTIONS=$(($CONFIG_NUM_SECTIONS + 1))
79 name="${name:-cfg$CONFIG_NUM_SECTIONS}"
80 append CONFIG_SECTIONS "$name"
81 [ -n "$NO_CALLBACK" ] || config_cb "$cfgtype" "$name"
82 export ${NO_EXPORT:+-n} CONFIG_SECTION="$name"
83 export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_TYPE=$cfgtype"
84 }
85
86 option () {
87 local varname="$1"; shift
88 local value="$*"
89
90 export ${NO_EXPORT:+-n} "CONFIG_${CONFIG_SECTION}_${varname}=$value"
91 [ -n "$NO_CALLBACK" ] || option_cb "$varname" "$*"
92 }
93
94 list() {
95 local varname="$1"; shift
96 local value="$*"
97 local len
98
99 config_get len "$CONFIG_SECTION" "${varname}_LENGTH" 0
100 [ $len = 0 ] && append CONFIG_LIST_STATE "${CONFIG_SECTION}_${varname}"
101 len=$(($len + 1))
102 config_set "$CONFIG_SECTION" "${varname}_ITEM$len" "$value"
103 config_set "$CONFIG_SECTION" "${varname}_LENGTH" "$len"
104 append "CONFIG_${CONFIG_SECTION}_${varname}" "$value" "$LIST_SEP"
105 list_cb "$varname" "$*"
106 }
107
108 config_rename() {
109 local OLD="$1"
110 local NEW="$2"
111 local oldvar
112 local newvar
113
114 [ -n "$OLD" -a -n "$NEW" ] || return
115 for oldvar in `set | grep ^CONFIG_${OLD}_ | \
116 sed -e 's/\(.*\)=.*$/\1/'` ; do
117 newvar="CONFIG_${NEW}_${oldvar##CONFIG_${OLD}_}"
118 eval "export ${NO_EXPORT:+-n} \"$newvar=\${$oldvar}\""
119 unset "$oldvar"
120 done
121 export ${NO_EXPORT:+-n} CONFIG_SECTIONS="$(echo " $CONFIG_SECTIONS " | sed -e "s, $OLD , $NEW ,")"
122
123 [ "$CONFIG_SECTION" = "$OLD" ] && export ${NO_EXPORT:+-n} CONFIG_SECTION="$NEW"
124 }
125
126 config_unset() {
127 config_set "$1" "$2" ""
128 }
129
130 config_clear() {
131 local SECTION="$1"
132 local oldvar
133
134 list_remove CONFIG_SECTIONS "$SECTION"
135 export ${NO_EXPORT:+-n} CONFIG_SECTIONS="${SECTION:+$CONFIG_SECTIONS}"
136
137 for oldvar in `set | grep ^CONFIG_${SECTION:+${SECTION}_} | \
138 sed -e 's/\(.*\)=.*$/\1/'` ; do
139 unset $oldvar
140 done
141 }
142
143 # config_get <variable> <section> <option> [<default>]
144 # config_get <section> <option>
145 config_get() {
146 case "$3" in
147 "") eval echo "\${CONFIG_${1}_${2}:-\${4}}";;
148 *) eval export ${NO_EXPORT:+-n} -- "${1}=\${CONFIG_${2}_${3}:-\${4}}";;
149 esac
150 }
151
152 # config_get_bool <variable> <section> <option> [<default>]
153 config_get_bool() {
154 local _tmp
155 config_get _tmp "$2" "$3" "$4"
156 case "$_tmp" in
157 1|on|true|enabled) _tmp=1;;
158 0|off|false|disabled) _tmp=0;;
159 *) _tmp="$4";;
160 esac
161 export ${NO_EXPORT:+-n} "$1=$_tmp"
162 }
163
164 config_set() {
165 local section="$1"
166 local option="$2"
167 local value="$3"
168 local old_section="$CONFIG_SECTION"
169
170 CONFIG_SECTION="$section"
171 option "$option" "$value"
172 CONFIG_SECTION="$old_section"
173 }
174
175 config_foreach() {
176 local function="$1"
177 [ "$#" -ge 1 ] && shift
178 local type="$1"
179 [ "$#" -ge 1 ] && shift
180 local section cfgtype
181
182 [ -z "$CONFIG_SECTIONS" ] && return 0
183 for section in ${CONFIG_SECTIONS}; do
184 config_get cfgtype "$section" TYPE
185 [ -n "$type" -a "x$cfgtype" != "x$type" ] && continue
186 eval "$function \"\$section\" \"\$@\""
187 done
188 }
189
190 config_list_foreach() {
191 [ "$#" -ge 3 ] || return 0
192 local section="$1"; shift
193 local option="$1"; shift
194 local function="$1"; shift
195 local val
196 local len
197 local c=1
198
199 config_get len "${section}" "${option}_LENGTH"
200 [ -z "$len" ] && return 0
201 while [ $c -le "$len" ]; do
202 config_get val "${section}" "${option}_ITEM$c"
203 eval "$function \"\$val\" \"$@\""
204 c="$(($c + 1))"
205 done
206 }
207
208 load_modules() {
209 [ -d /etc/modules.d ] && {
210 cd /etc/modules.d
211 sed 's/^[^#]/insmod &/' $* | ash 2>&- || :
212 }
213 }
214
215 include() {
216 local file
217
218 for file in $(ls $1/*.sh 2>/dev/null); do
219 . $file
220 done
221 }
222
223 find_mtd_part() {
224 local PART="$(grep "\"$1\"" /proc/mtd | awk -F: '{print $1}')"
225 local PREFIX=/dev/mtdblock
226
227 PART="${PART##mtd}"
228 [ -d /dev/mtdblock ] && PREFIX=/dev/mtdblock/
229 echo "${PART:+$PREFIX$PART}"
230 }
231
232 strtok() { # <string> { <variable> [<separator>] ... }
233 local tmp
234 local val="$1"
235 local count=0
236
237 shift
238
239 while [ $# -gt 1 ]; do
240 tmp="${val%%$2*}"
241
242 [ "$tmp" = "$val" ] && break
243
244 val="${val#$tmp$2}"
245
246 export ${NO_EXPORT:+-n} "$1=$tmp"; count=$((count+1))
247 shift 2
248 done
249
250 if [ $# -gt 0 -a -n "$val" ]; then
251 export ${NO_EXPORT:+-n} "$1=$val"; count=$((count+1))
252 fi
253
254 return $count
255 }
256
257
258 jffs2_mark_erase() {
259 local part="$(find_mtd_part "$1")"
260 [ -z "$part" ] && {
261 echo Partition not found.
262 return 1
263 }
264 echo -e "\xde\xad\xc0\xde" | mtd -qq write - "$1"
265 }
266
267 uci_apply_defaults() {
268 cd /etc/uci-defaults || return 0
269 files="$(ls)"
270 [ -z "$files" ] && return 0
271 mkdir -p /tmp/.uci
272 for file in $files; do
273 ( . "./$(basename $file)" ) && rm -f "$file"
274 done
275 uci commit
276 }
277
278 #
279 # service: simple wrapper around start-stop-daemon
280 #
281 # Usage: service ACTION EXEC ARGS...
282 #
283 # Action:
284 # -C check if EXEC is alive
285 # -S start EXEC, passing it ARGS as its arguments
286 # -K kill EXEC, sending it a TERM signal if not specified otherwise
287 #
288 # Environment variables exposed:
289 # SERVICE_DAEMONIZE run EXEC in background
290 # SERVICE_WRITE_PID create a pid-file and use it for matching
291 # SERVICE_MATCH_EXEC use EXEC command-line for matching (default)
292 # SERVICE_MATCH_NAME use EXEC process name for matching
293 # SERVICE_USE_PID assume EXEC create its own pid-file and use it for matching
294 # SERVICE_NAME process name to use (default to EXEC file part)
295 # SERVICE_PID_FILE pid file to use (default to /var/run/$SERVICE_NAME.pid)
296 # SERVICE_SIG signal to send when using -K
297 # SERVICE_SIG_RELOAD default signal used when reloading
298 # SERVICE_SIG_STOP default signal used when stopping
299 # SERVICE_STOP_TIME time to wait for a process to stop gracefully before killing it
300 # SERVICE_UID user EXEC should be run as
301 # SERVICE_GID group EXEC should be run as
302 #
303 # SERVICE_DEBUG don't do anything, but show what would be done
304 # SERVICE_QUIET don't print anything
305 #
306
307 SERVICE_QUIET=1
308 SERVICE_SIG_RELOAD="HUP"
309 SERVICE_SIG_STOP="TERM"
310 SERVICE_STOP_TIME=5
311 SERVICE_MATCH_EXEC=1
312
313 service() {
314 local ssd
315 local exec
316 local name
317 local start
318 ssd="${SERVICE_DEBUG:+echo }start-stop-daemon${SERVICE_QUIET:+ -q}"
319 case "$1" in
320 -C)
321 ssd="$ssd -K -t"
322 ;;
323 -S)
324 ssd="$ssd -S${SERVICE_DAEMONIZE:+ -b}${SERVICE_WRITE_PID:+ -m}"
325 start=1
326 ;;
327 -K)
328 ssd="$ssd -K${SERVICE_SIG:+ -s $SERVICE_SIG}"
329 ;;
330 *)
331 echo "service: unknown ACTION '$1'" 1>&2
332 return 1
333 esac
334 shift
335 exec="$1"
336 [ -n "$exec" ] || {
337 echo "service: missing argument" 1>&2
338 return 1
339 }
340 [ -x "$exec" ] || {
341 echo "service: file '$exec' is not executable" 1>&2
342 return 1
343 }
344 name="${SERVICE_NAME:-${exec##*/}}"
345 [ -z "$SERVICE_USE_PID$SERVICE_WRITE_PID$SERVICE_PID_FILE" ] \
346 || ssd="$ssd -p ${SERVICE_PID_FILE:-/var/run/$name.pid}"
347 [ -z "$SERVICE_MATCH_NAME" ] || ssd="$ssd -n $name"
348 ssd="$ssd${SERVICE_UID:+ -c $SERVICE_UID${SERVICE_GID:+:$SERVICE_GID}}"
349 [ -z "$SERVICE_MATCH_EXEC$start" ] || ssd="$ssd -x $exec"
350 shift
351 $ssd${1:+ -- "$@"}
352 }
353
354 service_check() {
355 service -C "$@"
356 }
357
358 service_signal() {
359 SERVICE_SIG="${SERVICE_SIG:-USR1}" service -K "$@"
360 }
361
362 service_start() {
363 service -S "$@"
364 }
365
366 service_stop() {
367 local try
368 SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_STOP}" service -K "$@" || return 1
369 while [ $((try++)) -lt $SERVICE_STOP_TIME ]; do
370 service -C "$@" || return 0
371 sleep 1
372 done
373 SERVICE_SIG="KILL" service -K "$@"
374 sleep 1
375 ! service -C "$@"
376 }
377
378 service_reload() {
379 SERVICE_SIG="${SERVICE_SIG:-$SERVICE_SIG_RELOAD}" service -K "$@"
380 }
381
382 service_kill() {
383 cat 1>&2 << __END_OF_WARNING__
384 #
385 # WARNING: the 'service_kill' function is now deprecated and might be
386 # removed soon. Consider using the other new service_* wrappers instead.
387 #
388 __END_OF_WARNING__
389 local name="${1}"
390 local pid="${2:-$(pidof "$name")}"
391 local grace="${3:-5}"
392
393 [ -f "$pid" ] && pid="$(head -n1 "$pid" 2>/dev/null)"
394
395 for pid in $pid; do
396 [ -d "/proc/$pid" ] || continue
397 local try=0
398 kill -TERM $pid 2>/dev/null && \
399 while grep -qs "$name" "/proc/$pid/cmdline" && [ $((try++)) -lt $grace ]; do sleep 1; done
400 kill -KILL $pid 2>/dev/null && \
401 while grep -qs "$name" "/proc/$pid/cmdline"; do sleep 1; done
402 done
403 }
404
405
406 group_add() {
407 local name="$1"
408 local gid="$2"
409 local rc
410 [ -f "${IPKG_INSTROOT}/etc/group" ] || return 1
411 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/group
412 echo "${name}:x:${gid}:" >> ${IPKG_INSTROOT}/etc/group
413 rc=$?
414 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/group
415 return $rc
416 }
417
418 group_exists() {
419 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/group
420 }
421
422 user_add() {
423 local name="${1}"
424 local uid="${2}"
425 local gid="${3:-$2}"
426 local desc="${4:-$1}"
427 local home="${5:-/var/run/$1}"
428 local shell="${6:-/bin/false}"
429 local rc
430 [ -f "${IPKG_INSTROOT}/etc/passwd" ] || return 1
431 [ -n "$IPKG_INSTROOT" ] || lock /var/lock/passwd
432 echo "${name}:x:${uid}:${gid}:${desc}:${home}:${shell}" >> ${IPKG_INSTROOT}/etc/passwd
433 rc=$?
434 [ -n "$IPKG_INSTROOT" ] || lock -u /var/lock/passwd
435 return $rc
436 }
437
438 user_exists() {
439 grep -qs "^${1}:" ${IPKG_INSTROOT}/etc/passwd
440 }
441
442
443 pi_include() {
444 if [ -f "/tmp/overlay/$1" ]; then
445 . "/tmp/overlay/$1"
446 elif [ -f "$1" ]; then
447 . "$1"
448 elif [ -d "/tmp/overlay/$1" ]; then
449 if [ -n "$(ls /tmp/overlay/$1/*.sh 2>/dev/null)" ]; then
450 for src_script in /tmp/overlay/$1/*.sh; do
451 . "$src_script"
452 done
453 fi
454 elif [ -d "$1" ]; then
455 if [ -n "$(ls $1/*.sh 2>/dev/null)" ]; then
456 for src_script in $1/*.sh; do
457 . "$src_script"
458 done
459 fi
460 else
461 echo "WARNING: $1 not found"
462 return 1
463 fi
464 return 0
465 }
466
467 [ -z "$IPKG_INSTROOT" -a -f /lib/config/uci.sh ] && . /lib/config/uci.sh
This page took 0.06821 seconds and 5 git commands to generate.