2019-08-28 06:55:22 +00:00
|
|
|
// Copyright 2018 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package procfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// For the proc file format details,
|
|
|
|
// 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.
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
// Constants for the various /proc/net/unix enumerations.
|
|
|
|
// TODO: match against x/sys/unix or similar?
|
2019-08-28 06:55:22 +00:00
|
|
|
const (
|
|
|
|
netUnixTypeStream = 1
|
|
|
|
netUnixTypeDgram = 2
|
|
|
|
netUnixTypeSeqpacket = 5
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
netUnixFlagDefault = 0
|
|
|
|
netUnixFlagListen = 1 << 16
|
2019-08-28 06:55:22 +00:00
|
|
|
|
|
|
|
netUnixStateUnconnected = 1
|
|
|
|
netUnixStateConnecting = 2
|
|
|
|
netUnixStateConnected = 3
|
|
|
|
netUnixStateDisconnected = 4
|
|
|
|
)
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
// NetUNIXType is the type of the type field.
|
|
|
|
type NetUNIXType uint64
|
2019-08-28 06:55:22 +00:00
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
// NetUNIXFlags is the type of the flags field.
|
|
|
|
type NetUNIXFlags uint64
|
2019-08-28 06:55:22 +00:00
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
// NetUNIXState is the type of the state field.
|
|
|
|
type NetUNIXState uint64
|
2019-08-28 06:55:22 +00:00
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
// NetUNIXLine represents a line of /proc/net/unix.
|
|
|
|
type NetUNIXLine struct {
|
2019-08-28 06:55:22 +00:00
|
|
|
KernelPtr string
|
|
|
|
RefCount uint64
|
|
|
|
Protocol uint64
|
2020-10-16 05:06:27 +00:00
|
|
|
Flags NetUNIXFlags
|
|
|
|
Type NetUNIXType
|
|
|
|
State NetUNIXState
|
2019-08-28 06:55:22 +00:00
|
|
|
Inode uint64
|
|
|
|
Path string
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
// NetUNIX holds the data read from /proc/net/unix.
|
|
|
|
type NetUNIX struct {
|
|
|
|
Rows []*NetUNIXLine
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
// NetUNIX returns data read from /proc/net/unix.
|
|
|
|
func (fs FS) NetUNIX() (*NetUNIX, error) {
|
|
|
|
return readNetUNIX(fs.proc.Path("net/unix"))
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
// 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)
|
2019-08-28 06:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2020-10-16 05:06:27 +00:00
|
|
|
|
|
|
|
return parseNetUNIX(f)
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
// 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()
|
|
|
|
|
2019-08-28 06:55:22 +00:00
|
|
|
// From the man page of proc(5), it does not contain an Inode field,
|
2020-10-16 05:06:27 +00:00
|
|
|
// but in actually it exists. This code works for both cases.
|
|
|
|
hasInode := strings.Contains(s.Text(), "Inode")
|
2019-08-28 06:55:22 +00:00
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
// Expect a minimum number of fields, but Inode and Path are optional:
|
|
|
|
// Num RefCount Protocol Flags Type St Inode Path
|
|
|
|
minFields := 6
|
2019-08-28 06:55:22 +00:00
|
|
|
if hasInode {
|
2020-10-16 05:06:27 +00:00
|
|
|
minFields++
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
|
|
|
var nu NetUNIX
|
|
|
|
for s.Scan() {
|
|
|
|
line := s.Text()
|
|
|
|
item, err := nu.parseLine(line, hasInode, minFields)
|
2019-08-28 06:55:22 +00:00
|
|
|
if err != nil {
|
2021-02-28 23:08:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse /proc/net/unix data %q: %w", line, err)
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2019-08-28 06:55:22 +00:00
|
|
|
nu.Rows = append(nu.Rows, item)
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
if err := s.Err(); err != nil {
|
2021-02-28 23:08:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to scan /proc/net/unix data: %w", err)
|
2020-10-16 05:06:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &nu, nil
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
func (u *NetUNIX) parseLine(line string, hasInode bool, min int) (*NetUNIXLine, error) {
|
2019-08-28 06:55:22 +00:00
|
|
|
fields := strings.Fields(line)
|
2020-10-16 05:06:27 +00:00
|
|
|
|
|
|
|
l := len(fields)
|
|
|
|
if l < min {
|
|
|
|
return nil, fmt.Errorf("expected at least %d fields but got %d", min, l)
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
|
|
|
// Field offsets are as follows:
|
|
|
|
// Num RefCount Protocol Flags Type St Inode Path
|
|
|
|
|
|
|
|
kernelPtr := strings.TrimSuffix(fields[0], ":")
|
|
|
|
|
|
|
|
users, err := u.parseUsers(fields[1])
|
2019-08-28 06:55:22 +00:00
|
|
|
if err != nil {
|
2021-02-28 23:08:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse ref count %q: %w", fields[1], err)
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
|
|
|
flags, err := u.parseFlags(fields[3])
|
2019-08-28 06:55:22 +00:00
|
|
|
if err != nil {
|
2021-02-28 23:08:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse flags %q: %w", fields[3], err)
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
|
|
|
typ, err := u.parseType(fields[4])
|
2019-08-28 06:55:22 +00:00
|
|
|
if err != nil {
|
2021-02-28 23:08:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse type %q: %w", fields[4], err)
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
|
|
|
state, err := u.parseState(fields[5])
|
2019-08-28 06:55:22 +00:00
|
|
|
if err != nil {
|
2021-02-28 23:08:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse state %q: %w", fields[5], err)
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
2019-08-28 06:55:22 +00:00
|
|
|
var inode uint64
|
|
|
|
if hasInode {
|
2020-10-16 05:06:27 +00:00
|
|
|
inode, err = u.parseInode(fields[6])
|
2019-08-28 06:55:22 +00:00
|
|
|
if err != nil {
|
2021-02-28 23:08:33 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse inode %q: %w", fields[6], err)
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
n := &NetUNIXLine{
|
2019-08-28 06:55:22 +00:00
|
|
|
KernelPtr: kernelPtr,
|
|
|
|
RefCount: users,
|
|
|
|
Type: typ,
|
|
|
|
Flags: flags,
|
|
|
|
State: state,
|
|
|
|
Inode: inode,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Path field is optional.
|
2020-10-16 05:06:27 +00:00
|
|
|
if l > min {
|
|
|
|
// Path occurs at either index 6 or 7 depending on whether inode is
|
|
|
|
// already present.
|
|
|
|
pathIdx := 7
|
2019-08-28 06:55:22 +00:00
|
|
|
if !hasInode {
|
|
|
|
pathIdx--
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
n.Path = fields[pathIdx]
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
return n, nil
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
func (u NetUNIX) parseUsers(s string) (uint64, error) {
|
|
|
|
return strconv.ParseUint(s, 16, 32)
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
func (u NetUNIX) parseType(s string) (NetUNIXType, error) {
|
|
|
|
typ, err := strconv.ParseUint(s, 16, 16)
|
2019-08-28 06:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
|
|
|
return NetUNIXType(typ), nil
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
func (u NetUNIX) parseFlags(s string) (NetUNIXFlags, error) {
|
|
|
|
flags, err := strconv.ParseUint(s, 16, 32)
|
2019-08-28 06:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
|
|
|
return NetUNIXFlags(flags), nil
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
func (u NetUNIX) parseState(s string) (NetUNIXState, error) {
|
|
|
|
st, err := strconv.ParseInt(s, 16, 8)
|
2019-08-28 06:55:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2020-10-16 05:06:27 +00:00
|
|
|
|
|
|
|
return NetUNIXState(st), nil
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
func (u NetUNIX) parseInode(s string) (uint64, error) {
|
|
|
|
return strconv.ParseUint(s, 10, 64)
|
2019-08-28 06:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
func (t NetUNIXType) String() string {
|
2019-08-28 06:55:22 +00:00
|
|
|
switch t {
|
|
|
|
case netUnixTypeStream:
|
|
|
|
return "stream"
|
|
|
|
case netUnixTypeDgram:
|
|
|
|
return "dgram"
|
|
|
|
case netUnixTypeSeqpacket:
|
|
|
|
return "seqpacket"
|
|
|
|
}
|
|
|
|
return "unknown"
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
func (f NetUNIXFlags) String() string {
|
2019-08-28 06:55:22 +00:00
|
|
|
switch f {
|
|
|
|
case netUnixFlagListen:
|
|
|
|
return "listen"
|
|
|
|
default:
|
|
|
|
return "default"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 05:06:27 +00:00
|
|
|
func (s NetUNIXState) String() string {
|
2019-08-28 06:55:22 +00:00
|
|
|
switch s {
|
|
|
|
case netUnixStateUnconnected:
|
|
|
|
return "unconnected"
|
|
|
|
case netUnixStateConnecting:
|
|
|
|
return "connecting"
|
|
|
|
case netUnixStateConnected:
|
|
|
|
return "connected"
|
|
|
|
case netUnixStateDisconnected:
|
|
|
|
return "disconnected"
|
|
|
|
}
|
|
|
|
return "unknown"
|
|
|
|
}
|