-
Notifications
You must be signed in to change notification settings - Fork 2
/
install_hook.sh
executable file
·178 lines (137 loc) · 2.94 KB
/
install_hook.sh
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
#!/bin/sh
#
# Copyright (C) 2017 Sergey Senozhatsky
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA
#
MTRACE_PATH=./build/lib/
function remount_rootfs_rw
{
if [ $1 -eq 0 ]; then
return 0
fi
sync
mount -o remount,rw /
if [ $? != 0 ]; then
echo "Can't remount rootfs"
exit 6
fi
}
function remount_rootfs_ro
{
if [ $1 -eq 0 ]; then
return 0
fi
sync
mount -o remount,ro /
if [ $? != 0 ]; then
echo "Can't remount rootfs"
exit 6
fi
}
function install
{
local path=$1
local remount=$2
if [ "z$path" == "z" ]; then
echo "No application path was provided"
exit 1
fi
if [ ! -e $path ]; then
echo "Unknown application $path"
exit 2
fi
if [ -e $path"_" ]; then
echo "mtrace backup file already exists"
exit 4
fi
if [ ! -e $MTRACE_PATH/libmtrace.so ]; then
echo "No libmtrace.so file found at $MTRACE_PATH"
exit 5
fi
remount_rootfs_rw $remount
mv $path $path"_"
cat << EOF > $path
#/bin/sh
MTRACE_LOG_DIR=/tmp/ MTRACE_ALLOC_MINWMARK=400 LD_PRELOAD=${MTRACE_PATH}/libmtrace.so ${path}_ "\$@"
EOF
chmod +x $path
remount_rootfs_ro $remount
return 0
}
function uninstall
{
local path=$1
local remount=$2
if [ "z$path" == "z" ]; then
echo "No application path was provided"
exit 1
fi
if [ ! -e $path ]; then
echo "Unknown application $path"
exit 2
fi
if [ ! -e ${path}_ ]; then
echo "No mtrace backup file was found"
exit 4
fi
remount_rootfs_rw $remount
rm $path
mv $path"_" $path
remount_rootfs_ro $remount
return 0
}
function process
{
if [ "z$1" == "zinstall" -o "z$1" == "zrinstall" ]; then
local remount=0
if [ "z$1" == "zrinstall" ]; then
remount=1
fi
install $2 $remount
return $?
fi
if [ "z$1" == "zuninstall" -o "z$1" == "zruninstall" ]; then
local remount=0
if [ "z$1" == "zruninstall" ]; then
remount=1
fi
uninstall $2 $remount
return $?
fi
return 7
}
function print_usage
{
echo "Usage example:"
echo "install_hook.sh install|uninstall /path/application"
echo " rinstall|runinstall - to force remount of FS in rw mode"
}
hook_args=("$@")
hook_args_num=${#hook_args[@]}
if [ $hook_args_num -le 1 ]; then
print_usage
exit 3
fi
for (( i=1; i<${hook_args_num}; i++ )); do
process ${hook_args[0]} ${hook_args[$i]}
code=$?
if [ $code != 0 ]; then
print_usage
exit $code
fi
done
exit 0