Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/Xamarin.Android.Tools.AndroidSdk/Runners/EmulatorRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

Expand Down Expand Up @@ -57,7 +58,23 @@ public Process LaunchEmulator (string avdName, bool coldBoot = false, List<strin
if (additionalArgs != null)
args.AddRange (additionalArgs);

var psi = ProcessUtils.CreateProcessStartInfo (emulatorPath, args.ToArray ());
ProcessStartInfo psi;
if (OS.IsWindows) {
psi = ProcessUtils.CreateProcessStartInfo (emulatorPath, args.ToArray ());
} else {
// On Unix, launch through a shell that ignores SIGINT before exec'ing
// the emulator. This prevents Ctrl+C in the parent terminal from killing
// the emulator process. 'trap "" INT' sets SIGINT to SIG_IGN, which POSIX
// guarantees is preserved across exec:
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html
var shellCmd = new StringBuilder ("trap '' INT; exec ");
shellCmd.Append (ShellQuote (emulatorPath));
foreach (var arg in args) {
shellCmd.Append (' ');
shellCmd.Append (ShellQuote (arg));
}
psi = ProcessUtils.CreateProcessStartInfo ("/bin/sh", "-c", shellCmd.ToString ());
Comment thread
jonathanpeppers marked this conversation as resolved.
}

if (environmentVariables != null) {
foreach (var kvp in environmentVariables)
Expand Down Expand Up @@ -317,5 +334,9 @@ async Task<EmulatorBootResult> WaitForFullBootAsync (
cancellationToken.ThrowIfCancellationRequested ();
return new EmulatorBootResult { Success = false, ErrorKind = EmulatorBootErrorKind.Cancelled, ErrorMessage = "Boot cancelled." };
}

/// Quotes a string for safe use in a POSIX shell command.
/// Wraps in single quotes and escapes embedded single quotes.
static string ShellQuote (string arg) => "'" + arg.Replace ("'", "'\\''") + "'";
}

Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ public void LaunchEmulator_ThrowsOnWhitespaceAvdName ()
Assert.Throws<ArgumentException> (() => runner.LaunchEmulator (" "));
}

// --- BootEmulatorAsync tests (ported from dotnet/android BootAndroidEmulatorTests) ---

[Test]
public async Task AlreadyOnlineDevice_PassesThrough ()
{
Expand Down Expand Up @@ -205,7 +203,7 @@ public async Task LaunchFailure_ReturnsError ()
var result = await runner.BootEmulatorAsync ("Pixel_7_API_35", mockAdb, options);

Assert.IsFalse (result.Success);
Assert.That (result.ErrorMessage, Does.Contain ("Failed to launch"));
Assert.AreEqual (EmulatorBootErrorKind.LaunchFailed, result.ErrorKind);
}

[Test]
Expand Down Expand Up @@ -295,8 +293,6 @@ public async Task MultipleEmulators_FindsCorrectAvd ()
Assert.AreEqual ("emulator-5556", result.Serial, "Should find the correct AVD among multiple emulators");
}

// --- Tests ported from dotnet/android BootAndroidEmulatorTests ---

[Test]
public async Task AlreadyOnlinePhysicalDevice_PassesThrough ()
{
Expand Down Expand Up @@ -480,6 +476,36 @@ public void BootEmulatorAsync_EmptyDeviceName_Throws ()
runner.BootEmulatorAsync ("", mockAdb));
}

[Test]
[Platform ("Linux,MacOsX")]
public void LaunchEmulator_SurvivesSigint ()
Comment thread
jonathanpeppers marked this conversation as resolved.
Comment on lines +479 to +481
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test passes on macOS:

Image

{
var (tempDir, emuPath) = CreateFakeEmulatorSdk ();
Process? process = null;
try {
var runner = new EmulatorRunner (emuPath);
process = runner.LaunchEmulator ("TestAVD");

Assert.IsFalse (process.HasExited, "Process should be running after launch");

// Send SIGINT to the emulator process
var killPsi = ProcessUtils.CreateProcessStartInfo ("kill", "-INT", process.Id.ToString ());
using var kill = new Process { StartInfo = killPsi };
kill.Start ();
Assert.IsTrue (kill.WaitForExit (5000), "kill command should exit promptly");
Assert.AreEqual (0, kill.ExitCode, "kill -INT should succeed");

// Give the signal a moment to be delivered
Thread.Sleep (500);

Assert.IsFalse (process.HasExited, "Emulator process should survive SIGINT");
} finally {
try { process?.Kill (); process?.WaitForExit (5000); } catch { }
process?.Dispose ();
Directory.Delete (tempDir, true);
}
}

[Test]
public async Task InvalidEmulatorBinary_ReturnsLaunchFailed ()
{
Expand Down Expand Up @@ -512,6 +538,35 @@ public async Task InvalidEmulatorBinary_ReturnsLaunchFailed ()
}
}

[Test]
[Platform ("Linux,MacOsX")]
public void ShellQuote_EscapesSingleQuotes ()
{
var tempDir = Path.Combine (Path.GetTempPath (), $"emu-quote-test-{Path.GetRandomFileName ()}");
var emulatorDir = Path.Combine (tempDir, "emu'dir");
Directory.CreateDirectory (emulatorDir);

var emuPath = Path.Combine (emulatorDir, "emulator");
File.WriteAllText (emuPath, "#!/bin/sh\nsleep 60\n");
var psi = ProcessUtils.CreateProcessStartInfo ("chmod", "+x", emuPath);
using (var chmod = new Process { StartInfo = psi }) {
chmod.Start ();
Assert.IsTrue (chmod.WaitForExit (5000), "chmod should exit promptly");
}

Process? process = null;
try {
var runner = new EmulatorRunner (emuPath);
process = runner.LaunchEmulator ("TestAVD");

Assert.IsFalse (process.HasExited, "Process should start even with single-quote in path");
} finally {
try { process?.Kill (); process?.WaitForExit (5000); } catch { }
process?.Dispose ();
Directory.Delete (tempDir, true);
}
}

// --- Helpers ---

static (string tempDir, string emulatorPath) CreateFakeEmulatorSdk ()
Expand Down