]> git.ipfire.org Git - people/stevee/network.git/blame - functions.util
Enhance logging with colors and output to stderr.
[people/stevee/network.git] / functions.util
CommitLineData
1848564d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2010 Michael Tremer & Christian Schmidt #
6# #
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. #
11# #
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. #
16# #
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/>. #
19# #
20###############################################################################
21
22# Print a pretty error message
23function error() {
2ab7f50f 24 echo -e " ${COLOUR_ERROR}ERROR${COLOUR_NORMAL} : $@" >&2
1848564d
MT
25}
26
1b7a1578
MT
27function error_log() {
28 error "$@"
29 log ERROR "$@"
30}
31
1848564d
MT
32# Print a pretty warn message
33function warning() {
2ab7f50f 34 echo -e " ${COLOUR_WARN}WARNING${COLOUR_NORMAL}: $@" >&2
1848564d
MT
35}
36
1b7a1578
MT
37function warning_log() {
38 warning "$@"
39 log WARNING "$@"
40}
41
940e6f36
MT
42# This function does not exist because we cannot use /usr/bin/sort.
43# It implements some kind of bubble sort which is generally very slow
44# but we only have to sort very small data.
1848564d 45function listsort() {
940e6f36
MT
46 local list=($@)
47 local list_prev
48
1848564d 49 local i
940e6f36
MT
50 local j
51 local var
52 while [ "${list[*]}" != "${list_prev}" ]; do
53 list_prev="${list[*]}"
54 for j in $(seq 1 ${#list[*]}); do
55 [ ${j} -ge ${#list[*]} ] && continue
56 i=$(( ${j} - 1 ))
57 if [[ "${list[${j}]}" < "${list[${i}]}" ]]; then
58 var="${list[${i}]}"
59 list[${i}]="${list[${j}]}"
60 list[${j}]="${var}"
61 fi
62 done
63 done
64
65 echo "${list[*]}"
1848564d
MT
66}
67
711ffac1
MT
68function listmatch() {
69 local match=${1}
70 shift
71
72 assert isset match
73
74 local i
75 for i in $@; do
76 [ "${match}" = "${i}" ] && return ${EXIT_OK}
77 done
78
79 return ${EXIT_ERROR}
80}
81
82function listlength() {
83 local length=0
84
85 local i
86 for i in $@; do
87 length=$(( ${length} + 1 ))
88 done
89
90 echo "${length}"
91}
92
1848564d
MT
93function config_read() {
94 local config_file=${1}
95
f6ee6bb1
AF
96 log DEBUG "Reading configuration: ${config_file}"
97
1848564d
MT
98 if [ -e "${config_file}" ]; then
99 . ${config_file}
100 config_check
101 fi
102}
103
104function config_write() {
105 local config_file=${1}
106 shift
107
108 # Check if all values to be written are sane
109 config_check
110
1b7a1578
MT
111 log DEBUG "Writing configuration file ${config_file}."
112
1848564d
MT
113 > ${config_file}
114
115 local param
116 for param in $(listsort $@); do
117 echo "${param}=\"${!param}\"" >> ${config_file}
118 done
119}
120
121function config_print() {
122 local param
123
124 for param in $(listsort $@); do
125 printf "%-16s = %s\n" "${param}" "${!param}"
126 done
127}
128
129function config_check() {
130 # If there is a function defined that is called __check
131 # we call that function
132 [ -n "$(type -t _check)" ] && _check
133}
134
31e59f2b
MT
135function config_hostname() {
136 local hostname=${1}
137
138 if [ -n "${hostname}" ]; then
139 echo "${hostname}" > ${CONFIG_HOSTNAME}
140 else
141 echo "$(<${CONFIG_HOSTNAME})"
142 fi
143}
144
1848564d
MT
145function network_config_set() {
146 while [ $# -gt 0 ]; do
147 case "${1}" in
148 *=*)
1b7a1578
MT
149 log INFO "Setting configuration option '${1}'".
150 eval ${1}
1848564d
MT
151 ;;
152 *)
153 warning "Invalid parameter given: ${1}"
154 ;;
155 esac
156 shift
157 done
158
159 # Write configuration to disk
160 network_config_write
161}
162
163function network_config_read() {
164 config_read ${CONFIG_FILE}
165}
166
167function network_config_write() {
168 config_write ${CONFIG_FILE} ${CONFIG_FILE_PARAMS}
169}
170
171function network_config_print() {
172 config_print ${CONFIG_FILE_PARAMS}
173}
174
175# Speedup function to avoid a call of the basename binary
176function basename() {
177 echo "${1##*/}"
178}
179
180function enabled() {
181 local param=${1}
182
183 [ "${!param}" = "yes" ] || [ "${!param}" = "on" ] || [ "${!param}" = "1" ]
184}
185
186function mac_generate() {
187 local mac=()
188 for i in $(seq 0 5); do
21dbdbb9
MT
189 mac[i]="$(uuid)"
190 mac[i]="0x${mac[i]:0:2}"
1848564d
MT
191 done
192
193 # Remove multicast bit
194 # and set address is software assigned
195 # XXX must doublecheck if this works
196 mac[0]=$((mac[0] & 0xfe))
197 mac[0]=$((mac[0] | 0x02))
198
199 local output
200 for i in ${mac[*]}; do
21dbdbb9
MT
201 if [ -n "${output}" ]; then
202 output="${output}:"
203 fi
1848564d 204
21dbdbb9 205 output="${output}$(printf "%02x" ${i})"
1848564d
MT
206 done
207
208 # Check if output is valid
209 assert mac_is_valid ${output}
210
211 echo ${output}
212}
213
18b43372
MT
214function mac_format() {
215 local mac=${1}
216
217 local output
218
219 if [ "${#mac}" = "12" ]; then
220 # Add colons (:) to mac address
221 output=${mac:0:2}
222 local i
223 for i in 2 4 6 8 10; do
224 output="${output}:${mac:${i}:2}"
225 done
226 fi
227
228 assert mac_is_valid ${output}
229
230 echo "${output}"
231}
232
1848564d
MT
233function mac_is_valid() {
234 local mac=${1}
235
236 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
237}
238
239function uuid() {
de543653 240 echo $(</proc/sys/kernel/random/uuid)
1848564d
MT
241}
242
243function isset() {
244 local var=${1}
245
246 [ -n "${!var}" ]
247}
248
943e3f7e 249# XXX Nearly same as listmatch
1848564d
MT
250function isoneof() {
251 local var=${!1}
252 shift
253
254 for i in $@; do
255 [ "${var}" = "${i}" ] && return ${EXIT_OK}
256 done
257
258 return ${EXIT_ERROR}
259}
260
261function isbool() {
262 local var=${1}
263
264 isoneof ${var} 0 1 no yes on off
265}
266
267function isinteger() {
268 local var=${!1}
269
270 [[ ${var} =~ ^[0-9]+$ ]]
271}
272
273function ismac() {
274 local mac=${!1}
275
276 mac_is_valid ${mac}
277}
278
711ffac1
MT
279function backtrace() {
280 local start=1
281
282 echo # Empty line
283 error_log "Backtrace (most recent call in first line):"
284
285 local i
286 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
287 [ -z "${FUNCNAME[${i}]}" ] && continue
288 [ "${FUNCNAME[${i}]}" == "main" ] && continue
289
290 error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${BASH_SOURCE[$(( ${i} + 1 ))]}:${BASH_LINENO[${i}]}"
291 done
292}
293
1848564d
MT
294function assert() {
295 local assertion="$@"
296
297 if ! ${assertion}; then
4c670d7c 298 error_log "Assertion '${assertion}' failed."
711ffac1 299 backtrace
1848564d
MT
300 exit ${EXIT_ERROR}
301 fi
302
303 return ${EXIT_OK}
304}
cad8bd85 305
711ffac1
MT
306function exec_cmd() {
307 local cmd=$@
308
309 log DEBUG "Running command: ${cmd}"
310
311 ${SHELL} ${cmd}
312 local ret=$?
313
314 #log DEBUG "Returned with code '${ret}'"
315
316 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
317 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
318 exit ${EXIT_ERROR_ASSERT}
319 fi
320
321 return ${ret}
322}
323
cad8bd85
MT
324function uppercase() {
325 local input
326 read input
327 echo "${input^^}"
328}
d82cf370 329
3efecbb3
MT
330function lowercase() {
331 local input
332 read input
333 echo "${input,,}"
334}
335
336function seq() {
337 if [ $# -eq 2 ]; then
338 eval echo {${1}..${2}}
339 elif [ $# -eq 3 ]; then
340 eval echo {${1}..${3}..${2}}
341 fi
342}
343
d82cf370
MT
344function beautify_time() {
345 local value=${1}
346
347 local unit
348 local limit
349 for unit in s m h d w; do
350 case "${unit}" in
351 s|m|h)
352 limit=60
353 ;;
354 d)
355 limit=24
356 ;;
357 w)
358 limit=7
359 ;;
360 esac
361
362 [ ${value} -lt ${limit} ] && break
363
364 value=$(( ${value} / ${limit} ))
365 done
366
367 echo "${value}${unit}"
368}
711ffac1
MT
369
370function beautify_bytes() {
371 local value=${1}
372
373 local unit
374 local limit=1024
375 for unit in B k M G T; do
376 [ ${value} -lt ${limit} ] && break
377 value=$(( ${value} / ${limit} ))
378 done
379
380 echo "${value}${unit}"
381}
943e3f7e
MT
382
383function module_load() {
384 local module=${1}
385
386 if ! grep -q "^${module}" /proc/modules; then
387 log DEBUG "Loading module '${module}'."
388 modprobe ${module}
389 fi
390}
6b3f9c85
MT
391
392function binary_exists() {
393 local binary=${1}
394
395 if [ -n "$(type -p ${binary})" ]; then
396 return ${EXIT_OK}
397 fi
398
399 return ${EXIT_ERROR}
400}
d76f5107
MT
401
402function process_kill() {
403 local process=${1}
404
405 if ! isinteger process; then
406 process=$(pidof ${process})
407 fi
408
409 local pid
410 local sig
411 for pid in ${process}; do
412 for sig in 15 9; do
413 [ -d "/proc/${pid}" ] || break
414
415 kill -${sig} ${pid}
416 sleep 1
417 done
418 done
419}
feb76eaf
MT
420
421function dec() {
422 local hex=${1}
423
424 if [ "${hex:0:2}" != "0x" ]; then
425 hex="0x${hex}"
426 fi
427
428 printf "%d\n" "${hex}"
429}