]> git.ipfire.org Git - people/ms/network.git/blame - functions.util
hostapd: Remove unused functions.
[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
73function enabled() {
74 local param=${1}
75
e726ef8d 76 list_match "${!param}" yes on true 1
1848564d
MT
77}
78
79function mac_generate() {
790b7ec9
MT
80 # Get a bunch of random hex digits
81 # and remove all dashes from the input.
82 local random=$(</proc/sys/kernel/random/uuid)
83 random=${random//-/}
84 assert isset random
1848564d
MT
85
86 local output
790b7ec9
MT
87
88 local i o
89 for i in $(seq 0 5); do
90 o="0x${random:0:2}"
91 random="${random:2:${#random}}"
92
93 case "${i}" in
94 0)
95 # Remove multicast bit
96 # and set address is software assigned
97 o=$(( ${o} & 0xfe ))
98 o=$(( ${o} | 0x02 ))
99
100 printf -v output "%02x" "${o}"
101 ;;
102 *)
103 printf -v output "%s:%02x" "${output}" "${o}"
104 ;;
105 esac
1848564d
MT
106 done
107
108 # Check if output is valid
109 assert mac_is_valid ${output}
110
790b7ec9 111 echo "${output}"
1848564d
MT
112}
113
18b43372
MT
114function mac_format() {
115 local mac=${1}
48bc31eb 116 assert isset mac
18b43372 117
48bc31eb
MT
118 # Remove all colons and make the rest lowercase.
119 mac=${mac//:/}
120 mac=${mac,,}
18b43372 121
48bc31eb 122 local output
18b43372
MT
123 if [ "${#mac}" = "12" ]; then
124 # Add colons (:) to mac address
125 output=${mac:0:2}
126 local i
127 for i in 2 4 6 8 10; do
128 output="${output}:${mac:${i}:2}"
129 done
48bc31eb
MT
130 else
131 output=${mac}
18b43372
MT
132 fi
133
134 assert mac_is_valid ${output}
135
48bc31eb 136 print "${output}"
18b43372
MT
137}
138
1848564d
MT
139function mac_is_valid() {
140 local mac=${1}
141
142 [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]]
143}
144
145function uuid() {
de543653 146 echo $(</proc/sys/kernel/random/uuid)
1848564d
MT
147}
148
149function isset() {
150 local var=${1}
151
152 [ -n "${!var}" ]
153}
154
155function isoneof() {
156 local var=${!1}
157 shift
158
e726ef8d 159 list_match "${var}" "$@"
1848564d
MT
160}
161
162function isbool() {
163 local var=${1}
164
165 isoneof ${var} 0 1 no yes on off
166}
167
168function isinteger() {
169 local var=${!1}
170
171 [[ ${var} =~ ^[0-9]+$ ]]
172}
173
174function ismac() {
175 local mac=${!1}
176
177 mac_is_valid ${mac}
178}
179
fef4edaf
MT
180function isipaddress() {
181 local addr=${!1}
182
183 ip_is_valid ${addr}
184}
185
711ffac1
MT
186function backtrace() {
187 local start=1
188
189 echo # Empty line
190 error_log "Backtrace (most recent call in first line):"
191
04608623 192 local i source
711ffac1
MT
193 for i in $(seq ${start} ${#BASH_SOURCE[*]}); do
194 [ -z "${FUNCNAME[${i}]}" ] && continue
195 [ "${FUNCNAME[${i}]}" == "main" ] && continue
196
04608623
MT
197 source=${BASH_SOURCE[$(( ${i} + 1 ))]}
198 error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}"
711ffac1
MT
199 done
200}
201
1848564d
MT
202function assert() {
203 local assertion="$@"
204
205 if ! ${assertion}; then
4c670d7c 206 error_log "Assertion '${assertion}' failed."
711ffac1 207 backtrace
cfbe0802 208 exit ${EXIT_ERROR_ASSERT}
1848564d
MT
209 fi
210
211 return ${EXIT_OK}
212}
cad8bd85 213
b0b2f995
MT
214# This function checks, if the given argument is an assert error
215# exit code. If this is the case, the script will halt immediately.
216function assert_check_retval() {
217 local ret=${1}
218
219 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
220 exit ${EXIT_ERROR_ASSERT}
221 fi
222
223 return ${ret}
224}
225
711ffac1
MT
226function exec_cmd() {
227 local cmd=$@
228
229 log DEBUG "Running command: ${cmd}"
230
b816e04b 231 DEBUG=${DEBUG} \
8c63fa13
MT
232 LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \
233 LOG_FACILITY="${LOG_FACILITY}" \
b816e04b 234 ${SHELL} ${cmd}
711ffac1
MT
235 local ret=$?
236
237 #log DEBUG "Returned with code '${ret}'"
238
239 if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then
240 error_log "Stopping parent process due to assertion error in child process: ${cmd}"
241 exit ${EXIT_ERROR_ASSERT}
242 fi
243
244 return ${ret}
245}
246
b816e04b
MT
247function cmd() {
248 local cmd=$@
249
250 log DEBUG "Running command: ${cmd}"
251
252 ${cmd}
253 local ret=$?
254
255 log DEBUG "Returned with code '${ret}'"
256
257 return ${ret}
258}
259
98146c00
MT
260function cmd_quiet() {
261 cmd $@ &>/dev/null
3efecbb3
MT
262}
263
264function seq() {
265 if [ $# -eq 2 ]; then
266 eval echo {${1}..${2}}
267 elif [ $# -eq 3 ]; then
268 eval echo {${1}..${3}..${2}}
269 fi
270}
271
76e6cd51
MT
272function which() {
273 type -P $@
274}
275
d82cf370
MT
276function beautify_time() {
277 local value=${1}
278
279 local unit
280 local limit
281 for unit in s m h d w; do
282 case "${unit}" in
283 s|m|h)
284 limit=60
285 ;;
286 d)
287 limit=24
288 ;;
289 w)
290 limit=7
291 ;;
292 esac
293
294 [ ${value} -lt ${limit} ] && break
295
296 value=$(( ${value} / ${limit} ))
297 done
298
299 echo "${value}${unit}"
300}
711ffac1
MT
301
302function beautify_bytes() {
303 local value=${1}
304
305 local unit
306 local limit=1024
307 for unit in B k M G T; do
308 [ ${value} -lt ${limit} ] && break
309 value=$(( ${value} / ${limit} ))
310 done
311
312 echo "${value}${unit}"
313}
943e3f7e
MT
314
315function module_load() {
316 local module=${1}
317
318 if ! grep -q "^${module}" /proc/modules; then
319 log DEBUG "Loading module '${module}'."
320 modprobe ${module}
321 fi
322}
6b3f9c85
MT
323
324function binary_exists() {
325 local binary=${1}
326
327 if [ -n "$(type -p ${binary})" ]; then
328 return ${EXIT_OK}
329 fi
330
331 return ${EXIT_ERROR}
332}
d76f5107
MT
333
334function process_kill() {
335 local process=${1}
336
337 if ! isinteger process; then
338 process=$(pidof ${process})
339 fi
340
341 local pid
342 local sig
343 for pid in ${process}; do
344 for sig in 15 9; do
345 [ -d "/proc/${pid}" ] || break
346
347 kill -${sig} ${pid}
348 sleep 1
349 done
350 done
351}
feb76eaf
MT
352
353function dec() {
354 local hex=${1}
355
356 if [ "${hex:0:2}" != "0x" ]; then
357 hex="0x${hex}"
358 fi
359
360 printf "%d\n" "${hex}"
361}
3a7fef62
MT
362
363function network_is_running() {
364 # Check, if the network service is running.
365 service_is_active network
366}