debian-forge-composer/internal/boot/helpers.go
Ondřej Budai 9f80c2ac8e test/image: print saner error messages
%#v was my bad understanding of Go's error formatting. Let's use the standard
%v that gives saner and human-readable error messages.

Signed-off-by: Ondřej Budai <ondrej@budai.cz>
2020-12-01 08:27:44 +01:00

52 lines
949 B
Go

// +build integration
package boot
import (
"log"
"os"
"syscall"
"time"
)
// durationMin returns the smaller of two given durations
func durationMin(a, b time.Duration) time.Duration {
if a < b {
return a
}
return b
}
// killProcessCleanly firstly sends SIGTERM to the process. If it still exists
// after the specified timeout, it sends SIGKILL
func killProcessCleanly(process *os.Process, timeout time.Duration) error {
err := process.Signal(syscall.SIGTERM)
if err != nil {
log.Printf("cannot send SIGTERM to process, sending SIGKILL instead: %v", err)
return process.Kill()
}
const pollInterval = 10 * time.Millisecond
for {
p, err := os.FindProcess(process.Pid)
if err != nil {
return nil
}
err = p.Signal(syscall.Signal(0))
if err != nil {
return nil
}
sleep := durationMin(pollInterval, timeout)
if sleep == 0 {
break
}
timeout -= sleep
time.Sleep(sleep)
}
return process.Kill()
}