summaryrefslogtreecommitdiff
path: root/internal/namespace/lifecycle.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/namespace/lifecycle.go')
-rw-r--r--internal/namespace/lifecycle.go45
1 files changed, 20 insertions, 25 deletions
diff --git a/internal/namespace/lifecycle.go b/internal/namespace/lifecycle.go
index 4ca725f..47a804f 100644
--- a/internal/namespace/lifecycle.go
+++ b/internal/namespace/lifecycle.go
@@ -6,32 +6,23 @@ import (
"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(baseDir, profile string) string {
- if baseDir == "" {
- baseDir = getRuntimeBaseDir()
- }
- return filepath.Join(baseDir, "profiles", profile)
-}
-
-func getRuntimeBaseDir() string {
- if envDir := os.Getenv("XDG_RUNTIME_DIR"); envDir != "" {
- return envDir
- }
- uid := os.Getuid()
- return fmt.Sprintf("/run/user/%d", uid)
+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(baseDir, profile string) string {
- return filepath.Join(GetProfileNamespacePath(baseDir, profile), "pids")
+func GetPidsDirPath(pm *paths.PathManager, profile string) string {
+ return pm.ProfilePidsDir(profile)
}
// RegisterProcess marks the current process as using the specified profile.
-func RegisterProcess(baseDir, profile string) error {
- pidsDir := GetPidsDirPath(baseDir, 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)
}
@@ -45,9 +36,9 @@ func RegisterProcess(baseDir, profile string) error {
}
// UnregisterProcess removes the current process from the profile's tracking.
-func UnregisterProcess(baseDir, profile string) error {
+func UnregisterProcess(pm *paths.PathManager, profile string) error {
pid := os.Getpid()
- pidFile := filepath.Join(GetPidsDirPath(baseDir, profile), strconv.Itoa(pid))
+ 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)
}
@@ -55,8 +46,8 @@ func UnregisterProcess(baseDir, profile string) error {
}
// PruneStalePids removes PID files that no longer correspond to active processes.
-func PruneStalePids(baseDir, profile string) error {
- pidsDir := GetPidsDirPath(baseDir, profile)
+func PruneStalePids(pm *paths.PathManager, profile string) error {
+ pidsDir := GetPidsDirPath(pm, profile)
files, err := os.ReadDir(pidsDir)
if err != nil {
if os.IsNotExist(err) {
@@ -73,21 +64,25 @@ func PruneStalePids(baseDir, profile string) error {
process, err := os.FindProcess(pid)
if err != nil {
- os.Remove(filepath.Join(pidsDir, file.Name()))
+ 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 {
- os.Remove(filepath.Join(pidsDir, file.Name()))
+ 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(baseDir, profile string) (bool, error) {
- pidsDir := GetPidsDirPath(baseDir, 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) {