-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathfunctions3.shlib
282 lines (245 loc) · 7.75 KB
/
functions3.shlib
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
#!/system/bin/sh
#
MYDIR=${0%/*}
# Forcing to reload audioservers
function reloadAudioServers()
{
# wait for system boot completion and audiosever boot up
local i
for i in `seq 1 3` ; do
if [ "`getprop sys.boot_completed`" = "1" -a -n "`getprop init.svc.audioserver`" ]; then
break
fi
sleep 1
done
if [ -n "`getprop init.svc.audioserver`" ]; then
setprop ctl.restart audioserver
sleep 0.2
if [ "`getprop init.svc.audioserver`" != "running" ]; then
# workaround for Android 12 old devices hanging up the audioserver after "setprop ctl.restart audioserver" is executed
local pid="`getprop init.svc_debug_pid.audioserver`"
if [ -n "$pid" ]; then
kill -HUP $pid 1>"/dev/null" 2>&1
fi
for i in `seq 1 10` ; do
sleep 0.2
if [ "`getprop init.svc.audioserver`" = "running" ]; then
break
elif [ $i -eq 10 ]; then
echo "audioserver reload failed!" 1>&2
return 1
fi
done
fi
if [ $# -gt 0 -a "$1" = "all" -a -n "`getprop init.svc.vendor.audio-hal`" ]; then
setprop ctl.restart "vendor.audio-hal" 1>"/dev/null" 2>&1
sleep 0.2
if [ "`getprop init.svc.vendor.audio-hal`" != "running" ]; then
kill "`getprop init.svc_debug_pid.vendor.audio-hal`" 1>"/dev/null" 2>&1
fi
fi
else
echo "audioserver is not found!" 1>&2
return 1
fi
}
# Find mount points paired with arg2 ("/data/...") in arg1 (typycally "/proc/self/mountinfo").
# arg3 1: show a key with its paired mount point, 0: moun point only.
function findMounts()
{
if [ $# -ne 3 ]; then
return 1
elif [ ! -r "$1" -o `expr "$2" : /data` -ne 5 ]; then
return 1
fi
local mf=${2:5}
local showKey=0
case "$3" in
"true" | "1" )
showKey=1
;;
"false" | "0" )
showKey=0
;;
esac
awk -v showKey=$showKey -v mFile="^$mf\$|^$mf//deleted$" '
BEGIN {
status=1
}
$4 ~ mFile && $10 ~ /^\/dev\/block\// {
if (showKey)
print $4 " " $5
else
print $5
status=0
}
END {
exit status
}' <"$1"
}
# Find a mount point arg2 in arg1 (typycally "/proc/self/mountinfo").
# arg3 "IncludeMagisk" (including Magisk mount points) or "ExcludeMagisk" (exclude Magisk mount points)
# arg4 "ShowKey" (print a key with its specified mount point) or "NoShowKey" (don't print the key)
# If found, then return 0 else return 1
function isMounted()
{
if [ $# -ne 4 ]; then
return 1
elif [ ! -r "$1" ]; then
return 1
fi
local mf="$2"
local includeMagisk=0
if [ "$3" = "IncludeMagisk" ]; then
includeMagisk=1
fi
local showKey=0
if [ "$4" = "ShowKey" ]; then
showKey=1
fi
awk -v showKey=$showKey -v includeMagisk=$includeMagisk -v mFile="^$mf\$" '
BEGIN {
status=1
}
$5 ~ mFile && (includeMagisk==1 || ($4 !~ /^\/adb\/modules\// && $4 !~ /^\/data\/adb\/modules\// && $10 ~ /^\/dev\/block\//)) {
if (showKey)
print $4 " " $5
status=0
exit
}
END {
exit status
}' <"$1"
}
# Remove somewheres bind mounted with arg1
function removeMountPoints()
{
if [ $# -eq 1 ]; then
local mDirs="`findMounts \"/proc/self/mountinfo\" \"$1\" 0`"
if [ -n "$mDirs" ]; then
local i
for i in $mDirs ; do
if [ -e "$i" ]; then
case "$i" in
/data/local/tmp/* )
;;
* )
umount "$i" 1>"/dev/null" 2>&1
;;
esac
fi
done
fi
fi
return 0
}
# Remove arg1 file probaby bind mounted somewheres
function removeGenFile()
{
if [ $# -eq 1 ]; then
removeMountPoints "$1"
if [ -e "$1" ]; then
rm -f "$1"
fi
# Remove Magisk overlayed files. The above remove may fail.
removeMountPoints "$1"
return $?
fi
return 1
}
# Does this device support the 7.0 audio policy configuration
function IsSeventhAudio()
{
# Busybox cannot execute "/vendor/lib{,64}" expansion properly unlike mksh, so manually expanded
if [ -z "`ls /vendor/lib64/android.hardware.audio@7.?.so 2>/dev/null`" -a -z "`ls /vendor/lib/android.hardware.audio@7.?.so 2>/dev/null`" ] && \
[ -z "`ls /system/lib64/android.hardware.audio@7.?.so 2>/dev/null`" -a -z "`ls /system/lib/android.hardware.audio@7.?.so 2>/dev/null`" ]; then
return 1
elif [ "`getprop ro.build.product`" = "jfltexx" ]; then
# Galaxy S4 has a bug for the 7.0 audio policy configuration
return 1
else
return 0
fi
}
# Get the active audio policy configuration fille from the audioserever
function getActivePolicyFile()
{
dumpsys media.audio_policy | awk '
/^ Config source: / {
print $3
}'
}
# Extract the file name of an audio_policy_volumes.xml
function getVolumeFile()
{
if [ $# -gt 0 -a -r "$1" ]; then
grep -m 1 -e '<xi:include[[:space:]]*href[[:space:]]*=[[:space:]]*".*audio_policy_volumes.*\.xml"' "$1" | awk -F '"' '{ print $2 }'
else
return 1
fi
}
# Extract the file name of a default_volume_tables.xml
function getDefaultVolumeFile()
{
if [ $# -gt 0 -a -r "$1" ]; then
grep -m 1 -e '<xi:include[[:space:]]*href[[:space:]]*=[[:space:]]*".*default_volume_tables.*\.xml"' "$1" | awk -F '"' '{ print $2 }'
else
return 1
fi
}
# Make a configuration file for this script
function makeConfigFile()
{
if [ $# -eq 1 ]; then
local policyFile="`getActivePolicyFile`"
local volumeFile=""
local defaultVolumeFile=""
if [ -z "$policyFile" -o ! -r "$policyFile" ]; then
policyFile="N/A"
else
volumeFile="`getVolumeFile \"$policyFile\"`"
defaultVolumeFile="`getDefaultVolumeFile \"$policyFile\"`"
fi
if [ -z "$volumeFile" ]; then
volumeFile="/vendor/etc/audio_policy_volumes.xml"
fi
if [ -z "$defaultVolumeFile" ]; then
defaultVolumeFile="/vendor/etc/default_volume_tables.xml"
fi
local bltHal
if [ "`getprop persist.bluetooth.bluetooth_audio_hal.disabled`" = "true" ]; then
case "`getprop ro.board.platform`" in
sdm* | msm* | sd* )
bltHal="legacy"
;;
* )
bltHal="safe"
;;
esac
elif [ -e "/vendor/lib64/hw/audio.bluetooth.default.so" ]; then
bltHal="bypass-safer"
elif [ "`getprop ro.bluetooth.a2dp_offload.supported`" = "true" ]; then
bltHal="offload"
else
case "`getprop ro.board.platform`" in
mt* )
bltHal="safe"
;;
* )
bltHal="safest"
;;
esac
fi
if [ -e "$1" ]; then
rm -f "$1"
fi
cat <<_EOT_ > "$1"
PolicyFile="$policyFile"
BluetoothHal="$bltHal"
VolumeFile="$volumeFile"
DefaultVolumeFile="$defaultVolumeFile"
_EOT_
return 0
fi
return 1
}