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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
package file
import (
"bufio"
"bytes"
"fmt"
"os"
"strconv"
"strings"
)
type File interface {
String() string
Name() string
FlagsString() string
}
type FdFile struct {
fd int32
name string
Flags *int32 // TODO: This must be a pointer as via dup(2) - means, we need to synchronise access as well .. use atomicint
flagsFromFdInfo bool
}
func NewFd(fd int32, name []byte, flags int32) FdFile {
return FdFile{
fd: fd,
name: stringValue(name),
Flags: &flags,
}
}
func NewFdWithPid(fd int32, pid uint32) FdFile {
linkName, err := os.Readlink(fmt.Sprintf("/proc/%d/fd/%d", pid, fd))
if err != nil {
fmt.Println("DEBUG", err)
var unknownFlags int32 = -1
return FdFile{
fd: fd,
name: "?",
Flags: &unknownFlags, // Unknown flags at this point
flagsFromFdInfo: true,
}
}
flags, _ := readFlagsFromFdInfo(fd, pid)
return FdFile{
fd: fd,
name: linkName,
Flags: &flags,
flagsFromFdInfo: true,
}
}
func (f FdFile) Dup(fd int32) FdFile {
duppedFd := f
duppedFd.fd = fd
duppedFd.flagsFromFdInfo = false
return duppedFd
}
func readFlagsFromFdInfo(fd int32, pid uint32) (int32, error) {
data, err := os.ReadFile(fmt.Sprintf("/proc/%d/fdinfo/%d", pid, fd))
if err != nil {
return -1, err
}
scanner := bufio.NewScanner(bytes.NewReader(data))
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "flags:") {
flagsStr := strings.Fields(line)[1]
flags, err := strconv.ParseUint(flagsStr, 8, 32)
return int32(flags), err
}
}
return -1, scanner.Err()
}
func (f FdFile) Name() string {
return f.name
}
func (f FdFile) FlagsString() string {
var flags int32 = -1
if f.Flags != nil {
flags = *f.Flags
}
return flagsToStr(flags)
}
func (f FdFile) String() string {
var sb strings.Builder
if len(f.name) == 0 {
sb.WriteString("?")
} else {
sb.WriteString(f.name)
sb.WriteString(" (")
sb.WriteString(strconv.FormatInt(int64(f.fd), 10))
sb.WriteString(",")
sb.WriteString(f.FlagsString())
sb.WriteString(")")
if f.flagsFromFdInfo {
sb.WriteString(" flags from fd info")
}
}
return sb.String()
}
type oldnameNewnameFile struct {
Oldname, Newname string
}
func NewOldnameNewname(oldname, newname []byte) oldnameNewnameFile {
return oldnameNewnameFile{stringValue(oldname), stringValue(newname)}
}
func (f oldnameNewnameFile) Name() string {
return f.Newname
}
func (f oldnameNewnameFile) FlagsString() string {
return ""
}
func (f oldnameNewnameFile) String() string {
var sb strings.Builder
sb.WriteString("old:")
sb.WriteString(f.Oldname)
sb.WriteString(" ->new:")
sb.WriteString(f.Newname)
sb.WriteString(" (")
sb.WriteString(f.FlagsString())
sb.WriteString(")")
return sb.String()
}
type pathnameFile struct {
Pathname string
}
func NewPathname(pathname []byte) pathnameFile {
return pathnameFile{stringValue(pathname)}
}
func (f pathnameFile) Name() string {
return f.Pathname
}
func (f pathnameFile) FlagsString() string {
return ""
}
func (f pathnameFile) String() string {
var sb strings.Builder
sb.WriteString("pathname:")
sb.WriteString(f.Pathname)
sb.WriteString(" (")
sb.WriteString(f.FlagsString())
sb.WriteString(")")
return sb.String()
}
// As data comes in from arrays, converted to slices, there will be null-bytes at the end..
func stringValue(byteStr []byte) string {
return string(byteStr[:bytes.IndexByte(byteStr, 0)])
}
|