Use SIGKILL to kill old processes

Fix #1455.

Note that in later Android versions, only processes under same uid will be listed and this will be quite efficient.
This commit is contained in:
Mygod
2017-11-18 02:10:34 -08:00
parent ae171a7084
commit dc2a5ca19c
3 changed files with 32 additions and 68 deletions
@@ -1,9 +1,14 @@
package com.github.shadowsocks.bg
import java.io.File
import java.util.Locale
import java.util.concurrent.TimeUnit
import android.text.TextUtils
import android.util.Log
import com.github.shadowsocks.JniHelper
import com.github.shadowsocks.ShadowsocksApplication.app
import scala.io.Source
/**
* @author Mygod
@@ -16,12 +21,16 @@ object Executable {
val TUN2SOCKS = "libtun2socks.so"
val OVERTURE = "liboverture.so"
val EXECUTABLES = Array(SS_LOCAL, SS_TUNNEL, PDNSD, REDSOCKS, TUN2SOCKS, OVERTURE)
val EXECUTABLES = Set(SS_LOCAL, SS_TUNNEL, PDNSD, REDSOCKS, TUN2SOCKS, OVERTURE)
def killAll() {
val killer = new ProcessBuilder("killall" +: EXECUTABLES: _*).start()
if (!killer.waitFor(1, TimeUnit.SECONDS))
Log.w("killall", "%s didn't exit within 1s. Post-crash clean-up may have failed."
.formatLocal(Locale.ENGLISH, killer.toString))
}
def killAll(): Unit =
for (process <- new File("/proc").listFiles((_, name) => TextUtils.isDigitsOnly(name))) {
val exe = new File(Source.fromFile(new File(process, "cmdline")).mkString.split("\0", 2).head)
if (exe.getParent == app.getApplicationInfo.nativeLibraryDir && EXECUTABLES.contains(exe.getName))
JniHelper.sigkill(process.getName.toInt) match {
case 0 =>
case errno => Log.w("kill", "SIGKILL %s (%d) failed with %d"
.formatLocal(Locale.ENGLISH, exe.getAbsolutePath, process.getName, errno))
}
}
}