0

I'm using TinyGo to build for an ARM-based Linux system that has limited resources. I'm trying to invoke a few other processes from my program, e.g. killall:

exec.Command("killall", "someproc").Start()

However, this results in a bunch of errors (Process not declared by package os) and indeed os/exec isn't supported by TinyGo.

Is there a way to invoke shell commands from Go without using os/exec? My first thought was to use Cgo, but this also doesn't seem to work with TinyGo:

// #include <unistd.h>
import "C"

func main() {
    C.fork()
    // exec etc.
}

While it works fine Go, I'm getting a linker error when using TinyGo (error: undefined symbol: fork). Is there a library that I need to link to get fork() and other system calls? Is there any alternative way to simply invoke a shell command from Go?

fstanis
  • 5,234
  • 1
  • 23
  • 42
  • First, that doesn’t work fine in Go because the Go runtime is multithreaded so you must follow up with exec. Second, you can’t exec without fork (unless you want to replace the entire process), so if TinyGo can’t fork, you can’t exec. – JimB Jan 22 '22 at 17:59
  • I'm aware just forking is insufficient, but since it already wouldn't work, I didn't bother to finish the code. Edited. – fstanis Jan 22 '22 at 18:03
  • Maybe you could implement `killall` in Go, if tinygo supports reading from /proc and OS signals. All `killall` does is identify the relevant processes and send them a signal. – erik258 Jan 22 '22 at 18:15
  • 1
    I would see what it takes to get C fork+exec support on your target system outside of go entirely, but doing that through tinygo looks like it’s still going to be difficult with limited cgo support. – JimB Jan 22 '22 at 18:15
  • @JimB the target platform definitely supports fork/exec since it's Linux based and I can easily do it in C, the issue seems to be with how TinyGo is linking this, that's why I'm hoping there's either an alternative way or something I can do to make TinyGo's linker behave. – fstanis Jan 22 '22 at 18:24

0 Answers0