Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
422 views
in Technique[技术] by (71.8m points)

android - How do I fix java.io.IOException: write failed: EPIPE (Broken pipe) when I am rooted?

I am running my code on a rooted Nexus, Android 6.0.1, with my apk as a system app (i.e. android:sharedUserId="android.uid.system" in system/priv-app. Since it is a system app, and I am rooted, I do not understand why I cannot run my su commands. Let me iterate, I can run su and rm -f /data/data/com.google.android.gms/shared_prefs/adid_settings.xml through an adb shell successfully.

This is what I am trying to run:

public void reset_GAID_3() throws Exception {
        try{
            Process su = Runtime.getRuntime().exec("su");
            DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

            outputStream.writeBytes("rm -f /data/data/com.google.android.gms/shared_prefs/adid_settings.xml
");
            outputStream.flush();

            outputStream.writeBytes("exit
");
            outputStream.flush();
            su.waitFor();
            outputStream.close();
        }catch(IOException e){
            throw new Exception(e);
        }catch(InterruptedException e){
            throw new Exception(e);
        }
}

There error is occuring at: outputStream.writeBytes("rm -f /data/data/com.google.android.gms/shared_prefs/adid_settings.xml ");

Here is the stacktrace:

Caused by: android.system.ErrnoException: write failed: EPIPE (Broken pipe)
        at libcore.io.Posix.writeBytes(Native Method)
        at libcore.io.Posix.write(Posix.java:271)
        at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313)
        at libcore.io.IoBridge.write(IoBridge.java:493)
        at java.io.FileOutputStream.write(FileOutputStream.java:186)?
        at java.io.OutputStream.write(OutputStream.java:82)?
        at java.io.DataOutputStream.writeBytes(DataOutputStream.java:156)?
        at com.example.adtry3.MainActivity.reset_GAID_3(MainActivity.java:363)?
        at com.example.adtry3.MainActivity.reset_GAID_button(MainActivity.java:336)?
        at java.lang.reflect.Method.invoke(Native Method)?
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)?
        at android.view.View.performClick(View.java:5204)?
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:992)?
        at android.view.View$PerformClick.run(View.java:21156)?
        at android.os.Handler.handleCallback(Handler.java:739)?
        at android.os.Handler.dispatchMessage(Handler.java:95)?
        at android.os.Looper.loop(Looper.java:148)?
        at android.app.ActivityThread.main(ActivityThread.java:5422)?
        at java.lang.reflect.Method.invoke(Native Method)?
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)?
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)?

My understanding is that I have an error because I am not executing the rm in a superuser instance, but I thought that I am. I attempted to change it to a single command:

su -c 'rm -f /data/data/com.google.android.gms/shared_prefs/adid_settings.xml'

This did not work. Any ideas where I am going wrong? Thanks.

question from:https://stackoverflow.com/questions/65909973/how-do-i-fix-java-io-ioexception-write-failed-epipe-broken-pipe-when-i-am-ro

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Not quite the best answer in the world, but I fixed it by removing the su instance and running it through a ProcessBuilder instead.

public void reset_GAID_3() throws Exception {
        ProcessBuilder su = new ProcessBuilder("rm", "-f", "/data/data/com.google.android.gms/shared_prefs/adid_settings.xml");
        try{
            Process pc = su.start();
            System.out.println("Ran SU cmd");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Failed SU cmd");
        }

    }

I am sure there was a problem with how the code was being executed after the su command, but this is a way around it. Since the su is what seemed to be causing the problem, I simply removed it. Removing it means I could use a simple ProcessBuilder to get what I needed done.

Why did this still work? Since this apk is located in system/priv-app, and it has android:sharedUserId="android.uid.system" in the Manifest, this means it's already operating with root permissions, so it would be redundant to use su.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...