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.go27
1 files changed, 27 insertions, 0 deletions
diff --git a/internal/namespace/lifecycle.go b/internal/namespace/lifecycle.go
index 47a804f..99209d5 100644
--- a/internal/namespace/lifecycle.go
+++ b/internal/namespace/lifecycle.go
@@ -20,6 +20,11 @@ func GetPidsDirPath(pm *paths.PathManager, profile string) string {
return pm.ProfilePidsDir(profile)
}
+// GetControllerPidPath returns the path to the file storing the PID of the tunnel controller.
+func GetControllerPidPath(pm *paths.PathManager, profile string) string {
+ return filepath.Join(pm.RuntimeBaseDir(), "profiles", profile, "controller.pid")
+}
+
// RegisterProcess marks the current process as using the specified profile.
func RegisterProcess(pm *paths.PathManager, profile string) error {
pidsDir := GetPidsDirPath(pm, profile)
@@ -57,6 +62,9 @@ func PruneStalePids(pm *paths.PathManager, profile string) error {
}
for _, file := range files {
+ if file.Name() == "controller.pid" {
+ continue
+ }
pid, err := strconv.Atoi(file.Name())
if err != nil {
continue // Ignore non-numeric files
@@ -108,3 +116,22 @@ func IsLastProcess(pm *paths.PathManager, profile string) (bool, error) {
return activeCount <= 1, nil
}
+
+// SetControllerPid records the current process as the owner of the namespace.
+func SetControllerPid(pm *paths.PathManager, profile string) error {
+ path := GetControllerPidPath(pm, profile)
+ if err := os.WriteFile(path, []byte(strconv.Itoa(os.Getpid())), 0644); err != nil {
+ return fmt.Errorf("failed to write controller pid: %w", err)
+ }
+ return nil
+}
+
+// GetControllerPid retrieves the PID of the process responsible for cleaning up the namespace.
+func GetControllerPid(pm *paths.PathManager, profile string) (int, error) {
+ path := GetControllerPidPath(pm, profile)
+ data, err := os.ReadFile(path)
+ if err != nil {
+ return 0, err
+ }
+ return strconv.Atoi(string(data))
+}