Commit Graph

462 Commits (master)

Author SHA1 Message Date
shirou 0665cafa1b chore: replace deprecated ioutil package to os and io 2 years ago
shirou febdeab871 chore: change CIRCLECI environment variable to CI. 2 years ago
Antoine Toulme 54c31d884d
Revert "test results"
This reverts commit ae119d36b8.
2 years ago
Antoine Toulme ae119d36b8 test results 2 years ago
Antoine Toulme 6fff1c0e99
proper benchmark 2 years ago
Antoine Toulme ca71a6db3c
lint 2 years ago
Antoine Toulme a48fd71137
Revert "send results"
This reverts commit a7a94986fb.
2 years ago
Antoine Toulme a7a94986fb send results 2 years ago
Antoine Toulme 4bc9e37b0f
faster file read 2 years ago
Lomanic 8b96d2e9e2 [process][posix] Realign process.Name() with python psutil to return same value on python3 scripts processes
e2c79a1 started to blindly set the process name to the full path (instead of the basename) of the cmdline exectuable
if the process name from the process comm was truncated on linux. Python psutil never did that, and this is just wrong
for python (or any executable interpreted script) where the process name is not the interpreter binary but the script
itself.

A new test to check process name value against psutil value is added here, which would hopefully catch any potential
future changes in psutil.

Reverts #542

Fixes #1485
2 years ago
Lomanic c0f3eb1dfb [process] Remove all noisy and useless Test_AllProcesses_X tests
These tests can't fail more than their invidiual counterparts and produce an incredibly verbose output
2 years ago
Antoine Toulme b712e59295 fix more linting reports 2 years ago
Antoine Toulme 38b94668ea allow to pass context values to override environment variables 2 years ago
cui fliter 9aa4e7a744 fix some comments
Signed-off-by: cui fliter <imcusg@gmail.com>
2 years ago
Eng Zer Jun 0439039205
refactor(process): compare error with `errors.Is`
Starting from Go 1.13, `errors.Is` is the preferable way to compare
error equality [1].

[1]: https://go.dev/blog/go1.13-errors

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2 years ago
Matthieu MOREL 1fb1e3e3e6 ci(lint): correct gci linter
Signed-off-by: Matthieu MOREL <matthieu.morel35@gmail.com>
2 years ago
Donal Hurley cf25de7460 Add support for getting process exe in FreeBSD 2 years ago
shirou 852f455217 fix lint 2 years ago
Eng Zer Jun 37894e9b28
test: use `T.Setenv` to set env vars in tests
This commit replaces `os.Setenv` with `t.Setenv` in tests. The
environment variable is automatically restored to its original value
when the test and all its subtests complete.

Reference: https://pkg.go.dev/testing#T.Setenv
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2 years ago
Lomanic 980cc82c08 [process][windows] Retrieve process name as basename of executable
We align ourself with psutil
8e4099d9f0/psutil/_pswindows.py (L749-L759)

Benchmarks show vast improvements

    go test -run=BenchmarkProcessName -bench=BenchmarkProcessName ./process
    goos: windows
    goarch: amd64
    pkg: github.com/shirou/gopsutil/v3/process
    cpu: Intel(R) Core(TM) i5-6300U CPU @ 2.40GHz
    BenchmarkProcessName-4               180           6564033 ns/op
    BenchmarkProcessNameViaExe-4       22111             51153 ns/op
    PASS
    ok      github.com/shirou/gopsutil/v3/process   3.914s

Fixes #1368
2 years ago
Lomanic bd4529a7cc [process][windows] Refator a tiny bit is32BitProcess() function to be more idiomatic Go 2 years ago
Brandon Duffany 38064605a8 Fix memory leaks in darwin_cgo 2 years ago
tienthanh1993 6bc339626b
fix(process,windows): compare len(cwd) to an incorrect value
process/process_windows.go#L411 len(cwd) need compare to userProcParams.CurrentDirectoryPathNameLength instead of userProcParams.CurrentDirectoryPathAddress
3 years ago
Tobias Klauser c76712b8da
host, process: delete darwin/386 code
The darwin/386 port was dropped in Go 1.15 and the imported version of
golang.org/x/sys also no longer supports the port. This module requires
at least Go 1.15 per go.mod, so it no longer builds on darwin/386
anyway.
3 years ago
shirou e2ae339eaf fix(process,linux): remove trailing multiple \x00 3 years ago
Lomanic 7501387fc1 [process][windows] Use WaitForSingleObject with a 0 delay in PidExistsWithContext
Reference https://stackoverflow.com/a/6493793

Fixes #1298
3 years ago
Martin Reindl 1d89235f34 host: add support for OpenBSD/armv7 3 years ago
shirou 7de7d48ef6
Merge pull request #1267 from easyops-cn/master
fix(process): fix OpenFilesWithContext panic problem
3 years ago
Ville Skyttä f7e1f36418 refactor: remove unnecessary exec.LookPath calls
Executing the command does the lookup if needed and returns the same
error when not found, no need to do it separately.
3 years ago
paul 3cd259698f fix(process): fix OpenFilesWithContext panic problem 3 years ago
shirou 9a667f1188
Merge pull request #1210 from mmorel-35/master
enable contextcheck linter
3 years ago
Rishabh Arya 0bf4299250 Add support for reading smaps_rollup 3 years ago
Sergey Kozlov 8ae037c8a1 Fix typing issues in createTimeWithContext on Freebsd/i386
Before change:
```
$ GOOS=freebsd GOARCH=386 go build ./process
process\process_freebsd.go:118:26: cannot use k.Start.Sec * 1000 + k.Start.Usec / 1000 (type int32) as type int64 in return argument
```
3 years ago
Matthieu MOREL b0469a470b
Merge branch 'master' into master 3 years ago
Ville Skyttä 1e56c6f421 Spelling and grammar fixes 3 years ago
shirou 50cad0760c
Merge branch 'master' into refactor/parentwithcontext-ppidwithcontext 3 years ago
shirou 34e74aaccb
Merge pull request #1229 from PierreF/darwin-drop-callps-step2
Darwin drop callps step2
3 years ago
Matthieu MOREL 8ba220d241 enable contextcheck linter 3 years ago
Ville Skyttä 0306525d78 [process] implement ParentWithContext using PpidWithContext
Removes need for redundant ParentWithContext implementations. It had led
to it being unsupported on FreeBSD and OpenBSD even though
PpidWithContext was available for them, and different implementations
for getting the parent info used in ParentWithContext and
PpidWithContext on Darwin and Linux.
3 years ago
shirou 35fbe38505 [process][linux] Fix error handling on Children.
If pgrep returns error, `CallPgrepWithContext` always returns empty pids. So this Children always returns ErrorNoChildren. This PR fixes that handling.
3 years ago
Pierre Fersing 43e50e1d7d Reduce call to ps for process package on darwin 3 years ago
Pierre Fersing 60eae48e6a Drop test for Darwin Pids() 3 years ago
Pierre Fersing b9b3dbe67a Avoid ps command and use KProc on MacOS 3 years ago
shirou dcac9d9b01 [process][windows] fix release handle 3 years ago
shirou fdfd273cf0
Merge pull request #1218 from scop/feat/freebsd-process-start
[process][freebsd] implement createTimeWithContext
3 years ago
Lomanic 01cf95a92d [process][windows] Fix #1216 fix compilation on arm/arm64
go build ./... with GOARCH=arm64 is otherwise failing with the following:

process\process_windows.go:412:11: undefined: readProcessMemory
process\process_windows.go:425:11: undefined: readProcessMemory
process\process_windows.go:896:32: undefined: PROCESS_MEMORY_COUNTERS
process\process_windows.go:897:10: undefined: PROCESS_MEMORY_COUNTERS
process\process_windows.go:910:50: undefined: PROCESS_MEMORY_COUNTERS
process\process_windows.go:950:21: undefined: queryPebAddress
process\process_windows.go:955:9: undefined: readProcessMemory
process\process_windows.go:961:8: undefined: readProcessMemory
process\process_windows.go:969:21: undefined: queryPebAddress
process\process_windows.go:974:9: undefined: readProcessMemory
process\process_windows.go:974:9: too many errors
3 years ago
Ville Skyttä 41e2595443 [process][freebsd] implement createTimeWithContext 3 years ago
Matthieu MOREL 4671e649aa
enable revive linter 3 years ago
mmorel-35 1e6b445a8a gofumpt 3 years ago
mmorel-35 eb5f6203d8 gofmt 3 years ago
Matthieu MOREL c8fd8c7b1f
enable unparam linter 3 years ago
Matthieu MOREL b673968d5d
enable errorlint linter 3 years ago
shirou d2e27c1712 fix dependabot v2 deletion. 3 years ago
shirou 0969c9436b delete v2 directory, move v3 to top #1078 3 years ago
kestrelcjx a0b6077502 chore(process): change the return value 3 years ago
kestrelcjx 3b61bb2833 chore(process): add ctx.Done() to break select 3 years ago
kestrelcjx db054ec106 fix(process): fix the bug that the program is hung when getting the file name 3 years ago
shirou 0ce33dbe64
Merge pull request #1166 from easyops-cn/kestrel/process
feat(process): implement the 'OpenFilesWithContext' function of the windows system
3 years ago
kestrelcjx 5832fdfb9b chore(process): open file in process test case 3 years ago
Max Altgelt a4679b798b
feat: Add support for Cwd() on Windows 3 years ago
Jan De Dobbeleer f169486407
fix: windows arm support 3 years ago
shirou 703175c3fb [v2][process][windows] fix Kill causes DuplicateHandle error 3 years ago
kestrel 3dabe50107 feat(process): implement the 'OpenFilesWithContext' function of the windows system 3 years ago
shirou 7be7e7807f [process] extend maxElapsedTime on process_test 4 years ago
shirou 0d0659af18
Merge pull request #1122 from secDre4mer/master
chore: Drop PROCESS_QUERY_INFORMATION support
4 years ago
Tom Barker 9248140c98 Wait for server connection to be established before checking
connections.
4 years ago
Tom Barker bc4661937d Minor cleanups motivated by staticcheck warnings. 4 years ago
Tom Barker 5ce887df8f Make sure that Test_AllProcesses_cmdLine doesn't ignore failures. 4 years ago
Tom Barker 34cdfa258b Test_Connections currently fails intermittently on Linux (and maybe
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.
4 years ago
Max Altgelt d07af877ef
chore: Drop PROCESS_QUERY_INFORMATION support
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.
4 years ago
shirou b27be3812b [v2][process][linux] port 1112 to v2.
This port includes only code. test is not included.
4 years ago
Max Altgelt 9ad6c64727
fix: Remove superfluous EnvironWithContext definitions
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>
4 years ago
Max Altgelt 5f61473ffd
fix: Correct UNICODE_STRING64 definition
Correct the UNICODE_STRING64 definition. The length is only
16 bits, followed by 16 bit max length and 32 bit padding.
4 years ago
Max Altgelt 73fee847e6
chore: add tests
Add simple tests to verify that environment variables are read.
4 years ago
Max Altgelt b02b92e16b
feat: Add support for environment variable read
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.
4 years ago
shirou d87e955d79
Merge pull request #1090 from MyaLongmire/windows_name_fix
simplify mutex usage
4 years ago
shirou daec167ba2
Merge pull request #1087 from ivandeex/openbsd-process-nocgo
process, v3/process (openbsd):  remove remaining CGO bits from CmdlineSliceWithContext
4 years ago
Mya cb512c8500 simplify mutex usage 4 years ago
shirou 7ea8062810
Merge pull request #1083 from elmeyer/fix-processes-smartos
process, v3/process (Linux): fix Processes in SmartOS lx containers
4 years ago
Ivan Andreev d6e0932b96 Remove CGO bits from process_openbsd.go (also v3)
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>
4 years ago
Lars Meyer e42c52cb35 [process][linux] add test for missing iotime in SmartOS lx containers 4 years ago
Lars Meyer 0ab3954dc4 [process][linux] fix Processes in SmartOS lx containers
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.
4 years ago
Tobias Klauser 611c8b576e process, v3/process: use SC_CLK_TCK sysconf value instead of hard-coding clock ticks
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.
4 years ago
shirou 9bd4bc70ae [process][linux] fix 1056 test and copy to v2 4 years ago
shirou 7ffa844cd5
Merge pull request #1075 from scop/feat/improve-solaris-exe
[process][solaris] improve Exe portability
4 years ago
Lomanic 8775def991
Merge pull request #1072 from djs55/externproc-arm64
process: fix ExternProc on Apple Silicon / Darwin arm64
4 years ago
David Scott 0a281012db process: change all pointers to uint64 in ExternProc for darwin arm64
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>
4 years ago
Ville Skyttä f43138a3ff [process][solaris] improve Exe portability
Resolving from path/a.out seems more portable than from execname.
4 years ago
shirou 9a5f86a30e
Merge pull request #1065 from scop/feat/solaris-process-some
Basic Solaris process support
4 years ago
Ville Skyttä e35ae34dce [process][solaris] process basics 4 years ago
Ville Skyttä b7d717ba80 [process][posix] use os.Lstat for symlink check
unix.DT_LNK is not portable; per glibc docs it's a BSD extension. It is
not available e.g. in Solaris.
https://www.gnu.org/software/libc/manual/html_node/Directory-Entries.html#index-DT_005fLNK

As a side effect, fixes incorrect equality comparison against
Stat_t.Mode, a bitmask.
4 years ago
shirou 2fa855f295 add MockEnv as more generic function. 4 years ago
shirou abacce2177 [process][linux] add test to parse fillFromStatus 4 years ago
shirou 008b5a41ef
Merge pull request #1060 from heptalium/master
Fix fillFromStatusWithContext() on systems with 128 bit signal masks.
4 years ago
Lomanic c7a38de76e
Merge pull request #1052 from gballet/openbsd-arm-support
add support for OpenBSD arm64
4 years ago
Jens Meißner fc074343f8
[process][linux] Fix fillFromStatusWithContext() on systems with 128 bit signal masks. 4 years ago
shirou 79048ccbfa
Merge pull request #1035 from jblesener/fixmacosspaces
Fix spaces on long process names for MacOS
4 years ago
John Blesener 07797b12d3
Make cmdNameWithContext lower-case to avoid exporting it
Signed-off-by: John Blesener <jblesener@reactivelabs.com>
4 years ago
Guillaume Ballet 992d17af94 add support for OpenBSD arm64 4 years ago
Lomanic 0881c11a9a [process][posix] Fix #1049 check if procfs is mounted before checking if pid exists there
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
4 years ago