podman

Форк
0
/
gvproxy_unix.go 
74 строки · 1.8 Кб
1
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd
2

3
package machine
4

5
import (
6
	"errors"
7
	"fmt"
8
	"syscall"
9
	"time"
10

11
	psutil "github.com/shirou/gopsutil/v3/process"
12
	"github.com/sirupsen/logrus"
13
	"golang.org/x/sys/unix"
14
)
15

16
const (
17
	loops     = 8
18
	sleepTime = time.Millisecond * 1
19
)
20

21
// backoffForProcess checks if the process still exists, for something like
22
// sigterm. If the process still exists after loops and sleep time are exhausted,
23
// an error is returned
24
func backoffForProcess(p *psutil.Process) error {
25
	sleepInterval := sleepTime
26
	for i := 0; i < loops; i++ {
27
		running, err := p.IsRunning()
28
		if err != nil {
29
			// It is possible that while in our loop, the PID vaporize triggering
30
			// an input/output error (#21845)
31
			if errors.Is(err, unix.EIO) {
32
				return nil
33
			}
34
			return fmt.Errorf("checking if process running: %w", err)
35
		}
36
		if !running {
37
			return nil
38
		}
39

40
		time.Sleep(sleepInterval)
41
		// double the time
42
		sleepInterval += sleepInterval
43
	}
44
	return fmt.Errorf("process %d has not ended", p.Pid)
45
}
46

47
// / waitOnProcess takes a pid and sends a sigterm to it. it then waits for the
48
// process to not exist.  if the sigterm does not end the process after an interval,
49
// then sigkill is sent.  it also waits for the process to exit after the sigkill too.
50
func waitOnProcess(processID int) error {
51
	logrus.Infof("Going to stop gvproxy (PID %d)", processID)
52

53
	p, err := psutil.NewProcess(int32(processID))
54
	if err != nil {
55
		return fmt.Errorf("looking up PID %d: %w", processID, err)
56
	}
57

58
	running, err := p.IsRunning()
59
	if err != nil {
60
		return fmt.Errorf("checking if gvproxy is running: %w", err)
61
	}
62
	if !running {
63
		return nil
64
	}
65

66
	if err := p.Kill(); err != nil {
67
		if errors.Is(err, syscall.ESRCH) {
68
			logrus.Debugf("Gvproxy already dead, exiting cleanly")
69
			return nil
70
		}
71
		return err
72
	}
73
	return backoffForProcess(p)
74
}
75

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.