在系统服务 AOSP 中以 root 身份运行 shell 命令 [英] Run shell command as root in system service AOSP

查看:51
本文介绍了在系统服务 AOSP 中以 root 身份运行 shell 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在自定义 AOSP.我有 system service,它由 SystemServer 和其他 system services 开始.我将从我的 service 在 shell 中执行一些命令.我正在使用 Runtime.getRuntime().exec(command) - 如果命令不需要 root 权限,这可以正常工作.我的系统服务具有系统"权限,而不是root"(这是合乎逻辑的).我正在尝试使用 "su -c" 前缀(例如 - "su -c mkdir/mnt/sdcard/NewFolder")执行命令,但没有成功.

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.

问题:如何以root身份执行shell命令?(su 二进制文件包含在 build 中).我应该创建以 root 权限运行的服务,而不是系统权限(如果可能的话!)

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!)

我不需要 SuperUser.apk,只需要 su 二进制文件(可能使用 busybox 以获得附加功能).

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

推荐答案

你遇到了什么错误?可能需要修改system/extras/su/su.c:

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;
}

这篇关于在系统服务 AOSP 中以 root 身份运行 shell 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆