summaryrefslogtreecommitdiff
path: root/pkg/libcontainer/nsinit/state.go
blob: 26d7fa4230378ca719b8ba742b2ce339d4f55949 (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
package nsinit

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
)

// StateWriter handles writing and deleting the pid file
// on disk
type StateWriter interface {
	WritePid(pid int, startTime string) error
	DeletePid() error
}

type DefaultStateWriter struct {
	Root string
}

// writePidFile writes the namespaced processes pid to pid in the rootfs for the container
func (d *DefaultStateWriter) WritePid(pid int, startTime string) error {
	err := ioutil.WriteFile(filepath.Join(d.Root, "pid"), []byte(fmt.Sprint(pid)), 0655)
	if err != nil {
		return err
	}
	return ioutil.WriteFile(filepath.Join(d.Root, "start"), []byte(startTime), 0655)
}

func (d *DefaultStateWriter) DeletePid() error {
	err := os.Remove(filepath.Join(d.Root, "pid"))
	if serr := os.Remove(filepath.Join(d.Root, "start")); err == nil {
		err = serr
	}
	return err
}