]>
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 | ||
04c27fe9 MT |
251 | make_directory() { |
252 | local path="${1}" | |
253 | ||
72848301 MT |
254 | # Exit silently when directory already exists |
255 | if [ -d "${path}" ]; then | |
256 | return ${EXIT_OK} | |
257 | fi | |
258 | ||
39aae674 MT |
259 | if ! mkdir -p "${path}"; then |
260 | log ERROR "Could not create directory ${path}" | |
261 | return ${EXIT_ERROR} | |
262 | fi | |
263 | ||
264 | log DEBUG "Created directory ${path}" | |
265 | return ${EXIT_OK} | |
04c27fe9 MT |
266 | } |
267 | ||
46954be3 | 268 | make_parent_directory() { |
c041b631 MT |
269 | local path="${1}" |
270 | ||
04c27fe9 | 271 | make_directory "$(dirname "${path}")" |
c041b631 MT |
272 | } |
273 | ||
1c6a4e30 | 274 | enabled() { |
1848564d MT |
275 | local param=${1} |
276 | ||
e726ef8d | 277 | list_match "${!param}" yes on true 1 |
1848564d MT |
278 | } |
279 | ||
0842edcc MT |
280 | disabled() { |
281 | local param="${1}" | |
282 | ||
283 | list_match "${!param}" no off false 0 | |
284 | } | |
285 | ||
1c6a4e30 | 286 | mac_generate() { |
fb1416c6 MT |
287 | local b="$(random 12)" |
288 | ||
289 | # Remove multicast bit | |
290 | # and set address is software assigned | |
291 | local first_byte=$(( 0x${b:0:2} & 0xfe )) | |
292 | first_byte=$(( ${first_byte} | 0x02 )) | |
1848564d MT |
293 | |
294 | local output | |
fb1416c6 | 295 | printf -v output "%02x" "${first_byte}" |
790b7ec9 | 296 | |
fb1416c6 | 297 | output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}" |
1848564d MT |
298 | |
299 | # Check if output is valid | |
fb1416c6 | 300 | assert mac_is_valid "${output}" |
1848564d | 301 | |
790b7ec9 | 302 | echo "${output}" |
1848564d MT |
303 | } |
304 | ||
1c6a4e30 | 305 | mac_format() { |
18b43372 | 306 | local mac=${1} |
48bc31eb | 307 | assert isset mac |
18b43372 | 308 | |
48bc31eb MT |
309 | # Remove all colons and make the rest lowercase. |
310 | mac=${mac//:/} | |
311 | mac=${mac,,} | |
18b43372 | 312 | |
48bc31eb | 313 | local output |
18b43372 MT |
314 | if [ "${#mac}" = "12" ]; then |
315 | # Add colons (:) to mac address | |
316 | output=${mac:0:2} | |
317 | local i | |
318 | for i in 2 4 6 8 10; do | |
319 | output="${output}:${mac:${i}:2}" | |
320 | done | |
48bc31eb MT |
321 | else |
322 | output=${mac} | |
18b43372 MT |
323 | fi |
324 | ||
325 | assert mac_is_valid ${output} | |
326 | ||
48bc31eb | 327 | print "${output}" |
18b43372 MT |
328 | } |
329 | ||
1c6a4e30 | 330 | mac_is_valid() { |
1848564d MT |
331 | local mac=${1} |
332 | ||
333 | [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] | |
334 | } | |
335 | ||
01ff07e9 MT |
336 | # Converts the given string to lowercase and returns true if it is a valid FQDN |
337 | fqdn_is_valid() { | |
338 | local fqdn="${1}" | |
339 | ||
340 | if grep -qP "^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$" <<< "${fqdn,,}"; then | |
341 | return ${EXIT_TRUE} | |
342 | fi | |
343 | ||
344 | return ${EXIT_FALSE} | |
345 | } | |
346 | ||
1c6a4e30 | 347 | uuid() { |
de543653 | 348 | echo $(</proc/sys/kernel/random/uuid) |
1848564d MT |
349 | } |
350 | ||
a24cff8f JS |
351 | abs() { |
352 | local val=${1} | |
353 | ||
354 | if [ ${val} -lt 0 ]; then | |
355 | (( val *= -1 )) | |
356 | fi | |
357 | ||
358 | echo ${val} | |
359 | } | |
360 | ||
fb1416c6 MT |
361 | rand() { |
362 | local uuid="$(uuid)" | |
363 | echo "${uuid//-/}" | |
364 | } | |
365 | ||
366 | random() { | |
367 | local length="${1:-8}" | |
368 | ||
369 | local random | |
370 | while [ ${#random} -lt ${length} ]; do | |
371 | random="${random}$(rand)" | |
372 | done | |
373 | ||
374 | echo "${random:0:${length}}" | |
375 | } | |
376 | ||
1c6a4e30 | 377 | isset() { |
1848564d MT |
378 | local var=${1} |
379 | ||
380 | [ -n "${!var}" ] | |
381 | } | |
382 | ||
1c6a4e30 | 383 | isoneof() { |
1848564d MT |
384 | local var=${!1} |
385 | shift | |
386 | ||
e726ef8d | 387 | list_match "${var}" "$@" |
1848564d MT |
388 | } |
389 | ||
1c6a4e30 | 390 | isbool() { |
1848564d MT |
391 | local var=${1} |
392 | ||
ec6afbdd | 393 | isoneof ${var} 0 1 no yes on off true false |
1848564d MT |
394 | } |
395 | ||
1c6a4e30 | 396 | isinteger() { |
1848564d MT |
397 | local var=${!1} |
398 | ||
399 | [[ ${var} =~ ^[0-9]+$ ]] | |
400 | } | |
401 | ||
1c6a4e30 | 402 | ismac() { |
1848564d MT |
403 | local mac=${!1} |
404 | ||
405 | mac_is_valid ${mac} | |
406 | } | |
407 | ||
1c6a4e30 | 408 | isipaddress() { |
fef4edaf MT |
409 | local addr=${!1} |
410 | ||
411 | ip_is_valid ${addr} | |
412 | } | |
413 | ||
48a64768 JS |
414 | mtu_is_valid() { |
415 | local proto=${1} | |
416 | local mtu=${2} | |
417 | ||
418 | case ${proto} in | |
ef53293c | 419 | ethernet|ipv4) |
48a64768 JS |
420 | [ ${mtu} -ge 576 ] && [ ${mtu} -le 9000 ] |
421 | ;; | |
422 | ipv6) | |
423 | [ ${mtu} -ge 1280 ] && [ ${mtu} -le 9000 ] | |
424 | ;; | |
425 | *) | |
426 | error "${proto} is not a valid proto" | |
427 | return ${EXIT_ERROR} | |
428 | ;; | |
429 | esac | |
430 | } | |
431 | ||
1c6a4e30 | 432 | backtrace() { |
711ffac1 MT |
433 | local start=1 |
434 | ||
435 | echo # Empty line | |
436 | error_log "Backtrace (most recent call in first line):" | |
437 | ||
04608623 | 438 | local i source |
711ffac1 MT |
439 | for i in $(seq ${start} ${#BASH_SOURCE[*]}); do |
440 | [ -z "${FUNCNAME[${i}]}" ] && continue | |
6396ccab MT |
441 | |
442 | # Print called binary with arguments. | |
443 | if [ "${FUNCNAME[${i}]}" == "main" ]; then | |
444 | local args="$(list_reverse ${BASH_ARGV[*]})" | |
445 | printf -v source "%20s" "$0" | |
446 | error_log " ${source} ${args}" | |
447 | continue | |
448 | fi | |
711ffac1 | 449 | |
04608623 MT |
450 | source=${BASH_SOURCE[$(( ${i} + 1 ))]} |
451 | error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}" | |
711ffac1 MT |
452 | done |
453 | } | |
454 | ||
1c6a4e30 | 455 | assert() { |
1848564d MT |
456 | local assertion="$@" |
457 | ||
458 | if ! ${assertion}; then | |
4c670d7c | 459 | error_log "Assertion '${assertion}' failed." |
711ffac1 | 460 | backtrace |
cfbe0802 | 461 | exit ${EXIT_ERROR_ASSERT} |
1848564d MT |
462 | fi |
463 | ||
464 | return ${EXIT_OK} | |
465 | } | |
cad8bd85 | 466 | |
b0b2f995 MT |
467 | # This function checks, if the given argument is an assert error |
468 | # exit code. If this is the case, the script will halt immediately. | |
1c6a4e30 | 469 | assert_check_retval() { |
b0b2f995 MT |
470 | local ret=${1} |
471 | ||
472 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
473 | exit ${EXIT_ERROR_ASSERT} | |
474 | fi | |
475 | ||
476 | return ${ret} | |
477 | } | |
478 | ||
2bb20bbd SS |
479 | # This function executes the given command and inverses the return code |
480 | not() { | |
481 | local command="$@" | |
482 | ||
483 | ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE} | |
484 | } | |
485 | ||
1c6a4e30 | 486 | exec_cmd() { |
711ffac1 MT |
487 | local cmd=$@ |
488 | ||
489 | log DEBUG "Running command: ${cmd}" | |
490 | ||
b816e04b | 491 | DEBUG=${DEBUG} \ |
8c63fa13 MT |
492 | LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \ |
493 | LOG_FACILITY="${LOG_FACILITY}" \ | |
b816e04b | 494 | ${SHELL} ${cmd} |
711ffac1 MT |
495 | local ret=$? |
496 | ||
497 | #log DEBUG "Returned with code '${ret}'" | |
498 | ||
499 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
500 | error_log "Stopping parent process due to assertion error in child process: ${cmd}" | |
501 | exit ${EXIT_ERROR_ASSERT} | |
502 | fi | |
503 | ||
504 | return ${ret} | |
505 | } | |
506 | ||
1c6a4e30 | 507 | cmd() { |
b816e04b MT |
508 | local cmd=$@ |
509 | ||
510 | log DEBUG "Running command: ${cmd}" | |
511 | ||
4b75fc83 MT |
512 | env -i -- \ |
513 | HOME="${HOME}" \ | |
514 | PATH="${PATH}" \ | |
515 | TERM="${TERM}" \ | |
516 | ${cmd} | |
517 | local ret=$? | |
b816e04b | 518 | |
4b75fc83 MT |
519 | case "${ret}" in |
520 | ${EXIT_OK}) | |
521 | return ${EXIT_OK} | |
522 | ;; | |
523 | *) | |
524 | log DEBUG "Returned with code '${ret}'" | |
525 | return ${ret} | |
526 | ;; | |
527 | esac | |
b816e04b MT |
528 | } |
529 | ||
1c6a4e30 | 530 | cmd_quiet() { |
2212045f | 531 | cmd "$@" &>/dev/null |
3efecbb3 MT |
532 | } |
533 | ||
1c6a4e30 | 534 | cmd_exec() { |
85f35e51 | 535 | local cmd=( "$@" ) |
f80ce052 MT |
536 | |
537 | log DEBUG "Exec'ing command: ${cmd}" | |
538 | ||
85f35e51 | 539 | exec "${cmd[@]}" |
f80ce052 MT |
540 | |
541 | log ERROR "Could not exec-ute: ${cmd}" | |
542 | exit ${EXIT_ERROR} | |
543 | } | |
544 | ||
1c6a4e30 | 545 | cmd_not_implemented() { |
2181765d MT |
546 | assert false "not implemented" |
547 | } | |
548 | ||
f5ee091e MT |
549 | # Executes the given command in background |
550 | cmd_background() { | |
2212045f | 551 | cmd_quiet "$@" & |
f5ee091e MT |
552 | } |
553 | ||
554 | # Prints the PID of the process that was started last | |
555 | cmd_background_get_pid() { | |
556 | print "${!}" | |
557 | } | |
558 | ||
559 | cmd_background_result() { | |
560 | local pids=$@ | |
561 | ||
562 | wait ${pids} | |
563 | } | |
564 | ||
b8026986 | 565 | # Increase security of the read command |
1c6a4e30 | 566 | read() { |
2212045f | 567 | builtin read -r "$@" |
b8026986 MT |
568 | } |
569 | ||
1c6a4e30 | 570 | seq() { |
3efecbb3 MT |
571 | if [ $# -eq 2 ]; then |
572 | eval echo {${1}..${2}} | |
573 | elif [ $# -eq 3 ]; then | |
574 | eval echo {${1}..${3}..${2}} | |
575 | fi | |
576 | } | |
577 | ||
de72bd91 MT |
578 | range() { |
579 | eval echo {0..$(( ${1} - 1 ))} | |
580 | } | |
581 | ||
582 | count() { | |
583 | local i=0 | |
584 | ||
585 | while read; do | |
586 | ((i++)) | |
587 | done | |
588 | ||
589 | echo ${i} | |
590 | } | |
591 | ||
1c6a4e30 | 592 | which() { |
2212045f | 593 | type -P "$@" |
76e6cd51 MT |
594 | } |
595 | ||
fe52c5e0 | 596 | # Prints the number of seconds since epoch. |
1c6a4e30 | 597 | timestamp() { |
fe52c5e0 MT |
598 | date -u "+%s" |
599 | } | |
600 | ||
1c6a4e30 | 601 | beautify_time() { |
d82cf370 MT |
602 | local value=${1} |
603 | ||
604 | local unit | |
605 | local limit | |
606 | for unit in s m h d w; do | |
607 | case "${unit}" in | |
608 | s|m|h) | |
609 | limit=60 | |
610 | ;; | |
611 | d) | |
612 | limit=24 | |
613 | ;; | |
614 | w) | |
615 | limit=7 | |
616 | ;; | |
617 | esac | |
618 | ||
619 | [ ${value} -lt ${limit} ] && break | |
620 | ||
621 | value=$(( ${value} / ${limit} )) | |
622 | done | |
623 | ||
624 | echo "${value}${unit}" | |
625 | } | |
711ffac1 | 626 | |
1c6a4e30 | 627 | beautify_bytes() { |
711ffac1 MT |
628 | local value=${1} |
629 | ||
630 | local unit | |
631 | local limit=1024 | |
632 | for unit in B k M G T; do | |
633 | [ ${value} -lt ${limit} ] && break | |
634 | value=$(( ${value} / ${limit} )) | |
635 | done | |
636 | ||
637 | echo "${value}${unit}" | |
638 | } | |
943e3f7e | 639 | |
1c6a4e30 | 640 | module_load() { |
943e3f7e MT |
641 | local module=${1} |
642 | ||
dd891ec4 MT |
643 | # Do nothing if the module is already loaded |
644 | if [ -d "/sys/module/${module//-/_}" ]; then | |
645 | return ${EXIT_OK} | |
943e3f7e | 646 | fi |
dd891ec4 MT |
647 | |
648 | log DEBUG "Loading kernel module ${module}" | |
649 | modprobe "${module}" | |
943e3f7e | 650 | } |
6b3f9c85 | 651 | |
1c6a4e30 | 652 | binary_exists() { |
6b3f9c85 MT |
653 | local binary=${1} |
654 | ||
655 | if [ -n "$(type -p ${binary})" ]; then | |
656 | return ${EXIT_OK} | |
657 | fi | |
658 | ||
659 | return ${EXIT_ERROR} | |
660 | } | |
d76f5107 | 661 | |
1c6a4e30 | 662 | function_exists() { |
1e6f187e MT |
663 | local function="${1}" |
664 | ||
665 | if [ "$(type -t "${function}")" = "function" ]; then | |
666 | return ${EXIT_TRUE} | |
667 | fi | |
668 | ||
669 | return ${EXIT_FALSE} | |
670 | } | |
671 | ||
1c6a4e30 | 672 | process_kill() { |
d76f5107 MT |
673 | local process=${1} |
674 | ||
675 | if ! isinteger process; then | |
676 | process=$(pidof ${process}) | |
677 | fi | |
678 | ||
679 | local pid | |
680 | local sig | |
681 | for pid in ${process}; do | |
682 | for sig in 15 9; do | |
683 | [ -d "/proc/${pid}" ] || break | |
684 | ||
685 | kill -${sig} ${pid} | |
686 | sleep 1 | |
687 | done | |
688 | done | |
689 | } | |
feb76eaf | 690 | |
1c6a4e30 | 691 | dec() { |
feb76eaf MT |
692 | local hex=${1} |
693 | ||
694 | if [ "${hex:0:2}" != "0x" ]; then | |
695 | hex="0x${hex}" | |
696 | fi | |
697 | ||
698 | printf "%d\n" "${hex}" | |
699 | } | |
3a7fef62 | 700 | |
1c6a4e30 | 701 | chr() { |
5cf0edf9 MT |
702 | local char="${1}" |
703 | ||
704 | [ ${char} -lt 256 ] || return ${EXIT_ERROR} | |
705 | ||
706 | printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n" | |
707 | } | |
708 | ||
1c6a4e30 | 709 | ord() { |
5cf0edf9 MT |
710 | LC_CTYPE="C" printf "%d\n" "'${1}" |
711 | } | |
712 | ||
1c6a4e30 | 713 | hex() { |
5cf0edf9 MT |
714 | printf "%X\n" "${1}" |
715 | } | |
716 | ||
1c6a4e30 | 717 | network_is_running() { |
3a7fef62 MT |
718 | # Check, if the network service is running. |
719 | service_is_active network | |
720 | } | |
f80ce052 | 721 | |
1c6a4e30 | 722 | contains_spaces() { |
f80ce052 MT |
723 | local var="$@" |
724 | ||
725 | # Eliminate spaces. | |
726 | local var2=${var// /} | |
727 | ||
728 | if [ ${#var} -ne ${#var2} ]; then | |
729 | return ${EXIT_TRUE} | |
730 | fi | |
731 | ||
732 | return ${EXIT_FALSE} | |
0d645497 MT |
733 | } |
734 | ||
735 | string_match() { | |
736 | local match=${1} | |
737 | local string=${2} | |
738 | ||
739 | [[ ${string} =~ ${match} ]] && return ${EXIT_TRUE} || return ${EXIT_FALSE} | |
f80ce052 | 740 | } |
5cf0edf9 | 741 | |
1c6a4e30 | 742 | string_split() { |
5cf0edf9 MT |
743 | local string="$@" |
744 | ||
745 | local pos=0 | |
746 | while [ ${pos} -lt ${#string} ]; do | |
747 | print "${string:${pos}:1}" | |
748 | pos=$(( ${pos} + 1 )) | |
749 | done | |
750 | ||
751 | return ${EXIT_OK} | |
752 | } | |
753 | ||
1c6a4e30 | 754 | string_reverse() { |
5cf0edf9 MT |
755 | local string="$@" |
756 | ||
757 | local output | |
758 | local pos=0 | |
759 | while [ ${pos} -lt ${#string} ]; do | |
760 | output="${string:${pos}:1}${output}" | |
761 | pos=$(( ${pos} + 1 )) | |
762 | done | |
763 | ||
764 | print "${output}" | |
765 | return ${EXIT_OK} | |
766 | } | |
767 | ||
1c6a4e30 | 768 | dec2bin() { |
5cf0edf9 MT |
769 | local number="${1}" |
770 | ||
771 | local output | |
772 | ||
773 | local i div | |
774 | for i in 7 6 5 4 3 2 1; do | |
775 | div=$(( 2 ** ${i} )) | |
776 | ||
777 | if [ $(( ${number} / ${div} )) -eq 1 ]; then | |
778 | output="${output}1" | |
779 | else | |
780 | output="${output}0" | |
781 | fi | |
782 | number="$(( ${number} % ${div} ))" | |
783 | done | |
784 | ||
785 | if [ $(( ${number} % 2 )) -eq 1 ]; then | |
786 | output="${output}1" | |
787 | else | |
788 | output="${output}0" | |
789 | fi | |
790 | ||
791 | print "${output}" | |
792 | } | |
793 | ||
1c6a4e30 | 794 | bin2dec() { |
5cf0edf9 MT |
795 | local string="${1}" |
796 | local number=0 | |
797 | ||
798 | local pos=0 char | |
799 | while [ ${pos} -lt ${#string} ]; do | |
800 | char="${string:${pos}:1}" | |
801 | pos=$(( ${pos} + 1 )) | |
802 | ||
803 | number=$(( ${number} << 1 )) | |
804 | ||
805 | case "${char}" in | |
806 | 0) ;; | |
807 | 1) | |
808 | number=$(( ${number} + 1 )) | |
809 | ;; | |
810 | *) | |
811 | assert false "Invalid character: ${char}" | |
812 | ;; | |
813 | esac | |
814 | done | |
815 | ||
816 | print "${number}" | |
817 | return ${EXIT_OK} | |
818 | } | |
819 | ||
1c6a4e30 | 820 | char2bin() { |
5cf0edf9 MT |
821 | local dec="$(ord "${1}")" |
822 | ||
823 | dec2bin "${dec}" | |
824 | } | |
825 | ||
1c6a4e30 | 826 | bin2char() { |
5cf0edf9 MT |
827 | local dec="$(bin2dec "$@")" |
828 | ||
829 | chr "${dec}" | |
830 | } | |
831 | ||
1c6a4e30 | 832 | bin2hex() { |
5cf0edf9 MT |
833 | local dec="$(bin2dec "$@")" |
834 | ||
835 | dec2hex "${dec}" | |
836 | } | |
837 | ||
1c6a4e30 | 838 | hex2bin() { |
5cf0edf9 MT |
839 | local dec="$(hex2dec "$@")" |
840 | ||
841 | dec2bin "${dec}" | |
842 | } | |
843 | ||
1c6a4e30 | 844 | hex2dec() { |
5cf0edf9 MT |
845 | local hex="${1}" |
846 | ||
847 | # Prepend 0x if necessary. | |
848 | [ "${hex:0:2}" = "0x" ] || hex="0x${hex}" | |
849 | ||
850 | printf "%d\n" "${hex}" | |
851 | } | |
852 | ||
1c6a4e30 | 853 | dec2hex() { |
5cf0edf9 MT |
854 | printf "%02x\n" "${1}" |
855 | } | |
a95d16fc | 856 | |
10b53c87 | 857 | # This function just copy config files |
a95d16fc | 858 | copy() { |
a95d16fc JS |
859 | assert [ $# -eq 2 ] |
860 | ||
861 | local src=${1} | |
862 | local dst=${2} | |
863 | ||
287c2e60 MT |
864 | # Check if we can read from the source |
865 | if [ ! -r "${src}" ]; then | |
866 | log ERROR "Cannot read ${src}" | |
867 | return ${EXIT_ERROR} | |
868 | fi | |
869 | ||
a95d16fc | 870 | # Check if ${dst} is a directory |
1fade616 | 871 | if [ -d "${dst}" ]; then |
9c802e1d | 872 | log ERROR "${dst} is a directory" |
a95d16fc JS |
873 | return ${EXIT_ERROR} |
874 | fi | |
875 | ||
2012c1b6 MT |
876 | # Create destination directory if it doesn't exist, yet |
877 | if ! make_parent_directory "${dst}"; then | |
878 | return ${EXIT_ERROR} | |
879 | fi | |
880 | ||
a95d16fc JS |
881 | if ! fread "${src}" > "${dst}"; then |
882 | log ERROR "Could not copy data from ${src} to ${dst}" | |
883 | return ${EXIT_ERROR} | |
884 | fi | |
885 | } | |
a8d8d2c5 JS |
886 | |
887 | normalize() { | |
888 | local string="$@" | |
889 | ||
890 | tr -sc [:alnum:] "-" < <(printf "%s" "${string,,}") | |
891 | } | |
4ad5981f MT |
892 | |
893 | get_driver_from_path() { | |
894 | local path="${1}" | |
895 | ||
896 | if file_exists "${path}"; then | |
897 | # Resolve symlink | |
898 | local driver="$(readlink "${path}")" | |
899 | ||
900 | # Print module name | |
901 | basename "${driver}" | |
902 | return ${EXIT_OK} | |
903 | fi | |
904 | ||
905 | return ${EXIT_ERROR} | |
906 | } |