-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
169 lines (141 loc) · 4.08 KB
/
Copy pathmain.lua
File metadata and controls
169 lines (141 loc) · 4.08 KB
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
local config = {
whitelist = "",
dry_run = true,
}
local WHITELIST_FILE = PLUGIN_DIR .. "/whitelist.txt"
local function load_config()
local function bool(v, d)
if v == nil or v == "" then return d end
return v == "true"
end
config.whitelist = get_config("whitelist") or ""
config.dry_run = bool(get_config("dry_run"), true)
end
local function trim(s)
return (string.gsub(s, "^%s*(.-)%s*$", "%1"))
end
local function load_whitelist_file()
local wl = {}
local ok, data = pcall(read_file, WHITELIST_FILE)
if not ok or not data or data == "" then
return wl
end
for line in string.gmatch(data, "[^\r\n]+") do
local pkg = trim(line)
if pkg ~= "" and string.sub(pkg, 1, 1) ~= "#" then
wl[pkg] = true
end
end
return wl
end
local function get_whitelist()
local wl = {}
for entry in string.gmatch(config.whitelist .. ",", "([^,]+)") do
local pkg = trim(entry)
if pkg ~= "" then wl[pkg] = true end
end
for pkg, _ in pairs(load_whitelist_file()) do
wl[pkg] = true
end
return wl
end
local function get_user_packages()
local pkgs = {}
local r = exec("sh", "-c", "pm list packages -3")
if not r.ok then return pkgs end
for line in string.gmatch(r.stdout, "package:([^\n]+)") do
table.insert(pkgs, line)
end
return pkgs
end
local function is_running(pkg)
local r = exec("sh", "-c", "pidof " .. pkg .. " 2>/dev/null")
if r.ok and r.stdout and r.stdout ~= "" then
return true
end
local r2 = exec("sh", "-c", "ps -A -o PID,NAME | grep -w " .. pkg .. " 2>/dev/null")
if r2.ok and r2.stdout and r2.stdout ~= "" then
return true
end
return false
end
local function kill_app(pkg)
if config.dry_run then
info("[DRY RUN] " .. pkg)
return true
end
if not is_running(pkg) then
return true
end
exec("sh", "-c", "am force-stop --user 0 " .. pkg .. " 2>/dev/null")
os.execute("sleep 1")
if not is_running(pkg) then
info("killed " .. pkg)
return true
end
local r = exec("sh", "-c", "pidof " .. pkg .. " 2>/dev/null")
if r.ok and r.stdout and r.stdout ~= "" then
local pids = {}
for pid in string.gmatch(r.stdout, "%d+") do
table.insert(pids, pid)
end
if #pids > 0 then
exec("sh", "-c", "for pid in " .. table.concat(pids, " ") .. "; do kill -9 $pid 2>/dev/null; done")
os.execute("sleep 1")
end
end
exec("sh", "-c", "killall -9 " .. pkg .. " 2>/dev/null || true")
os.execute("sleep 1")
if not is_running(pkg) then
info("killed " .. pkg)
return true
end
warn(pkg .. " survived")
return false
end
local function run_kill()
load_config()
local whitelist = get_whitelist()
local pkgs = get_user_packages()
local protected_count = 0
local killed_count = 0
local skipped_count = 0
local failed_count = 0
for _, pkg in ipairs(pkgs) do
if whitelist[pkg] then
protected_count = protected_count + 1
info("protected " .. pkg)
goto next_pkg
end
if not is_running(pkg) then
skipped_count = skipped_count + 1
goto next_pkg
end
if kill_app(pkg) then
killed_count = killed_count + 1
else
failed_count = failed_count + 1
end
::next_pkg::
end
info("done: " .. #pkgs .. " user apps, " .. protected_count .. " protected, " .. skipped_count .. " already dead, " .. killed_count .. " killed, " .. failed_count .. " failed")
end
load_config()
return {
post_fs_data = function()
info("quiet-kill loaded (dry_run: " .. tostring(config.dry_run) .. ")")
end,
service = function()
info("quiet-kill ready")
end,
boot_completed = function()
info("quiet-kill idle — tap Kill Now to run")
end,
action = function()
info("kill triggered")
run_kill()
end,
kill_now = function()
run_kill()
end,
}