]>
Commit | Line | Data |
---|---|---|
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 | 22 | # A simple print statement |
1c6a4e30 | 23 | print() { |
acc9efd5 MT |
24 | local fmt=${1}; shift |
25 | ||
40e3553f | 26 | printf -- "${fmt}\n" "$@" |
97cb552e MT |
27 | } |
28 | ||
67baa452 MT |
29 | print_indent() { |
30 | local i=${1} | |
31 | shift | |
32 | ||
33 | while (( i-- )); do | |
34 | printf "\t" | |
35 | done | |
36 | ||
37 | print "%s" "$@" | |
38 | } | |
39 | ||
cb965348 MT |
40 | # The args() function takes a number of arguments like |
41 | # var1="abc d" var2="abc" var3="abcd e" | |
42 | # and splits them into several arguments, devided by newline | |
1c6a4e30 | 43 | args() { |
cb965348 MT |
44 | echo "$@" | xargs printf "%s\n" |
45 | } | |
46 | ||
1c6a4e30 | 47 | unquote() { |
04854c77 MT |
48 | local var="$@" |
49 | ||
50 | if [ "${var:0:1}" = "\"" ]; then | |
51 | var=${var:1} | |
52 | fi | |
53 | ||
54 | local last=$(( ${#var} - 1 )) | |
55 | if [ ${last} -ge 0 ] && [ "${var:${last}:1}" = "\"" ]; then | |
56 | var=${var:0:${last}} | |
57 | fi | |
58 | ||
59 | print "${var}" | |
60 | } | |
61 | ||
1c6a4e30 | 62 | quote() { |
04854c77 MT |
63 | print "\"%s\"" "$@" |
64 | } | |
65 | ||
1c6a4e30 | 66 | strip() { |
fe52c5e0 MT |
67 | local value="$@" |
68 | ||
69 | # remove leading whitespace characters | |
70 | value="${value#"${value%%[![:space:]]*}"}" | |
71 | ||
72 | # remove trailing whitespace characters | |
73 | value="${value%"${value##*[![:space:]]}"}" | |
74 | ||
75 | print "${value}" | |
76 | } | |
77 | ||
1848564d | 78 | # Print a pretty error message |
1c6a4e30 | 79 | error() { |
fcbf6823 | 80 | echo -e " ${CLR_RED_B}ERROR${CLR_RESET} : $@" >&2 |
1848564d MT |
81 | } |
82 | ||
1c6a4e30 | 83 | error_log() { |
1b7a1578 MT |
84 | log ERROR "$@" |
85 | } | |
86 | ||
1848564d | 87 | # Print a pretty warn message |
1c6a4e30 | 88 | warning() { |
fcbf6823 | 89 | echo -e " ${CLR_YELLOW_B}WARNING${CLR_RESET}: $@" >&2 |
1848564d MT |
90 | } |
91 | ||
1c6a4e30 | 92 | warning_log() { |
1b7a1578 MT |
93 | log WARNING "$@" |
94 | } | |
95 | ||
1848564d | 96 | # Speedup function to avoid a call of the basename binary |
1c6a4e30 | 97 | basename() { |
1848564d MT |
98 | echo "${1##*/}" |
99 | } | |
100 | ||
1c6a4e30 | 101 | format() { |
e5651e17 MT |
102 | local key=${1} |
103 | assert isset key | |
104 | ||
105 | local format=${2} | |
106 | assert isset format | |
107 | ||
108 | shift 2 | |
109 | ||
110 | printf -v "${key}" "${format}" "$@" | |
111 | } | |
112 | ||
d13929d4 MT |
113 | format_time() { |
114 | local s=${1} | |
115 | local ret m | |
116 | ||
117 | local units="s m h" | |
118 | ||
119 | local unit | |
120 | for unit in ${units}; do | |
121 | m=$(( ${s} % 60 )) | |
122 | s=$(( ${s} / 60 )) | |
123 | ||
124 | if [ ${m} -gt 0 ]; then | |
125 | ret="${m}${unit} ${ret}" | |
126 | fi | |
127 | done | |
128 | ||
129 | # Remove whitespace | |
130 | echo ${ret} | |
131 | } | |
132 | ||
b383499d MT |
133 | parse_time() { |
134 | local ret=0 | |
135 | ||
136 | local arg | |
2212045f | 137 | for arg in "$@"; do |
b383499d MT |
138 | local unit |
139 | ||
140 | case "${arg}" in | |
141 | *h|*m|*s) | |
142 | # Store unit | |
143 | unit="${arg: -1}" | |
144 | ||
145 | # Remove unit | |
146 | arg="${arg:0:-1}" | |
147 | ;; | |
148 | esac | |
149 | ||
150 | if ! isinteger arg; then | |
151 | return ${EXIT_ERROR} | |
152 | fi | |
153 | ||
154 | # Convert hours and minutes into seconds | |
155 | case "${unit}" in | |
156 | h) | |
157 | arg=$(( ${arg} * 3600 )) | |
158 | ;; | |
159 | m) | |
160 | arg=$(( ${arg} * 60 )) | |
161 | ;; | |
162 | esac | |
163 | ||
164 | # Add up everything | |
165 | ret=$(( ${ret} + ${arg} )) | |
166 | done | |
167 | ||
168 | print "${ret}" | |
169 | } | |
170 | ||
1c6a4e30 | 171 | assign() { |
b79ad79b MT |
172 | local key=${1} |
173 | assert isset key | |
174 | shift | |
175 | ||
e5651e17 | 176 | format "${key}" "%s" "$@" |
b79ad79b MT |
177 | } |
178 | ||
1c6a4e30 | 179 | fread() { |
b79ad79b MT |
180 | local file=${1} |
181 | assert isset file | |
182 | ||
183 | [ -r "${file}" ] || return ${EXIT_ERROR} | |
184 | ||
185 | print "$(<${file})" | |
186 | } | |
187 | ||
1c6a4e30 | 188 | fwrite() { |
b79ad79b MT |
189 | local file=${1} |
190 | assert isset file | |
191 | shift | |
192 | ||
644d3bb8 MT |
193 | if ! print "%s" "$@" > ${file} 2>/dev/null; then |
194 | error "Could not write to file: ${file}" | |
195 | return ${EXIT_ERROR} | |
196 | fi | |
197 | ||
198 | return ${EXIT_OK} | |
199 | } | |
200 | ||
201 | fappend() { | |
202 | local file=${1} | |
203 | assert isset file | |
204 | shift | |
205 | ||
e1947a76 | 206 | if [ -e "${file}" ] && [ ! -w "${file}" ]; then |
8d4e0d52 MT |
207 | log ERROR "${file}: No such file" |
208 | return ${EXIT_ERROR} | |
209 | fi | |
210 | ||
211 | print "%s" "$@" >> ${file} 2>/dev/null | |
b79ad79b MT |
212 | } |
213 | ||
fa33d830 MT |
214 | file_delete() { |
215 | local file=${1} | |
216 | ||
217 | unlink "${file}" 2>/dev/null | |
218 | } | |
219 | ||
e1947a76 MT |
220 | file_exists() { |
221 | local file=${1} | |
222 | ||
223 | [ -e "${file}" ] && return ${EXIT_TRUE} || return ${EXIT_FALSE} | |
224 | } | |
225 | ||
226 | file_is_newer_than() { | |
227 | local file1="${1}" | |
228 | local file2="${2}" | |
229 | ||
230 | local age1=$(file_get_age "${file1}") | |
231 | local age2=$(file_get_age "${file2}") | |
232 | ||
233 | if [ ${age1} -gt ${age2} ]; then | |
234 | return ${EXIT_TRUE} | |
235 | else | |
236 | return ${EXIT_FALSE} | |
237 | fi | |
238 | } | |
239 | ||
240 | file_get_age() { | |
241 | local file="${1}" | |
242 | ||
243 | if [ -e "${file}" ]; then | |
244 | stat --format="%Y" "${file}" | |
245 | return $? | |
246 | fi | |
247 | ||
248 | return ${EXIT_ERROR} | |
249 | } | |
250 | ||
7c91c167 MT |
251 | file_to_log() { |
252 | local level="${1}" | |
253 | assert isset level | |
254 | ||
255 | local file="${2}" | |
256 | assert file_exists "${file}" | |
257 | ||
258 | local line | |
259 | while read line; do | |
260 | log "${level}" "${line}" | |
261 | done < "${file}" | |
262 | } | |
263 | ||
04c27fe9 MT |
264 | make_directory() { |
265 | local path="${1}" | |
266 | ||
72848301 MT |
267 | # Exit silently when directory already exists |
268 | if [ -d "${path}" ]; then | |
269 | return ${EXIT_OK} | |
270 | fi | |
271 | ||
39aae674 MT |
272 | if ! mkdir -p "${path}"; then |
273 | log ERROR "Could not create directory ${path}" | |
274 | return ${EXIT_ERROR} | |
275 | fi | |
276 | ||
277 | log DEBUG "Created directory ${path}" | |
278 | return ${EXIT_OK} | |
04c27fe9 MT |
279 | } |
280 | ||
46954be3 | 281 | make_parent_directory() { |
c041b631 MT |
282 | local path="${1}" |
283 | ||
04c27fe9 | 284 | make_directory "$(dirname "${path}")" |
c041b631 MT |
285 | } |
286 | ||
1c6a4e30 | 287 | enabled() { |
1848564d MT |
288 | local param=${1} |
289 | ||
e726ef8d | 290 | list_match "${!param}" yes on true 1 |
1848564d MT |
291 | } |
292 | ||
0842edcc MT |
293 | disabled() { |
294 | local param="${1}" | |
295 | ||
296 | list_match "${!param}" no off false 0 | |
297 | } | |
298 | ||
1c6a4e30 | 299 | mac_generate() { |
fb1416c6 MT |
300 | local b="$(random 12)" |
301 | ||
302 | # Remove multicast bit | |
303 | # and set address is software assigned | |
304 | local first_byte=$(( 0x${b:0:2} & 0xfe )) | |
305 | first_byte=$(( ${first_byte} | 0x02 )) | |
1848564d MT |
306 | |
307 | local output | |
fb1416c6 | 308 | printf -v output "%02x" "${first_byte}" |
790b7ec9 | 309 | |
fb1416c6 | 310 | output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}" |
1848564d MT |
311 | |
312 | # Check if output is valid | |
fb1416c6 | 313 | assert mac_is_valid "${output}" |
1848564d | 314 | |
790b7ec9 | 315 | echo "${output}" |
1848564d MT |
316 | } |
317 | ||
1c6a4e30 | 318 | mac_format() { |
18b43372 | 319 | local mac=${1} |
48bc31eb | 320 | assert isset mac |
18b43372 | 321 | |
48bc31eb MT |
322 | # Remove all colons and make the rest lowercase. |
323 | mac=${mac//:/} | |
324 | mac=${mac,,} | |
18b43372 | 325 | |
48bc31eb | 326 | local output |
18b43372 MT |
327 | if [ "${#mac}" = "12" ]; then |
328 | # Add colons (:) to mac address | |
329 | output=${mac:0:2} | |
330 | local i | |
331 | for i in 2 4 6 8 10; do | |
332 | output="${output}:${mac:${i}:2}" | |
333 | done | |
48bc31eb MT |
334 | else |
335 | output=${mac} | |
18b43372 MT |
336 | fi |
337 | ||
338 | assert mac_is_valid ${output} | |
339 | ||
48bc31eb | 340 | print "${output}" |
18b43372 MT |
341 | } |
342 | ||
1c6a4e30 | 343 | mac_is_valid() { |
1848564d MT |
344 | local mac=${1} |
345 | ||
346 | [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] | |
347 | } | |
348 | ||
01ff07e9 MT |
349 | # Converts the given string to lowercase and returns true if it is a valid FQDN |
350 | fqdn_is_valid() { | |
351 | local fqdn="${1}" | |
352 | ||
353 | if grep -qP "^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$" <<< "${fqdn,,}"; then | |
354 | return ${EXIT_TRUE} | |
355 | fi | |
356 | ||
357 | return ${EXIT_FALSE} | |
358 | } | |
359 | ||
1c6a4e30 | 360 | uuid() { |
de543653 | 361 | echo $(</proc/sys/kernel/random/uuid) |
1848564d MT |
362 | } |
363 | ||
a24cff8f JS |
364 | abs() { |
365 | local val=${1} | |
366 | ||
367 | if [ ${val} -lt 0 ]; then | |
368 | (( val *= -1 )) | |
369 | fi | |
370 | ||
371 | echo ${val} | |
372 | } | |
373 | ||
fb1416c6 MT |
374 | rand() { |
375 | local uuid="$(uuid)" | |
376 | echo "${uuid//-/}" | |
377 | } | |
378 | ||
379 | random() { | |
380 | local length="${1:-8}" | |
381 | ||
382 | local random | |
383 | while [ ${#random} -lt ${length} ]; do | |
384 | random="${random}$(rand)" | |
385 | done | |
386 | ||
387 | echo "${random:0:${length}}" | |
388 | } | |
389 | ||
1c6a4e30 | 390 | isset() { |
1848564d MT |
391 | local var=${1} |
392 | ||
393 | [ -n "${!var}" ] | |
394 | } | |
395 | ||
1c6a4e30 | 396 | isoneof() { |
1848564d MT |
397 | local var=${!1} |
398 | shift | |
399 | ||
e726ef8d | 400 | list_match "${var}" "$@" |
1848564d MT |
401 | } |
402 | ||
1c6a4e30 | 403 | isbool() { |
1848564d MT |
404 | local var=${1} |
405 | ||
ec6afbdd | 406 | isoneof ${var} 0 1 no yes on off true false |
1848564d MT |
407 | } |
408 | ||
1c6a4e30 | 409 | isinteger() { |
1848564d MT |
410 | local var=${!1} |
411 | ||
412 | [[ ${var} =~ ^[0-9]+$ ]] | |
413 | } | |
414 | ||
1c6a4e30 | 415 | ismac() { |
1848564d MT |
416 | local mac=${!1} |
417 | ||
418 | mac_is_valid ${mac} | |
419 | } | |
420 | ||
1c6a4e30 | 421 | isipaddress() { |
fef4edaf MT |
422 | local addr=${!1} |
423 | ||
424 | ip_is_valid ${addr} | |
425 | } | |
426 | ||
48a64768 JS |
427 | mtu_is_valid() { |
428 | local proto=${1} | |
429 | local mtu=${2} | |
430 | ||
431 | case ${proto} in | |
ef53293c | 432 | ethernet|ipv4) |
48a64768 JS |
433 | [ ${mtu} -ge 576 ] && [ ${mtu} -le 9000 ] |
434 | ;; | |
435 | ipv6) | |
436 | [ ${mtu} -ge 1280 ] && [ ${mtu} -le 9000 ] | |
437 | ;; | |
438 | *) | |
439 | error "${proto} is not a valid proto" | |
440 | return ${EXIT_ERROR} | |
441 | ;; | |
442 | esac | |
443 | } | |
444 | ||
1c6a4e30 | 445 | backtrace() { |
711ffac1 MT |
446 | local start=1 |
447 | ||
448 | echo # Empty line | |
449 | error_log "Backtrace (most recent call in first line):" | |
450 | ||
04608623 | 451 | local i source |
711ffac1 MT |
452 | for i in $(seq ${start} ${#BASH_SOURCE[*]}); do |
453 | [ -z "${FUNCNAME[${i}]}" ] && continue | |
6396ccab MT |
454 | |
455 | # Print called binary with arguments. | |
456 | if [ "${FUNCNAME[${i}]}" == "main" ]; then | |
457 | local args="$(list_reverse ${BASH_ARGV[*]})" | |
458 | printf -v source "%20s" "$0" | |
459 | error_log " ${source} ${args}" | |
460 | continue | |
461 | fi | |
711ffac1 | 462 | |
04608623 MT |
463 | source=${BASH_SOURCE[$(( ${i} + 1 ))]} |
464 | error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}" | |
711ffac1 MT |
465 | done |
466 | } | |
467 | ||
1c6a4e30 | 468 | assert() { |
1848564d MT |
469 | local assertion="$@" |
470 | ||
471 | if ! ${assertion}; then | |
711ffac1 | 472 | backtrace |
9532462f MT |
473 | |
474 | # End the program here | |
475 | abort "Assertion failed: ${assertion}" | |
1848564d MT |
476 | fi |
477 | ||
478 | return ${EXIT_OK} | |
479 | } | |
cad8bd85 | 480 | |
9532462f MT |
481 | # Ends the program immediately without cleaning up |
482 | abort() { | |
483 | local msg="$@" | |
484 | ||
485 | # Print message | |
486 | if isset msg; then | |
487 | log ERROR "${msg}" | |
488 | fi | |
489 | ||
490 | exit ${EXIT_ERROR_ASSERT} | |
491 | } | |
492 | ||
b0b2f995 MT |
493 | # This function checks, if the given argument is an assert error |
494 | # exit code. If this is the case, the script will halt immediately. | |
1c6a4e30 | 495 | assert_check_retval() { |
b0b2f995 MT |
496 | local ret=${1} |
497 | ||
498 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
499 | exit ${EXIT_ERROR_ASSERT} | |
500 | fi | |
501 | ||
502 | return ${ret} | |
503 | } | |
504 | ||
2bb20bbd SS |
505 | # This function executes the given command and inverses the return code |
506 | not() { | |
507 | local command="$@" | |
508 | ||
509 | ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE} | |
510 | } | |
511 | ||
1c6a4e30 | 512 | exec_cmd() { |
711ffac1 MT |
513 | local cmd=$@ |
514 | ||
515 | log DEBUG "Running command: ${cmd}" | |
516 | ||
b816e04b | 517 | DEBUG=${DEBUG} \ |
8c63fa13 MT |
518 | LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \ |
519 | LOG_FACILITY="${LOG_FACILITY}" \ | |
b816e04b | 520 | ${SHELL} ${cmd} |
711ffac1 MT |
521 | local ret=$? |
522 | ||
523 | #log DEBUG "Returned with code '${ret}'" | |
524 | ||
525 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
526 | error_log "Stopping parent process due to assertion error in child process: ${cmd}" | |
527 | exit ${EXIT_ERROR_ASSERT} | |
528 | fi | |
529 | ||
530 | return ${ret} | |
531 | } | |
532 | ||
1c6a4e30 | 533 | cmd() { |
b816e04b MT |
534 | local cmd=$@ |
535 | ||
536 | log DEBUG "Running command: ${cmd}" | |
537 | ||
4b75fc83 MT |
538 | env -i -- \ |
539 | HOME="${HOME}" \ | |
540 | PATH="${PATH}" \ | |
541 | TERM="${TERM}" \ | |
542 | ${cmd} | |
543 | local ret=$? | |
b816e04b | 544 | |
4b75fc83 MT |
545 | case "${ret}" in |
546 | ${EXIT_OK}) | |
547 | return ${EXIT_OK} | |
548 | ;; | |
549 | *) | |
550 | log DEBUG "Returned with code '${ret}'" | |
551 | return ${ret} | |
552 | ;; | |
553 | esac | |
b816e04b MT |
554 | } |
555 | ||
1c6a4e30 | 556 | cmd_quiet() { |
2212045f | 557 | cmd "$@" &>/dev/null |
3efecbb3 MT |
558 | } |
559 | ||
1c6a4e30 | 560 | cmd_exec() { |
85f35e51 | 561 | local cmd=( "$@" ) |
f80ce052 MT |
562 | |
563 | log DEBUG "Exec'ing command: ${cmd}" | |
564 | ||
85f35e51 | 565 | exec "${cmd[@]}" |
f80ce052 MT |
566 | |
567 | log ERROR "Could not exec-ute: ${cmd}" | |
568 | exit ${EXIT_ERROR} | |
569 | } | |
570 | ||
1c6a4e30 | 571 | cmd_not_implemented() { |
2181765d MT |
572 | assert false "not implemented" |
573 | } | |
574 | ||
f5ee091e MT |
575 | # Executes the given command in background |
576 | cmd_background() { | |
2212045f | 577 | cmd_quiet "$@" & |
f5ee091e MT |
578 | } |
579 | ||
580 | # Prints the PID of the process that was started last | |
581 | cmd_background_get_pid() { | |
582 | print "${!}" | |
583 | } | |
584 | ||
585 | cmd_background_result() { | |
586 | local pids=$@ | |
587 | ||
588 | wait ${pids} | |
589 | } | |
590 | ||
b8026986 | 591 | # Increase security of the read command |
1c6a4e30 | 592 | read() { |
2212045f | 593 | builtin read -r "$@" |
b8026986 MT |
594 | } |
595 | ||
1c6a4e30 | 596 | seq() { |
3efecbb3 MT |
597 | if [ $# -eq 2 ]; then |
598 | eval echo {${1}..${2}} | |
599 | elif [ $# -eq 3 ]; then | |
600 | eval echo {${1}..${3}..${2}} | |
601 | fi | |
602 | } | |
603 | ||
de72bd91 MT |
604 | range() { |
605 | eval echo {0..$(( ${1} - 1 ))} | |
606 | } | |
607 | ||
608 | count() { | |
609 | local i=0 | |
610 | ||
611 | while read; do | |
612 | ((i++)) | |
613 | done | |
614 | ||
615 | echo ${i} | |
616 | } | |
617 | ||
1c6a4e30 | 618 | which() { |
2212045f | 619 | type -P "$@" |
76e6cd51 MT |
620 | } |
621 | ||
fe52c5e0 | 622 | # Prints the number of seconds since epoch. |
1c6a4e30 | 623 | timestamp() { |
fe52c5e0 MT |
624 | date -u "+%s" |
625 | } | |
626 | ||
1c6a4e30 | 627 | beautify_time() { |
d82cf370 MT |
628 | local value=${1} |
629 | ||
630 | local unit | |
631 | local limit | |
632 | for unit in s m h d w; do | |
633 | case "${unit}" in | |
634 | s|m|h) | |
635 | limit=60 | |
636 | ;; | |
637 | d) | |
638 | limit=24 | |
639 | ;; | |
640 | w) | |
641 | limit=7 | |
642 | ;; | |
643 | esac | |
644 | ||
645 | [ ${value} -lt ${limit} ] && break | |
646 | ||
647 | value=$(( ${value} / ${limit} )) | |
648 | done | |
649 | ||
650 | echo "${value}${unit}" | |
651 | } | |
711ffac1 | 652 | |
1c6a4e30 | 653 | beautify_bytes() { |
711ffac1 MT |
654 | local value=${1} |
655 | ||
656 | local unit | |
657 | local limit=1024 | |
658 | for unit in B k M G T; do | |
659 | [ ${value} -lt ${limit} ] && break | |
660 | value=$(( ${value} / ${limit} )) | |
661 | done | |
662 | ||
663 | echo "${value}${unit}" | |
664 | } | |
943e3f7e | 665 | |
1c6a4e30 | 666 | module_load() { |
943e3f7e MT |
667 | local module=${1} |
668 | ||
dd891ec4 MT |
669 | # Do nothing if the module is already loaded |
670 | if [ -d "/sys/module/${module//-/_}" ]; then | |
671 | return ${EXIT_OK} | |
943e3f7e | 672 | fi |
dd891ec4 MT |
673 | |
674 | log DEBUG "Loading kernel module ${module}" | |
675 | modprobe "${module}" | |
943e3f7e | 676 | } |
6b3f9c85 | 677 | |
1c6a4e30 | 678 | binary_exists() { |
6b3f9c85 MT |
679 | local binary=${1} |
680 | ||
681 | if [ -n "$(type -p ${binary})" ]; then | |
682 | return ${EXIT_OK} | |
683 | fi | |
684 | ||
685 | return ${EXIT_ERROR} | |
686 | } | |
d76f5107 | 687 | |
1c6a4e30 | 688 | function_exists() { |
1e6f187e MT |
689 | local function="${1}" |
690 | ||
691 | if [ "$(type -t "${function}")" = "function" ]; then | |
692 | return ${EXIT_TRUE} | |
693 | fi | |
694 | ||
695 | return ${EXIT_FALSE} | |
696 | } | |
697 | ||
1c6a4e30 | 698 | process_kill() { |
d76f5107 MT |
699 | local process=${1} |
700 | ||
701 | if ! isinteger process; then | |
702 | process=$(pidof ${process}) | |
703 | fi | |
704 | ||
705 | local pid | |
706 | local sig | |
707 | for pid in ${process}; do | |
708 | for sig in 15 9; do | |
709 | [ -d "/proc/${pid}" ] || break | |
710 | ||
711 | kill -${sig} ${pid} | |
712 | sleep 1 | |
713 | done | |
714 | done | |
715 | } | |
feb76eaf | 716 | |
1c6a4e30 | 717 | dec() { |
feb76eaf MT |
718 | local hex=${1} |
719 | ||
720 | if [ "${hex:0:2}" != "0x" ]; then | |
721 | hex="0x${hex}" | |
722 | fi | |
723 | ||
724 | printf "%d\n" "${hex}" | |
725 | } | |
3a7fef62 | 726 | |
1c6a4e30 | 727 | chr() { |
5cf0edf9 MT |
728 | local char="${1}" |
729 | ||
730 | [ ${char} -lt 256 ] || return ${EXIT_ERROR} | |
731 | ||
732 | printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n" | |
733 | } | |
734 | ||
1c6a4e30 | 735 | ord() { |
5cf0edf9 MT |
736 | LC_CTYPE="C" printf "%d\n" "'${1}" |
737 | } | |
738 | ||
1c6a4e30 | 739 | hex() { |
5cf0edf9 MT |
740 | printf "%X\n" "${1}" |
741 | } | |
742 | ||
1c6a4e30 | 743 | network_is_running() { |
3a7fef62 MT |
744 | # Check, if the network service is running. |
745 | service_is_active network | |
746 | } | |
f80ce052 | 747 | |
1c6a4e30 | 748 | contains_spaces() { |
f80ce052 MT |
749 | local var="$@" |
750 | ||
751 | # Eliminate spaces. | |
752 | local var2=${var// /} | |
753 | ||
754 | if [ ${#var} -ne ${#var2} ]; then | |
755 | return ${EXIT_TRUE} | |
756 | fi | |
757 | ||
758 | return ${EXIT_FALSE} | |
0d645497 MT |
759 | } |
760 | ||
d695b280 MT |
761 | contains_non_ascii_characters() { |
762 | local value="$@" | |
763 | ||
764 | # Strip away all ASCII characters | |
765 | local non_ascii="${value//[[:ascii:]]/}" | |
766 | ||
767 | if isset non_ascii; then | |
768 | return ${EXIT_TRUE} | |
769 | fi | |
770 | ||
771 | return ${EXIT_FALSE} | |
772 | } | |
773 | ||
0d645497 MT |
774 | string_match() { |
775 | local match=${1} | |
776 | local string=${2} | |
777 | ||
778 | [[ ${string} =~ ${match} ]] && return ${EXIT_TRUE} || return ${EXIT_FALSE} | |
f80ce052 | 779 | } |
5cf0edf9 | 780 | |
1c6a4e30 | 781 | string_split() { |
5cf0edf9 MT |
782 | local string="$@" |
783 | ||
784 | local pos=0 | |
785 | while [ ${pos} -lt ${#string} ]; do | |
786 | print "${string:${pos}:1}" | |
787 | pos=$(( ${pos} + 1 )) | |
788 | done | |
789 | ||
790 | return ${EXIT_OK} | |
791 | } | |
792 | ||
1c6a4e30 | 793 | string_reverse() { |
5cf0edf9 MT |
794 | local string="$@" |
795 | ||
796 | local output | |
797 | local pos=0 | |
798 | while [ ${pos} -lt ${#string} ]; do | |
799 | output="${string:${pos}:1}${output}" | |
800 | pos=$(( ${pos} + 1 )) | |
801 | done | |
802 | ||
803 | print "${output}" | |
804 | return ${EXIT_OK} | |
805 | } | |
806 | ||
1c6a4e30 | 807 | dec2bin() { |
5cf0edf9 MT |
808 | local number="${1}" |
809 | ||
810 | local output | |
811 | ||
812 | local i div | |
813 | for i in 7 6 5 4 3 2 1; do | |
814 | div=$(( 2 ** ${i} )) | |
815 | ||
816 | if [ $(( ${number} / ${div} )) -eq 1 ]; then | |
817 | output="${output}1" | |
818 | else | |
819 | output="${output}0" | |
820 | fi | |
821 | number="$(( ${number} % ${div} ))" | |
822 | done | |
823 | ||
824 | if [ $(( ${number} % 2 )) -eq 1 ]; then | |
825 | output="${output}1" | |
826 | else | |
827 | output="${output}0" | |
828 | fi | |
829 | ||
830 | print "${output}" | |
831 | } | |
832 | ||
1c6a4e30 | 833 | bin2dec() { |
5cf0edf9 MT |
834 | local string="${1}" |
835 | local number=0 | |
836 | ||
837 | local pos=0 char | |
838 | while [ ${pos} -lt ${#string} ]; do | |
839 | char="${string:${pos}:1}" | |
840 | pos=$(( ${pos} + 1 )) | |
841 | ||
842 | number=$(( ${number} << 1 )) | |
843 | ||
844 | case "${char}" in | |
845 | 0) ;; | |
846 | 1) | |
847 | number=$(( ${number} + 1 )) | |
848 | ;; | |
849 | *) | |
850 | assert false "Invalid character: ${char}" | |
851 | ;; | |
852 | esac | |
853 | done | |
854 | ||
855 | print "${number}" | |
856 | return ${EXIT_OK} | |
857 | } | |
858 | ||
1c6a4e30 | 859 | char2bin() { |
5cf0edf9 MT |
860 | local dec="$(ord "${1}")" |
861 | ||
862 | dec2bin "${dec}" | |
863 | } | |
864 | ||
1c6a4e30 | 865 | bin2char() { |
5cf0edf9 MT |
866 | local dec="$(bin2dec "$@")" |
867 | ||
868 | chr "${dec}" | |
869 | } | |
870 | ||
1c6a4e30 | 871 | bin2hex() { |
5cf0edf9 MT |
872 | local dec="$(bin2dec "$@")" |
873 | ||
874 | dec2hex "${dec}" | |
875 | } | |
876 | ||
1c6a4e30 | 877 | hex2bin() { |
5cf0edf9 MT |
878 | local dec="$(hex2dec "$@")" |
879 | ||
880 | dec2bin "${dec}" | |
881 | } | |
882 | ||
1c6a4e30 | 883 | hex2dec() { |
5cf0edf9 MT |
884 | local hex="${1}" |
885 | ||
886 | # Prepend 0x if necessary. | |
887 | [ "${hex:0:2}" = "0x" ] || hex="0x${hex}" | |
888 | ||
889 | printf "%d\n" "${hex}" | |
890 | } | |
891 | ||
1c6a4e30 | 892 | dec2hex() { |
5cf0edf9 MT |
893 | printf "%02x\n" "${1}" |
894 | } | |
a95d16fc | 895 | |
10b53c87 | 896 | # This function just copy config files |
a95d16fc | 897 | copy() { |
a95d16fc JS |
898 | assert [ $# -eq 2 ] |
899 | ||
900 | local src=${1} | |
901 | local dst=${2} | |
902 | ||
287c2e60 MT |
903 | # Check if we can read from the source |
904 | if [ ! -r "${src}" ]; then | |
905 | log ERROR "Cannot read ${src}" | |
906 | return ${EXIT_ERROR} | |
907 | fi | |
908 | ||
a95d16fc | 909 | # Check if ${dst} is a directory |
1fade616 | 910 | if [ -d "${dst}" ]; then |
9c802e1d | 911 | log ERROR "${dst} is a directory" |
a95d16fc JS |
912 | return ${EXIT_ERROR} |
913 | fi | |
914 | ||
2012c1b6 MT |
915 | # Create destination directory if it doesn't exist, yet |
916 | if ! make_parent_directory "${dst}"; then | |
917 | return ${EXIT_ERROR} | |
918 | fi | |
919 | ||
a95d16fc JS |
920 | if ! fread "${src}" > "${dst}"; then |
921 | log ERROR "Could not copy data from ${src} to ${dst}" | |
922 | return ${EXIT_ERROR} | |
923 | fi | |
924 | } | |
a8d8d2c5 JS |
925 | |
926 | normalize() { | |
927 | local string="$@" | |
928 | ||
929 | tr -sc [:alnum:] "-" < <(printf "%s" "${string,,}") | |
930 | } | |
4ad5981f MT |
931 | |
932 | get_driver_from_path() { | |
933 | local path="${1}" | |
934 | ||
935 | if file_exists "${path}"; then | |
936 | # Resolve symlink | |
937 | local driver="$(readlink "${path}")" | |
938 | ||
939 | # Print module name | |
940 | basename "${driver}" | |
941 | return ${EXIT_OK} | |
942 | fi | |
943 | ||
944 | return ${EXIT_ERROR} | |
945 | } |