lyngio: added processes and docs for it, JVM and linux

This commit is contained in:
sergeych committed 2026-01-06 02:35:24 +01:00
1 parent d91acd593a
commit 660a80a26b
22 files changed
+1563 -3

No files matched your search

@@ -0,0 +1,33 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
actual fun getPlatformDetails(): PlatformDetails {
return PlatformDetails(
name = "Android",
version = android.os.Build.VERSION.RELEASE,
arch = android.os.Build.SUPPORTED_ABIS.firstOrNull() ?: "unknown",
kernelVersion = System.getProperty("os.version")
)
}
actual fun isProcessSupported(): Boolean = false
actual fun getSystemProcessRunner(): LyngProcessRunner {
throw UnsupportedOperationException("Processes are not supported on Android yet")
}
@@ -0,0 +1,234 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng.io.process
import kotlinx.coroutines.flow.Flow
import net.sergeych.lyng.ModuleScope
import net.sergeych.lyng.Scope
import net.sergeych.lyng.miniast.*
import net.sergeych.lyng.obj.*
import net.sergeych.lyng.pacman.ImportManager
import net.sergeych.lyng.statement
import net.sergeych.lyngio.process.*
import net.sergeych.lyngio.process.security.ProcessAccessDeniedException
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
/**
* Install Lyng module `lyng.io.process` into the given scope's ImportManager.
*/
fun createProcessModule(policy: ProcessAccessPolicy, scope: Scope): Boolean =
createProcessModule(policy, scope.importManager)
/** Same as [createProcessModule] but with explicit [ImportManager]. */
fun createProcessModule(policy: ProcessAccessPolicy, manager: ImportManager): Boolean {
val name = "lyng.io.process"
if (manager.packageNames.contains(name)) return false
manager.addPackage(name) { module ->
buildProcessModule(module, policy)
}
return true
}
private suspend fun buildProcessModule(module: ModuleScope, policy: ProcessAccessPolicy) {
val runner = try {
SecuredLyngProcessRunner(getSystemProcessRunner(), policy)
} catch (e: Exception) {
null
}
val runningProcessType = object : ObjClass("RunningProcess") {}
runningProcessType.apply {
addFnDoc(
name = "stdout",
doc = "Get standard output stream as a Flow of lines.",
returns = type("lyng.Flow"),
moduleName = module.packageName
) {
val self = thisAs<ObjRunningProcess>()
self.process.stdout.toLyngFlow(this)
}
addFnDoc(
name = "stderr",
doc = "Get standard error stream as a Flow of lines.",
returns = type("lyng.Flow"),
moduleName = module.packageName
) {
val self = thisAs<ObjRunningProcess>()
self.process.stderr.toLyngFlow(this)
}
addFnDoc(
name = "signal",
doc = "Send a signal to the process (e.g. 'SIGINT', 'SIGTERM', 'SIGKILL').",
params = listOf(ParamDoc("signal", type("lyng.String"))),
moduleName = module.packageName
) {
processGuard {
val sigStr = requireOnlyArg<ObjString>().value.uppercase()
val sig = try {
ProcessSignal.valueOf(sigStr)
} catch (e: Exception) {
try {
ProcessSignal.valueOf("SIG$sigStr")
} catch (e2: Exception) {
raiseIllegalArgument("Unknown signal: $sigStr")
}
}
thisAs<ObjRunningProcess>().process.sendSignal(sig)
ObjVoid
}
}
addFnDoc(
name = "waitFor",
doc = "Wait for the process to exit and return its exit code.",
returns = type("lyng.Int"),
moduleName = module.packageName
) {
processGuard {
thisAs<ObjRunningProcess>().process.waitFor().toObj()
}
}
addFnDoc(
name = "destroy",
doc = "Forcefully terminate the process.",
moduleName = module.packageName
) {
thisAs<ObjRunningProcess>().process.destroy()
ObjVoid
}
}
val processType = object : ObjClass("Process") {}
processType.apply {
addClassFnDoc(
name = "execute",
doc = "Execute a process with arguments.",
params = listOf(ParamDoc("executable", type("lyng.String")), ParamDoc("args", type("lyng.List"))),
returns = type("RunningProcess"),
moduleName = module.packageName
) {
if (runner == null) raiseError("Processes are not supported on this platform")
processGuard {
val executable = requiredArg<ObjString>(0).value
val args = requiredArg<ObjList>(1).list.map { it.toString() }
val lp = runner.execute(executable, args)
ObjRunningProcess(runningProcessType, lp)
}
}
addClassFnDoc(
name = "shell",
doc = "Execute a command via system shell.",
params = listOf(ParamDoc("command", type("lyng.String"))),
returns = type("RunningProcess"),
moduleName = module.packageName
) {
if (runner == null) raiseError("Processes are not supported on this platform")
processGuard {
val command = requireOnlyArg<ObjString>().value
val lp = runner.shell(command)
ObjRunningProcess(runningProcessType, lp)
}
}
}
val platformType = object : ObjClass("Platform") {}
platformType.apply {
addClassFnDoc(
name = "details",
doc = "Get platform core details.",
returns = type("lyng.Map"),
moduleName = module.packageName
) {
val d = getPlatformDetails()
ObjMap(mutableMapOf(
ObjString("name") to ObjString(d.name),
ObjString("version") to ObjString(d.version),
ObjString("arch") to ObjString(d.arch),
ObjString("kernelVersion") to (d.kernelVersion?.toObj() ?: ObjNull)
))
}
addClassFnDoc(
name = "isSupported",
doc = "Check if processes are supported on this platform.",
returns = type("lyng.Bool"),
moduleName = module.packageName
) {
isProcessSupported().toObj()
}
}
module.addConstDoc(
name = "Process",
value = processType,
doc = "Process execution and control.",
type = type("Process"),
moduleName = module.packageName
)
module.addConstDoc(
name = "Platform",
value = platformType,
doc = "Platform information.",
type = type("Platform"),
moduleName = module.packageName
)
module.addConstDoc(
name = "RunningProcess",
value = runningProcessType,
doc = "Handle to a running process.",
type = type("RunningProcess"),
moduleName = module.packageName
)
}
class ObjRunningProcess(
override val objClass: ObjClass,
val process: LyngProcess
) : Obj() {
override fun toString(): String = "RunningProcess($process)"
}
private suspend inline fun Scope.processGuard(crossinline block: suspend () -> Obj): Obj {
return try {
block()
} catch (e: ProcessAccessDeniedException) {
raiseError(ObjIllegalOperationException(this, e.reasonDetail ?: "process access denied"))
} catch (e: Exception) {
raiseError(ObjIllegalOperationException(this, e.message ?: "process error"))
}
}
private fun Flow<String>.toLyngFlow(flowScope: Scope): ObjFlow {
val producer = statement {
val builder = (this as? net.sergeych.lyng.ClosureScope)?.callScope?.thisObj as? ObjFlowBuilder
?: this.thisObj as? ObjFlowBuilder
this@toLyngFlow.collect {
try {
builder?.output?.send(ObjString(it))
} catch (e: Exception) {
// Channel closed or other error, stop collecting
return@collect
}
}
ObjVoid
}
return ObjFlow(producer, flowScope)
}
@@ -1,3 +1,20 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
/*
* Filesystem module builtin docs registration, located in lyngio so core library
* does not depend on external packages. The IDEA plugin (and any other tooling)
@@ -0,0 +1,93 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.coroutines.flow.Flow
import net.sergeych.lyngio.process.security.ProcessAccessOp
import net.sergeych.lyngio.process.security.ProcessAccessPolicy
/**
* Common signals for process control.
*/
enum class ProcessSignal {
SIGINT, SIGTERM, SIGKILL
}
/**
* Multiplatform process representation.
*/
interface LyngProcess {
/**
* Standard output stream as a flow of strings (lines).
*/
val stdout: Flow<String>
/**
* Standard error stream as a flow of strings (lines).
*/
val stderr: Flow<String>
/**
* Send a signal to the process.
* Throws exception if signals are not supported on the platform or for this process.
*/
suspend fun sendSignal(signal: ProcessSignal)
/**
* Wait for the process to exit and return the exit code.
*/
suspend fun waitFor(): Int
/**
* Forcefully terminate the process.
*/
fun destroy()
}
/**
* Interface for running processes.
*/
interface LyngProcessRunner {
/**
* Execute a process with the given executable and arguments.
*/
suspend fun execute(executable: String, args: List<String>): LyngProcess
/**
* Execute a command via the platform's default shell.
*/
suspend fun shell(command: String): LyngProcess
}
/**
* Secured implementation of [LyngProcessRunner] that checks against a [ProcessAccessPolicy].
*/
class SecuredLyngProcessRunner(
private val runner: LyngProcessRunner,
private val policy: ProcessAccessPolicy
) : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess {
policy.require(ProcessAccessOp.Execute(executable, args))
return runner.execute(executable, args)
}
override suspend fun shell(command: String): LyngProcess {
policy.require(ProcessAccessOp.Shell(command))
return runner.shell(command)
}
}
@@ -0,0 +1,44 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
/**
* Platform core details.
*/
data class PlatformDetails(
val name: String,
val version: String,
val arch: String,
val kernelVersion: String? = null
)
/**
* Get the current platform core details.
*/
expect fun getPlatformDetails(): PlatformDetails
/**
* Check whether the current platform supports processes and shell execution.
*/
expect fun isProcessSupported(): Boolean
/**
* Get the system default [LyngProcessRunner].
* Throws [UnsupportedOperationException] if processes are not supported on this platform.
*/
expect fun getSystemProcessRunner(): LyngProcessRunner
@@ -0,0 +1,59 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process.security
import net.sergeych.lyngio.fs.security.AccessContext
import net.sergeych.lyngio.fs.security.AccessDecision
import net.sergeych.lyngio.fs.security.Decision
/**
* Primitive process operations for access control decisions.
*/
sealed interface ProcessAccessOp {
data class Execute(val executable: String, val args: List<String>) : ProcessAccessOp
data class Shell(val command: String) : ProcessAccessOp
}
class ProcessAccessDeniedException(
val op: ProcessAccessOp,
val reasonDetail: String? = null,
) : IllegalStateException("Process access denied for $op" + (reasonDetail?.let { ": $it" } ?: ""))
/**
* Policy interface that decides whether a specific process operation is allowed.
*/
interface ProcessAccessPolicy {
suspend fun check(op: ProcessAccessOp, ctx: AccessContext = AccessContext()): AccessDecision
// Convenience helpers
suspend fun require(op: ProcessAccessOp, ctx: AccessContext = AccessContext()) {
val res = check(op, ctx)
if (!res.isAllowed()) throw ProcessAccessDeniedException(op, res.reason)
}
suspend fun canExecute(executable: String, args: List<String>, ctx: AccessContext = AccessContext()) =
check(ProcessAccessOp.Execute(executable, args), ctx).isAllowed()
suspend fun canShell(command: String, ctx: AccessContext = AccessContext()) =
check(ProcessAccessOp.Shell(command), ctx).isAllowed()
}
object PermitAllProcessAccessPolicy : ProcessAccessPolicy {
override suspend fun check(op: ProcessAccessOp, ctx: AccessContext): AccessDecision =
AccessDecision(Decision.Allow)
}
@@ -0,0 +1,26 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
internal actual fun getNativeKernelVersion(): String? = null
internal actual fun isNativeProcessSupported(): Boolean = false
internal actual fun getNativeProcessRunner(): LyngProcessRunner {
throw UnsupportedOperationException("Processes are not supported on iOS")
}
@@ -0,0 +1,32 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
actual fun getPlatformDetails(): PlatformDetails {
return PlatformDetails(
name = "JavaScript",
version = "unknown",
arch = "unknown"
)
}
actual fun isProcessSupported(): Boolean = false
actual fun getSystemProcessRunner(): LyngProcessRunner {
throw UnsupportedOperationException("Processes are not supported on JS")
}
@@ -0,0 +1,107 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext
actual fun getPlatformDetails(): PlatformDetails {
val osName = System.getProperty("os.name")
return PlatformDetails(
name = osName,
version = System.getProperty("os.version"),
arch = System.getProperty("os.arch"),
kernelVersion = if (osName.lowercase().contains("linux")) {
System.getProperty("os.version")
} else null
)
}
actual fun isProcessSupported(): Boolean = true
actual fun getSystemProcessRunner(): LyngProcessRunner = JvmProcessRunner
object JvmProcessRunner : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess {
val process = ProcessBuilder(listOf(executable) + args)
.start()
return JvmLyngProcess(process)
}
override suspend fun shell(command: String): LyngProcess {
val os = System.getProperty("os.name").lowercase()
val shellCmd = if (os.contains("win")) {
listOf("cmd.exe", "/c", command)
} else {
listOf("sh", "-c", command)
}
val process = ProcessBuilder(shellCmd)
.start()
return JvmLyngProcess(process)
}
}
class JvmLyngProcess(private val process: Process) : LyngProcess {
override val stdout: Flow<String> = flow {
val reader = process.inputStream.bufferedReader()
while (true) {
val line = reader.readLine() ?: break
emit(line)
}
}
override val stderr: Flow<String> = flow {
val reader = process.errorStream.bufferedReader()
while (true) {
val line = reader.readLine() ?: break
emit(line)
}
}
override suspend fun sendSignal(signal: ProcessSignal) {
when (signal) {
ProcessSignal.SIGINT -> {
// SIGINT is hard on JVM without native calls or external 'kill'
val os = System.getProperty("os.name").lowercase()
if (os.contains("win")) {
throw UnsupportedOperationException("SIGINT not supported on Windows JVM")
} else {
// Try to use kill -2 <pid>
try {
val pid = process.pid()
Runtime.getRuntime().exec(arrayOf("kill", "-2", pid.toString())).waitFor()
} catch (e: Exception) {
throw UnsupportedOperationException("Failed to send SIGINT: ${e.message}")
}
}
}
ProcessSignal.SIGTERM -> process.destroy()
ProcessSignal.SIGKILL -> process.destroyForcibly()
}
}
override suspend fun waitFor(): Int = withContext(Dispatchers.IO) {
process.waitFor()
}
override fun destroy() {
process.destroy()
}
}
@@ -0,0 +1,87 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng.io.process
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
import kotlin.test.Test
import kotlin.test.assertTrue
class LyngProcessModuleTest {
@Test
fun testLyngProcess() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
var p = Process.execute("echo", ["hello", "lyng"])
var output = []
for (line in p.stdout()) {
output.add(line)
}
p.waitFor()
output
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("hello lyng"))
}
@Test
fun testLyngShell() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
var p = Process.shell("echo 'shell lyng'")
var output = ""
for (line in p.stdout()) {
output = output + line
}
p.waitFor()
output
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("shell lyng"))
}
@Test
fun testPlatformDetails() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
Platform.details()
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("name"), "Result should contain 'name', but was: ${result.inspect(scope)}")
}
}
@@ -0,0 +1,57 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class JvmProcessTest {
@Test
fun testExecute() = runBlocking {
val runner = getSystemProcessRunner()
val secured = SecuredLyngProcessRunner(runner, PermitAllProcessAccessPolicy)
val process = secured.execute("echo", listOf("hello", "world"))
val output = process.stdout.toList()
assertEquals(listOf("hello world"), output)
assertEquals(0, process.waitFor())
}
@Test
fun testShell() = runBlocking {
val runner = getSystemProcessRunner()
val secured = SecuredLyngProcessRunner(runner, PermitAllProcessAccessPolicy)
val process = secured.shell("echo 'hello shell'")
val output = process.stdout.toList()
assertEquals(listOf("hello shell"), output)
assertEquals(0, process.waitFor())
}
@Test
fun testPlatformDetails() {
val details = getPlatformDetails()
assertTrue(details.name.isNotEmpty())
assertTrue(isProcessSupported())
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.cinterop.*
import platform.posix.*
@OptIn(ExperimentalForeignApi::class)
internal actual fun getNativeKernelVersion(): String? {
return memScoped {
val u = alloc<utsname>()
if (uname(u.ptr) == 0) {
u.release.toKString()
} else null
}
}
internal actual fun isNativeProcessSupported(): Boolean = true
internal actual fun getNativeProcessRunner(): LyngProcessRunner = PosixProcessRunner
@@ -0,0 +1,152 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.cinterop.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import platform.posix.*
@OptIn(ExperimentalForeignApi::class)
internal class NativeLyngProcess(
private val pid: pid_t,
private val stdoutFd: Int,
private val stderrFd: Int
) : LyngProcess {
override val stdout: Flow<String> = createPipeFlow(stdoutFd)
override val stderr: Flow<String> = createPipeFlow(stderrFd)
override suspend fun sendSignal(signal: ProcessSignal) {
val sig = when (signal) {
ProcessSignal.SIGINT -> SIGINT
ProcessSignal.SIGTERM -> SIGTERM
ProcessSignal.SIGKILL -> SIGKILL
}
if (kill(pid, sig) != 0) {
throw RuntimeException("Failed to send signal $signal to process $pid: ${strerror(errno)?.toKString()}")
}
}
override suspend fun waitFor(): Int = withContext(Dispatchers.Default) {
memScoped {
val status = alloc<IntVar>()
if (waitpid(pid, status.ptr, 0) == -1) {
throw RuntimeException("Failed to wait for process $pid: ${strerror(errno)?.toKString()}")
}
val s = status.value
if ((s and 0x7f) == 0) (s shr 8) and 0xff else -1
}
}
override fun destroy() {
kill(pid, SIGKILL)
}
}
@OptIn(ExperimentalForeignApi::class)
private fun createPipeFlow(fd: Int): Flow<String> = flow {
val buffer = ByteArray(4096)
val lineBuffer = StringBuilder()
try {
while (true) {
val bytesRead = buffer.usePinned { pinned ->
read(fd, pinned.addressOf(0), buffer.size.toULong())
}
if (bytesRead <= 0L) break
val text = buffer.decodeToString(endIndex = bytesRead.toInt())
lineBuffer.append(text)
var newlineIdx = lineBuffer.indexOf('\n')
while (newlineIdx != -1) {
val line = lineBuffer.substring(0, newlineIdx)
emit(line)
lineBuffer.deleteRange(0, newlineIdx + 1)
newlineIdx = lineBuffer.indexOf('\n')
}
}
if (lineBuffer.isNotEmpty()) {
emit(lineBuffer.toString())
}
} finally {
close(fd)
}
}.flowOn(Dispatchers.Default)
@OptIn(ExperimentalForeignApi::class)
object PosixProcessRunner : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess = withContext(Dispatchers.Default) {
memScoped {
val pipeStdout = allocArray<IntVar>(2)
val pipeStderr = allocArray<IntVar>(2)
if (pipe(pipeStdout) != 0) throw RuntimeException("Failed to create stdout pipe")
if (pipe(pipeStderr) != 0) {
close(pipeStdout[0])
close(pipeStdout[1])
throw RuntimeException("Failed to create stderr pipe")
}
val pid = fork()
if (pid == -1) {
close(pipeStdout[0])
close(pipeStdout[1])
close(pipeStderr[0])
close(pipeStderr[1])
throw RuntimeException("Failed to fork: ${strerror(errno)?.toKString()}")
}
if (pid == 0) {
// Child process
dup2(pipeStdout[1], 1)
dup2(pipeStderr[1], 2)
close(pipeStdout[0])
close(pipeStdout[1])
close(pipeStderr[0])
close(pipeStderr[1])
val argv = allocArray<CPointerVar<ByteVar>>(args.size + 2)
argv[0] = executable.cstr.ptr
for (i in args.indices) {
argv[i + 1] = args[i].cstr.ptr
}
argv[args.size + 1] = null
execvp(executable, argv)
// If we are here, exec failed
_exit(1)
}
// Parent process
close(pipeStdout[1])
close(pipeStderr[1])
NativeLyngProcess(pid, pipeStdout[0], pipeStderr[0])
}
}
override suspend fun shell(command: String): LyngProcess {
return execute("/bin/sh", listOf("-c", command))
}
}
@@ -0,0 +1,95 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.coroutines.flow.toList
import kotlinx.coroutines.runBlocking
import net.sergeych.lyng.Compiler
import net.sergeych.lyng.Script
import net.sergeych.lyng.io.process.createProcessModule
import net.sergeych.lyngio.process.security.PermitAllProcessAccessPolicy
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue
class LinuxProcessTest {
@Test
fun testExecuteEcho() = runBlocking {
val process = PosixProcessRunner.execute("echo", listOf("hello", "native"))
val stdout = process.stdout.toList()
val exitCode = process.waitFor()
assertEquals(0, exitCode)
assertEquals(listOf("hello native"), stdout)
}
@Test
fun testShellCommand() = runBlocking {
val process = PosixProcessRunner.shell("echo 'shell native' && printf 'line2'")
val stdout = process.stdout.toList()
val exitCode = process.waitFor()
assertEquals(0, exitCode)
assertEquals(listOf("shell native", "line2"), stdout)
}
@Test
fun testStderrCapture() = runBlocking {
val process = PosixProcessRunner.shell("echo 'to stdout'; echo 'to stderr' >&2")
val stdout = process.stdout.toList()
val stderr = process.stderr.toList()
process.waitFor()
assertEquals(listOf("to stdout"), stdout)
assertEquals(listOf("to stderr"), stderr)
}
@Test
fun testPlatformDetails() {
val details = getPlatformDetails()
assertEquals("LINUX", details.name)
assertTrue(details.kernelVersion != null)
assertTrue(details.kernelVersion!!.isNotEmpty())
println("Linux Native Details: $details")
}
@Test
fun testLyngModuleNative() = runBlocking {
val scope = Script.newScope()
createProcessModule(PermitAllProcessAccessPolicy, scope)
val code = """
import lyng.io.process
var p = Process.execute("echo", ["hello", "lyng", "native"])
var output = []
for (line in p.stdout()) {
output.add(line)
}
p.waitFor()
println(output)
assertEquals("hello lyng native", output.joinToString(" "))
output
""".trimIndent()
val script = Compiler.compile(code)
val result = script.execute(scope)
assertTrue(result.inspect(scope).contains("hello lyng native"))
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.cinterop.*
import platform.posix.*
@OptIn(ExperimentalForeignApi::class)
internal actual fun getNativeKernelVersion(): String? {
return memScoped {
val u = alloc<utsname>()
if (uname(u.ptr) == 0) {
u.release.toKString()
} else null
}
}
internal actual fun isNativeProcessSupported(): Boolean = true
internal actual fun getNativeProcessRunner(): LyngProcessRunner = PosixProcessRunner
@@ -0,0 +1,152 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlinx.cinterop.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import platform.posix.*
@OptIn(ExperimentalForeignApi::class)
internal class NativeLyngProcess(
private val pid: pid_t,
private val stdoutFd: Int,
private val stderrFd: Int
) : LyngProcess {
override val stdout: Flow<String> = createPipeFlow(stdoutFd)
override val stderr: Flow<String> = createPipeFlow(stderrFd)
override suspend fun sendSignal(signal: ProcessSignal) {
val sig = when (signal) {
ProcessSignal.SIGINT -> SIGINT
ProcessSignal.SIGTERM -> SIGTERM
ProcessSignal.SIGKILL -> SIGKILL
}
if (kill(pid, sig) != 0) {
throw RuntimeException("Failed to send signal $signal to process $pid: ${strerror(errno)?.toKString()}")
}
}
override suspend fun waitFor(): Int = withContext(Dispatchers.Default) {
memScoped {
val status = alloc<IntVar>()
if (waitpid(pid, status.ptr, 0) == -1) {
throw RuntimeException("Failed to wait for process $pid: ${strerror(errno)?.toKString()}")
}
val s = status.value
if ((s and 0x7f) == 0) (s shr 8) and 0xff else -1
}
}
override fun destroy() {
kill(pid, SIGKILL)
}
}
@OptIn(ExperimentalForeignApi::class)
private fun createPipeFlow(fd: Int): Flow<String> = flow {
val buffer = ByteArray(4096)
val lineBuffer = StringBuilder()
try {
while (true) {
val bytesRead = buffer.usePinned { pinned ->
read(fd, pinned.addressOf(0), buffer.size.toULong())
}
if (bytesRead <= 0L) break
val text = buffer.decodeToString(endIndex = bytesRead.toInt())
lineBuffer.append(text)
var newlineIdx = lineBuffer.indexOf('\n')
while (newlineIdx != -1) {
val line = lineBuffer.substring(0, newlineIdx)
emit(line)
lineBuffer.deleteRange(0, newlineIdx + 1)
newlineIdx = lineBuffer.indexOf('\n')
}
}
if (lineBuffer.isNotEmpty()) {
emit(lineBuffer.toString())
}
} finally {
close(fd)
}
}.flowOn(Dispatchers.Default)
@OptIn(ExperimentalForeignApi::class)
object PosixProcessRunner : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess = withContext(Dispatchers.Default) {
memScoped {
val pipeStdout = allocArray<IntVar>(2)
val pipeStderr = allocArray<IntVar>(2)
if (pipe(pipeStdout) != 0) throw RuntimeException("Failed to create stdout pipe")
if (pipe(pipeStderr) != 0) {
close(pipeStdout[0])
close(pipeStdout[1])
throw RuntimeException("Failed to create stderr pipe")
}
val pid = fork()
if (pid == -1) {
close(pipeStdout[0])
close(pipeStdout[1])
close(pipeStderr[0])
close(pipeStderr[1])
throw RuntimeException("Failed to fork: ${strerror(errno)?.toKString()}")
}
if (pid == 0) {
// Child process
dup2(pipeStdout[1], 1)
dup2(pipeStderr[1], 2)
close(pipeStdout[0])
close(pipeStdout[1])
close(pipeStderr[0])
close(pipeStderr[1])
val argv = allocArray<CPointerVar<ByteVar>>(args.size + 2)
argv[0] = executable.cstr.ptr
for (i in args.indices) {
argv[i + 1] = args[i].cstr.ptr
}
argv[args.size + 1] = null
execvp(executable, argv)
// If we are here, exec failed
_exit(1)
}
// Parent process
close(pipeStdout[1])
close(pipeStderr[1])
NativeLyngProcess(pid, pipeStdout[0], pipeStderr[0])
}
}
override suspend fun shell(command: String): LyngProcess {
return execute("/bin/sh", listOf("-c", command))
}
}
@@ -0,0 +1,34 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
internal actual fun getNativeKernelVersion(): String? = null
internal actual fun isNativeProcessSupported(): Boolean = true
internal actual fun getNativeProcessRunner(): LyngProcessRunner = WindowsProcessRunner
object WindowsProcessRunner : LyngProcessRunner {
override suspend fun execute(executable: String, args: List<String>): LyngProcess {
throw UnsupportedOperationException("Windows native process execution not implemented yet")
}
override suspend fun shell(command: String): LyngProcess {
return execute("cmd.exe", listOf("/c", command))
}
}
@@ -0,0 +1,40 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyngio.process
import kotlin.experimental.ExperimentalNativeApi
@OptIn(ExperimentalNativeApi::class)
actual fun getPlatformDetails(): PlatformDetails {
return PlatformDetails(
name = Platform.osFamily.name,
version = "unknown",
arch = Platform.cpuArchitecture.name,
kernelVersion = getNativeKernelVersion()
)
}
internal expect fun getNativeKernelVersion(): String?
actual fun isProcessSupported(): Boolean = isNativeProcessSupported()
internal expect fun isNativeProcessSupported(): Boolean
actual fun getSystemProcessRunner(): LyngProcessRunner = getNativeProcessRunner()
internal expect fun getNativeProcessRunner(): LyngProcessRunner