Search code examples
androidandroid-source

Run shell command as root in system service AOSP


I'm customizing AOSP. I have system service, which starts by SystemServer with other system services. I will execute some commands in shell from my service. I'm using Runtime.getRuntime().exec(command) - this works fine if commands does not require root permissions. My system service has "system" rights, not "root" (it is logical). I'm trying to execute commands with "su -c" prefix (ex. - "su -c mkdir /mnt/sdcard/NewFolder"), without success.

Question: how to execute shell commands as root? (su binary is included in build). Should I create service which runs with root rights, not with system rights (if it is possible!)

EDIT: I don't need SuperUser.apk, just su binary (possible busybox for additional functionality).


Solution

  • What error do you get? May be you need to modify system/extras/su/su.c:

    /* Until we have something better, only root and the shell can use su. */
    myuid = getuid();
    if (myuid != AID_ROOT && myuid != AID_SHELL && myuid != AID_SYSTEM) {
        fprintf(stderr,"su: uid %d not allowed to su\n", myuid);
        return 1;
    }