2

How can I, within my application, edit a file in the /system/ directory?

Do I have to make the system R/W accessible?

I ve tried:

process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes("mount -o remount,rw /system\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();

and many other ways, without success.

If anybody can help me, I'd greatly appreciated it! :)

Also, if i finally made it, will it worked with all rooted phones? Or is it different with some phones?

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
Paschalis
  • 11,929
  • 9
  • 52
  • 82
  • This is generally not something one should be attempting to do from an android application. But to do it, the file system must be remounted writeable, and then either the file must be modified from a root process (which an android app is not, though it may be able to launch a slave process as root), or have it's permissions changed by a root process to be subsequently writable by a non-root process such as an android app. – Chris Stratton Nov 20 '11 at 05:13

2 Answers2

6

I use:

    os.writeBytes("mount -o remount rw /system/\n"); 
    //instead of a comma, I have a space. 
    //When I tried it with a comma, mine didn't work either.

And that allows me to successfully mount.

If you exit right after that, of course it will not work. You have to stay within the same process and use the linux commands to edit the file.

I have no idea how to edit the files, but I suggest googling how to do things in linux terminal, and then putting the proper code in os.writeBytes("CODE_HERE");

Though, as far as the mounting process is concerned, I don't know if that command will work universally. It may just fortunately work on my device.

EDIT:
I now use RootTools: http://code.google.com/p/roottools/downloads/list
And here is the Wiki page:
http://code.google.com/p/roottools/w/list

But I now am using:

RootTools.remount(file, mountType);
//For example:
RootTools.remount("/system/", "rw");

I believe that is universal

Reed
  • 14,703
  • 8
  • 66
  • 110
  • 1
    I implemented this. its much bigger code actually. And to make it universal you have find runtime each device specific mount command, because it depents where /system is. And make it not depend on busybox , because maybe somebody has his busybox on old version etc etc. Thanks for replying. – Paschalis Nov 20 '11 at 00:01
  • 1
    You're right. I figured it wasn't universal. I updated my answer, and I believe RootTools handles the whole process. Even if not, you also have `RootTools.getMounts()`. The RootTools are great, though. I found out about them on XDA. – Reed Nov 20 '11 at 04:02
1

Edit: All version of codes below DOES NOT mount system as RW. *Read comments below to see why. Solution of this is not a simple command.

Edit1: I went on Super User apk, Settings tab, and "tapped" at the last item, to update the su binary. With that update, everything below isnt working.

Edit2: started a whole conversation with my self here. Fix for the current latest binary is at the bottom of the post

==================================================================================

Found out how to do it! Second day of efforts, and finally found it!!!!! Tried several things, and answer was to a simple change mode,

what i have done:

First Version Code:(doesnt work)

String[] mountRW = { "su", "-c",
"chmod  777 /system/etc/build.prop"};

String[] mountRO = {"su", "-c",
"chmod  755 /system/etc/build.prop"};

//TODO REMOVE testing purposes          
File file2 = new File("/system/build.prop");

//Make file Read-Write
process = Runtime.getRuntime().exec(mountRW);
process.waitFor();

//TODO REMOVE testing purposes
Log.d("MOUNT RW?", "RW WRITABLE? "+ file2.canWrite());

///////////////////////
// process the file
//////////////////////

// After editing finish,
//make Read Only file again
process = Runtime.getRuntime().exec(mountRO);
process.waitFor();

//TODO REMOVE
Log.d("MOUNT RO?", "RO WRITABLE? "+ file2.canWrite());

I didnt paste some try catch cases. Also i got another problem.. And i solved it in Version 2. THe little problem was, that, i was asking for a specific for a su command, and the user, had to accept SU cmd for RO, SU cmd for RW.. and another time for other stuff in my program. In 2nd version i m using the generic su command, so user has to accept SU privileges only ONE time, and i m using output stream.

Code Version 2(Recomended) (doesnt work):

String mountRW = "chmod  777 /system/build.prop";
String mountRO = "chmod  755 /system/build.prop";

//TODO REMOVE
File file2 = new File("/system/build.prop");

//Make file Read-Write
process = Runtime.getRuntime().exec("su"); //Generic SU Command
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(mountRW + " \n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();


//TODO REMOVE
Log.d("MOUNT RW?", " RW WRITABLE? "+ file2.canWrite());

////////////////////////////
/// mod the file
///////////////////////////

// After editing finish, make Read Only file again
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(mountRO + " \n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();

//TODO REMOVE
Log.d("MOUNT RO?", "RO WRITABLE? "+ file2.canWrite());
  • Both codes require Root on your device.
  • Both versions doesnt include catch cases. (Eclpise will found them for you)
  • Check out your logcat(adb logcat), to see that indeed it works!
  • With latest su binary, this code changes slightly. The change mode command requires 4 digits. 0777 for rw permissions, and 0755 for ro permissions!
  • This code by its own, it does nothing to your device.

Only it mounts built.prop RW, and then mounts it back to RO. Although if you change it, you may brick your device! Take care!

Paschalis
  • 11,929
  • 9
  • 52
  • 82
  • You are not "mounting" anything, and as a result this code will not normally work. Ordinarily, you must remount the /system partition writeable, probably you did in a previous try. Your command strings that you erroneously term "mountRW" & "mountRO" do not accomplish mounting, but instead change the file permissions so the non-root process running your java code can change the file. You should re-name these to "chmodRW" or something like that. And if you want it to be generally useful (to the extent anything requiring root can be general) you will need to handle the remounting of /system – Chris Stratton Aug 20 '11 at 17:07
  • Hello Chris! Thanks for spending your time answering. The Ver2, worked well on the su binary version i had on my superuser.apk on my device. But i update the su binary to the latest, and it failed. Now i figured out again. The chmod cmd needs 4 digits to work. eg "0777" for mounting. This code it's like a prototype, that i want to share, because it took me lot of time and effort to figure. It's not what i m actually doing in my program. Isnt it good, that i m not mounting RW the whole system, but a specific file i want to mod? And then give back RO permission? I'd like to have your opinion!:) – Paschalis Aug 20 '11 at 17:35
  • @Psachialis. You are mistaken. "chmod" does not "mount" anything. Your code solves a different problem of letting a non-root process modify a protected file on an already writeable file system, and is only sufficient because you have already mounted the entire /system writeable at some previous time since the last reboot. Power cycle your device you should find that it will then fail (unless you have a nonstandard and poorly designed device that normally has /system mounted writeable) – Chris Stratton Aug 20 '11 at 17:38
  • Yes! you are 100% right! That worked for me, after every time i edited the specific file with root explorer. And chmod made work. After i did a restart, it didnt work. Can you please tell me how to mount the system? to edit a file? And if it's possible, a universal version. That will work with all rooted phones. Thanks again for your help, and your patience, pointing out my mistake! – Paschalis Aug 20 '11 at 18:41
  • A universal solution will be tricky http://stackoverflow.com/questions/5467881/a-terminal-command-for-a-rooted-android-to-remount-system-as-read-write – Chris Stratton Aug 20 '11 at 20:58
  • @ChrisStratton Check out the Root Tools: http://code.google.com/p/roottools/ They should help. I believe it handles the whole mounting process. – Reed Nov 20 '11 at 04:03