-
Notifications
You must be signed in to change notification settings - Fork 8
/
sysstate.go
84 lines (70 loc) · 1.3 KB
/
sysstate.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
package main
import "fmt"
type SysState struct {
FS *FS
Proc *Proc
}
type FS struct {
seq int
pipe int
inodePath map[int]string
pathInode map[string]int
}
type Proc struct {
lastID int
}
func NewSysState() *SysState {
return &SysState{FS: NewFS(), Proc: NewProc()}
}
func NewFS() *FS {
return &FS{
inodePath: map[int]string{},
pathInode: map[string]int{},
}
}
func (fs *FS) Inode(path string) int {
if inode, ok := fs.pathInode[path]; ok {
return inode
}
fs.seq++
fs.inodePath[fs.seq] = path
fs.pathInode[path] = fs.seq
return fs.seq
}
func (fs *FS) Path(inode int) string {
return fs.inodePath[inode]
}
func (fs *FS) Pipe() int {
fs.pipe++
return fs.Inode(fmt.Sprint("/dev/fptrace/pipe/", fs.pipe))
}
func (fs *FS) Rename(oldpath, newpath string) {
if oldpath == newpath {
return
}
oldInode := fs.pathInode[oldpath]
delete(fs.pathInode, oldpath)
fs.pathInode[newpath] = oldInode
fs.inodePath[oldInode] = newpath
}
func NewProc() *Proc {
return &Proc{}
}
func (p *Proc) NextID() int {
return p.lastID + 1
}
func (p *Proc) Exec(ps *ProcState) {
cmd := ps.NextCmd
if ps.CurCmd != nil {
cmd.Parent = ps.CurCmd.ID
}
p.lastID++
cmd.ID = p.lastID
ps.CurCmd = &cmd
for n, b := range ps.FDCX {
if b {
delete(ps.FDs, n)
}
}
ps.FDCX = make(map[int32]bool)
}