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