other OSs), and fails consistently if run with `go test -times=N`
On inspection, Go closes TCP connections when they go out of scope and
are garbage collected. I've re-written Test_Connections() to explicitly
close connectections once the test has finished. This has the other
benefit of closing gracefully, which means the -times argument should
work.
I've also removed the t.Skip() calls inside goroutines as they are
unsupported.
PROCESS_QUERY_INFORMATION was used for compatibility reasons with
Windows XP / Server 2003. Both are no longer supported in Golang
and haven't been for a while (since Go 1.11). It should be safe
to drop this flag. PROCESS_QUERY_LIMITED_INFORMATION supports
PPL process such as LSASS, which are not queryable without this
change.
Since Darwin / FreeBSD / OpenBSD all use process_bsd.go, remove
the duplicated "not implemented" EnvironWithContext definitions
from their custom go files.
Signed-off-by: Max Altgelt <max.altgelt@nextron-systems.com>
Add support (for Linux and Windows for now) for reading the
environment variables of a process, with a similar syntax to
os.Environ().
For Windows, this includes some refactoring for clean access to the
RTL_USER_PROCESS_PARAMETERS structure which points to the command
line and the environment variables.
Prior to this commit CGO was used in OpenBSD implementation of
Process.CmdlineSliceWithContext() for parsing the "kern.proc.args"
sysctl output. It requires some pointer arithmetics and raw pointer
dereferencing.
Having CGO in the "process" module prevents it from being go vet'ted
on any platform other than OpenBSD. In order to overcome this
limitation, the sysctl output parsing was reimplemented without raw
pointer deferencing. The resulting code might be slightly slower
than the original one, but it is cleaner and safer.
Since this fix allows go vet with GOOS=openbsd to run without any
issues on all platforms, openbsd entries were also added to the "vet"
Makefile target.
Co-authored-by: Sergey Vinogradov <cbrpnk@gmail.com>
In Linux containers running in LX Branded Zones on SmartOS (potentially
other Solaris-based OSes with LX Branded Zones),
fillFromTIDStatWithContext panics as the delayacct_blkio_ticks field is
not present and thus results in an out-of-bounds slice access.
Check the slice length before and only attempt to parse the
delayacct_blkio_ticks field if there is an appropriate number of fields.
The github.com/tklauser/go-sysconf package is already a dependency used
in the cpu and v3/cpu packages to determine clock ticks using
`sysconf.Sysconf(sysconf.SC_CLK_TCK)`, see #1036. Use the same in
packages process and v3/process as well instead of hard-coding clock
ticks to 100.
This is the arm64 version of the change in
20c3ddbfe5
Before this patch:
```
dave@m1 process % GO111MODULE=off CGO_ENABLED=0 go test
--- FAIL: Test_Process_Name (0.00s)
process_test.go:312: invalid Exe
--- FAIL: Test_Process_Long_Name_With_Spaces (0.22s)
process_test.go:357: loooong name with spaces.go.exe !=
--- FAIL: Test_Process_Long_Name (0.22s)
process_test.go:402: looooooooooooooooooooong.go.exe !=
--- FAIL: Test_Username (0.00s)
process_test.go:593:
Error Trace: process_test.go:593
Error: Not equal:
expected: "dave"
actual : "root"
Diff:
--- Expected
+++ Actual
@@ -1 +1 @@
-dave
+root
Test: Test_Username
process_test.go:595: root
FAIL
exit status 1
FAIL github.com/shirou/gopsutil/process 9.955s
```
After this patch:
```
dave@m1 process % GO111MODULE=off CGO_ENABLED=0 go test
PASS
ok github.com/shirou/gopsutil/process 9.784s
dave@m1 process % GO111MODULE=off CGO_ENABLED=1 go test
PASS
ok github.com/shirou/gopsutil/process 3.180s
```
Signed-off-by: David Scott <dave@recoil.org>
Benchmark before this change (process.NewProcess() calls process.PidExistsWithContext()
internally)
go test -bench=BenchmarkNewProcess github.com/shirou/gopsutil/process
goos: linux
goarch: amd64
pkg: github.com/shirou/gopsutil/process
BenchmarkNewProcess-4 14722 78751 ns/op
PASS
ok github.com/shirou/gopsutil/process 3.685s
Benchmark with this change applied
go test -bench=BenchmarkNewProcess github.com/shirou/gopsutil/process
goos: linux
goarch: amd64
pkg: github.com/shirou/gopsutil/process
BenchmarkNewProcess-4 14835 80180 ns/op
PASS
ok github.com/shirou/gopsutil/process 3.761s
* All context-less wrapping functions (the ones without WithContext
suffix) were moved into process.go since they all are the same.
* Call context is now passed to all underlying functions in
*WithContext() functions.
* All common *BSD bits were moved to process_bsd.go.
* Process.Tgid() method lacked a WithContext counterpart, so
Process.TgidWithContext() was added for uniformity.
* NewProcessWithContext() function was added since NewProcess() is
used a lot throughout the module, and there is no way to pass a
context to it.
This is a part of #761 effort.
More like a workaround, wanted to port process.getKProcWithContext() to use unix.SysctlRaw() to get rid of exec calls to ps
in the same time but didn't have time.
TestGetProcInodesAll: create a server so there are some opened inodes
TestUsers: skip if Users is empty, because of an empty /var/run/utmp
Test_Process_Groups: skip if Groups is empty
TestConnectionsMax: skip on CI, not only CircleCI
Continuation of #721, this type using mktypes.sh that I didn't know at the time.
Weirdly, `go tool cgo -godefs` doesn't produce propery go-fmt-ed code, had to do run go-fmt manually afterwards.
ARM has some tab characters in smaps instead of spaces, hence switching to strings.Fields instead of strings.Split which handles splitting on all whitespace instead of just spaces.
All functions used from github.com/shirou/w32 are also available from
golang.org/x/sys/windows which is already used in other places. Convert
the remaining usages to use the functions from x/sys/windows.
gopsutil is a transitive dependency of another project that I am integrating
into an internal build system. We target multiple platforms and as a part
of the build system for the large internal repo, we calculate the build
graph used to determine what targets have changed and need to be build /
tested as a single DAG for all platforms.
gopsutil currently does not form a DAG if linux and any other platform are
considered at the same time. linux is the only platform where the process
package imports the host package.
To remove this cycle, the relevant methods have been moved to internal/common
with the linux build tag and are consumed the host and process packages.
This commit add hour handling in convertCPUTimes function.
The time string usually comes from macOS command line:
ps -a -o stime,utime -p <pid>
which could contain hour string.