khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | // Copyright 2018 The Prometheus Authors |
| 2 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | // you may not use this file except in compliance with the License. |
| 4 | // You may obtain a copy of the License at |
| 5 | // |
| 6 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | // |
| 8 | // Unless required by applicable law or agreed to in writing, software |
| 9 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | // See the License for the specific language governing permissions and |
| 12 | // limitations under the License. |
| 13 | |
| 14 | package procfs |
| 15 | |
| 16 | import ( |
| 17 | "bufio" |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 18 | "fmt" |
| 19 | "io" |
| 20 | "os" |
| 21 | "strconv" |
| 22 | "strings" |
| 23 | ) |
| 24 | |
| 25 | // For the proc file format details, |
| 26 | // see https://elixir.bootlin.com/linux/v4.17/source/net/unix/af_unix.c#L2815 |
| 27 | // and https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/net.h#L48. |
| 28 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 29 | // Constants for the various /proc/net/unix enumerations. |
| 30 | // TODO: match against x/sys/unix or similar? |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 31 | const ( |
| 32 | netUnixTypeStream = 1 |
| 33 | netUnixTypeDgram = 2 |
| 34 | netUnixTypeSeqpacket = 5 |
| 35 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 36 | netUnixFlagDefault = 0 |
| 37 | netUnixFlagListen = 1 << 16 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 38 | |
| 39 | netUnixStateUnconnected = 1 |
| 40 | netUnixStateConnecting = 2 |
| 41 | netUnixStateConnected = 3 |
| 42 | netUnixStateDisconnected = 4 |
| 43 | ) |
| 44 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 45 | // NetUNIXType is the type of the type field. |
| 46 | type NetUNIXType uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 47 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 48 | // NetUNIXFlags is the type of the flags field. |
| 49 | type NetUNIXFlags uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 50 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 51 | // NetUNIXState is the type of the state field. |
| 52 | type NetUNIXState uint64 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 53 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 54 | // NetUNIXLine represents a line of /proc/net/unix. |
| 55 | type NetUNIXLine struct { |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 56 | KernelPtr string |
| 57 | RefCount uint64 |
| 58 | Protocol uint64 |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 59 | Flags NetUNIXFlags |
| 60 | Type NetUNIXType |
| 61 | State NetUNIXState |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 62 | Inode uint64 |
| 63 | Path string |
| 64 | } |
| 65 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 66 | // NetUNIX holds the data read from /proc/net/unix. |
| 67 | type NetUNIX struct { |
| 68 | Rows []*NetUNIXLine |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 69 | } |
| 70 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 71 | // NetUNIX returns data read from /proc/net/unix. |
| 72 | func (fs FS) NetUNIX() (*NetUNIX, error) { |
| 73 | return readNetUNIX(fs.proc.Path("net/unix")) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 74 | } |
| 75 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 76 | // readNetUNIX reads data in /proc/net/unix format from the specified file. |
| 77 | func readNetUNIX(file string) (*NetUNIX, error) { |
| 78 | // This file could be quite large and a streaming read is desirable versus |
| 79 | // reading the entire contents at once. |
| 80 | f, err := os.Open(file) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 81 | if err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | defer f.Close() |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 85 | |
| 86 | return parseNetUNIX(f) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 87 | } |
| 88 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 89 | // parseNetUNIX creates a NetUnix structure from the incoming stream. |
| 90 | func parseNetUNIX(r io.Reader) (*NetUNIX, error) { |
| 91 | // Begin scanning by checking for the existence of Inode. |
| 92 | s := bufio.NewScanner(r) |
| 93 | s.Scan() |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 94 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 95 | // From the man page of proc(5), it does not contain an Inode field, |
| 96 | // but in actually it exists. This code works for both cases. |
| 97 | hasInode := strings.Contains(s.Text(), "Inode") |
| 98 | |
| 99 | // Expect a minimum number of fields, but Inode and Path are optional: |
| 100 | // Num RefCount Protocol Flags Type St Inode Path |
| 101 | minFields := 6 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 102 | if hasInode { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 103 | minFields++ |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 104 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 105 | |
| 106 | var nu NetUNIX |
| 107 | for s.Scan() { |
| 108 | line := s.Text() |
| 109 | item, err := nu.parseLine(line, hasInode, minFields) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 110 | if err != nil { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 111 | return nil, fmt.Errorf("failed to parse /proc/net/unix data %q: %w", line, err) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 112 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 113 | |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 114 | nu.Rows = append(nu.Rows, item) |
| 115 | } |
| 116 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 117 | if err := s.Err(); err != nil { |
| 118 | return nil, fmt.Errorf("failed to scan /proc/net/unix data: %w", err) |
| 119 | } |
| 120 | |
| 121 | return &nu, nil |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 122 | } |
| 123 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 124 | func (u *NetUNIX) parseLine(line string, hasInode bool, min int) (*NetUNIXLine, error) { |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 125 | fields := strings.Fields(line) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 126 | |
| 127 | l := len(fields) |
| 128 | if l < min { |
| 129 | return nil, fmt.Errorf("expected at least %d fields but got %d", min, l) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 130 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 131 | |
| 132 | // Field offsets are as follows: |
| 133 | // Num RefCount Protocol Flags Type St Inode Path |
| 134 | |
| 135 | kernelPtr := strings.TrimSuffix(fields[0], ":") |
| 136 | |
| 137 | users, err := u.parseUsers(fields[1]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 138 | if err != nil { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 139 | return nil, fmt.Errorf("failed to parse ref count %q: %w", fields[1], err) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 140 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 141 | |
| 142 | flags, err := u.parseFlags(fields[3]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 143 | if err != nil { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 144 | return nil, fmt.Errorf("failed to parse flags %q: %w", fields[3], err) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 145 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 146 | |
| 147 | typ, err := u.parseType(fields[4]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 148 | if err != nil { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 149 | return nil, fmt.Errorf("failed to parse type %q: %w", fields[4], err) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 150 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 151 | |
| 152 | state, err := u.parseState(fields[5]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 153 | if err != nil { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 154 | return nil, fmt.Errorf("failed to parse state %q: %w", fields[5], err) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 155 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 156 | |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 157 | var inode uint64 |
| 158 | if hasInode { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 159 | inode, err = u.parseInode(fields[6]) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 160 | if err != nil { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 161 | return nil, fmt.Errorf("failed to parse inode %q: %w", fields[6], err) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 165 | n := &NetUNIXLine{ |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 166 | KernelPtr: kernelPtr, |
| 167 | RefCount: users, |
| 168 | Type: typ, |
| 169 | Flags: flags, |
| 170 | State: state, |
| 171 | Inode: inode, |
| 172 | } |
| 173 | |
| 174 | // Path field is optional. |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 175 | if l > min { |
| 176 | // Path occurs at either index 6 or 7 depending on whether inode is |
| 177 | // already present. |
| 178 | pathIdx := 7 |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 179 | if !hasInode { |
| 180 | pathIdx-- |
| 181 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 182 | |
| 183 | n.Path = fields[pathIdx] |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 184 | } |
| 185 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 186 | return n, nil |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 187 | } |
| 188 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 189 | func (u NetUNIX) parseUsers(s string) (uint64, error) { |
| 190 | return strconv.ParseUint(s, 16, 32) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 191 | } |
| 192 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 193 | func (u NetUNIX) parseType(s string) (NetUNIXType, error) { |
| 194 | typ, err := strconv.ParseUint(s, 16, 16) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 195 | if err != nil { |
| 196 | return 0, err |
| 197 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 198 | |
| 199 | return NetUNIXType(typ), nil |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 200 | } |
| 201 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 202 | func (u NetUNIX) parseFlags(s string) (NetUNIXFlags, error) { |
| 203 | flags, err := strconv.ParseUint(s, 16, 32) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 204 | if err != nil { |
| 205 | return 0, err |
| 206 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 207 | |
| 208 | return NetUNIXFlags(flags), nil |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 209 | } |
| 210 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 211 | func (u NetUNIX) parseState(s string) (NetUNIXState, error) { |
| 212 | st, err := strconv.ParseInt(s, 16, 8) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 213 | if err != nil { |
| 214 | return 0, err |
| 215 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 216 | |
| 217 | return NetUNIXState(st), nil |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 218 | } |
| 219 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 220 | func (u NetUNIX) parseInode(s string) (uint64, error) { |
| 221 | return strconv.ParseUint(s, 10, 64) |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 222 | } |
| 223 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 224 | func (t NetUNIXType) String() string { |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 225 | switch t { |
| 226 | case netUnixTypeStream: |
| 227 | return "stream" |
| 228 | case netUnixTypeDgram: |
| 229 | return "dgram" |
| 230 | case netUnixTypeSeqpacket: |
| 231 | return "seqpacket" |
| 232 | } |
| 233 | return "unknown" |
| 234 | } |
| 235 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 236 | func (f NetUNIXFlags) String() string { |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 237 | switch f { |
| 238 | case netUnixFlagListen: |
| 239 | return "listen" |
| 240 | default: |
| 241 | return "default" |
| 242 | } |
| 243 | } |
| 244 | |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 245 | func (s NetUNIXState) String() string { |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 246 | switch s { |
| 247 | case netUnixStateUnconnected: |
| 248 | return "unconnected" |
| 249 | case netUnixStateConnecting: |
| 250 | return "connecting" |
| 251 | case netUnixStateConnected: |
| 252 | return "connected" |
| 253 | case netUnixStateDisconnected: |
| 254 | return "disconnected" |
| 255 | } |
| 256 | return "unknown" |
| 257 | } |