diff --git a/daemon/src/main/java/org/lsposed/lspd/service/BridgeService.java b/daemon/src/main/java/org/lsposed/lspd/service/BridgeService.java index 6ff22c46..c19e7cc1 100644 --- a/daemon/src/main/java/org/lsposed/lspd/service/BridgeService.java +++ b/daemon/src/main/java/org/lsposed/lspd/service/BridgeService.java @@ -7,8 +7,6 @@ import android.os.IBinder; import android.os.Looper; import android.os.Parcel; import android.os.ServiceManager; -import android.system.ErrnoException; -import android.system.Os; import android.util.Log; import java.lang.reflect.Field; @@ -71,87 +69,72 @@ public class BridgeService { }; // For service - // This MUST run in main thread private static synchronized void sendToBridge(IBinder binder, boolean isRestart) { - assert Looper.myLooper() == Looper.getMainLooper(); - try { - Os.seteuid(0); - } catch (ErrnoException e) { - 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(); + do { + bridgeService = ServiceManager.getService(SERVICE_NAME); + if (bridgeService != null && bridgeService.pingBinder()) { + break; } + Log.i(TAG, "service " + SERVICE_NAME + " is not started, wait 1s."); + try { - bridgeService.linkToDeath(bridgeRecipient, 0); + //noinspection BusyWait + Thread.sleep(1000); } 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; sendToBridge(binder, snapshot == null || !snapshot.isBinderAlive()); return; + } finally { + data.recycle(); + reply.recycle(); } - 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; - sendToBridge(binder, snapshot == null || !snapshot.isBinderAlive()); - return; - } finally { - data.recycle(); - reply.recycle(); - } + if (res) break; - 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 { - Os.seteuid(1000); - } catch (ErrnoException e) { - Log.e(TAG, "seteuid 1000", e); + Thread.sleep(1000); + } catch (InterruptedException ignored) { } } + + if (listener != null) { + listener.onResponseFromBridgeService(res); + } } public static void send(LSPosedService service, Listener listener) {