-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservice.sh
326 lines (287 loc) · 7.96 KB
/
service.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
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
#!/bin/sh
# YetAnotherBootLoopProtector by @rhythmcache
MARKER_DIR="${0%/*}"
LOGFILE="/data/local/tmp/service.log"
MAGISK_MODULES_DIR="/data/adb/modules"
BOOT_TIMEOUT=100
PACKAGE="com.android.systemui" #default: SystemUI
MONITOR_DISABLE_FILE="/data/adb/systemui.monitor.disable"
lock_file="/data/adb/.tmp.lock"
file=/data/adb/YABP/allowed-modules.txt
file2=/data/adb/YABP/allowed-scripts.txt
# Log events
log_event() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >>"$LOGFILE"
}
# Check if boot is completed
is_boot_completed() {
BOOT_COMPLETED=$(getprop sys.boot_completed)
if [ "$BOOT_COMPLETED" = "1" ]; then
return 0
else
return 1
fi
}
# disable modules
disable_magisk_modules() {
log_event "Disabling all Magisk modules..."
# Read allowed modules
allowed_modules=""
if [ -f "$file" ]; then
while IFS= read -r line; do
if [ "${line#\#}" != "$line" ] || [ -z "$line" ]; then
continue
else
allowed_modules="$allowed_modules $line"
fi
done < "$file"
fi
# Read allowed scripts
allowed_scripts=""
if [ -f "$file2" ]; then
while IFS= read -r line; do
if [ "${line#\#}" != "$line" ] || [ -z "$line" ]; then
continue
else
allowed_scripts="$allowed_scripts $line"
fi
done < "$file2"
fi
# Process modules using find
find "$MAGISK_MODULES_DIR" -mindepth 1 -maxdepth 1 -type d | while read -r MODULE; do
MODULE_NAME=$(basename "$MODULE")
if [ -n "$(echo " $allowed_modules " | grep " $MODULE_NAME ")" ]; then
log_event "Skipping module: $MODULE_NAME"
else
touch "$MODULE/disable"
log_event "Disabled module: $MODULE_NAME"
fi
done
# Process scripts using find
for DIR in /data/adb/service.d /data/adb/post-fs-data.d /data/adb/post-mount.d /data/adb/boot-completed.d; do
if [ -d "$DIR" ]; then
find "$DIR" -mindepth 1 -maxdepth 1 -type f | while read -r SCRIPT; do
SCRIPT_NAME=$(basename "$SCRIPT")
# Skip status.sh file
if [ "$SCRIPT_NAME" = ".status.sh" ]; then
log_event "Skipping .status.sh"
continue
fi
# Check if script is in allowed list
if [ -n "$(echo " $allowed_scripts " | grep " $SCRIPT_NAME ")" ]; then
log_event "Skipping script: $SCRIPT_NAME"
else
chmod 644 "$SCRIPT"
log_event "Changed permissions for script: $SCRIPT_NAME"
fi
done
# Log if no scripts found in directory
if [ -z "$(find "$DIR" -mindepth 1 -maxdepth 1 -type f 2>/dev/null)" ]; then
log_event "No scripts found in $DIR"
fi
fi
done
}
# Check for marker files
check_marker_files() {
MARKER1="$MARKER_DIR/marker1"
MARKER2="$MARKER_DIR/marker2"
MARKER3="$MARKER_DIR/marker3"
if [ -f "$MARKER3" ]; then
log_event "Bootloop detected (3 markers found). Disabling Magisk modules and rebooting..."
disable_magisk_modules
rm -f "$MARKER1" "$MARKER2" "$MARKER3"
log_event "Deleted all marker files. Rebooting..."
reboot
elif [ -f "$MARKER2" ]; then
log_event "Second failed boot detected, creating marker3"
touch "$MARKER3"
elif [ -f "$MARKER1" ]; then
log_event "First failed boot detected, creating marker2"
touch "$MARKER2"
else
log_event "No markers found, creating marker1"
touch "$MARKER1"
fi
}
# Check if a package is running
is_package_running() {
if pidof "$PACKAGE" >/dev/null; then
return 0 # Package is running
else
return 1 # Package is not running
fi
}
# monitor zygote
zygote_monitor() {
dur=30 # Duration
int=4 # Interval
max=4 # Max PID changes
changes=0
last_pid=""
start=$(date +%s)
arch=$(getprop ro.product.cpu.abi)
if [ "$arch" = "arm64-v8a" ] || [ "$arch" = "x86_64" ]; then
check="zygote64"
else
check="zygote"
fi
log_event "Zygote monitor started."
while :; do
now=$(date +%s)
if [ $((now - start)) -ge "$dur" ]; then
break
fi
cur_pid=$(pidof "$check" 2>/dev/null || echo "")
if [ -n "$cur_pid" ]; then
overlap=0
for pid in $(echo "$last_pid" | tr ' ' '\n'); do
case " $cur_pid " in
*" $pid "*)
overlap=1
break
;;
esac
done
if [ "$overlap" -eq 0 ]; then
changes=$((changes + 1))
log_event "PID changed: $last_pid -> $cur_pid (Count: $changes)"
fi
last_pid="$cur_pid"
fi
if [ "$changes" -ge "$max" ]; then
log_event "PID changed $changes times. Disabling modules."
disable_magisk_modules
reboot
return
fi
sleep "$int"
done
log_event "Zygote is OK"
}
#SystemUI - PID
systemui_monitor() {
local monitor_dur=30 # Monitor duration
local check_int=3 # time between PID checks
local max_chg=3 # Max PID changes
local pid_chg=0 # Counter for PID changes
local last_pid="" #
local curr_pid #
local start_time=$(date +%s) # Start time of monitoring
local curr_time
log_event "Checking systemui pid "
touch "$lock_file"
while true; do
curr_time=$(date +%s)
if [ $((curr_time - start_time)) -ge $monitor_dur ]; then
break
fi
curr_pid=$(pidof "$PACKAGE")
if [ -n "$curr_pid" ] && [ "$curr_pid" != "$last_pid" ]; then
pid_chg=$((pid_chg + 1))
log_event "SystemUI PID changed: $last_pid -> $curr_pid (Change count: $pid_chg)"
last_pid="$curr_pid"
fi
if [ $pid_chg -ge $max_chg ]; then
log_event "SystemUI pid changed $pid_chg times within 30s"
disable_magisk_modules
rm -f "$lock_file"
reboot
return
fi
sleep "$check_int"
done
log_event "pid check completed "
rm -f "$lock_file"
}
# Monitor SystemUI
monitor_package() {
if [ -f "$MONITOR_DISABLE_FILE" ]; then
log_event "SystemUI monitoring is disabled. Exiting monitor."
return
fi
log_event "Starting continuous monitor for package: $PACKAGE"
local MONITOR_TIMEOUT=25 # Total timeout in seconds
local CHECK_INTERVAL=5 # Check interval in seconds
local FAILURE_TIME=0 # Time elapsed since package stopped running
local PREVIOUS_PID="" # Previous PID of the package
while true; do
CURRENT_PID=$(pidof "$PACKAGE")
if [ -n "$CURRENT_PID" ]; then
if [ "$CURRENT_PID" != "$PREVIOUS_PID" ]; then
log_event "PID of $PACKAGE has changed from $PREVIOUS_PID to $CURRENT_PID."
PREVIOUS_PID="$CURRENT_PID"
if [ ! -f "$lock_file" ]; then
systemui_monitor &
fi
fi
FAILURE_TIME=0
else
log_event "$PACKAGE is not running. Failure timer: $FAILURE_TIME seconds."
FAILURE_TIME=$((FAILURE_TIME + CHECK_INTERVAL))
if [ ! -f "$lock_file" ]; then
systemui_monitor &
fi
if [ $FAILURE_TIME -ge $MONITOR_TIMEOUT ]; then
log_event "$PACKAGE has not been running for $MONITOR_TIMEOUT seconds. Disabling Magisk modules and rebooting..."
disable_magisk_modules
reboot
fi
fi
sleep $CHECK_INTERVAL
done
}
# post fs check
signature() {
S1="$MARKER_DIR/s1"
S2="$MARKER_DIR/s2"
S3="$MARKER_DIR/s3"
DETECTED=0
# Check if any of the files exist
if [ -f "$S1" ]; then
DETECTED=1
log_event "Found s1"
fi
if [ -f "$S2" ]; then
DETECTED=1
log_event "Found s2"
fi
if [ -f "$S3" ]; then
DETECTED=1
log_event "Found s3"
fi
if [ "$DETECTED" -eq 1 ]; then
log_event "Post-fs signature detected..."
rm -f "$S1" "$S2" "$S3"
else
log_event "Warning: Post-fs signature not found.."
fi
}
# Main logic
signature
log_event "Service started. Waiting for boot completion..."
check_marker_files
sleep 3
zygote_monitor
rm -f "$lock_file"
# Wait for boot to complete
SECONDS_PASSED=0
while [ $SECONDS_PASSED -lt $BOOT_TIMEOUT ]; do
if is_boot_completed; then
log_event "Boot completed successfully. Cleaning up marker files."
rm -f "$MARKER_DIR/marker1" "$MARKER_DIR/marker2" "$MARKER_DIR/marker3"
break
fi
sleep 5
SECONDS_PASSED=$((SECONDS_PASSED + 5))
done
#reboot
if ! is_boot_completed; then
log_event "Boot did not complete within $BOOT_TIMEOUT seconds. Disabling Magisk modules and rebooting..."
disable_magisk_modules
rm -f "$MARKER_DIR/marker1" "$MARKER_DIR/marker2" "$MARKER_DIR/marker3"
log_event "Rebooting the device..."
reboot
fi
#monitoring
monitor_package