在系统分区写入文件

发布于 2024-12-11 16:02:13 字数 758 浏览 1 评论 0原文

我正在尝试将我的应用程序生成的文件写入系统分区。由于我无法在应用程序中创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初与友歌 2024-12-18 16:02:13

我的猜测是,只有第一次调用 Runtime.exec():

r.exec("su");

才能获得 root 权限。 Runtime.exec() 的文档表示每个调用都作为单独的进程运行。因此,对 execchownchmodmv 的所有后续调用都以当前的应用程序进程 - 因此失败。

您也许可以编写一个小型 shell 脚本,然后将该 shell 脚本作为参数传递给 su。然后所有命令都将以 root 权限运行。

My guess is that only the first call to Runtime.exec():

r.exec("su");

acheives root. The docs for Runtime.exec() say that each invocation runs as a separate process. So all of the subsequent calls to exec, for chown, 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.

故事与诗 2024-12-18 16:02:13

好的,我找到了如何使用 root 权限执行多个命令。诀窍是将所有命令发送到一个 su 进程:

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();
Process suProcess = r.exec("su");
DataOutputStream dos = new DataOutputStream(suProcess.getOutputStream());
dos.writeBytes("chown 0.0 " + filename + "\n");
dos.flush();
dos.writeBytes("chmod 644 " + filename + "\n");
dos.flush();
dos.writeBytes("mv " + filename + " /system/myfile.test\n");
dos.flush();

剩下的唯一一件事(不包括上面的代码)是使 /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:

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();
Process suProcess = r.exec("su");
DataOutputStream dos = new DataOutputStream(suProcess.getOutputStream());
dos.writeBytes("chown 0.0 " + filename + "\n");
dos.flush();
dos.writeBytes("chmod 644 " + filename + "\n");
dos.flush();
dos.writeBytes("mv " + filename + " /system/myfile.test\n");
dos.flush();

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文