Can an app check if it has Magisk `sudo` granted without triggering the permission prompt?
Aug 23, 12:44pm

Yes, an app can check if it has Magisk sudo (root) access granted without triggering the permission prompt by attempting a harmless root command and checking its success or failure. This is typically done using the su binary provided by Magisk.

How It Works

  1. Silent Check: The app can execute a command like su -c "id" or su -c "whoami" and check the output or exit code.

    • If the command succeeds (exit code 0), the app has root access.
    • If it fails (exit code non-zero), the app does not have root access, and the prompt was not triggered.
  2. No Prompt: Magisk (and most root management apps) will not show a prompt if the app already has root access granted. The prompt only appears if the app has never been granted root or if the user revoked access.


Example in Android (Java/Kotlin)

Here’s a simple way to check for root access in an Android app:

Java Example:

java
public boolean hasRootAccess() {
    Process process = null;
    try {
        process = Runtime.getRuntime().exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());
        os.writeBytes("id\n");
        os.flush();
        int exitCode = process.waitFor();
        return exitCode == 0;
    } catch (Exception e) {
        return false;
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
}

Kotlin Example:

kotlin
fun hasRootAccess(): Boolean {
    return try {
        val process = Runtime.getRuntime().exec("su")
        val os = DataOutputStream(process.outputStream)
        os.writeBytes("id\n")
        os.flush()
        val exitCode = process.waitFor()
        exitCode == 0
    } catch (e: Exception) {
        false
    }
}

Key Notes:

  • Magisk Behavior: Magisk (and its su binary) will not trigger a prompt if the app is already in the root-allowed list.
  • No Guarantee: If the user revoked access manually, the app will not have root until the prompt is shown again.
  • Alternative Methods: Some apps use files like /sbin/su, /system/bin/su, or /system/xbin/su to check for root binaries, but this doesn’t confirm granted access—only that root is available on the device.

Why This Works

  • Magisk’s su binary returns 0 (success) if the app has root access.
  • If the app is not in the allowed list, the command fails, and the prompt is not shown unless explicitly requested (e.g., via su -c with a new session).

Would you like a more specific implementation for your use case?

Aug 23, 12:44pm

This is a shared conversation and may include unverified content that doesn't reflect Mistral AI's views. Login to resume chatting.

Vibe can make mistakes. Check answers. Learn more