-
Notifications
You must be signed in to change notification settings - Fork 4
/
ove
executable file
·16702 lines (14321 loc) · 413 KB
/
ove
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# SPDX-License-Identifier: MIT
#
# MIT License
#
# Copyright (c) 2019 Ericsson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is furnished
# to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
# OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
__i=0
case $- in
*i*) __i=1;;
esac
if [ "${__i}" -eq 1 ]; then
# shellcheck disable=SC2009
__p=$(\ps | \grep -w $$ | \grep -v grep)
if [ "${__p#*bash}" = "$__p" ]; then
echo 'error: run this in bash'
unset __i
unset __p
return 1
fi
unset __p
fi
unset __i
function ove_version_check_early {
local ret=0
if command -v dpkg > /dev/null; then
if ! \dpkg --compare-versions ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} ge 4.3; then
ret=1
fi
else
if [ ${BASH_VERSINFO[0]} -lt 4 ]; then
ret=1
elif [ ${BASH_VERSINFO[0]} -eq 4 ] && [ ${BASH_VERSINFO[1]} -lt 3 ]; then
ret=1
fi
fi
if [ ${ret} -ne 0 ]; then
echo "error: bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} is too old. I need bash>=4.3" 1>&2
fi
return ${ret}
}
if ! ove_version_check_early; then
return 1
fi
function ove_read_config_validate_name {
local re="^[a-zA-Z_]{1,}[a-zA-Z0-9_]{0,}$"
local var="$1"
if ! [[ "${var}" =~ ${re} ]]; then
return 1
fi
return 0
}
# remember and set duplicate configs
function ove_read_config_pre {
local f
local p
local p_arr
local val
local var
declare -A a_hash
if [ "${OVE_CONFIG_DUPLICATES}" != "" ]; then
unset OVE_CONFIG_DUPLICATES
fi
for f in "$@"; do
if [ ! -e "${f}" ]; then
continue
fi
while read -r p; do
# ignore comments and empty lines
if [[ ${p} == \#* ]] || [[ ${p} == '' ]]; then
continue
fi
read -a p_arr -r <<<"${p}"
var=${p_arr[0]}
if test "${a_hash["${var}"]+isset}"; then
val=${p_arr[*]:1}
# escape all "
val=${val//\"/\\\"}
if ! eval ${var}=\"${val}\" 2> /dev/null; then
echo "warning: config '${var}' could not be set:" 1>&2
${ove_cmd2pathname["grep"]:?} -w ^${var} -H -n ${1} 1>&2
continue
fi
# shellcheck disable=SC2163
export ${var}
# remember duplicates
if [ "${OVE_CONFIG_DUPLICATES}" = "" ]; then
OVE_CONFIG_DUPLICATES="$var"
else
OVE_CONFIG_DUPLICATES+=" $var"
fi
export OVE_CONFIG_DUPLICATES
# remember configs
if [ "${OVE_CONFIG_ALL}" = "" ]; then
OVE_CONFIG_ALL="$var"
else
OVE_CONFIG_ALL+=" $var"
fi
export OVE_CONFIG_ALL
fi
a_hash["${var}"]=1
done<${f}
done
}
function ove_read_config {
local p
local p_arr
local re
local restore
local val
local var
declare -A c
if [ ! -s "$1" ]; then
return 1
fi
restore=0
if [[ $- != *f* ]]; then
restore=1
fi
# disable pathname expansion
set -o noglob
while read -r p; do
# ignore comments and empty lines
if [[ ${p} == \#* ]] || [[ ${p} == '' ]]; then
continue
fi
read -a p_arr -r <<<"${p}"
var=${p_arr[0]}
if [ $# -eq 1 ] && ! ove_read_config_validate_name ${var}; then
echo "error: config name '${var}' is not allowed:" 1>&2
${ove_cmd2pathname["grep"]:?} -w ^${var} -H -n ${1} 1>&2
continue
fi
if [ "$2" = "clean" ]; then
unset ${var} 2> /dev/null
continue
fi
# duplicates within one config file
if [[ -v c[${var}] ]]; then
ove_echo_warning_noprefix "found config '$var' duplicate in '$1'"
echo "$var=${p_arr[*]:1} (ignored)" 1>&2
continue
fi
c[$var]=1
# duplicates across config files
re=\\b${var}\\b
if [[ "${OVE_CONFIG_DUPLICATES}" =~ ${re} ]]; then
continue
fi
val=${p_arr[*]:1}
# escape all "
val=${val//\"/\\\"}
if ! eval ${var}=\"${val}\" 2> /dev/null; then
ove_echo_warning_noprefix "config '${var}' could not be set:" 1>&2
${ove_cmd2pathname["grep"]:?} -w ^${var} -H -n ${1} 1>&2
continue
fi
# shellcheck disable=SC2163
export ${var}
# remember configs
if [ "${OVE_CONFIG_ALL}" = "" ]; then
OVE_CONFIG_ALL="$var"
else
OVE_CONFIG_ALL+=" $var"
fi
export OVE_CONFIG_ALL
done <$1
if [ $restore -eq 1 ]; then
# restore pathname expansion
set +o noglob
fi
}
function ove_unsource_user_env {
# cleanup PATH
if [ "${OVE_STAGE_DIR}" != "" ] && \
[[ ${PATH} == *"${OVE_STAGE_DIR}"* ]]; then
PATH=${PATH/${OVE_STAGE_DIR}${OVE_PREFIX}\/bin:/}
PATH=${PATH/${OVE_STAGE_DIR}${OVE_PREFIX}\/sbin:/}
fi
# cleanup LD_LIBRARY_PATH
if [ -n "${LD_LIBRARY_PATH}" ] && [ -n "${OVE_LD_LIBRARY_PATH}" ]; then
LD_LIBRARY_PATH=${LD_LIBRARY_PATH/${OVE_LD_LIBRARY_PATH}/}
if [ "${LD_LIBRARY_PATH}" = "" ]; then
unset LD_LIBRARY_PATH
elif [ ${LD_LIBRARY_PATH:0:1} = ":" ]; then
LD_LIBRARY_PATH=${LD_LIBRARY_PATH:1}
fi
fi
# cleanup PKG_CONFIG_PATH
if [ -n "${PKG_CONFIG_PATH}" ] && [ -n "${OVE_PKG_CONFIG_PATH}" ]; then
PKG_CONFIG_PATH=${PKG_CONFIG_PATH/${OVE_PKG_CONFIG_PATH}/}
if [ "${PKG_CONFIG_PATH}" = "" ]; then
unset PKG_CONFIG_PATH
elif [ ${PKG_CONFIG_PATH:0:1} = ":" ]; then
PKG_CONFIG_PATH=${PKG_CONFIG_PATH:1}
fi
fi
# cleanup MAKEFLAGS
if [ -n "${MAKEFLAGS}" ] && \
[[ ${MAKEFLAGS} == *"${OVE_MAKEFLAGS}"* ]]; then
MAKEFLAGS=${MAKEFLAGS/${OVE_MAKEFLAGS}/}
if [ "${MAKEFLAGS}" = "" ]; then
unset MAKEFLAGS
elif [ "${MAKEFLAGS: -1}" = " " ]; then
MAKEFLAGS=${MAKEFLAGS::-1}
fi
fi
}
function ove_unsource {
local f
ove_unsource_user_env
# clean config variables
for f in ${OVE_CONFIG_FILES_ALL}; do
if [ -e "${f}" ]; then
ove_read_config ${f} clean
fi
done
# unset any ove[-_] functions
unset -f $(compgen -A function | \
\grep -E '^ove($|[-_])' | \
\grep -E -v "ove_read_config|ove-unsource|${FUNCNAME[0]}|ove_version_check_early" | \
\awk '{print $1}')
# unset any ove_/OVE_ variables
unset $(set -o posix ; set | \
\grep -i ^ove_ | \
\cut -d= -f1)
# unalias any ove[-_] alias
f=$(compgen -A alias | \
\grep -E '^ove($|[-_])')
if [ "$f" != "" ]; then
unalias $f
fi
# unset completion for ove
if complete -p | \grep -q ' ove$'; then
complete -r ove
fi
}
# unsource: :clean up all OVE vars/funcs from this shell:CORE
function ove-unsource {
ove_entry || return
ove_unsource
# unset leftovers and myself
unset -f $(compgen -A function | \
\grep -E '^ove($|[-_])' | \
\awk '{print $1}') \
${FUNCNAME[0]}
}
# interactive shell? clean up
if [[ $- == *i* ]]; then
ove_unsource
fi
function ove_welcome_msg {
local script_ver
ove-version
if ! [ -e "${HOME}/.ove.bash" ]; then
echo
echo "Do you want to skip the initial 'source ove' step? Run this:"
echo " cp -a ${OVE_DIR}/ove.bash ${HOME}/.ove.bash && echo '[ -f ~/.ove.bash ] && source ~/.ove.bash' >> ${HOME}/.bashrc"
fi
# shellcheck disable=SC2016
script_ver=$(${ove_cmd2pathname["script"]:?} --version 2> /dev/null | \
${ove_cmd2pathname["awk"]:?} '{print $NF}')
if [[ ${script_ver} =~ ^2.3[12] ]]; then
echo
ove_echo_note_noprefix "due to a bug in ${ove_cmd2pathname["script"]:?} 2.31/2.32 (you have ${script_ver}), it's not possible to Ctrl-C any OVE command. Upgrade package: '$(ove_command_to_package script)'."
fi
}
function ove_version_check {
local ret=0
local git_arr
local git_ver
local git_opts
local major
local minor
local patch
git_ver=$(${ove_cmd2pathname["git"]:?} --version)
git_ver=${git_ver/git version /}
IFS='.' read -r -a git_arr <<< "${git_ver}"
major=${git_arr[0]}
minor=${git_arr[1]}
patch=${git_arr[2]}
if command -v dpkg > /dev/null; then
if ! \dpkg --compare-versions ${major}.${minor}.${patch} ge 1.8.5; then
ret=1
fi
elif [ ${major} -lt 2 ]; then
if [ ${major} -eq 1 ] && [ ${minor} -lt 8 ]; then
ret=1
elif [ ${major} -eq 1 ] && [ ${minor} -eq 8 ] && [ ${patch} -lt 5 ]; then
ret=1
elif [ ${major} -eq 0 ]; then
ret=1
fi
fi
if [ ${ret} -ne 0 ]; then
ove_echo_error_noprefix "git ${git_ver} is too old. I need >=1.8.5"
return ${ret}
fi
# --no-optional-locks was introduced in 2.15.0
if command -v dpkg > /dev/null; then
if \dpkg --compare-versions ${major}.${minor}.${patch} ge 2.15.0; then
git_opts="--no-optional-locks"
fi
elif [ ${major} -ge 3 ]; then
git_opts="--no-optional-locks"
elif [ ${major} -eq 2 ] && [ ${minor} -lt 15 ]; then
:
elif [ ${major} -lt 2 ]; then
:
else
git_opts="--no-optional-locks"
fi
OVE_GIT_OPTIONS="${git_opts}"
export OVE_GIT_OPTIONS
return ${ret}
}
function ove_main {
ove_externals_init
# some error messages use colors. I.e. keep this early
export OVE_COLOR_GREEN="\033[0;32;7m"
export OVE_COLOR_CYAN="\033[0;36;7m"
export OVE_COLOR_BLUE="\033[0;34;7m"
export OVE_COLOR_YELLOW_X="0;33;7m"
export OVE_COLOR_YELLOW="\033[${OVE_COLOR_YELLOW_X}"
export OVE_COLOR_RED="\033[0;31;7m"
export OVE_COLOR_BLANK="\033[0m"
# set OVE_OS_* variables ('ove_deps' need them)
ove_determine_dist_version_and_pack_manager
# dependency checks
if ! ove_deps; then
ove-unsource
return 1
fi
# OVE_USER:the user that runs OVE commands
OVE_USER=
if [ "${USER}" != "" ]; then
OVE_USER="${USER}"
elif [ "${LOGNAME}" != "" ]; then
OVE_USER="${LOGNAME}"
elif command -v whoami > /dev/null; then
OVE_USER="$(${ove_cmd2pathname["whoami"]:?})"
else
OVE_USER="ove"
fi
export OVE_USER
# OVE_TMP:temporary directory for all OVE workspaces (per user)
OVE_TMP=/tmp/${OVE_USER}/ove
export OVE_TMP
if ! [ -d "${OVE_TMP}" ]; then
if ! ${ove_cmd2pathname["mkdir"]:?} -p "${OVE_TMP}" 2> /dev/null; then
if ! OVE_TMP="$(${ove_cmd2pathname["mktemp"]:?} -d)"; then
ove_echo_error_noprefix "weird! '/tmp' is missing/none-writable and 'mktemp -d' fails"
ove-unsource
return 1
fi
fi
fi
# run 'ove init' if this is not a workspace (interactive shells)
if [[ $- == *i* ]]; then
# turn off monitor mode to get rid of "Done" messages
set +m
if [ -e ".owel" ] && [ -e ove ]; then
:
else
ove-init
fi
fi
# init workspace
if ! ove_init; then
ove-unsource
return 1
fi
# welcome and post checks (interactive shells)
if [[ $- == *i* ]]; then
if [ ${OVE_LOGLEVEL} -eq 0 ] || [[ "$*" == *"hush"* ]]; then
:
else
ove_welcome_msg
fi
ove_post_checks
# turn on monitor mode
set -m
fi
}
# $1: pid
function ove_strace_execve_one_pid {
local cmd
local exit_code
local line
local syscall
local t0
cmd=
pid=$1
while read -r line; do
if [ "${line}" = "" ]; then
continue
elif [[ "${line}" == *"ENOENT"* ]]; then
continue
fi
syscall=${line#* }
syscall=${syscall%%(*}
if [ "${syscall}" != "execve" ]; then
continue
fi
# for slow string substitution in bash
if [ ${#line} -gt 1000 ]; then
cmd=$(echo $line | ${ove_cmd2pathname["cut"]:?} -d'[' -f2 | \
${ove_cmd2pathname["cut"]:?} -d']' -f1 | \
${ove_cmd2pathname["sed"]:?} -e 's,"\, ", ,g' )
cmd=${cmd:1:-1}
else
cmd=${line##* [\"}
cmd=${cmd%%\"]*}
cmd=${cmd//\", \"/ }
fi
if [ "${cmd}" = "" ]; then
continue
fi
exit_code=$(ove_strace_get_exit_code ${pid})
if [ ${OVE_LOGLEVEL} -eq 3 ]; then
t0=$(ove_strace_get_t0 ${pid})
echo "${t0}:${pid}:${exit_code}:${cmd}"
elif [ ${OVE_LOGLEVEL} -eq 4 ]; then
echo "$(ove_strace_get_duration_s ${pid}):${pid}:${exit_code}:${cmd}"
fi
done <<< "${all_files[$1]}"
}
# $1: directory with strace pid files
function ove_analyze_strace_execve_timing {
local all_files
local f
local pid
if [ ! -d "$1" ]; then
return 1
fi
declare -A all_files
{
for f in $(${ove_cmd2pathname["find"]:?} $1 -type f); do
pid=${f##*.}
all_files[${pid}]="$(<${f})"
ove_strace_execve_one_pid ${pid} &
done
wait
} | LC_ALL="C" ${ove_cmd2pathname["sort"]:?} -n$([ ${OVE_LOGLEVEL} -eq 4 ] && echo r)
}
function ove_line2ipv4 {
${ove_cmd2pathname["grep"]:?} -E -o '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' <<<"$@"
}
# $1: IPv4 address
function ove_ip2host {
local ip
local ip_lookup
if ! ove_check_command "dig"; then
return 1
fi
ip=${1}
if [[ ${ip} == 127* ]]; then
ip_lookup="localhost"
else
ip_lookup=$(${ove_cmd2pathname["dig"]:?} +short -x ${ip} | \
${ove_cmd2pathname["tr"]:?} '\n' ' ' | \
${ove_cmd2pathname["xargs"]:?})
if [ "${ip_lookup}" = "" ]; then
ip_lookup="NA"
else
ip_lookup=${ip_lookup::-1}
fi
fi
echo ${ip_lookup}
}
# $1: IPv4 address
function ove_ip2org {
local ip
local org
if ! ove_check_command "whois"; then
return 1
fi
ip=${1}
if [[ ${ip} == 127* ]]; then
org="NA"
else
org=$(${ove_cmd2pathname["timeout"]:?} 3 \
${ove_cmd2pathname["whois"]:?} ${ip} 2> /dev/null | \
${ove_cmd2pathname["grep"]:?} -E -i 'netrange:|netname|parent' | \
${ove_cmd2pathname["tr"]:?} -s ' ')
if [ "${org}" = "" ]; then
return
fi
fi
printf "${org}"
}
# $1: port
function ove_port2service {
local port
local port_service
port=${1}
if [ "${port}" -eq 0 ]; then
return 0
fi
if [ -e /etc/services ]; then
# shellcheck disable=SC2016
port_service=$(${ove_cmd2pathname["grep"]:?} -E -v '^#|^$' /etc/services | \
${ove_cmd2pathname["awk"]:?} '{print $1, $2}' | \
${ove_cmd2pathname["grep"]:?} -m1 -w ${port} | \
${ove_cmd2pathname["awk"]:?} '{print $1}')
fi
if [ "${port_service}" = "" ]; then
port_service="NA"
fi
echo "${port_service}"
}
# $1: pid
function ove_strace_connect_one_pid {
local cmd
local ip
local ip_lookup
local line
local port
local port_service
local ret
local sa_family
local str
local sun_path
local syscall
cmd=
pid=$1
while read -r line; do
if [ "${line}" = "" ]; then
continue
fi
syscall=${line#* }
syscall=${syscall%%(*}
if [ "${syscall}" = "bind" ] ||
[ "${syscall}" = "connect" ]; then
sa_family=${line##*sa_family=}
sa_family=${sa_family%%,*}
t0=${line%% *}
ret=${line##* =}
ret=${ret% <*}
if [[ "${ret}" == " -1"* ]] && ! [[ "${ret}" != "EINPROGRESS"* ]]; then
continue
fi
if [ "${sa_family}" = "AF_LOCAL" ] || \
[ "${sa_family}" = "AF_UNIX" ]; then
sun_path=${line#*\"}
sun_path=${sun_path%\"*}
# abstract sockets
if [[ "${line}" == *"=@"* ]]; then
sun_path="@${sun_path}"
fi
cmd=$(ove_strace_get_execve ${pid})
str+="${t0}:${syscall}:${sa_family}:${sun_path}:${cmd}\n"
elif [ "${sa_family}" = "AF_INET" ]; then
ip=$(ove_line2ipv4 ${line})
port=$(${ove_cmd2pathname["cut"]:?} -d'(' -f3- <<<"${line}" | \
${ove_cmd2pathname["cut"]:?} -d')' -f1)
ip_lookup=${ove_ip2host_db["${ip}"]}
port_service=${ove_port2service_db["${port}"]}
cmd=$(ove_strace_get_execve ${pid})
str+="${t0}:${syscall}:${sa_family}:${ip}:${ip_lookup}:${port}:${port_service}:${cmd}\n"
fi
fi
done <<< "${all_files[${pid}]}"
if [ "${str}" != "" ]; then
printf "${str}"
fi
}
function ove_read_connect_cache {
local line
unset ove_ip2host_db
unset ove_ip2org_db
unset ove_port2service_db
declare -A -g ove_ip2host_db
declare -A -g ove_ip2org_db
declare -A -g ove_port2service_db
if [ -e ${OVE_GLOBAL_STATE_DIR}/ip2host.cache ]; then
for line in $(${ove_cmd2pathname["cat"]:?} ${OVE_GLOBAL_STATE_DIR}/ip2host.cache); do
ove_ip2host_db["${line%%:*}"]="${line##*:}"
done
fi
if [ -e ${OVE_GLOBAL_STATE_DIR}/port2service.cache ]; then
for line in $(${ove_cmd2pathname["cat"]:?} ${OVE_GLOBAL_STATE_DIR}/port2service.cache); do
ove_port2service_db["${line%%:*}"]="${line##*:}"
done
fi
if [ -e ${OVE_GLOBAL_STATE_DIR}/ip2org.cache ]; then
while read -r line; do
ove_ip2org_db["${line%%:*}"]="$(${ove_cmd2pathname["tr"]:?} '@' '\n' <<<"${line#*:}")"
done < <(${ove_cmd2pathname["cat"]:?} ${OVE_GLOBAL_STATE_DIR}/ip2org.cache)
fi
}
# $1: directory with strace pid files
function ove_update_connect_db {
local host
local f
local ip
local line
local org
local port
local service
ove_read_connect_cache
if command -v ag > /dev/null; then
${ove_cmd2pathname["ag"]:?} --nobreak --nofilename -o --nonumbers 'AF_INET,.* =' $1 | \
LC_ALL="C" ${ove_cmd2pathname["sort"]:?} -u > ${OVE_OWEL_TMP_DIR}/af_inet
else
${ove_cmd2pathname["grep"]:?} -E -R -o -h 'AF_INET,.* =' $1 | \
LC_ALL="C" ${ove_cmd2pathname["sort"]:?} -u > ${OVE_OWEL_TMP_DIR}/af_inet
fi
if ! [ -s "${OVE_OWEL_TMP_DIR}/af_inet" ]; then
${ove_cmd2pathname["rm"]:?} ${OVE_OWEL_TMP_DIR}/af_inet
return
fi
while read -r line; do
ip=$(ove_line2ipv4 ${line})
if [ "${ip}" = "" ]; then
continue
fi
port=$(${ove_cmd2pathname["cut"]:?} -d'(' -f2- <<<"${line}" | \
${ove_cmd2pathname["cut"]:?} -d')' -f1)
if [ "${port}" = "" ]; then
continue
elif [[ ! "${port}" =~ ^[0-9]+$ ]]; then
continue
fi
if ! test "${ove_ip2host_db["${ip}"]+isset}"; then
host="$(ove_ip2host ${ip})"
if [ "${host}" != "" ]; then
ove_ip2host_db["${ip}"]="${host}"
echo "${ip}:${host}" >> ${OVE_GLOBAL_STATE_DIR}/ip2host.cache
fi
fi
if ! test "${ove_ip2org_db["${ip}"]+isset}"; then
org="$(ove_ip2org ${ip} | ${ove_cmd2pathname["tr"]:?} '\n' '@')"
if [ "${org}" != "" ]; then
ove_ip2org_db["${ip}"]="$(echo ${org} | ${ove_cmd2pathname["tr"]:?} '@' '\n')"
echo "${ip}:${org}" >> ${OVE_GLOBAL_STATE_DIR}/ip2org.cache
fi
fi
if ! test "${ove_port2service_db["${port}"]+isset}"; then
service="$(ove_port2service ${port})"
if [ "${service}" != "" ]; then
ove_port2service_db["${port}"]=${service}
echo "${port}:${service}" >> ${OVE_GLOBAL_STATE_DIR}/port2service.cache
fi
fi
done <${OVE_OWEL_TMP_DIR}/af_inet
${ove_cmd2pathname["rm"]:?} ${OVE_OWEL_TMP_DIR}/af_inet
for f in ${OVE_GLOBAL_STATE_DIR}/{ip2host,ip2org,port2service}.cache; do
if [ ! -s "$f" ]; then
continue
fi
LC_ALL="C" ${ove_cmd2pathname["sort"]:?} -V -u -o ${f} ${f} &
done
wait
}
# $1: directory with strace pid files
function ove_analyze_strace_raw {
${ove_cmd2pathname["find"]:?} "$1" \
-type f \
-exec cat {} \; | \
LC_ALL=C ${ove_cmd2pathname["sort"]:?} -nr
}
# $1: directory with strace pid files
function ove_analyze_strace_file {
local f
local line
local re
local syscall
re="^[A-Za-z_0-9]+$"
${ove_cmd2pathname["find"]:?} "$1" -type f -exec cat {} \; | \
while read -r line; do
syscall=${line#* }
syscall=${syscall%%(*}
if [[ "${line}" == *trace.cont* ]]; then
continue
elif [[ "$syscall" =~ ${re} ]]; then
f=${line#*\"}
f=${f%\"*}
if [[ "$line" == *ENOENT* ]]; then
echo "${line%% *}:ENOENT:$syscall:$f"
else
echo "${line%% *}:0:$syscall:$f"
fi
fi
done
}
# $1: directory with strace pid files
function ove_analyze_strace_connect {
local all_files
local f
local pid
if [ ! -d "$1" ]; then
return 1
fi
declare -A all_files
ove_update_connect_db $1
{
for f in $(${ove_cmd2pathname["find"]:?} $1 -type f); do
pid=${f##*.}
all_files[${pid}]="$(<${f})"
ove_strace_connect_one_pid ${pid} &
done
wait
} | LC_ALL="C" ${ove_cmd2pathname["sort"]:?} -n
}
# $1: pid
function ove_strace_get_exit_code {
local exit_code
local strace_file
strace_file=${all_files[$1]}
exit_code=${strace_file##*$'\n'}
if [[ "${exit_code}" == *"killed by"* ]]; then
exit_code=${exit_code##* killed by }
exit_code=${exit_code%% +++}
elif [[ "${exit_code}" == *"exited with"* ]]; then
exit_code=${exit_code##* exited with }
exit_code=${exit_code%% +++}
else
exit_code="alive"
fi
echo ${exit_code}
}
# $1: pid
function ove_strace_get_duration_s {
local diff
local strace_file
local t0
local t1
strace_file=${all_files[$1]}
t0=${strace_file%% *}
t1=${strace_file##*$'\n'}
t1=${t1%% *}
if [ "${t0}" = "${t1}" ]; then
echo "0.000000"
return
fi
# shellcheck disable=SC2016
diff=$(${ove_cmd2pathname["awk"]:?} "BEGIN{printf \"%.6f\n\", (${t1} - ${t0})}")
echo ${diff}
}
# $1: pid
function ove_strace_get_duration_ms {
local diff
local strace_file
local t0
local t1
strace_file=${all_files[$1]}
t0=${strace_file%% *}
t1=${strace_file##*$'\n'}
t1=${t1%% *}
if [ "${t0}" = "${t1}" ]; then
echo "0"
return
fi
# shellcheck disable=SC2016
diff=$(${ove_cmd2pathname["awk"]:?} "BEGIN{printf \"%.0f\n\", (${t1} - ${t0}) * 1000}")
echo ${diff}
}
# $1: pid
function ove_strace_get_t0 {
local strace_file
local t0
strace_file=${all_files[$1]}
t0=${strace_file%% *}
echo ${t0}
}
# $1: pid
function ove_strace_get_execve {
local cmd
local line
local syscall
while read -r line; do
if [ "${line}" = "" ]; then
continue
fi
syscall=${line#* }
syscall=${syscall%%(*}
if [ "${syscall}" != "execve" ]; then
continue
elif [[ "${line}" == *"ENOENT"* ]]; then
continue
fi
# for slow string substitution in bash
if [ ${#line} -gt 1000 ]; then
cmd=$(echo $line | \
${ove_cmd2pathname["cut"]:?} -d'[' -f2 | \
${ove_cmd2pathname["cut"]:?} -d']' -f1 | \
${ove_cmd2pathname["sed"]:?} -e 's,"\, ", ,g' )
cmd=${cmd:1:-1}
else
cmd=${line##* [\"}
cmd=${cmd%%\"]*}
cmd=${cmd//\", \"/ }
fi
done <<< "${all_files[$1]}"
if [ "${cmd}" = "" ]; then
cmd="NA"
fi
echo -e "${cmd}"
}
# $1: pid
function ove_strace_one_pid {
local cmd
local duration_ms
local duration_s
local ip
local ip_lookup
local ip_org
local key
local line
local out_1
local out_2
local p
local pid
local port
local port_service
local ret
local sa_family
local suffix
local sun_path
local t
pid=$1
duration_ms=$(ove_strace_get_duration_ms ${pid})
duration_s=$(ove_strace_get_duration_s ${pid})
t=$(ove_strace_get_t0 ${pid})
# label START
out_1=" ${pid} [label=\"\lt: ${t}\lp: ${pid}\ld: ${duration_ms} ms (${duration_s} s)\l"
# exit code
exit_code=$(ove_strace_get_exit_code ${pid})
if [[ "${exit_code}" =~ ^[0-9]+$ ]]; then
if [ ${exit_code} -ne 0 ]; then
out_1+="e: ${exit_code}\l"
border_color='"red"'
else
border_color='"green"'
fi
elif [ "${exit_code}" = "alive" ]; then
border_color='"blue"'
else
out_1+="e: ${exit_code}\l"
border_color='"black"'