Reputation: 21765
I'm trying to execute a command using su
and looking at some of the question
here,
I decided to try the following methods:
var command = "su -c 'touch /mnt/extsd/somefile'"; //Could be any command
using (var process = Java.Lang.Runtime.GetRuntime().Exec(command)) { }
In this case I'm trying to execute the command by passing it directly to su
,
on my target device that means I have to pass the -c parameter, other devices do not require this.
While this does not throw an exception of any kind (not in the app, not in the system),
it also doesn't actually do anything. It's simply being ignored.
var command = "touch /mnt/extsd/somefile";
using (var su = Java.Lang.Runtime.GetRuntime())
using (var process = su.Exec("su"))
using (var writer = new StreamWriter(process.OutputStream))
{
writer.WriteLine(command);
}
No matter what the actuall command is, the result is always the same:
06-20 22:46:02.614 E/su-binary(27808): ----su-----
06-20 22:46:02.794 W/System.err(22894): java.io.SyncFailedException: fsync failed: EINVAL (Invalid argument)
06-20 22:46:02.794 W/System.err(22894): at java.io.FileDescriptor.sync(FileDescriptor.java:77)
06-20 22:46:02.804 W/System.err(22894): at java.io.FileOutputStream.flush(FileOutputStream.java:194)
06-20 22:46:02.804 W/System.err(22894): at dalvik.system.NativeStart.run(Native Method)
06-20 22:46:02.804 W/System.err(22894): Caused by: libcore.io.ErrnoException: fsync failed: EINVAL (Invalid argument)
06-20 22:46:02.804 D/TouchD ( 1471): Entering CheckVIDinlsusb()
06-20 22:46:02.814 D/TouchD ( 1471): Command busybox lsusb | grep 0eef output to /dev/null failed.
What's causing this issue and is there any way to resolve this?
Upvotes: 1
Views: 942
Reputation: 5340
Edit: Removed original incorrect answer
It seems you need to read the output of the process like this:
var command = "touch '/mnt/extsd/someFile'";
using (var process = new JL.ProcessBuilder().Command("su", "-c", command)
.RedirectErrorStream(true)
.Start())
{
var buffer = new byte[256];
var read = 0;
do
{
read = process.InputStream.Read(buffer, 0, buffer.Length);
Console.WriteLine(ASCIIEncoding.ASCII.GetString(buffer, 0, read));
} while (read > 0);
}
It's important the parameters to the second command (touch in this example) are enclosed within quotes. In order to avoid namespace conflicts the following using statement has been added:
using JL=Java.Lang;
Reference: http://developer.android.com/reference/java/lang/Process.html
Upvotes: 2