netstat: check the scanner result and error (#868)
parseNetstat discards the first Scan result and never calls Err, so a scanner failure is invisible. The loop then returns netStat, nil with whatever was parsed before the failure, and the caller cannot tell a short read from a complete one.
The header case is worse than a lost error. After Scan reports false for a too-long token, the next Scan returns true with the bytes already buffered, so headers stays empty while the counter loop runs, and headers[num] panics with index out of range.
net_dev.go, net_sockstat.go, net_unix.go and stat.go all end their scan with scanner.Err(); this is the outlier.
Signed-off-by: manon youdie006@users.noreply.github.com Co-authored-by: manon youdie006@users.noreply.github.com Co-authored-by: Ben Kochie superq@gmail.com
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
procfs
This package provides functions to retrieve system, kernel, and process metrics from the pseudo-filesystems /proc and /sys.
WARNING: This package is a work in progress. Its API may still break in backwards-incompatible ways without warnings. Use it at your own risk.
Usage
The procfs library is organized by packages based on whether the gathered data is coming from /proc, /sys, or both. Each package contains an
FStype which represents the path to either /proc, /sys, or both. For example, cpu statistics are gathered from/proc/statand are available via the root procfs package. First, the proc filesystem mount point is initialized, and then the stat information is read.Some sub-packages such as
blockdevice, require access to both the proc and sys filesystems.Package Organization
The packages in this project are organized according to (1) whether the data comes from the
/procor/sysfilesystem and (2) the type of information being retrieved. For example, most process information can be gathered from the functions in the rootprocfspackage. Information about block devices such as disk drives is available in theblockdevicessub-package.Building and Testing
The procfs library is intended to be built as part of another application, so there are no distributable binaries.
However, most of the API includes unit tests which can be run with
make test.Updating Test Fixtures
The procfs library includes a set of test fixtures which include many example files from the
/procand/sysfilesystems. These fixtures are included as a ttar file which is extracted automatically during testing. To add/update the test fixtures, first ensure thetestdata/fixturesdirectory is up to date by removing the existing directory and then extracting the ttar file usingmake testdata/fixtures/.unpackedor justmake test.Next, make the required changes to the extracted files in the
testdata/fixturesdirectory. When the changes are complete, runmake update_fixturesto create a newfixtures.ttarfile based on the updatedfixturesdirectory. And finally, verify the changes usinggit diff testdata/fixtures.ttar.