Broken pipe error when executing Android method more than once?

2.4k views Asked by At

I'm trying to execute the following method to spoof the MAC address of an android device. The method runs every time the user clicks a button:

public void changeMac(View v) throws IOException {
        editText = (EditText)findViewById(R.id.newMacText);
        String mac = editText.getText().toString();
        Log.d("my log", "" + mac);
        String[] cmds = {"ip link set wlan0 address " + mac};

        Log.d("my log", "in method");
        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();


        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(p.getInputStream()));

        StringBuilder result = new StringBuilder();
        String line = "";
        while ((line = bufferedReader.readLine()) != null) {
            result.append(line);
        }

       Log.d("my log", "new mac " + Utils.getMACAddress("wlan0"));
        Log.d("my log", result.toString());

    }

Now when the user clicks the button again, the app crashes and I get "broken pipe" error.

 android.system.ErrnoException: write failed: EPIPE (Broken pipe)

and also an illegal state exception. What can I do to make this error stop happening and let the method be called multiple times?

3

There are 3 answers

2
Robert On BEST ANSWER

When you execute the command os.writeBytes("exit\n"); this ends your su session. The su process ends itself and the pipe your are using for writing commands to the su shell gets broken.

Therefore if you want to execute another command you have to restart a new su session or do not close the old one calling exit.

Anyway I would not start the su process in your onCreate() method. SuperUser permissions should be acquired only when needed and not on app start.

0
Puneet Kumar On

why you are executing same code again and again you can save the mac address by running code one time like we create a session if you change the device then in that case you have to check the mac address again.

1
Gordak On

I doubt that you can change the MAC address on any device.

From wikipedia:

MAC addresses are most often assigned by the manufacturer of a network interface controller (NIC) and are stored in its hardware, such as the card's read-only memory or some other firmware mechanism