parent
e8cce7a88e
commit
0a26993039
|
|
@ -7,8 +7,6 @@ import android.os.IBinder;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.os.ServiceManager;
|
import android.os.ServiceManager;
|
||||||
import android.system.ErrnoException;
|
|
||||||
import android.system.Os;
|
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
|
@ -71,87 +69,72 @@ public class BridgeService {
|
||||||
};
|
};
|
||||||
|
|
||||||
// For service
|
// For service
|
||||||
// This MUST run in main thread
|
|
||||||
private static synchronized void sendToBridge(IBinder binder, boolean isRestart) {
|
private static synchronized void sendToBridge(IBinder binder, boolean isRestart) {
|
||||||
assert Looper.myLooper() == Looper.getMainLooper();
|
do {
|
||||||
try {
|
bridgeService = ServiceManager.getService(SERVICE_NAME);
|
||||||
Os.seteuid(0);
|
if (bridgeService != null && bridgeService.pingBinder()) {
|
||||||
} catch (ErrnoException e) {
|
break;
|
||||||
Log.e(TAG, "seteuid 0", e);
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
do {
|
|
||||||
bridgeService = ServiceManager.getService(SERVICE_NAME);
|
|
||||||
if (bridgeService != null && bridgeService.pingBinder()) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.i(TAG, "service " + SERVICE_NAME + " is not started, wait 1s.");
|
|
||||||
|
|
||||||
try {
|
|
||||||
//noinspection BusyWait
|
|
||||||
Thread.sleep(1000);
|
|
||||||
} catch (Throwable e) {
|
|
||||||
Log.w(TAG, "sleep" + Log.getStackTraceString(e));
|
|
||||||
}
|
|
||||||
} while (true);
|
|
||||||
|
|
||||||
if (isRestart && listener != null) {
|
|
||||||
listener.onSystemServerRestarted();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Log.i(TAG, "service " + SERVICE_NAME + " is not started, wait 1s.");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
bridgeService.linkToDeath(bridgeRecipient, 0);
|
//noinspection BusyWait
|
||||||
|
Thread.sleep(1000);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
Log.w(TAG, "linkToDeath " + Log.getStackTraceString(e));
|
Log.w(TAG, "sleep" + Log.getStackTraceString(e));
|
||||||
|
}
|
||||||
|
} while (true);
|
||||||
|
|
||||||
|
if (isRestart && listener != null) {
|
||||||
|
listener.onSystemServerRestarted();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
bridgeService.linkToDeath(bridgeRecipient, 0);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
Log.w(TAG, "linkToDeath " + Log.getStackTraceString(e));
|
||||||
|
var snapshot = bridgeService;
|
||||||
|
sendToBridge(binder, snapshot == null || !snapshot.isBinderAlive());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Parcel data = Parcel.obtain();
|
||||||
|
Parcel reply = Parcel.obtain();
|
||||||
|
boolean res = false;
|
||||||
|
// try at most three times
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
try {
|
||||||
|
data.writeInterfaceToken(DESCRIPTOR);
|
||||||
|
data.writeInt(ACTION.ACTION_SEND_BINDER.ordinal());
|
||||||
|
Log.v(TAG, "binder " + binder.toString());
|
||||||
|
data.writeStrongBinder(binder);
|
||||||
|
if (bridgeService == null) break;
|
||||||
|
res = bridgeService.transact(TRANSACTION_CODE, data, reply, 0);
|
||||||
|
reply.readException();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
Log.e(TAG, "send binder " + Log.getStackTraceString(e));
|
||||||
var snapshot = bridgeService;
|
var snapshot = bridgeService;
|
||||||
sendToBridge(binder, snapshot == null || !snapshot.isBinderAlive());
|
sendToBridge(binder, snapshot == null || !snapshot.isBinderAlive());
|
||||||
return;
|
return;
|
||||||
|
} finally {
|
||||||
|
data.recycle();
|
||||||
|
reply.recycle();
|
||||||
}
|
}
|
||||||
|
|
||||||
Parcel data = Parcel.obtain();
|
if (res) break;
|
||||||
Parcel reply = Parcel.obtain();
|
|
||||||
boolean res = false;
|
|
||||||
// try at most three times
|
|
||||||
for (int i = 0; i < 3; i++) {
|
|
||||||
try {
|
|
||||||
data.writeInterfaceToken(DESCRIPTOR);
|
|
||||||
data.writeInt(ACTION.ACTION_SEND_BINDER.ordinal());
|
|
||||||
Log.v(TAG, "binder " + binder.toString());
|
|
||||||
data.writeStrongBinder(binder);
|
|
||||||
if (bridgeService == null) break;
|
|
||||||
res = bridgeService.transact(TRANSACTION_CODE, data, reply, 0);
|
|
||||||
reply.readException();
|
|
||||||
} catch (Throwable e) {
|
|
||||||
Log.e(TAG, "send binder " + Log.getStackTraceString(e));
|
|
||||||
var snapshot = bridgeService;
|
|
||||||
sendToBridge(binder, snapshot == null || !snapshot.isBinderAlive());
|
|
||||||
return;
|
|
||||||
} finally {
|
|
||||||
data.recycle();
|
|
||||||
reply.recycle();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (res) break;
|
Log.w(TAG, "no response from bridge, retry in 1s");
|
||||||
|
|
||||||
Log.w(TAG, "no response from bridge, retry in 1s");
|
|
||||||
|
|
||||||
try {
|
|
||||||
Thread.sleep(1000);
|
|
||||||
} catch (InterruptedException ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (listener != null) {
|
|
||||||
listener.onResponseFromBridgeService(res);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
try {
|
try {
|
||||||
Os.seteuid(1000);
|
Thread.sleep(1000);
|
||||||
} catch (ErrnoException e) {
|
} catch (InterruptedException ignored) {
|
||||||
Log.e(TAG, "seteuid 1000", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (listener != null) {
|
||||||
|
listener.onResponseFromBridgeService(res);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void send(LSPosedService service, Listener listener) {
|
public static void send(LSPosedService service, Listener listener) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue