Android:写入/sys/目录

发布于 2024-09-14 09:40:24 字数 712 浏览 4 评论 0原文

我正在开发一个不打算作为标准应用程序发布的应用程序。它将在单个 root 设备上运行。

我需要能够写入 /sys/ 目录中的文件。有问题的文件由 root 所有并具有 -rw-rw-rw- 权限。

我知道我的代码运行的虚拟机可能存在限制,禁止写入文件系统的该区域,但我观察到另一个应用程序显然这样做了。

这可能吗?如何实现?手机是否需要root(我正在使用的开发版是)并不重要。


我研究过简单地使用 FileWriter 写入文件,导致 .flush() 出现以下错误:

java.io.IOException: Invalid argument
at org.apache.harmony.luni.platform.OSFileSystem.writeImpl(Native Method)

我还尝试从 Java 执行 shell 命令,无论是否带有“su ”。分别返回“请求被拒绝”和“权限被拒绝”。

Process process = Runtime.getRuntime().exec("echo 'hello' > /sys/file");

最后,我尝试使用 NDK 和 JNI,以防 C 神奇地设法写入此文件。当尝试fflush到此文件时,我收到一个EOF(意味着发生了错误)。


非常欢迎所有建议!

I'm developing an application that is not intended to be released as a standard app. It'll be run on a single rooted device.

I require the ability to write to a file in the /sys/ directory. The file in question is owned by root and has -rw-rw-rw- permissions.

I am aware that there may be restrictions on the VM my code runs within that prohibit writing to this area of the file system, but I have observed another application apparently do this.

Is this possible? How might it be achieved? It doesn't matter if the phone needs to be rooted (the dev one I'm using is).


I've looked into simply using a FileWriter to write to the file, causing the following error on .flush():

java.io.IOException: Invalid argument
at org.apache.harmony.luni.platform.OSFileSystem.writeImpl(Native Method)

I've also tried executing a shell command from Java, both with and without "su". Which returned "request rejected" and "Permission Denied" respectively.

Process process = Runtime.getRuntime().exec("echo 'hello' > /sys/file");

Lastly I've tried using the NDK and JNI in case C some how magically managed to write to this file. When attempting to fflush to this file I receive an EOF (meaning an error has occured).


All suggestions greatly welcome!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

橘香 2024-09-21 09:40:24

Runtime.getRuntime().exec(String) 方法只是将输入字符串视为可执行文件的文件名。它不会接受您的 echo 命令。
您应该使用 Runtime.getRuntime().exec(String[]) 代替,例如:

String[] cmd = { "/system/bin/sh", "-c", "echo xxx > /sys/somefile", };
Runtime.getRuntime().exec(cmd);

或者,要将某些内容写入 sys 文件,您应该使用 java.io.PrintWriter 类。
请参阅 http://www. netmite.com/android/mydroid/frameworks/base/core/java/android/os/Debug.java 并搜索“startNativeTracing()”以获取示例。

当然,只有当 sys 文件对于您的应用程序可写时,上述所有内容才有效。

The method Runtime.getRuntime().exec(String) simply treats the input string as a file name of an executable. It won't accept your echo command.
You should use the Runtime.getRuntime().exec(String[]) instead, like:

String[] cmd = { "/system/bin/sh", "-c", "echo xxx > /sys/somefile", };
Runtime.getRuntime().exec(cmd);

Alternatively, to write something to a sys file, you should use java.io.PrintWriter class.
Please refer to http://www.netmite.com/android/mydroid/frameworks/base/core/java/android/os/Debug.java and search for 'startNativeTracing()' for an example.

Of cause, all of above can only work if the sys file is writable for your application.

成熟稳重的好男人 2024-09-21 09:40:24

我认为您必须分享实际的代码并告诉我们您真正想要做什么。 “无效参数”对应于 write(2) 因 EINVAL 失败,当您尝试写入不合适的内容时会发生这种情况。不是权限问题;一个类型的东西。这个文件不是普通文件,是吗?

i think you're going to have to share the actual code and tell us what you're really trying to do. "Invalid argument" corresponds to write(2) failing with EINVAL, which happens when you're trying to write to something unsuitable. not a permissions thing; a type thing. this file isn't a regular file, is it?

蓝礼 2024-09-21 09:40:24

您必须修改您的清单,以便您的程序要求 root 用户访问权限。

http://developer.android.com/reference/android/Manifest.permission。 html

公共静态最终字符串 FACTORY_TEST
自以下版本起: API 级别 1

作为制造商测试应用程序运行,以 root 用户身份运行。仅当设备在制造商测试模式下运行时可用。
常量值:“android.permission.FACTORY_TEST”

You have to modify your manifest so your program asks for root user acess.

http://developer.android.com/reference/android/Manifest.permission.html

public static final String FACTORY_TEST
Since: API Level 1

Run as a manufacturer test application, running as the root user. Only available when the device is running in manufacturer test mode.
Constant Value: "android.permission.FACTORY_TEST"

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