public static SdbRunResult RunSdbCommand(SDBDeviceInfo device, string command, OutputDataProcessor outputDataProcessor, TimeSpan?timeout = null) { int exitCode; return(RunSdbCommand(device, command, outputDataProcessor, out exitCode, timeout)); }
public static bool RunSdbCommandAndGetError(SDBDeviceInfo device, string shellCommand, OutputDataProcessor outputDataProcessor, out string errorString, TimeSpan?timeout = null) { int exitResult = 0; string nonEmptyLines = ""; errorString = ""; SDBLib.SdbRunResult sdbResult = RunSdbCommand(device, shellCommand, (bool isStdOut, string line) => { if (line != "") { nonEmptyLines += line; } return(false); }, out exitResult, timeout); if (exitResult != 0) { sdbResult = SdbRunResult.OtherError; errorString = nonEmptyLines; } return(sdbResult == SdbRunResult.Success); }
public static bool RunSdbShellCommandAndCheckExitStatus(SDBDeviceInfo device, string shellCommand, OutputDataProcessor outputDataProcessor, out string errorMessage, TimeSpan?timeout = null, bool isRoot = false) { int exitCode; bool success = false; SdbRunResult sdbResult = RunSdbCommand(device, $"shell \"{shellCommand} && echo {ShellSuccess} || echo {ShellFailure}\"", (bool isStdOut, string line) => { if (line.Contains(ShellSuccess)) { success = true; return(true); } if (line.Contains(ShellFailure)) { return(true); } if (outputDataProcessor != null) { if (outputDataProcessor(isStdOut, line)) { success = true; return(true); } } return(false); }, out exitCode, timeout, isRoot); if (sdbResult == SdbRunResult.Success) { if (!success) { errorMessage = "Command failed"; return(false); } errorMessage = ""; return(true); } errorMessage = "Cannot run command. " + FormatSdbRunResult(sdbResult, exitCode); return(false); }
/// <summary> /// Run the specified command using SDB and process its output using outputDataProcessor (if its not null). /// The outputDataProcessor delegate returns true to indicate it got some expected result so the function /// may exit (the command continues to work), and false if the result has not been obtained yet so the delegate /// "wants" to continue receiving the command output. /// </summary> /// <param name="command">the command to run (e.g. 'shell \"ls /usr\"')</param> /// <param name="outputDataProcessor">the delegate which processes the output of the command and returns /// true to stop (in this case the function exits but the command continues to work) or false to continue</param> /// <param name="outputLog">only if outputDataProcessor == null: copy process output to Tizen pane</param> /// <param name="exitCode">if SDB process has finished before the function returns then the SDB process exit code /// is copied to this out parameter else it's equal to -1</param> /// <param name="timeout">the timeout: if it elapses before outputDataProcessor returned true or the /// command finished then the function returns Timeout (the command continues to work); /// the default value is 30 seconds</param> /// <param name="isRoot">whether to execute command from root</param> /// <returns> /// if outputDataProcessor is not null: /// Success if outputDataProcessor returned true, or another SdbCommandResult value otherwise (if an error /// occurred or the command finished or the timeout elapsed); /// if outputDataProcessor is null: /// Success if the command finished before the timeout elapsed, or another SdbCommandResult value otherwise. /// </returns> public static SdbRunResult RunSdbCommand(SDBDeviceInfo device, string command, OutputDataProcessor outputDataProcessor, out int exitCode, TimeSpan?timeout, bool isRoot) { if (isRoot) { if (!SwitchToRoot(device, true)) { exitCode = -1; return(SdbRunResult.OtherError); } } try { return(RunSdbCommand(device, command, outputDataProcessor, out exitCode, timeout)); } finally { if (isRoot) { SwitchToRoot(device, false); } } }
public static SdbRunResult RunSdbCommand(SDBDeviceInfo device, string command, OutputDataProcessor outputDataProcessor, out int exitCode, TimeSpan?timeout = null) { Debug.Assert((timeout == null) || (timeout >= TimeSpan.Zero)); exitCode = -1; TimeSpan effectiveTimeout = timeout ?? TimeSpan.FromSeconds(30); // the default timeout is 30 seconds using (ProcessProxy process = CreateSdbProcess(true, false)) { if (process == null) { return(SdbRunResult.CreateProcessError); } process.StartInfo.Arguments = DeviceManager.AdjustSdbArgument(device, command); Debug.WriteLine("{0} RunSdbCommand command '{1}'", DateTime.Now, process.StartInfo.Arguments); if (outputDataProcessor != null) { object eventsGuard = new object(); var gotOutputEvent = new ManualResetEvent(false); try { bool stopped = false; // should be volatile actually but it's not allowed for locals process.OutputDataReceived += (sender, args) => { if (!stopped && (args.Data != null)) { lock (eventsGuard) { if (outputDataProcessor == null) { return; } if (outputDataProcessor(true, args.Data)) { gotOutputEvent.Set(); stopped = true; } } } }; process.ErrorDataReceived += (sender, args) => { if (!stopped && (args.Data != null)) { lock (eventsGuard) { if (outputDataProcessor == null) { return; } if (outputDataProcessor(false, args.Data)) { gotOutputEvent.Set(); stopped = true; } } } }; if (!RunProcess(process)) { return(SdbRunResult.RunProcessError); } process.BeginOutputReadLine(); process.BeginErrorReadLine(); Stopwatch watch = Stopwatch.StartNew(); do { try { if (process.WaitForExit(0)) { process.WaitForExit(); // wait until redirected stdin/stdout streams are processed exitCode = process.ExitCode; return(SdbRunResult.Success); } } catch (Exception ex) { Debug.WriteLine(ex.Message); return(SdbRunResult.OtherError); } }while (watch.Elapsed < effectiveTimeout); if (!process.HasExited) { process.Kill(); } } finally { lock (eventsGuard) { outputDataProcessor = null; } gotOutputEvent.Dispose(); } } else // outputDataProcessor == null { if (!RunProcess(process)) { return(SdbRunResult.RunProcessError); } try { double timeoutMilliseconds = effectiveTimeout.TotalMilliseconds; if (process.WaitForExit((timeoutMilliseconds <= int.MaxValue) ? (int)timeoutMilliseconds : int.MaxValue)) { exitCode = process.ExitCode; return(SdbRunResult.Success); } } catch (Exception ex) { Debug.WriteLine(ex.Message); return(SdbRunResult.OtherError); } } } return(SdbRunResult.Timeout); }
public static bool RunSdbShellSecureCommand(SDBDeviceInfo device, string secureCommand, OutputDataProcessor outputDataProcessor, out string errorString, TimeSpan?timeout = null) { return(RunSdbCommandAndGetError(device, $"shell 0 {secureCommand}", outputDataProcessor, out errorString, timeout)); }