2014-02-22 08:29:21 +00:00
|
|
|
package nsinit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2014-02-22 09:21:26 +00:00
|
|
|
"path/filepath"
|
2014-02-22 08:29:21 +00:00
|
|
|
)
|
|
|
|
|
2014-02-25 05:11:52 +00:00
|
|
|
// StateWriter handles writing and deleting the pid file
|
|
|
|
// on disk
|
2014-02-22 08:29:21 +00:00
|
|
|
type StateWriter interface {
|
2014-03-26 06:48:16 +00:00
|
|
|
WritePid(pid int, startTime string) error
|
2014-02-22 08:29:21 +00:00
|
|
|
DeletePid() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultStateWriter struct {
|
2014-02-22 09:21:26 +00:00
|
|
|
Root string
|
2014-02-22 08:29:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 20:41:31 +00:00
|
|
|
// writePidFile writes the namespaced processes pid to pid in the rootfs for the container
|
2014-03-26 06:48:16 +00:00
|
|
|
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)
|
2014-02-22 08:29:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 09:21:26 +00:00
|
|
|
func (d *DefaultStateWriter) DeletePid() error {
|
2014-03-26 06:48:16 +00:00
|
|
|
err := os.Remove(filepath.Join(d.Root, "pid"))
|
|
|
|
if serr := os.Remove(filepath.Join(d.Root, "start")); err == nil {
|
|
|
|
err = serr
|
|
|
|
}
|
|
|
|
return err
|
2014-02-22 08:29:21 +00:00
|
|
|
}
|