在系统分区写入文件
我正在尝试将我的应用程序生成的文件写入系统分区。由于我无法在应用程序中创建 FileOutputStream,因此我在应用程序的数据目录中创建文件,更正权限,然后将其移动到系统分区。
目前,下面的代码错过了 /system 的可写重新挂载 - 出于测试目的,我已通过 adb remount 成功执行了此步骤 - 因此这不应该是问题。
该应用程序也成功获得root权限。
但下面的代码不起作用。它仅创建文件,但不会将其移动到系统分区。我有什么错?
FileOutputStream out = openFileOutput("myfile.test", MODE_WORLD_READABLE);
File f = getFileStreamPath("myfile.test");
writeDataToOutputStream(out);
out.close();
String filename = f.getAbsolutePath();
Runtime r = Runtime.getRuntime();
r.exec("su");
// Waiting here some seconds does not make any difference
r.exec(new String[] { "chown", "root.root", filename });
r.exec(new String[] { "chmod", "644", filename });
r.exec(new String[] { "mv", filename, "/system/myfile.test" });
I am trying to write a file, generated by my app, onto the system partition. As I can not create an FileOutputStream inside my app I create the file in the data directory of my app, correct the permissions and then move it to the system partition.
At the moment the code below misses the writable remount of /system - for testing purposes I have performed this step successfully via adb remount
- therefore this should not be the problem.
The app also gets successfully root permissions.
But the code below does not work. It only creates the file but does not move it to the system partition. What's my mistake?
FileOutputStream out = openFileOutput("myfile.test", MODE_WORLD_READABLE);
File f = getFileStreamPath("myfile.test");
writeDataToOutputStream(out);
out.close();
String filename = f.getAbsolutePath();
Runtime r = Runtime.getRuntime();
r.exec("su");
// Waiting here some seconds does not make any difference
r.exec(new String[] { "chown", "root.root", filename });
r.exec(new String[] { "chmod", "644", filename });
r.exec(new String[] { "mv", filename, "/system/myfile.test" });
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的猜测是,只有第一次调用 Runtime.exec():
才能获得 root 权限。 Runtime.exec() 的文档表示每个调用都作为单独的进程运行。因此,对
exec
、chown
、chmod
、mv
的所有后续调用都以当前的应用程序进程 - 因此失败。您也许可以编写一个小型 shell 脚本,然后将该 shell 脚本作为参数传递给
su
。然后所有命令都将以 root 权限运行。My guess is that only the first call to
Runtime.exec()
:acheives root. The docs for
Runtime.exec()
say that each invocation runs as a separate process. So all of the subsequent calls toexec
, forchown
,chmod
,mv
all run with the permissions of the current app process - and hence fail.You might be able to write a small shell script, then pass the shell script as an argument to
su
. Then all the commands would get run with root permission.好的,我找到了如何使用 root 权限执行多个命令。诀窍是将所有命令发送到一个 su 进程:
剩下的唯一一件事(不包括上面的代码)是使
/system
可写。可能像
mount -o rw,remount /system
这样的附加命令就足够了 - 将测试这一点。OK, I found how to execute multiple commands with root permissions. The trick is to send all commands to the one su process:
The only thing left (not include din the code above) is to make
/system
writable.May be one additional command like
mount -o rw,remount /system
is sufficient - will test that.