-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpid.go
54 lines (48 loc) · 1.06 KB
/
pid.go
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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"syscall"
)
func writeSlirpPIDFile(pidFile string, slirpPID int) (err error) {
if err = ioutil.WriteFile(pidFile, []byte(strconv.Itoa(slirpPID)), 0600); err != nil {
return fmt.Errorf("error writing slirp PID file: %v", err)
}
return
}
func getSlirpPID(pidFile string) (slirpPID int, err error) {
b, err := ioutil.ReadFile(pidFile)
if err != nil {
if os.IsNotExist(err) {
return 0, nil
}
return
}
slirpPID, err = strconv.Atoi(string(b))
if err != nil || !pidExists(slirpPID) {
return 0, nil
}
return
}
func deleteSlirpPIDFile(pidFile string) (err error) {
if e := os.Remove(pidFile); e != nil {
if !os.IsNotExist(e) {
err = fmt.Errorf("remove slirp PID file: %v", e)
}
}
return
}
func slirpPIDFile(containerID, ifName string) string {
return filepath.Join("/dev/shm", "slirp4netns-"+containerID+"-"+ifName)
}
func pidExists(pid int) bool {
proc, err := os.FindProcess(pid)
if err != nil {
return false
}
err = proc.Signal(syscall.Signal(0))
return err == nil
}