开发者

/etc/hosts file manipulation programmatically in android

Though I've write some small applications with android before, now I trying to write a root application and having some problems regarding file permissions.

Basically, my aim is to manipulate /etc/hosts file with a Java application. As my first action, I've gained root privileges with runtime.exec("su") and tried to open hosts file with standard ways like using File() or FileOutputStream(). But unsuccessfully, my application threw the following exception "java.io.FileNotFoundException: /etc/hosts (Permission denied)". Then I checked the permissions for file via accessing shell, and it was like rw-r--r-- for root. Even if it supposed to work with this permissions, I changed it to 777 from shell and voila! my application started to open file without any problem. However, since android replaces the hosts file with default version time to time, it also changes the permission for the file and with previous method, each time I have t开发者_如何学Pythono go and change permission from adb and surely this is not I want. So, step by step;

-I tried runtime.exec("chmod 777 /etc/hosts") method and this didn't work without any exception or error message.

-Then I tried to use File.setWritable() method but it doesn't exist in my andoid system, though it seems supposed to be android 2.2 according to javadoc.

-Lastly, I tried to set working directory with System.setProperty("user.dir","/etc/") and use openFileOutput("hosts", MODE_WORLD_WRITABLE) but of course this didn't work, either. Now, I'm all out of any ideas right now. What is the way to manipulate system files in android? regards,


I've gained root privileges with runtime.exec("su")

Not for your java code you haven't. What su does is let you launch a stand alone native executable as root. It does not change the uid of an existing process, such as the dalvik vm running your code.

Even if it supposed to work with this permissions, I changed it to 777 from shell and voila! my application started to open file without any problem.

Yes, you did the privileged task from a privileged process, and then your non-privileged java app could take care of the followup.

However, since android replaces the hosts file with default version time to time, it also changes the permission for the file and with previous method, each time I have to go and change permission from adb and surely this is not I want.

This suggests that changing the hosts file yourself is a bad idea, as you could end up in a revert war with Android (though why Android would try changing a file located on a normally read-only file system is a bit mysterious)

What is the way to manipulate system files in android? regards,

Short answer: you don't

Long answer: you take time understand how Android wants to use them, and you change this behavior of Android itself in a modified version. (you might start with a grep of the sources for the file name)


If you really must edit the hosts file (which is a bad idea, as stated in other answer(s)) you find it under /system/etc/hosts and do that via adb pull, editing the file on your computer, adb remount and adb push. Files in the / partition are mounted with a NAND lock, that would have to be lifted by a custom rom, which means that your app would then probably only work with one special rom that does that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜