[VOL-4291] Rw-core updates for gRPC migration
Change-Id: I8d5a554409115b29318089671ca4e1ab3fa98810
diff --git a/vendor/github.com/prometheus/procfs/net_unix.go b/vendor/github.com/prometheus/procfs/net_unix.go
index 240340a..98aa8e1 100644
--- a/vendor/github.com/prometheus/procfs/net_unix.go
+++ b/vendor/github.com/prometheus/procfs/net_unix.go
@@ -15,7 +15,6 @@
import (
"bufio"
- "errors"
"fmt"
"io"
"os"
@@ -27,25 +26,15 @@
// see https://elixir.bootlin.com/linux/v4.17/source/net/unix/af_unix.c#L2815
// and https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/net.h#L48.
-const (
- netUnixKernelPtrIdx = iota
- netUnixRefCountIdx
- _
- netUnixFlagsIdx
- netUnixTypeIdx
- netUnixStateIdx
- netUnixInodeIdx
-
- // Inode and Path are optional.
- netUnixStaticFieldsCnt = 6
-)
-
+// Constants for the various /proc/net/unix enumerations.
+// TODO: match against x/sys/unix or similar?
const (
netUnixTypeStream = 1
netUnixTypeDgram = 2
netUnixTypeSeqpacket = 5
- netUnixFlagListen = 1 << 16
+ netUnixFlagDefault = 0
+ netUnixFlagListen = 1 << 16
netUnixStateUnconnected = 1
netUnixStateConnecting = 2
@@ -53,129 +42,127 @@
netUnixStateDisconnected = 4
)
-var errInvalidKernelPtrFmt = errors.New("Invalid Num(the kernel table slot number) format")
+// NetUNIXType is the type of the type field.
+type NetUNIXType uint64
-// NetUnixType is the type of the type field.
-type NetUnixType uint64
+// NetUNIXFlags is the type of the flags field.
+type NetUNIXFlags uint64
-// NetUnixFlags is the type of the flags field.
-type NetUnixFlags uint64
+// NetUNIXState is the type of the state field.
+type NetUNIXState uint64
-// NetUnixState is the type of the state field.
-type NetUnixState uint64
-
-// NetUnixLine represents a line of /proc/net/unix.
-type NetUnixLine struct {
+// NetUNIXLine represents a line of /proc/net/unix.
+type NetUNIXLine struct {
KernelPtr string
RefCount uint64
Protocol uint64
- Flags NetUnixFlags
- Type NetUnixType
- State NetUnixState
+ Flags NetUNIXFlags
+ Type NetUNIXType
+ State NetUNIXState
Inode uint64
Path string
}
-// NetUnix holds the data read from /proc/net/unix.
-type NetUnix struct {
- Rows []*NetUnixLine
+// NetUNIX holds the data read from /proc/net/unix.
+type NetUNIX struct {
+ Rows []*NetUNIXLine
}
-// NewNetUnix returns data read from /proc/net/unix.
-func NewNetUnix() (*NetUnix, error) {
- fs, err := NewFS(DefaultMountPoint)
- if err != nil {
- return nil, err
- }
-
- return fs.NewNetUnix()
+// NetUNIX returns data read from /proc/net/unix.
+func (fs FS) NetUNIX() (*NetUNIX, error) {
+ return readNetUNIX(fs.proc.Path("net/unix"))
}
-// NewNetUnix returns data read from /proc/net/unix.
-func (fs FS) NewNetUnix() (*NetUnix, error) {
- return NewNetUnixByPath(fs.proc.Path("net/unix"))
-}
-
-// NewNetUnixByPath returns data read from /proc/net/unix by file path.
-// It might returns an error with partial parsed data, if an error occur after some data parsed.
-func NewNetUnixByPath(path string) (*NetUnix, error) {
- f, err := os.Open(path)
+// readNetUNIX reads data in /proc/net/unix format from the specified file.
+func readNetUNIX(file string) (*NetUNIX, error) {
+ // This file could be quite large and a streaming read is desirable versus
+ // reading the entire contents at once.
+ f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
- return NewNetUnixByReader(f)
+
+ return parseNetUNIX(f)
}
-// NewNetUnixByReader returns data read from /proc/net/unix by a reader.
-// It might returns an error with partial parsed data, if an error occur after some data parsed.
-func NewNetUnixByReader(reader io.Reader) (*NetUnix, error) {
- nu := &NetUnix{
- Rows: make([]*NetUnixLine, 0, 32),
- }
- scanner := bufio.NewScanner(reader)
- // Omit the header line.
- scanner.Scan()
- header := scanner.Text()
- // From the man page of proc(5), it does not contain an Inode field,
- // but in actually it exists.
- // This code works for both cases.
- hasInode := strings.Contains(header, "Inode")
+// parseNetUNIX creates a NetUnix structure from the incoming stream.
+func parseNetUNIX(r io.Reader) (*NetUNIX, error) {
+ // Begin scanning by checking for the existence of Inode.
+ s := bufio.NewScanner(r)
+ s.Scan()
- minFieldsCnt := netUnixStaticFieldsCnt
+ // From the man page of proc(5), it does not contain an Inode field,
+ // but in actually it exists. This code works for both cases.
+ hasInode := strings.Contains(s.Text(), "Inode")
+
+ // Expect a minimum number of fields, but Inode and Path are optional:
+ // Num RefCount Protocol Flags Type St Inode Path
+ minFields := 6
if hasInode {
- minFieldsCnt++
+ minFields++
}
- for scanner.Scan() {
- line := scanner.Text()
- item, err := nu.parseLine(line, hasInode, minFieldsCnt)
+
+ var nu NetUNIX
+ for s.Scan() {
+ line := s.Text()
+ item, err := nu.parseLine(line, hasInode, minFields)
if err != nil {
- return nu, err
+ return nil, fmt.Errorf("failed to parse /proc/net/unix data %q: %w", line, err)
}
+
nu.Rows = append(nu.Rows, item)
}
- return nu, scanner.Err()
+ if err := s.Err(); err != nil {
+ return nil, fmt.Errorf("failed to scan /proc/net/unix data: %w", err)
+ }
+
+ return &nu, nil
}
-func (u *NetUnix) parseLine(line string, hasInode bool, minFieldsCnt int) (*NetUnixLine, error) {
+func (u *NetUNIX) parseLine(line string, hasInode bool, min int) (*NetUNIXLine, error) {
fields := strings.Fields(line)
- fieldsLen := len(fields)
- if fieldsLen < minFieldsCnt {
- return nil, fmt.Errorf(
- "Parse Unix domain failed: expect at least %d fields but got %d",
- minFieldsCnt, fieldsLen)
+
+ l := len(fields)
+ if l < min {
+ return nil, fmt.Errorf("expected at least %d fields but got %d", min, l)
}
- kernelPtr, err := u.parseKernelPtr(fields[netUnixKernelPtrIdx])
+
+ // Field offsets are as follows:
+ // Num RefCount Protocol Flags Type St Inode Path
+
+ kernelPtr := strings.TrimSuffix(fields[0], ":")
+
+ users, err := u.parseUsers(fields[1])
if err != nil {
- return nil, fmt.Errorf("Parse Unix domain num(%s) failed: %s", fields[netUnixKernelPtrIdx], err)
+ return nil, fmt.Errorf("failed to parse ref count %q: %w", fields[1], err)
}
- users, err := u.parseUsers(fields[netUnixRefCountIdx])
+
+ flags, err := u.parseFlags(fields[3])
if err != nil {
- return nil, fmt.Errorf("Parse Unix domain ref count(%s) failed: %s", fields[netUnixRefCountIdx], err)
+ return nil, fmt.Errorf("failed to parse flags %q: %w", fields[3], err)
}
- flags, err := u.parseFlags(fields[netUnixFlagsIdx])
+
+ typ, err := u.parseType(fields[4])
if err != nil {
- return nil, fmt.Errorf("Parse Unix domain flags(%s) failed: %s", fields[netUnixFlagsIdx], err)
+ return nil, fmt.Errorf("failed to parse type %q: %w", fields[4], err)
}
- typ, err := u.parseType(fields[netUnixTypeIdx])
+
+ state, err := u.parseState(fields[5])
if err != nil {
- return nil, fmt.Errorf("Parse Unix domain type(%s) failed: %s", fields[netUnixTypeIdx], err)
+ return nil, fmt.Errorf("failed to parse state %q: %w", fields[5], err)
}
- state, err := u.parseState(fields[netUnixStateIdx])
- if err != nil {
- return nil, fmt.Errorf("Parse Unix domain state(%s) failed: %s", fields[netUnixStateIdx], err)
- }
+
var inode uint64
if hasInode {
- inodeStr := fields[netUnixInodeIdx]
- inode, err = u.parseInode(inodeStr)
+ inode, err = u.parseInode(fields[6])
if err != nil {
- return nil, fmt.Errorf("Parse Unix domain inode(%s) failed: %s", inodeStr, err)
+ return nil, fmt.Errorf("failed to parse inode %q: %w", fields[6], err)
}
}
- nuLine := &NetUnixLine{
+ n := &NetUNIXLine{
KernelPtr: kernelPtr,
RefCount: users,
Type: typ,
@@ -185,61 +172,56 @@
}
// Path field is optional.
- if fieldsLen > minFieldsCnt {
- pathIdx := netUnixInodeIdx + 1
+ if l > min {
+ // Path occurs at either index 6 or 7 depending on whether inode is
+ // already present.
+ pathIdx := 7
if !hasInode {
pathIdx--
}
- nuLine.Path = fields[pathIdx]
+
+ n.Path = fields[pathIdx]
}
- return nuLine, nil
+ return n, nil
}
-func (u NetUnix) parseKernelPtr(str string) (string, error) {
- if !strings.HasSuffix(str, ":") {
- return "", errInvalidKernelPtrFmt
- }
- return str[:len(str)-1], nil
+func (u NetUNIX) parseUsers(s string) (uint64, error) {
+ return strconv.ParseUint(s, 16, 32)
}
-func (u NetUnix) parseUsers(hexStr string) (uint64, error) {
- return strconv.ParseUint(hexStr, 16, 32)
-}
-
-func (u NetUnix) parseProtocol(hexStr string) (uint64, error) {
- return strconv.ParseUint(hexStr, 16, 32)
-}
-
-func (u NetUnix) parseType(hexStr string) (NetUnixType, error) {
- typ, err := strconv.ParseUint(hexStr, 16, 16)
+func (u NetUNIX) parseType(s string) (NetUNIXType, error) {
+ typ, err := strconv.ParseUint(s, 16, 16)
if err != nil {
return 0, err
}
- return NetUnixType(typ), nil
+
+ return NetUNIXType(typ), nil
}
-func (u NetUnix) parseFlags(hexStr string) (NetUnixFlags, error) {
- flags, err := strconv.ParseUint(hexStr, 16, 32)
+func (u NetUNIX) parseFlags(s string) (NetUNIXFlags, error) {
+ flags, err := strconv.ParseUint(s, 16, 32)
if err != nil {
return 0, err
}
- return NetUnixFlags(flags), nil
+
+ return NetUNIXFlags(flags), nil
}
-func (u NetUnix) parseState(hexStr string) (NetUnixState, error) {
- st, err := strconv.ParseInt(hexStr, 16, 8)
+func (u NetUNIX) parseState(s string) (NetUNIXState, error) {
+ st, err := strconv.ParseInt(s, 16, 8)
if err != nil {
return 0, err
}
- return NetUnixState(st), nil
+
+ return NetUNIXState(st), nil
}
-func (u NetUnix) parseInode(inodeStr string) (uint64, error) {
- return strconv.ParseUint(inodeStr, 10, 64)
+func (u NetUNIX) parseInode(s string) (uint64, error) {
+ return strconv.ParseUint(s, 10, 64)
}
-func (t NetUnixType) String() string {
+func (t NetUNIXType) String() string {
switch t {
case netUnixTypeStream:
return "stream"
@@ -251,7 +233,7 @@
return "unknown"
}
-func (f NetUnixFlags) String() string {
+func (f NetUNIXFlags) String() string {
switch f {
case netUnixFlagListen:
return "listen"
@@ -260,7 +242,7 @@
}
}
-func (s NetUnixState) String() string {
+func (s NetUNIXState) String() string {
switch s {
case netUnixStateUnconnected:
return "unconnected"