File: monitor.go

package info (click to toggle)
golang-bugsnag-panicwrap 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, trixie
  • size: 156 kB
  • sloc: makefile: 2
file content (61 lines) | stat: -rw-r--r-- 982 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// +build !windows,!solaris

package panicwrap

import (
	"os"
	"os/exec"
)

func monitor(c *WrapConfig) (int, error) {

	// If we're the child process, absorb panics.
	if Wrapped(c) {
		panicCh := make(chan string)

		go trackPanic(os.Stdin, os.Stderr, c.DetectDuration, panicCh)

		// Wait on the panic data
		panicTxt := <-panicCh
		if panicTxt != "" {
			if !c.HidePanic {
				os.Stderr.Write([]byte(panicTxt))
			}

			c.Handler(panicTxt)
		}

		os.Exit(0)
	}

	exePath, err := os.Executable()
	if err != nil {
		return -1, err
	}
	cmd := exec.Command(exePath, os.Args[1:]...)

	read, write, err := os.Pipe()
	if err != nil {
		return -1, err
	}

	cmd.Stdin = read
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	cmd.Env = append(os.Environ(), c.CookieKey+"="+c.CookieValue)

	if err != nil {
		return -1, err
	}
	err = cmd.Start()
	if err != nil {
		return -1, err
	}

	err = dup2(int(write.Fd()), int(os.Stderr.Fd()))
	if err != nil {
		return -1, err
	}

	return -1, nil
}