-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvjhandle_freebsd.go
101 lines (89 loc) · 2.16 KB
/
vjhandle_freebsd.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package vnet
import (
"fmt"
"os"
"golang.org/x/sys/unix"
)
// NsHandle is a handle to a vnet jail. It can be cast directly
// to an int and used as a jail ID.
type VjHandle int
// Equal determines if two vnet handles refer to the same vnet jail.
// This is done by comparing the device and inode that the jail point to.
func (vj VjHandle) Equal(other VjHandle) bool {
if vj == other {
return true
}
var s1, s2 unix.Stat_t
f1, err := os.Open(vnetPath(vj))
if err != nil {
return false
}
defer f1.Close()
f2, err := os.Open(vnetPath(other))
if err != nil {
return false
}
defer f2.Close()
if err = unix.Fstat(int(f1.Fd()), &s1); err != nil {
return false
}
if err = unix.Fstat(int(f2.Fd()), &s2); err != nil {
return false
}
return (s1.Dev == s2.Dev) && (s1.Ino == s2.Ino)
}
// String shows the jail ID and dev and inode.
func (vj VjHandle) String() string {
if vj == -1 {
return "vnet(none)"
}
var s unix.Stat_t
f, err := os.Open(vnetPath(vj))
if err != nil {
return "vnet(none)"
}
defer f.Close()
if err := unix.Fstat(int(f.Fd()), &s); err != nil {
return "vnet(unknown)"
}
return fmt.Sprintf("vnet(%d: %d, %d)", vj, s.Dev, s.Ino)
}
// UniqueId returns a string which uniquely identifies the jail
// associated with the VjHandle.
func (vj VjHandle) UniqueId() string {
if vj == -1 {
return "vnet(none)"
}
var s unix.Stat_t
f, err := os.Open(vnetPath(vj))
if err != nil {
return "vnet(none)"
}
defer f.Close()
if err := unix.Fstat(int(f.Fd()), &s); err != nil {
return "vnet(unknown)"
}
return fmt.Sprintf("vnet(%d:%d)", s.Dev, s.Ino)
}
// IsOpen returns true if Close() has not been called.
func (vj VjHandle) IsOpen() bool {
return vj != -1
}
// Close closes the NsHandle and resets its file descriptor to -1.
// In FreeBSD, Close must be called after the process in jail.
func (vj VjHandle) Close() error {
_, _, errno := unix.Syscall(unix.SYS_JAIL_REMOVE, uintptr(int(vj)), 0, 0)
if errno != 0 {
return fmt.Errorf("jail_remove failed: %s", errno.Error())
}
err := os.Remove(vnetPath(vj))
if err != nil {
return err
}
vj = -1
return nil
}
// None gets an empty (closed) NsHandle.
func None() VjHandle {
return VjHandle(-1)
}