]> git.ipfire.org Git - people/stevee/network.git/blame - functions.util
aiccu: Add documentation.
[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
97cb552e
MT
22# A simple print statement
23function print() {
acc9efd5
MT
24 local fmt=${1}; shift
25
40e3553f 26 printf -- "${fmt}\n" "$@"
97cb552e
MT
27}
28
cb965348
MT
29# The args() function takes a number of arguments like
30# var1="abc d" var2="abc" var3="abcd e"
31# and splits them into several arguments, devided by newline
32function args() {
33 echo "$@" | xargs printf "%s\n"
34}
35
04854c77
MT
36function unquote() {
37 local var="$@"
38
39 if [ "${var:0:1}" = "\"" ]; then
40 var=${var:1}
41 fi
42
43 local last=$(( ${#var} - 1 ))
44 if [ ${last} -ge 0 ] && [ "${var:${last}:1}" = "\"" ]; then
45 var=${var:0:${last}}
46 fi
47
48 print "${var}"
49}
50
51function quote() {
52 print "\"%s\"" "$@"
53}
54
1848564d
MT
55# Print a pretty error message
56function error() {
fcbf6823 57 echo -e " ${CLR_RED_B}ERROR${CLR_RESET} : $@" >&2
1848564d
MT
58}
59
1b7a1578 60function error_log() {
1b7a1578
MT
61 log ERROR "$@"
62}
63
1848564d
MT
64# Print a pretty warn message
65function warning() {
fcbf6823 66 echo -e " ${CLR_YELLOW_B}WARNING${CLR_RESET}: $@" >&2
1848564d
MT
67}
68
1b7a1578 69function warning_log() {
1b7a1578
MT
70 log WARNING "$@"
71}
72
e726ef8d
MT
73# The next three functions are kept for backwards
74# compatibility. The need to be dropped at some time.
1848564d 75function listsort() {
e726ef8d 76 list_sort $@
1848564d
MT
77}
78
711ffac1 79function listmatch() {
e726ef8d 80 list_match $@
711ffac1
MT
81}
82
83function listlength() {
e726ef8d 84 list_length $@
711ffac1
MT
85}
86
1848564d
MT
87# Speedup function to avoid a call of the basename binary
88function basename() {
89 echo "${1##*/}"
90}
91
e5651e17
MT
92function format() {
93 local key=${1}
94 assert isset key
95
96 local format=${2}
97 assert isset format
98
99 shift 2
100
101 printf -v "${key}" "${format}" "$@"
102}
103
b79ad79b
MT
104function assign() {
105 local key=${1}
106 assert isset key
107 shift
108
e5651e17 109 format "${key}" "%s" "$@"
b79ad79b
MT
110}
111
112function fread() {
113 local file=${1}
114 assert isset file
115
116 [ -r "${file}" ] || return ${EXIT_ERROR}
117
118 print "$(<${file})"
119}
120
121function fwrite() {
122 local file=${1}
123 assert isset file
124 shift
125
126 print "%s" "$@" >> ${file}
127}
128
1848564d
MT
129function enabled() {
130 local param=${1}
131
e726ef8d 132 list_match "${!param}" yes on true 1
1848564d
MT
133}
134
135function mac_generate() {
790b7ec9
MT
136 # Get a bunch of random hex digits
137 # and remove all dashes from the input.
138 local random=$(</proc/sys/kernel/random/uuid)
139 random=${random//-/}
140 assert isset random
1848564d
MT
141
142 local output
790b7ec9
MT
143
144 local i o
145 for i in $(seq 0 5); do
146 o="0x${random:0:2}"
147 random="${random:2:${#random}}"
148
149 case "${i}" in
150 0)
151 # Remove multicast bit
152 # and set address is software assigned
153 o=$(( ${o} & 0xfe ))
154 o=$(( ${o} | 0x02 ))
155
156 printf -v output "%02x" "${o}"
157 ;;
158 *)
159 printf -v output "%s:%02x" "${output}" "${o}"
160 ;;
161 esac
1848564d
MT
162 done
163
164 # Check if output is valid
165 assert mac_is_valid ${output}
166
790b7ec9 167 echo "${output}"
1848564d
MT
168}
169
18b43372
MT
170function mac_format() {
171 local mac=${1}
48bc31eb 172 assert isset mac
18b43372 173
48bc31eb
MT
174 # Remove all colons and make the rest lowercase.
175 mac=${mac//:/}
176 mac=${mac,,}
18b43372 177
48bc31eb 178 local output
18b43372
MT
179 if [ "${#mac}" = "12" ]; then
180 # Add colons (:) to mac address
181 output=${mac:0:2}
182 local i
183 for i in 2 4 6 8 10; do
184 output="${output}:${mac:${i}:2}"
185 done
48bc31eb
MT
186 else
187 output=${mac}
18b43372
MT
188 fi
189
190 assert mac_is_valid ${output}
191
48bc31eb 192 print "${output}"
18b43372
MT
193}
194
1848564d
MT
195function mac_is_valid() {
196 local mac=${1}
197
198 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
199}
200
201function uuid() {
de543653 202 echo $(</proc/sys/kernel/random/uuid)
1848564d
MT
203}
204
205function isset() {
206 local var=${1}
207
208 [ -n "${!var}" ]
209}
210
211function isoneof() {
212 local var=${!1}
213 shift
214
e726ef8d 215 list_match "${var}" "$@"
1848564d
MT
216}
217
218function isbool() {
219 local var=${1}
220
221 isoneof ${var} 0 1 no yes on off
222}
223
224function isinteger() {
225 local var=${!1}
226
227 [[ ${var} =~ ^[0-9]+$ ]]
228}
229
230function ismac() {
231 local mac=${!1}
232
233 mac_is_valid ${mac}
234}
235
fef4edaf
MT
236function isipaddress() {
237 local addr=${!1}
238
239 ip_is_valid ${addr}
240}
241
711ffac1
MT
242function backtrace() {
243 local start=1
244
245 echo # Empty line
246 error_log "Backtrace (most recent call in first line):"
247
04608623 248 local i source
711ffac1
MT
249 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
250 [ -z "${FUNCNAME[${i}]}" ] && continue
251 [ "${FUNCNAME[${i}]}" == "main" ] && continue
252
04608623
MT
253 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
254 error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
711ffac1
MT
255 done
256}
257
1848564d
MT
258function assert() {
259 local assertion="$@"
260
261 if ! ${assertion}; then
4c670d7c 262 error_log "Assertion '${assertion}' failed."
711ffac1 263 backtrace
cfbe0802 264 exit ${EXIT_ERROR_ASSERT}
1848564d
MT
265 fi
266
267 return ${EXIT_OK}
268}
cad8bd85 269
b0b2f995
MT
270# This function checks, if the given argument is an assert error
271# exit code. If this is the case, the script will halt immediately.
272function assert_check_retval() {
273 local ret=${1}
274
275 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
276 exit ${EXIT_ERROR_ASSERT}
277 fi
278
279 return ${ret}
280}
281
711ffac1
MT
282function exec_cmd() {
283 local cmd=$@
284
285 log DEBUG "Running command: ${cmd}"
286
b816e04b 287 DEBUG=${DEBUG} \
8c63fa13
MT
288 LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \
289 LOG_FACILITY="${LOG_FACILITY}" \
b816e04b 290 ${SHELL} ${cmd}
711ffac1
MT
291 local ret=$?
292
293 #log DEBUG "Returned with code '${ret}'"
294
295 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
296 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
297 exit ${EXIT_ERROR_ASSERT}
298 fi
299
300 return ${ret}
301}
302
b816e04b
MT
303function cmd() {
304 local cmd=$@
305
306 log DEBUG "Running command: ${cmd}"
307
308 ${cmd}
309 local ret=$?
310
311 log DEBUG "Returned with code '${ret}'"
312
313 return ${ret}
314}
315
98146c00
MT
316function cmd_quiet() {
317 cmd $@ &>/dev/null
3efecbb3
MT
318}
319
f80ce052
MT
320function cmd_exec() {
321 local cmd=$@
322
323 log DEBUG "Exec'ing command: ${cmd}"
324
325 exec ${cmd}
326
327 log ERROR "Could not exec-ute: ${cmd}"
328 exit ${EXIT_ERROR}
329}
330
3efecbb3
MT
331function seq() {
332 if [ $# -eq 2 ]; then
333 eval echo {${1}..${2}}
334 elif [ $# -eq 3 ]; then
335 eval echo {${1}..${3}..${2}}
336 fi
337}
338
76e6cd51
MT
339function which() {
340 type -P $@
341}
342
d82cf370
MT
343function beautify_time() {
344 local value=${1}
345
346 local unit
347 local limit
348 for unit in s m h d w; do
349 case "${unit}" in
350 s|m|h)
351 limit=60
352 ;;
353 d)
354 limit=24
355 ;;
356 w)
357 limit=7
358 ;;
359 esac
360
361 [ ${value} -lt ${limit} ] && break
362
363 value=$(( ${value} / ${limit} ))
364 done
365
366 echo "${value}${unit}"
367}
711ffac1
MT
368
369function beautify_bytes() {
370 local value=${1}
371
372 local unit
373 local limit=1024
374 for unit in B k M G T; do
375 [ ${value} -lt ${limit} ] && break
376 value=$(( ${value} / ${limit} ))
377 done
378
379 echo "${value}${unit}"
380}
943e3f7e
MT
381
382function module_load() {
383 local module=${1}
384
385 if ! grep -q "^${module}" /proc/modules; then
386 log DEBUG "Loading module '${module}'."
387 modprobe ${module}
388 fi
389}
6b3f9c85
MT
390
391function binary_exists() {
392 local binary=${1}
393
394 if [ -n "$(type -p ${binary})" ]; then
395 return ${EXIT_OK}
396 fi
397
398 return ${EXIT_ERROR}
399}
d76f5107
MT
400
401function process_kill() {
402 local process=${1}
403
404 if ! isinteger process; then
405 process=$(pidof ${process})
406 fi
407
408 local pid
409 local sig
410 for pid in ${process}; do
411 for sig in 15 9; do
412 [ -d "/proc/${pid}" ] || break
413
414 kill -${sig} ${pid}
415 sleep 1
416 done
417 done
418}
feb76eaf
MT
419
420function dec() {
421 local hex=${1}
422
423 if [ "${hex:0:2}" != "0x" ]; then
424 hex="0x${hex}"
425 fi
426
427 printf "%d\n" "${hex}"
428}
3a7fef62
MT
429
430function network_is_running() {
431 # Check, if the network service is running.
432 service_is_active network
433}
f80ce052
MT
434
435function contains_spaces() {
436 local var="$@"
437
438 # Eliminate spaces.
439 local var2=${var// /}
440
441 if [ ${#var} -ne ${#var2} ]; then
442 return ${EXIT_TRUE}
443 fi
444
445 return ${EXIT_FALSE}
446}