4

I have an compiled executable that is supposed to copy itself from the res folder, and into the /data/data/package-name/ folder, and change the permissions, and then execute. Every step completes all the way to the end. The output stream seems to be writing, etc. Except when I go check the file system, nothing has been done. I first tried with 'sh' then with 'su' (I have a rooted Cyanogen rom).

Here is the code:

public class UnexecutableActivity extends Activity {

    String executablePath;
    TextView outputView;
    private UnexecutableTask mUnexecutableTask;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        executablePath = getIntent().getData().getPath();
        System.out.println(executablePath);
        setContentView(R.layout.main);
        outputView = (TextView)findViewById(R.id.outputView);
        try {
            Process process = Runtime.getRuntime().exec("su");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Runnable runexecutable = new Runnable(){

            @Override
            public void run() {
                mUnexecutableTask = new UnexecutableTask();
                mUnexecutableTask.execute("");
            }

        };

        runOnUiThread(runexecutable);

    }


    private class UnexecutableTask extends AsyncTask<String, String, String> {



        public UnexecutableTask() {
            super();
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            outputView.setText(outputView.getText() + "\n" + executablePath + "converted to ");
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            outputView.setText("About to un-executable " + executablePath + " ...");
        }

        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            outputView.setText(outputView.getText() + "\n" + values[0]);
        }

        @Override
        protected String doInBackground(String... params) {
        String bashEscapedPath = executablePath.replace(" ", "\\ ");
        try{
            String[] commands;
            publishProgress("Loading unexecutable...");
            InputStream unexecutableInputStream = getAssets().open("unexecutable");
            FileOutputStream fos = new FileOutputStream(getDir("", MODE_WORLD_WRITEABLE) + "/unexecutable");
             byte[] tmp = new byte[2048];
                int l;
                while ((l = unexecutableInputStream.read(tmp)) != -1) {
                    fos.write(tmp, 0, l);
                }
                fos.flush();
                fos.close();
                unexecutableInputStream.close();

            publishProgress("Changing file permissions...");
            commands = new String[] {"/system/bin/chmod","744", getDir("", MODE_WORLD_WRITEABLE) + "/unexecutable"};
            Process process = Runtime.getRuntime().exec("su");
            StringBuffer res = new StringBuffer();
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            DataInputStream osRes = new DataInputStream(new
                    BufferedInputStream(process.getInputStream()));
            for (String single : commands) {
               os.writeBytes(single + "\n");
               os.flush();
               //publishProgress(String.valueOf(osRes.readByte()));
            }
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();


            publishProgress("Performing un-executable...");
            commands = new String[] {"/data/data/" + getPackageName() + "/unexecutable", bashEscapedPath};
            process = Runtime.getRuntime().exec("su");
            res = new StringBuffer();
            os = new DataOutputStream(process.getOutputStream());
            osRes = new DataInputStream(process.getInputStream());
            for (String single : commands) {
               os.writeBytes(single + "\n");
               os.flush();
            }
            os.writeBytes("exit\n");
            os.flush();
            publishProgress("Finishing...");
            process.waitFor();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return "Success";
    }

  }

If anyone could fix this up for me, (and hopefully use sh!) I would be eternally grateful.

5
  • I am not so versed with pastebin - could you make that a link please. Commented Aug 12, 2010 at 17:09
  • why dont you just put the code here? SO supports syntax highlighting and such. Commented Aug 12, 2010 at 17:13
  • 1
    what version of CM are you running because chmod doesn't exist in my /system/bin. its in /system/xbin/ - im running 5.0.8 Commented Aug 12, 2010 at 17:17
  • 1
    well my phone's bricked, so now I don't have a rooted phone anymore. Hopefully I wont need su? Commented Aug 12, 2010 at 17:39
  • re-rooted and rommed my phone :) Any ideas? Commented Aug 13, 2010 at 0:54

2 Answers 2

1

Not you should not use shell commands. The SDK does not include any shell commands, and you can not rely on these working consistently across devices. You definitely can't rely on su working across devices. :}

Sign up to request clarification or add additional context in comments.

1 Comment

Plenty of Android applications specify the need for having root access, and are very successful on the Android market. I don't think you even need su in this case, because apps have full access to run write execute in the /data/data/app-package directory
0
    executablePath = getIntent().getData().getPath();

This line is giving a null pointer exception for me. Apparently the URI (java.net.Uri) returned by getIntent().getData is null. I am trying to work around it and see if I can create the file with the rest of your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.