gotest.tools/gotestsum@v1.11.0/cmd/internal/signalhandlerdriver/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"os/signal"
     8  	"strconv"
     9  	"syscall"
    10  	"time"
    11  )
    12  
    13  func main() {
    14  	if len(os.Args) < 2 {
    15  		log("missing required filename argument")
    16  		os.Exit(1)
    17  	}
    18  
    19  	pid := []byte(strconv.Itoa(os.Getpid()))
    20  	if err := ioutil.WriteFile(os.Args[1], pid, 0644); err != nil {
    21  		log("failed to write file:", err.Error())
    22  		os.Exit(1)
    23  	}
    24  
    25  	c := make(chan os.Signal, 1)
    26  	signal.Notify(c)
    27  
    28  	var s os.Signal
    29  	select {
    30  	case s = <-c:
    31  	case <-time.After(time.Minute):
    32  		log("timeout waiting for signal")
    33  		os.Exit(1)
    34  	}
    35  
    36  	log("Received signal:", s)
    37  	switch n := s.(type) {
    38  	case syscall.Signal:
    39  		os.Exit(100 + int(n))
    40  	default:
    41  		log("failed to parse signal number")
    42  		os.Exit(3)
    43  	}
    44  }
    45  
    46  func log(v ...interface{}) {
    47  	fmt.Fprintln(os.Stderr, v...)
    48  }