1818import timber .log .Timber ;
1919
2020public class InstallerInitializer extends Shell .Initializer {
21- public static final int ERROR_NO_PATH = 1 ;
22- public static final int ERROR_NO_SU = 2 ;
23- public static final int ERROR_OTHER = 3 ;
24- private static final File MAGISK_SBIN = new File ("/sbin/magisk" );
25- private static final File MAGISK_SYSTEM = new File ("/system/bin/magisk" );
26- private static final File MAGISK_SYSTEM_EX = new File ("/system/xbin/magisk" );
27- private static final boolean HAS_MAGISK = MAGISK_SBIN .exists () || MAGISK_SYSTEM .exists () || MAGISK_SYSTEM_EX .exists ();
21+ private static final File MAGISK_SBIN =
22+ new File ("/sbin/magisk" );
23+ private static final File MAGISK_SYSTEM =
24+ new File ("/system/bin/magisk" );
25+ private static final File MAGISK_SYSTEM_EX =
26+ new File ("/system/xbin/magisk" );
27+ private static final boolean HAS_MAGISK = MAGISK_SBIN .exists () ||
28+ MAGISK_SYSTEM .exists () || MAGISK_SYSTEM_EX .exists ();
2829 private static String MAGISK_PATH ;
2930 private static int MAGISK_VERSION_CODE ;
3031 private static boolean HAS_RAMDISK ;
3132
33+ public static final int ERROR_NO_PATH = 1 ;
34+ public static final int ERROR_NO_SU = 2 ;
35+ public static final int ERROR_OTHER = 3 ;
36+
37+ public interface Callback {
38+ void onPathReceived (String path );
39+
40+ void onFailure (int error );
41+ }
42+
3243 @ Nullable
3344 public static NotificationType getErrorNotification () {
3445 Boolean hasRoot = Shell .isAppGrantedRoot ();
35- if (MAGISK_PATH != null && hasRoot != Boolean .FALSE ) {
46+ if (MAGISK_PATH != null &&
47+ hasRoot != Boolean .FALSE ) {
3648 return null ;
3749 }
3850 if (!HAS_MAGISK ) {
@@ -48,11 +60,13 @@ public static String peekMagiskPath() {
4860 }
4961
5062 public static String peekMirrorPath () {
51- return InstallerInitializer .MAGISK_PATH == null ? null : InstallerInitializer .MAGISK_PATH + "/.magisk/mirror" ;
63+ return InstallerInitializer .MAGISK_PATH == null ? null :
64+ InstallerInitializer .MAGISK_PATH + "/.magisk/mirror" ;
5265 }
5366
5467 public static String peekModulesPath () {
55- return InstallerInitializer .MAGISK_PATH == null ? null : InstallerInitializer .MAGISK_PATH + "/.magisk/modules" ;
68+ return InstallerInitializer .MAGISK_PATH == null ? null :
69+ InstallerInitializer .MAGISK_PATH + "/.magisk/modules" ;
5670 }
5771
5872 public static int peekMagiskVersion () {
@@ -67,7 +81,7 @@ public static void tryGetMagiskPathAsync(Callback callback) {
6781 tryGetMagiskPathAsync (callback , false );
6882 }
6983
70- public static void tryGetMagiskPathAsync (Callback callback , boolean forceCheck ) {
84+ public static void tryGetMagiskPathAsync (Callback callback ,boolean forceCheck ) {
7185 final String MAGISK_PATH = InstallerInitializer .MAGISK_PATH ;
7286 Thread thread = new Thread ("Magisk GetPath Thread" ) {
7387 @ Override
@@ -109,30 +123,26 @@ public void run() {
109123 private static String tryGetMagiskPath (boolean forceCheck ) {
110124 String MAGISK_PATH = InstallerInitializer .MAGISK_PATH ;
111125 int MAGISK_VERSION_CODE ;
112- boolean HAS_RAMDISK ;
126+ boolean HAS_RAMDISK = InstallerInitializer . HAS_RAMDISK ;
113127 if (MAGISK_PATH != null && !forceCheck ) return MAGISK_PATH ;
114128 ArrayList <String > output = new ArrayList <>();
115- if (!Shell .cmd ("magisk -V" , "magisk --path" ).to (output ).exec ().isSuccess ()) {
116- // log the output of each command
117- if (output .size () > 0 ) {
118- for (String line : output ) {
119- Timber .w ("Could not run magisk: %s" , line );
120- }
121- } else {
122- Timber .w ("Could not run magisk" );
129+ if (!Shell .cmd ("if grep ' / ' /proc/mounts | grep -q '/dev/root' &> /dev/null; " +
130+ "then echo true; else echo false; fi" , "magisk -V" , "magisk --path" )
131+ .to (output ).exec ().isSuccess ()) {
132+ if (output .size () != 0 ) {
133+ HAS_RAMDISK = "false" .equals (output .get (0 )) ||
134+ "true" .equalsIgnoreCase (System .getProperty ("ro.build.ab_update" ));
123135 }
136+ InstallerInitializer .HAS_RAMDISK = HAS_RAMDISK ;
124137 return null ;
125138 }
126- if (output .size () != 2 ) {
127- return null ;
128- }
129- HAS_RAMDISK = "true" .equalsIgnoreCase (System .getProperty ("ro.build.ab_update" ));
130- InstallerInitializer .HAS_RAMDISK = HAS_RAMDISK ;
131- MAGISK_PATH = output .get (1 );
139+ MAGISK_PATH = output .size () < 3 ? "" : output .get (2 );
132140 Timber .i ("Magisk runtime path: %s" , MAGISK_PATH );
133- MAGISK_VERSION_CODE = Integer .parseInt (output .get (0 ));
141+ MAGISK_VERSION_CODE = Integer .parseInt (output .get (1 ));
134142 Timber .i ("Magisk version code: %s" , MAGISK_VERSION_CODE );
135- if (MAGISK_VERSION_CODE >= Constants .MAGISK_VER_CODE_FLAT_MODULES && MAGISK_VERSION_CODE < Constants .MAGISK_VER_CODE_PATH_SUPPORT && (MAGISK_PATH .isEmpty () || !Files .existsSU (new File (MAGISK_PATH )))) {
143+ if (MAGISK_VERSION_CODE >= Constants .MAGISK_VER_CODE_FLAT_MODULES &&
144+ MAGISK_VERSION_CODE < Constants .MAGISK_VER_CODE_PATH_SUPPORT &&
145+ (MAGISK_PATH .isEmpty () || !new File (MAGISK_PATH ).exists ())) {
136146 MAGISK_PATH = "/sbin" ;
137147 }
138148 if (MAGISK_PATH .length () != 0 && Files .existsSU (new File (MAGISK_PATH ))) {
@@ -147,46 +157,8 @@ private static String tryGetMagiskPath(boolean forceCheck) {
147157
148158 @ Override
149159 public boolean onInit (@ NonNull Context context , @ NonNull Shell shell ) {
150- // open a new shell
151- shell .newJob ().add ("id" ).exec ().getOut ();
152- // if Shell.isAppGrantedRoot() returns null, loop until it doesn't
153- if (Shell .isAppGrantedRoot () == null ) {
154- Timber .w ("Waiting for root access..." );
155- while (Shell .isAppGrantedRoot () == null ) {
156- try {
157- //noinspection BusyWait
158- Thread .sleep (100 );
159- } catch (InterruptedException e ) {
160- Timber .e (e );
161- }
162- }
163- }
164- boolean hasRoot = Boolean .TRUE .equals (Shell .isAppGrantedRoot ());
165- if (!hasRoot ) {
166- Timber .w ("No root access, or libsu is misreporting" );
167- return false ;
168- }
169- // switch to global namespace
170- // first, try to copy /data/adb/magisk/busybox to /data/local/tmp
171- // if that fails, return false
172- if (shell .newJob ().add ("cp -f /data/adb/magisk/busybox /data/local/tmp" ).exec ().isSuccess ()) {
173- // switch to local namespace
174- // first check if nsenter is available
175- if (!shell .newJob ().add ("which nsenter" ).exec ().isSuccess ()) {
176- Timber .w ("nsenter cmd unavailable" );
177- return shell .newJob ().add ("export ASH_STANDALONE=1; /data/local/tmp/busybox ash" ).exec ().isSuccess ();
178- } else {
179- return shell .newJob ().add ("export ASH_STANDALONE=1; nsenter -t 1 -m -u /data/local/tmp/busybox ash" ).exec ().isSuccess ();
180- }
181- } else {
182- Timber .w ("Could not copy bb" );
183- return false ;
184- }
185- }
186-
187- public interface Callback {
188- void onPathReceived (String path );
189-
190- void onFailure (int error );
160+ if (!shell .isRoot ())
161+ return true ;
162+ return shell .newJob ().add ("export ASH_STANDALONE=1" ).exec ().isSuccess ();
191163 }
192164}
0 commit comments