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