简体   繁体   English

尝试使用su执行命令时出现SyncFailedException

[英]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.

相关问题 SU命令用于更新 - SU command for updates Mysql连接器.Net:尝试使用参数执行命令时的FormatException。 - Mysql Connector .Net: FormatException when trying to execute a command with parameters. “此命令不可用。” 尝试执行 PasteAndFormat 函数时发生错误 - "This command is not available." error occurs when trying to execute PasteAndFormat function vs Addin尝试执行命令失败 - vs Addin failing trying to Execute command 尝试使用C#执行命令行脚本时获取System.InvalidOperation异常 - Getting System.InvalidOperation exception while trying to execute command line script using C# 由mono命令执行时发生异常 - Exception on when execute on by mono command 尝试执行生成的代码时发生异常 - Exception when trying to execute generated code NHibernate尝试在TransactionScope.Dispose上执行SQL命令 - NHibernate trying to execute a SQL Command on TransactionScope.Dispose 使用 xaml 绑定和视图模型执行带参数的命令 - Execute command with parameter using xaml binding and viewmodel 使用 EF Core 多次执行命令 - Execute a command multiple times using EF Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM