功能:rk3399 9.0 上root权限,第三方APP可以调用su指令。

1.修改system/extras/su/su.cpp 文件

diff --git a/su/su.cpp b/su/su.cpp
index f3e4ff0..00a41a4 100644
--- a/su/su.cpp
+++ b/su/su.cpp
@@ -80,8 +80,8 @@ void extract_uidgids(const char* uidgids, uid_t* uid, gid_t* gid, gid_t* gids, i
 }
 
 int main(int argc, char** argv) {
-    uid_t current_uid = getuid();
-    if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed");
+    //uid_t current_uid = getuid();
+    //if (current_uid != AID_ROOT && current_uid != AID_SHELL) error(1, 0, "not allowed");

2.修改system/core/libcutils/libcutils/fs_config.cpp文件

diff --git a/libcutils/fs_config.cpp b/libcutils/fs_config.cpp
index 62e91cf..5f5a405 100644
--- a/libcutils/fs_config.cpp
+++ b/libcutils/fs_config.cpp
@@ -173,7 +173,7 @@ static const struct fs_path_config android_files[] = {
     // the following two files are INTENTIONALLY set-uid, but they
     // are NOT included on user builds.
     { 06755, AID_ROOT,      AID_ROOT,      0, "system/xbin/procmem" },
-    { 04755, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },
+    { 06755, AID_ROOT,      AID_SHELL,     0, "system/xbin/su" },

3.在device/rockchip/common/BoardConfig.mk中设置

BOARD_SELINUX_ENFORCING := false

4.APP调用su方法

1):创建类 ShellUtil.java

public class ShellUtil {
    public static void execShell(String[] cmds) {
        Process p = null;
        try {
            p = Runtime.getRuntime().exec("su");
        } catch (IOException e) {
            e.printStackTrace();
        }
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String tmpCmd : cmds) {
            try {
                os.writeBytes(tmpCmd+"\n");
                os.flush();
                try {
                    p.waitFor();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

2):调用代码

String[] cmds={"reboot"};
new Thread(new Runnable() {
    @Override
    public void run() {
        ShellUtil.execShell(cmds);
    }
}).start();

欢迎关注,有问题可以交流

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐