fix(native): a native process starts in its working directory's real place

This commit is contained in:
Kurt Himebauch committed 2026-11-09 15:20:58 -05:00
1 parent f69e47a3a1
commit 714959fc27
2 files changed
+18 -1

No files matched your search

@@ -71,10 +71,14 @@ object NativeGuest {
).filterValues { it.isNotEmpty() }
}
/** Where guest directory [cwd] is, through the session's mounts ([binds], "src:dst"); the rootfs if it is nowhere. */
fun workingDirectory(root: File, binds: List<String>, cwd: String): File =
File(BwrapSpawner.hostPath(binds.map { BwrapSpawner.Bind.parse(it) }, root.path, cwd)).takeIf { it.isDirectory } ?: root
/** A process for [guest] (argv) started as proot would with [binds], in guest directory [cwd]. */
fun processBuilder(context: Context, root: File, binds: List<String>, guest: List<String>, cwd: String = "/root"): ProcessBuilder =
ProcessBuilder(command(root, guest.first(), guest.drop(1)))
.directory(File(root, cwd.removePrefix("/")).takeIf { it.isDirectory } ?: root)
.directory(workingDirectory(root, binds, cwd))
.also { builder ->
builder.environment().apply {
remove("LD_AUDIT")
@@ -12,6 +12,19 @@ class NativeGuestTest {
File(it, "usr/lib/ld-linux-aarch64.so.1").writeText("")
}
@Test
fun aWorkingDirectoryInAMountIsItsSource() {
val root = Files.createTempDirectory("rootfs").toFile()
val storage = Files.createTempDirectory("storage").toFile()
File(root, "root").mkdirs()
// a mount spec is "src:dst"; Windows' drive letters cannot be written in one
org.junit.Assume.assumeFalse(storage.path.contains(':'))
val binds = listOf("${storage.path}:/root/Storage", "/proc")
assertEquals(storage, NativeGuest.workingDirectory(root, binds, "/root/Storage"))
assertEquals(File(root, "root"), NativeGuest.workingDirectory(root, binds, "/root"))
assertEquals(root, NativeGuest.workingDirectory(root, binds, "/nowhere"))
}
@Test
fun commandRunsTheProgramThroughTheRootfsLoader() {
val root = rootWithLoader()