Call to systemd-coredump program using golang fails

8/26/2020

I am trying to call systemd-coredump program using golang in order to generate core dump but it somehow fails with no such file or directory error. I am containerizing this code and deploying it as a pod in kubernetes.

Kubernetes has rhel version 7.6

Here is my code :

package main
import (
	"os/exec"
	"os"
    "fmt"
)

func main(){

  coreProcessID := "<some process id>"
  partialCmd := "<arguments for core dump like %u %g %s %t %c %E>"
  coreCmd := "/usr/lib/systemd/systemd-coredump " + coreProcessID + " " + partialCmd
  fmt.Println("coreCmd is ", coreCmd)
        cmd := exec.Command(coreCmd)
		output, err := cmd.CombinedOutput()
		if err != nil {
			fmt.Println(fmt.Sprint(err) + ": " + string(output))
		} else {
			fmt.Println(string(output))
		}
}

I am getting this error : fork/exec /usr/lib/systemd/systemd-coredump 59009 %u %g %s %t %c %E: no such file or directory: I even tried using syscall.Exec but it just exits the program without any error or output.

In Python it works fine using os.system(core_cmd) but somehow fails for golang. How can I call systemd-coredump program using golang?

Retried with the below changes

cmd := exec.Command(coreCmd, coreProcessID, partialCmd)
		var out bytes.Buffer
		var stderr bytes.Buffer
		cmd.Stdout = &out
		cmd.Stderr = &stderr
		err := cmd.Run()
		if err != nil {
			fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
		}
		fmt.Println("Result: " + out.String())

But it does not retrieve the output or generate core dump

-- kms
go
kubernetes
linux
rhel

1 Answer

9/10/2020

your original example works to some extent if re imagined like this

import (
	"fmt"
	"os"
	"os/exec"
	"strings"
)

func main() {

	coreProcessID := os.Args[1]
	partialCmd := "%u %g %s %t %c %E"
	coreCmdArgs := coreProcessID + " " + partialCmd
	fmt.Println("coreCmdArgs are ", coreCmdArgs)
	coreCmdArgsAsSlice := strings.Split(" ", coreCmdArgs)
	cmd := exec.Command("/usr/lib/systemd/systemd-coredump", coreCmdArgsAsSlice...)
	output, err := cmd.CombinedOutput()
	if err != nil {
		fmt.Println(fmt.Sprint(err) + ": " + string(output))
	} else {
		fmt.Println(string(output))
	}
}

the above on my system gives this error, which is presumably due to the way that systemd-coredump works

coreCmdArgs are  885 %u %g %s %t %c %E
exit status 1: Not enough arguments passed by the kernel (1, expected 6).
-- Vorsprung
Source: StackOverflow