Reputation: 39
My device is rooted. I'm getting events through adb. But dn't know how to fetch same continuous stream of events in android application. Below bunch of code ask prompt for SuperUser permission but dnt fetch anything.
Process process = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
os.writeBytes("chmod a=rw /dev/input/event2" + "\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
process.getOutputStream().
final BufferedReader reader = new BufferedReader(newInputStreamReader(process.getInputStream()));
int read;
final char[] buffer = new char[4096];
while ((read = reader.read(buffer)) > 0) {
stringBuilder.append(buffer, 0, read);
tv.setText(stringBuilder.toString());
}
reader.close();
// Waits for the command to finish.
process.waitFor();
}
Upvotes: 0
Views: 872
Reputation: 365157
Are you trying to feed su
some commands on its stdin or something? Why not just su -c 'command1; command2'
.
Or do you think that .exec("su")
is going to raise the privileges of the current process? That's not how it works.
When you su
or sudo
from the command line, the process tree looks like this:
bash───sudo───bash
(from pstree -s $$
to show the parents of the current shell).
So although you get a root prompt in the same terminal, you're actually running a different shell, after your non-root shell forks + execs su
.
So if .exec("su");
doesn't fork first, you're replacing your current process by execve()
ing su
in place of your process. I don't know those Java functions, but that might explain getting no output: the function call never returns and your process is replaced with su
with no args.
Oh, nvm that theory, I checked the docs and .exec
does run the command in a new process. So it's more like POSIX system()
than POSIX exec()
. Still, it runs a new process and doesn't give your current process root privileges. I also don't know why you're printing chmod\n
to stdout.
Upvotes: 1