Welcome to mirror list, hosted at ThFree Co, Russian Federation.

main.go « wrapper « cmd - gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2120e7a589b5123cee39610f95136d1369e115f8 (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main

import (
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"os/signal"
	"path"
	"strconv"
	"syscall"
	"time"

	"github.com/sirupsen/logrus"
	"gitlab.com/gitlab-org/gitaly/internal/bootstrap"
	"gitlab.com/gitlab-org/gitaly/internal/ps"
)

const (
	envJSONLogging = "WRAPPER_JSON_LOGGING"
)

func main() {
	if jsonLogging() {
		logrus.SetFormatter(&logrus.JSONFormatter{})
	}

	if len(os.Args) < 2 {
		logrus.Fatalf("usage: %s forking_binary [args]", os.Args[0])
	}

	bin, args := os.Args[1], os.Args[2:]

	log := logrus.WithField("wrapper", os.Getpid()).WithField("binary", bin)
	log.Info("Wrapper started")

	if bootstrap.PidFile() == "" {
		log.Fatalf("missing pid file ENV variable %q", bootstrap.PidFileEnvVar)
	}

	log.WithField("pid_file", bootstrap.PidFile()).Info("finding process")
	proc, err := findProcess()
	if err != nil {
		log.WithError(err).Fatal("find process")
	}

	if proc != nil && matches(proc, bin) {
		log.Info("adopting a process")
	} else {
		log.Info("spawning a process")

		newProc, err := spawn(bin, args)
		if err != nil {
			log.WithError(err).Fatal("spawn process")
		}

		proc = newProc
	}

	log = log.WithField("process", proc.Pid)
	log.Info("monitoring process")

	forwardSignals(proc, log)

	// wait
	for isAlive(proc) {
		time.Sleep(1 * time.Second)
	}

	log.Error("wrapper for process shutting down")
}

func findProcess() (*os.Process, error) {
	pid, err := getPid()
	if err != nil && !os.IsNotExist(err) {
		return nil, err
	}

	// os.FindProcess on unix do not return an error if the process does not exist
	process, err := os.FindProcess(pid)
	if err != nil {
		return nil, err
	}

	if isAlive(process) {
		return process, nil
	}

	return nil, nil
}

func spawn(bin string, args []string) (*os.Process, error) {
	cmd := exec.Command(bin, args...)
	cmd.Env = append(os.Environ(), fmt.Sprintf("%s=true", bootstrap.UpgradesEnabledEnvVar))

	cmd.Stdin = os.Stdin
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	if err := cmd.Start(); err != nil {
		return nil, err
	}

	// This cmd.Wait() is crucial. Without it we cannot detect if the command we just spawned has crashed.
	go cmd.Wait()

	return cmd.Process, nil
}

func forwardSignals(proc *os.Process, log *logrus.Entry) {
	sigs := make(chan os.Signal, 1)
	go func() {
		for sig := range sigs {
			log.WithField("signal", sig).Warning("forwarding signal")

			if err := proc.Signal(sig); err != nil {
				log.WithField("signal", sig).WithError(err).Error("can't forward the signal")
			}
		}
	}()

	signal.Notify(sigs)
}

func getPid() (int, error) {
	data, err := ioutil.ReadFile(bootstrap.PidFile())
	if err != nil {
		return 0, err
	}

	return strconv.Atoi(string(data))
}

func isAlive(p *os.Process) bool {
	// After p exits, and after it gets reaped, this p.Signal will fail. It is crucial that p gets reaped.
	// If p was spawned by the current process, it will get reaped from a goroutine that does cmd.Wait().
	// If p was spawned by someone else we rely on them to reap it, or on p to become an orphan.
	// In the orphan case p should get reaped by the OS (PID 1).
	return p.Signal(syscall.Signal(0)) == nil
}

func matches(p *os.Process, bin string) bool {
	command, err := ps.Comm(p.Pid)
	if err != nil {
		return false
	}

	if path.Base(command) == path.Base(bin) {
		return true
	}

	return false
}

func jsonLogging() bool {
	return os.Getenv(envJSONLogging) == "true"
}