Commit cad6d7b6 authored by ayanamist's avatar ayanamist

Recover unexpected process exit on MIUI

parent e4e1a421
/*
* Shadowsocks - A shadowsocks client for Android
* Copyright (C) 2015 <max.c.lv@gmail.com>
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*
* ___====-_ _-====___
* _--^^^#####// \\#####^^^--_
* _-^##########// ( ) \\##########^-_
* -############// |\^^/| \\############-
* _/############// (@::@) \\############\_
* /#############(( \\// ))#############\
* -###############\\ (oo) //###############-
* -#################\\ / VV \ //#################-
* -###################\\/ \//###################-
* _#/|##########/\######( /\ )######/\##########|\#_
* |/ |#/\#/\#/\/ \#/\##\ | | /##/\#/ \/\#/\#/\#| \|
* ` |/ V V ` V \#\| | | |/#/ V ' V V \| '
* ` ` ` ` / | | | | \ ' ' ' '
* ( | | | | )
* __\ | | | | /__
* (vvv(VVV)(VVV)vvv)
*
* HERE BE DRAGONS
*
*/
package com.github.shadowsocks;
import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.System;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;
public class GuardedProcess extends Process {
private static final String TAG = GuardedProcess.class.getSimpleName();
private final Thread guardThread;
private volatile boolean isDestroyed = false;
private volatile Process process = null;
public GuardedProcess(String... cmd) throws InterruptedException, IOException {
this(Arrays.asList(cmd));
}
public GuardedProcess(final List<String> cmd) throws InterruptedException, IOException {
this(cmd, null);
}
public GuardedProcess(final List<String> cmd, final Runnable onRestartCallback) throws InterruptedException, IOException {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final AtomicReference<IOException> atomicIoException = new AtomicReference<IOException>(null);
guardThread = new Thread(new Runnable() {
@Override
public void run() {
try {
while (!isDestroyed) {
Log.i(TAG, "start process: " + cmd);
long startTime = System.currentTimeMillis();
process = startProcess(cmd);
if (onRestartCallback != null && countDownLatch.getCount() <= 0) {
onRestartCallback.run();
}
countDownLatch.countDown();
process.waitFor();
if (System.currentTimeMillis() - startTime < 1000) {
Log.w(TAG, "process exit too fast, stop guard: " + cmd);
break;
}
}
} catch (InterruptedException ignored) {
Log.i(TAG, "thread interrupt, destroy process: " + cmd);
process.destroy();
} catch (IOException e) {
atomicIoException.compareAndSet(null, e);
} finally {
countDownLatch.countDown();
}
}
}, "GuardThread-" + cmd);
guardThread.start();
countDownLatch.await();
IOException ioException = atomicIoException.get();
if (ioException != null) {
throw ioException;
}
}
private static Process startProcess(List<String> cmd) throws IOException {
return new ProcessBuilder(cmd).redirectErrorStream(true).start();
}
public void destroy() {
isDestroyed = true;
guardThread.interrupt();
process.destroy();
try {
guardThread.join();
} catch (InterruptedException ignored) {
}
}
@Override
public int exitValue() {
throw new UnsupportedOperationException();
}
@Override
public InputStream getErrorStream() {
throw new UnsupportedOperationException();
}
@Override
public InputStream getInputStream() {
throw new UnsupportedOperationException();
}
@Override
public OutputStream getOutputStream() {
throw new UnsupportedOperationException();
}
@Override
public int waitFor() throws InterruptedException {
guardThread.join();
return 0;
}
}
...@@ -196,10 +196,7 @@ class ShadowsocksNatService extends BaseService { ...@@ -196,10 +196,7 @@ class ShadowsocksNatService extends BaseService {
} }
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
sslocalProcess = new ProcessBuilder() sslocalProcess = new GuardedProcess(cmd)
.command(cmd)
.redirectErrorStream(true)
.start()
} }
def startTunnel() { def startTunnel() {
...@@ -225,10 +222,7 @@ class ShadowsocksNatService extends BaseService { ...@@ -225,10 +222,7 @@ class ShadowsocksNatService extends BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
sstunnelProcess = new ProcessBuilder() sstunnelProcess = new GuardedProcess(cmd)
.command(cmd)
.redirectErrorStream(true)
.start()
} else { } else {
val conf = ConfigUtils val conf = ConfigUtils
...@@ -251,10 +245,7 @@ class ShadowsocksNatService extends BaseService { ...@@ -251,10 +245,7 @@ class ShadowsocksNatService extends BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmdBuf.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmdBuf.mkString(" "))
sstunnelProcess = new ProcessBuilder() sstunnelProcess = new GuardedProcess(cmdBuf)
.command(cmdBuf)
.redirectErrorStream(true)
.start()
} }
} }
...@@ -277,10 +268,7 @@ class ShadowsocksNatService extends BaseService { ...@@ -277,10 +268,7 @@ class ShadowsocksNatService extends BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
pdnsdProcess = new ProcessBuilder() pdnsdProcess = new GuardedProcess(cmd.split(" ").toSeq)
.command(cmd.split(" ").toSeq)
.redirectErrorStream(true)
.start()
} }
def getVersionName: String = { def getVersionName: String = {
...@@ -304,10 +292,7 @@ class ShadowsocksNatService extends BaseService { ...@@ -304,10 +292,7 @@ class ShadowsocksNatService extends BaseService {
}) })
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
redsocksProcess = new ProcessBuilder() redsocksProcess = new GuardedProcess(cmd.split(" ").toSeq)
.command(cmd.split(" ").toSeq)
.redirectErrorStream(true)
.start()
} }
/** Called when the activity is first created. */ /** Called when the activity is first created. */
......
...@@ -256,18 +256,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -256,18 +256,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
} }
val fd = startVpn() val fd = startVpn()
sendFd(fd)
if (fd != -1) {
var tries = 1
while (tries < 5) {
Thread.sleep(1000 * tries)
if (System.sendfd(fd, getApplicationInfo.dataDir + "/sock_path") != -1) {
return true
}
tries += 1
}
}
false
} }
def startShadowsocksDaemon() { def startShadowsocksDaemon() {
...@@ -307,10 +296,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -307,10 +296,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
sslocalProcess = new ProcessBuilder() sslocalProcess = new GuardedProcess(cmd)
.command(cmd)
.redirectErrorStream(true)
.start()
} }
def startDnsTunnel() = { def startDnsTunnel() = {
...@@ -335,10 +321,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -335,10 +321,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" ")) if (BuildConfig.DEBUG) Log.d(TAG, cmd.mkString(" "))
sstunnelProcess = new ProcessBuilder() sstunnelProcess = new GuardedProcess(cmd)
.command(cmd)
.redirectErrorStream(true)
.start()
} }
def startDnsDaemon() { def startDnsDaemon() {
...@@ -361,10 +344,7 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -361,10 +344,7 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
pdnsdProcess = new ProcessBuilder() pdnsdProcess = new GuardedProcess(cmd.split(" ").toSeq)
.command(cmd.split(" ").toSeq)
.redirectErrorStream(true)
.start()
} }
override def getContext = getBaseContext override def getContext = getBaseContext
...@@ -454,14 +434,29 @@ class ShadowsocksVpnService extends VpnService with BaseService { ...@@ -454,14 +434,29 @@ class ShadowsocksVpnService extends VpnService with BaseService {
if (BuildConfig.DEBUG) Log.d(TAG, cmd) if (BuildConfig.DEBUG) Log.d(TAG, cmd)
tun2socksProcess = new ProcessBuilder() tun2socksProcess = new GuardedProcess(cmd.split(" ").toSeq, new Runnable {
.command(cmd.split(" ").toSeq) override def run(): Unit = {
.redirectErrorStream(true) sendFd(fd)
.start() }
})
fd fd
} }
def sendFd(fd: Int): Boolean = {
if (fd != -1) {
var tries = 1
while (tries < 5) {
Thread.sleep(1000 * tries)
if (System.sendfd(fd, getApplicationInfo.dataDir + "/sock_path") != -1) {
return true
}
tries += 1
}
}
false
}
override def getTag = TAG override def getTag = TAG
override def getServiceMode = Mode.VPN override def getServiceMode = Mode.VPN
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment