[英]SyncFailedException when trying to execute a command using su
I'm trying to execute a command using su
and looking at some of the question here, 我正在尝试使用
su
执行命令,并在此处查看一些问题,
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
, 在这种情况下,我试图通过将命令直接传递给
su
来执行命令,
on my target device that means I have to pass the -c parameter, other devices do not require this. 在我的目标设备上,这意味着我必须传递-c参数,其他设备不需要此参数。
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? 是什么导致此问题,并且有什么方法可以解决此问题?
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. 重要的是第二个命令的参数(在此示例中为touch)用引号引起来。 In order to avoid namespace conflicts the following using statement has been added:
为了避免名称空间冲突,添加了以下using语句:
using JL=Java.Lang;
Reference: http://developer.android.com/reference/java/lang/Process.html 参考: http : //developer.android.com/reference/java/lang/Process.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.