views:

92

answers:

3

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!

A: 

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"

kurast
"Only available when the device is running in manufacturer test mode." That's probably not going to help him.
Joshua
+1  A: 

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?

Elliott Hughes
My overall aim is to write to /sys/class/htc_accessory/fm/flag on an HTC device. So my understand is it's not a regular file, as you said.
bdls
right, so you'll need to read the kernel source that implements that file to find out how you're supposed to use it.
Elliott Hughes
A: 

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.

zevoid

related questions