]>
git.ipfire.org Git - people/ms/network.git/blob - functions.util
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2010 Michael Tremer & Christian Schmidt #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
22 # Print a pretty error message
24 echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2
27 function error_log
() {
31 # Print a pretty warn message
33 echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
36 function warning_log
() {
44 done |
sort |
tr '\n' ' '
48 function listmatch
() {
56 [ "${match}" = "${i}" ] && return ${EXIT_OK}
62 function listlength() {
67 length=$(( ${length} + 1 ))
73 # Speedup function to avoid a call of the basename binary
81 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
84 function mac_generate() {
86 for i in $(seq 0 5); do
88 mac[i]="0x
${mac[i]:0:2}"
91 # Remove multicast bit
92 # and set address is software assigned
93 # XXX must doublecheck if this works
94 mac[0]=$((mac[0] & 0xfe))
95 mac[0]=$((mac[0] | 0x02))
98 for i in ${mac[*]}; do
99 if [ -n "${output}" ]; then
103 output="${output}$
(printf "%02x" ${i})"
106 # Check if output is valid
107 assert mac_is_valid ${output}
112 function mac_format() {
117 if [ "${#mac}" = "12" ]; then
118 # Add colons (:) to mac address
121 for i in 2 4 6 8 10; do
122 output="${output}:${mac:${i}:2}"
126 assert mac_is_valid ${output}
131 function mac_is_valid() {
134 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
138 echo $(</proc/sys/kernel/random/uuid)
147 # XXX Nearly same as listmatch
153 [ "${var}" = "${i}" ] && return ${EXIT_OK}
162 isoneof
${var} 0 1 no
yes on off
165 function isinteger
() {
168 [[ ${var} =~ ^
[0-9]+$
]]
177 function backtrace
() {
181 error_log
"Backtrace (most recent call in first line):"
184 for i
in $
(seq ${start} ${#BASH_SOURCE[*]}); do
185 [ -z "${FUNCNAME[${i}]}" ] && continue
186 [ "${FUNCNAME[${i}]}" == "main" ] && continue
188 error_log
" $(printf "%20s
" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
195 if ! ${assertion}; then
196 error_log "Assertion '${assertion}' failed."
204 function exec_cmd() {
207 log DEBUG "Running command: ${cmd}"
210 LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \
211 LOG_FACILITY="${LOG_FACILITY}" \
215 #log DEBUG "Returned with code '${ret}'"
217 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
218 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
219 exit ${EXIT_ERROR_ASSERT}
228 log DEBUG "Running command: ${cmd}"
233 log DEBUG "Returned with code '${ret}'"
238 function cmd_quiet() {
243 if [ $# -eq 2 ]; then
244 eval echo {${1}..${2}}
245 elif [ $# -eq 3 ]; then
246 eval echo {${1}..${3}..${2}}
250 function beautify_time() {
255 for unit in s m h d w; do
268 [ ${value} -lt ${limit} ] && break
270 value=$(( ${value} / ${limit} ))
273 echo "${value}${unit}"
276 function beautify_bytes() {
281 for unit in B k M G T; do
282 [ ${value} -lt ${limit} ] && break
283 value=$(( ${value} / ${limit} ))
286 echo "${value}${unit}"
289 function module_load() {
292 if ! grep -q "^${module}" /proc/modules; then
293 log DEBUG "Loading module '${module}'."
298 function binary_exists() {
301 if [ -n "$(type -p ${binary})" ]; then
308 function process_kill() {
311 if ! isinteger process; then
312 process=$(pidof ${process})
317 for pid in ${process}; do
319 [ -d "/proc/${pid}" ] || break
330 if [ "${hex:0:2}" != "0x" ]; then
334 printf "%d\n" "${hex}"
337 function network_is_running() {
338 # Check, if the network service is running.
339 service_is_active network