-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathexecutable_wrapper
executable file
·173 lines (141 loc) · 4.11 KB
/
executable_wrapper
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
#!/bin/bash
help() {
echo 'usage: executable_wrapper (unwrap|wrap|test) executable'
echo
echo ' This script is used to wrap executables in order to'
echo ' analyse how they are called by other scripts, daemons etc.'
echo
echo ' The original file will be renamed to `executable.orig`.'
echo
echo ' It will log all parameters and the environment of the'
echo ' originally called executable to a log file for inspection.'
echo ' The log file will be called `/tmp/executable.log.XXXXXX`,'
echo " where 'XXXXX' will be some random string."
echo
echo ' wrap - wrap the given `executable`'
echo
echo ' unwrap - unwrap the given `executable`'
echo
echo ' test - test this script'
echo
exit 1
}
# fail "text" exit_code
#
fail() {
echo "ERROR: $1" >&2
exit "$2"
}
wrap_executable_with() {
local executable_to_wrap="$1"
local wrapper="$2"
local new_executable_name="$executable_to_wrap.orig"
# -n do not overwrite existing
if ! mv -n "$executable_to_wrap" "$new_executable_name"; then
local err = $?
if [ -e "$new_executable_name" ]; then
fail "can not overwrite existing '$new_executable_name'" 2
else
fail "I wasn't able to 'mv -n '$executable_to_wrap' '$new_executable_name''" 3
fi
fi
# replace original executable path with a copy of ourselves
if cp "$wrapper" "$executable_to_wrap"; then
echo "Successfully wrapped '$executable_to_wrap'"
exit 0
else
fail "something went wrong. Aborted" 4
fi
}
unwrap_executable() {
local executable_to_unwrap="$1"
local path_of_executable_orig="$executable_to_unwrap.orig"
if ! grep -q "$MARKER" "$executable_to_unwrap"; then
fail "it does not seem like '$executable_to_unwrap' is wrapped. Aborting" 5
else
if mv "$path_of_executable_orig" "$executable_to_unwrap"; then
echo "Successfully unwrapped '$executable_to_unwrap'"
exit 0
else
fail "something went wrong. Aborted" 6
fi
fi
}
# test_fail test_number
#
test_fail() {
fail "Test $1 failed" $1
}
test() {
local myself="$1"
tmp_executable=$( mktemp )
echo "true" > "$tmp_executable"
chmod +x "$tmp_executable"
# == wrap ==
"$myself" wrap "$tmp_executable"
[ -e "$tmp_executable.orig" ] || test_fail 1
grep -q "$MARKER" "$tmp_executable" ] || test_fail 2
# == exec ==
"$tmp_executable" gogo || test_fail 3
grep -q "Executed as:" "$tmp_executable.log."* \
|| test_fail 4
grep -q "Param 1: _gogo_" "$tmp_executable.log."* \
|| test_fail 5
# == unwrap ==
"$myself" unwrap "$tmp_executable"
[ -e "$tmp_executable.orig" ] && test_fail 6
grep -q "$MARKER" "$tmp_executable" && test_fail 7
# == clean up ==
rm "$tmp_executable"
rm "$tmp_executable.log."*
echo "hooray, it seems like $myself works correctly :-)!"
}
# =============== main ===============
MARKER="this marker is being used to identify the executable_wrapper script"
if [[ "/$0" =~ /executable_wrapper$ ]]; then
# we are called as "executable_wrapper"...
executable="$2"
case "$1" in
--help)
help # and exit
;;
wrap)
wrap_executable_with "$executable" "$0" # and exit
;;
unwrap)
unwrap_executable "$executable" # and exit
;;
test)
test "$0" # and exit
;;
*)
help # and exit
;;
esac
else
# we are not called as "executable_wrapper" but as some
# wrapped executable
original_exec_path="$0.orig"
executed_as_name=$( basename "$0" )
log_name=$( mktemp "/tmp/$executed_as_name.log.XXXXXX" )
(
cat <<- EOHERE
Executed as: $0
Num of params: $#
Param 1: _${1}_
Param 2: _${2}_
Param 3: _${3}_
Param 4: _${4}_
Param 5: _${5}_
Param 6: _${6}_
Param 7: _${7}_
Param 8: _${8}_
Param 9: _${9}_
All params @: _${@}_
ID: $( id )
Environment:
`set`
EOHERE
) >> "$log_name"
exec "$original_exec_path" "$@"
fi