1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
19 "git.curoverse.com/arvados.git/lib/crunchstat"
22 const MaxLogLine = 1 << 14 // Child stderr lines >16KiB will be split
25 signalOnDeadPPID int = 15
26 ppidCheckInterval = time.Second
31 reporter := crunchstat.Reporter{
32 Logger: log.New(os.Stderr, "crunchstat: ", 0),
35 flag.StringVar(&reporter.CgroupRoot, "cgroup-root", "", "Root of cgroup tree")
36 flag.StringVar(&reporter.CgroupParent, "cgroup-parent", "", "Name of container parent under cgroup")
37 flag.StringVar(&reporter.CIDFile, "cgroup-cid", "", "Path to container id file")
38 flag.IntVar(&signalOnDeadPPID, "signal-on-dead-ppid", signalOnDeadPPID, "Signal to send child if crunchstat's parent process disappears (0 to disable)")
39 flag.DurationVar(&ppidCheckInterval, "ppid-check-interval", ppidCheckInterval, "Time between checks for parent process disappearance")
40 pollMsec := flag.Int64("poll", 1000, "Reporting interval, in milliseconds")
41 getVersion := flag.Bool("version", false, "Print version information and exit.")
45 // Print version information if requested
47 fmt.Printf("crunchstat %s\n", version)
51 reporter.Logger.Printf("crunchstat %s started", version)
53 if reporter.CgroupRoot == "" {
54 reporter.Logger.Fatal("error: must provide -cgroup-root")
55 } else if signalOnDeadPPID < 0 {
56 reporter.Logger.Fatalf("-signal-on-dead-ppid=%d is invalid (use a positive signal number, or 0 to disable)", signalOnDeadPPID)
58 reporter.PollPeriod = time.Duration(*pollMsec) * time.Millisecond
61 err := runCommand(flag.Args(), reporter.Logger)
64 if err, ok := err.(*exec.ExitError); ok {
65 // The program has exited with an exit code != 0
67 // This works on both Unix and Windows. Although
68 // package syscall is generally platform dependent,
69 // WaitStatus is defined for both Unix and Windows and
70 // in both cases has an ExitStatus() method with the
72 if status, ok := err.Sys().(syscall.WaitStatus); ok {
73 os.Exit(status.ExitStatus())
75 reporter.Logger.Fatalln("ExitError without WaitStatus:", err)
77 } else if err != nil {
78 reporter.Logger.Fatalln("error in cmd.Wait:", err)
82 func runCommand(argv []string, logger *log.Logger) error {
83 cmd := exec.Command(argv[0], argv[1:]...)
85 logger.Println("Running", argv)
87 // Child process will use our stdin and stdout pipes
88 // (we close our copies below)
90 cmd.Stdout = os.Stdout
92 // Forward SIGINT and SIGTERM to child process
93 sigChan := make(chan os.Signal, 1)
94 go func(sig <-chan os.Signal) {
96 if cmd.Process != nil {
97 cmd.Process.Signal(catch)
99 logger.Println("notice: caught signal:", catch)
101 signal.Notify(sigChan, syscall.SIGTERM)
102 signal.Notify(sigChan, syscall.SIGINT)
104 // Kill our child proc if our parent process disappears
105 if signalOnDeadPPID != 0 {
106 go sendSignalOnDeadPPID(ppidCheckInterval, signalOnDeadPPID, os.Getppid(), cmd, logger)
109 // Funnel stderr through our channel
110 stderrPipe, err := cmd.StderrPipe()
112 logger.Fatalln("error in StderrPipe:", err)
116 if err := cmd.Start(); err != nil {
117 logger.Fatalln("error in cmd.Start:", err)
120 // Close stdin/stdout in this (parent) process
124 copyPipeToChildLog(stderrPipe, log.New(os.Stderr, "", 0))
129 func sendSignalOnDeadPPID(intvl time.Duration, signum, ppidOrig int, cmd *exec.Cmd, logger *log.Logger) {
130 ticker := time.NewTicker(intvl)
133 if ppid == ppidOrig {
136 if cmd.Process == nil {
137 // Child process isn't running yet
140 logger.Printf("notice: crunchstat ppid changed from %d to %d -- killing child pid %d with signal %d", ppidOrig, ppid, cmd.Process.Pid, signum)
141 err := cmd.Process.Signal(syscall.Signal(signum))
143 logger.Printf("error: sending signal: %s", err)
151 func copyPipeToChildLog(in io.ReadCloser, logger *log.Logger) {
152 reader := bufio.NewReaderSize(in, MaxLogLine)
155 line, isPrefix, err := reader.ReadLine()
158 } else if err != nil {
159 logger.Fatal("error reading child stderr:", err)
165 logger.Print(prefix, string(line), suffix)
166 // Set up prefix for following line