#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2010 Michael Tremer & Christian Schmidt # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # ############################################################################### # A simple print statement function print() { local fmt=${1}; shift printf -- "${fmt}\n" "$@" } # Print a pretty error message function error() { echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2 } function error_log() { log ERROR "$@" } # Print a pretty warn message function warning() { echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2 } function warning_log() { log WARNING "$@" } # The next three functions are kept for backwards # compatibility. The need to be dropped at some time. function listsort() { list_sort $@ } function listmatch() { list_match $@ } function listlength() { list_length $@ } # Speedup function to avoid a call of the basename binary function basename() { echo "${1##*/}" } function enabled() { local param=${1} list_match "${!param}" yes on true 1 } function mac_generate() { # Get a bunch of random hex digits # and remove all dashes from the input. local random=$(}:${BASH_LINENO[${i}]}" done } function assert() { local assertion="$@" if ! ${assertion}; then error_log "Assertion '${assertion}' failed." backtrace exit ${EXIT_ERROR_ASSERT} fi return ${EXIT_OK} } # This function checks, if the given argument is an assert error # exit code. If this is the case, the script will halt immediately. function assert_check_retval() { local ret=${1} if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then exit ${EXIT_ERROR_ASSERT} fi return ${ret} } function exec_cmd() { local cmd=$@ log DEBUG "Running command: ${cmd}" DEBUG=${DEBUG} \ LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \ LOG_FACILITY="${LOG_FACILITY}" \ ${SHELL} ${cmd} local ret=$? #log DEBUG "Returned with code '${ret}'" if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then error_log "Stopping parent process due to assertion error in child process: ${cmd}" exit ${EXIT_ERROR_ASSERT} fi return ${ret} } function cmd() { local cmd=$@ log DEBUG "Running command: ${cmd}" ${cmd} local ret=$? log DEBUG "Returned with code '${ret}'" return ${ret} } function cmd_quiet() { cmd $@ &>/dev/null } function seq() { if [ $# -eq 2 ]; then eval echo {${1}..${2}} elif [ $# -eq 3 ]; then eval echo {${1}..${3}..${2}} fi } function which() { type -P $@ } function beautify_time() { local value=${1} local unit local limit for unit in s m h d w; do case "${unit}" in s|m|h) limit=60 ;; d) limit=24 ;; w) limit=7 ;; esac [ ${value} -lt ${limit} ] && break value=$(( ${value} / ${limit} )) done echo "${value}${unit}" } function beautify_bytes() { local value=${1} local unit local limit=1024 for unit in B k M G T; do [ ${value} -lt ${limit} ] && break value=$(( ${value} / ${limit} )) done echo "${value}${unit}" } function module_load() { local module=${1} if ! grep -q "^${module}" /proc/modules; then log DEBUG "Loading module '${module}'." modprobe ${module} fi } function binary_exists() { local binary=${1} if [ -n "$(type -p ${binary})" ]; then return ${EXIT_OK} fi return ${EXIT_ERROR} } function process_kill() { local process=${1} if ! isinteger process; then process=$(pidof ${process}) fi local pid local sig for pid in ${process}; do for sig in 15 9; do [ -d "/proc/${pid}" ] || break kill -${sig} ${pid} sleep 1 done done } function dec() { local hex=${1} if [ "${hex:0:2}" != "0x" ]; then hex="0x${hex}" fi printf "%d\n" "${hex}" } function network_is_running() { # Check, if the network service is running. service_is_active network }