summaryrefslogtreecommitdiff
path: root/internal/namespace/lifecycle.go
blob: 47a804fc2aa7b5e51f0244ccbe862f846ba5680a (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
package namespace

import (
	"fmt"
	"os"
	"path/filepath"
	"strconv"
	"syscall"

	"git.theodohertyfamily.com/tools/wg-wrap/internal/paths"
)

// GetProfileNamespacePath returns the path to the pinned namespace file for a profile.
func GetProfileNamespacePath(pm *paths.PathManager, profile string) string {
	return pm.ProfileNamespacePath(profile)
}

// GetPidsDirPath returns the path to the directory where process PIDs are tracked for a profile.
func GetPidsDirPath(pm *paths.PathManager, profile string) string {
	return pm.ProfilePidsDir(profile)
}

// RegisterProcess marks the current process as using the specified profile.
func RegisterProcess(pm *paths.PathManager, profile string) error {
	pidsDir := GetPidsDirPath(pm, profile)
	if err := os.MkdirAll(pidsDir, 0755); err != nil {
		return fmt.Errorf("failed to create pids directory: %v", err)
	}

	pid := os.Getpid()
	pidFile := filepath.Join(pidsDir, strconv.Itoa(pid))
	if err := os.WriteFile(pidFile, []byte(""), 0644); err != nil {
		return fmt.Errorf("failed to register process pid %d: %v", pid, err)
	}
	return nil
}

// UnregisterProcess removes the current process from the profile's tracking.
func UnregisterProcess(pm *paths.PathManager, profile string) error {
	pid := os.Getpid()
	pidFile := filepath.Join(GetPidsDirPath(pm, profile), strconv.Itoa(pid))
	if err := os.Remove(pidFile); err != nil && !os.IsNotExist(err) {
		return fmt.Errorf("failed to unregister process pid %d: %v", pid, err)
	}
	return nil
}

// PruneStalePids removes PID files that no longer correspond to active processes.
func PruneStalePids(pm *paths.PathManager, profile string) error {
	pidsDir := GetPidsDirPath(pm, profile)
	files, err := os.ReadDir(pidsDir)
	if err != nil {
		if os.IsNotExist(err) {
			return nil
		}
		return fmt.Errorf("failed to read pids directory: %v", err)
	}

	for _, file := range files {
		pid, err := strconv.Atoi(file.Name())
		if err != nil {
			continue // Ignore non-numeric files
		}

		process, err := os.FindProcess(pid)
		if err != nil {
			if err := os.Remove(filepath.Join(pidsDir, file.Name())); err != nil {
				fmt.Printf("failed to remove stale pid file %s: %v\n", file.Name(), err)
			}
			continue
		}

		err = process.Signal(syscall.Signal(0))
		if err != nil {
			if err := os.Remove(filepath.Join(pidsDir, file.Name())); err != nil {
				fmt.Printf("failed to remove stale pid file %s: %v\n", file.Name(), err)
			}
		}
	}
	return nil
}

// IsLastProcess checks if the current process is the only active user of the profile.
func IsLastProcess(pm *paths.PathManager, profile string) (bool, error) {
	pidsDir := GetPidsDirPath(pm, profile)
	files, err := os.ReadDir(pidsDir)
	if err != nil {
		if os.IsNotExist(err) {
			return true, nil
		}
		return false, fmt.Errorf("failed to read pids directory: %v", err)
	}

	activeCount := 0
	for _, file := range files {
		pid, err := strconv.Atoi(file.Name())
		if err != nil {
			continue
		}
		process, err := os.FindProcess(pid)
		if err != nil {
			continue
		}
		if process.Signal(syscall.Signal(0)) == nil {
			activeCount++
		}
	}

	return activeCount <= 1, nil
}