【发布时间】:2011-03-29 01:32:31
【问题描述】:
我有一个编译后的可执行文件,它应该从 res 文件夹中复制自身,并将其复制到 /data/data/package-name/ 文件夹中,并更改权限,然后执行。每一步都完成到最后。输出流似乎正在写入等。除了我去检查文件系统时,什么都没有做。我首先尝试使用“sh”,然后使用“su”(我有一个根深蒂固的 Cyanogen rom)。
代码如下:
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";
}
}
如果有人能为我解决这个问题,(并希望使用 sh!)我将永远感激不尽。
【问题讨论】:
-
我对 pastebin 不太熟悉 - 你能把它做个链接吗?
-
为什么不把代码放在这里? SO支持语法高亮等。
-
您正在运行什么版本的 CM,因为我的 /system/bin 中不存在 chmod。它在 /system/xbin/ - 我正在运行 5.0.8
-
好吧,我的手机已经变砖了,所以现在我没有手机了。希望我不需要 su?
-
重新扎根并盗用我的手机 :) 有什么想法吗?