Files
podman/pkg/machine/gvproxy.go
T

70 lines
1.6 KiB
Go

package machine
import (
"errors"
"fmt"
"io/fs"
"path/filepath"
"strconv"
"time"
"go.podman.io/podman/v6/pkg/machine/define"
)
const (
pidFileWaitTimeout = 2 * time.Second
pidFileCheckInterval = 50 * time.Millisecond
)
func readPIDFileWithRetry(f define.VMFile) ([]byte, error) {
deadline := time.Now().Add(pidFileWaitTimeout)
for time.Now().Before(deadline) {
gvPid, err := f.Read()
if err == nil {
return gvPid, nil
}
if !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
time.Sleep(pidFileCheckInterval)
}
// Final attempt after timeout
return f.Read()
}
// CleanupGVProxy reads the --pid-file for gvproxy, stops the process,
// removes the PID file, and cleans up the notification socket.
func CleanupGVProxy(f define.VMFile) error {
gvPid, err := readPIDFileWithRetry(f)
if err != nil {
// The file will also be removed by gvproxy when it exits so
// we need to account for the race and can just ignore it here.
if errors.Is(err, fs.ErrNotExist) {
return nil
}
return fmt.Errorf("unable to read gvproxy pid file: %w", err)
}
proxyPid, err := strconv.Atoi(string(gvPid))
if err != nil {
return fmt.Errorf("unable to convert pid to integer: %w", err)
}
return cleanupGVProxy(proxyPid, f)
}
func cleanupGVProxy(proxyPID int, f define.VMFile) error {
if err := waitOnProcess(proxyPID); err != nil {
return err
}
if err := removeGVProxyPIDFile(f); err != nil {
return err
}
// Remove notification socket after gvproxy has exited
runtimeDir := filepath.Dir(f.GetPath())
CleanupGvProxyNotifySocket(runtimeDir)
return nil
}