@@ -18,10 +18,11 @@ import (
18
18
"unicode/utf16"
19
19
"unsafe"
20
20
21
+ "golang.org/x/sys/windows"
22
+
21
23
"github.com/shirou/gopsutil/v4/cpu"
22
24
"github.com/shirou/gopsutil/v4/internal/common"
23
25
"github.com/shirou/gopsutil/v4/net"
24
- "golang.org/x/sys/windows"
25
26
)
26
27
27
28
type Signal = syscall.Signal
@@ -245,7 +246,7 @@ func pidsWithContext(ctx context.Context) ([]int32, error) {
245
246
// inspired by https://gist.github.com/henkman/3083408
246
247
// and https://github.com/giampaolo/psutil/blob/1c3a15f637521ba5c0031283da39c733fda53e4c/psutil/arch/windows/process_info.c#L315-L329
247
248
var ret []int32
248
- var read uint32 = 0
249
+ var read uint32
249
250
var psSize uint32 = 1024
250
251
const dwordSize uint32 = 4
251
252
@@ -288,10 +289,10 @@ func PidExistsWithContext(ctx context.Context, pid int32) (bool, error) {
288
289
return false , err
289
290
}
290
291
h , err := windows .OpenProcess (windows .SYNCHRONIZE , false , uint32 (pid ))
291
- if err == windows .ERROR_ACCESS_DENIED {
292
+ if errors . Is ( err , windows .ERROR_ACCESS_DENIED ) {
292
293
return true , nil
293
294
}
294
- if err == windows .ERROR_INVALID_PARAMETER {
295
+ if errors . Is ( err , windows .ERROR_INVALID_PARAMETER ) {
295
296
return false , nil
296
297
}
297
298
if err != nil {
@@ -330,7 +331,7 @@ func (p *Process) NameWithContext(ctx context.Context) (string, error) {
330
331
331
332
exe , err := p .ExeWithContext (ctx )
332
333
if err != nil {
333
- return "" , fmt .Errorf ("could not get Name: %s " , err )
334
+ return "" , fmt .Errorf ("could not get Name: %w " , err )
334
335
}
335
336
336
337
return filepath .Base (exe ), nil
@@ -370,7 +371,7 @@ func (p *Process) ExeWithContext(ctx context.Context) (string, error) {
370
371
func (p * Process ) CmdlineWithContext (_ context.Context ) (string , error ) {
371
372
cmdline , err := getProcessCommandLine (p .Pid )
372
373
if err != nil {
373
- return "" , fmt .Errorf ("could not get CommandLine: %s " , err )
374
+ return "" , fmt .Errorf ("could not get CommandLine: %w " , err )
374
375
}
375
376
return cmdline , nil
376
377
}
@@ -386,15 +387,15 @@ func (p *Process) CmdlineSliceWithContext(ctx context.Context) ([]string, error)
386
387
func (p * Process ) createTimeWithContext (ctx context.Context ) (int64 , error ) {
387
388
ru , err := getRusage (p .Pid )
388
389
if err != nil {
389
- return 0 , fmt .Errorf ("could not get CreationDate: %s " , err )
390
+ return 0 , fmt .Errorf ("could not get CreationDate: %w " , err )
390
391
}
391
392
392
393
return ru .CreationTime .Nanoseconds () / 1000000 , nil
393
394
}
394
395
395
396
func (p * Process ) CwdWithContext (_ context.Context ) (string , error ) {
396
397
h , err := windows .OpenProcess (processQueryInformation | windows .PROCESS_VM_READ , false , uint32 (p .Pid ))
397
- if err == windows .ERROR_ACCESS_DENIED || err == windows .ERROR_INVALID_PARAMETER {
398
+ if errors . Is ( err , windows .ERROR_ACCESS_DENIED ) || errors . Is ( err , windows .ERROR_INVALID_PARAMETER ) {
398
399
return "" , nil
399
400
}
400
401
if err != nil {
@@ -822,9 +823,9 @@ func (p *Process) KillWithContext(ctx context.Context) error {
822
823
}
823
824
824
825
func (p * Process ) EnvironWithContext (ctx context.Context ) ([]string , error ) {
825
- envVars , err := getProcessEnvironmentVariables (p .Pid , ctx )
826
+ envVars , err := getProcessEnvironmentVariables (ctx , p .Pid )
826
827
if err != nil {
827
- return nil , fmt .Errorf ("could not get environment variables: %s " , err )
828
+ return nil , fmt .Errorf ("could not get environment variables: %w " , err )
828
829
}
829
830
return envVars , nil
830
831
}
@@ -872,7 +873,7 @@ func ProcessesWithContext(ctx context.Context) ([]*Process, error) {
872
873
873
874
pids , err := PidsWithContext (ctx )
874
875
if err != nil {
875
- return out , fmt .Errorf ("could not get Processes %s " , err )
876
+ return out , fmt .Errorf ("could not get Processes %w " , err )
876
877
}
877
878
878
879
for _ , pid := range pids {
@@ -1038,9 +1039,9 @@ func is32BitProcess(h windows.Handle) bool {
1038
1039
return procIs32Bits
1039
1040
}
1040
1041
1041
- func getProcessEnvironmentVariables (pid int32 , ctx context.Context ) ([]string , error ) {
1042
+ func getProcessEnvironmentVariables (ctx context.Context , pid int32 ) ([]string , error ) {
1042
1043
h , err := windows .OpenProcess (processQueryInformation | windows .PROCESS_VM_READ , false , uint32 (pid ))
1043
- if err == windows .ERROR_ACCESS_DENIED || err == windows .ERROR_INVALID_PARAMETER {
1044
+ if errors . Is ( err , windows .ERROR_ACCESS_DENIED ) || errors . Is ( err , windows .ERROR_INVALID_PARAMETER ) {
1044
1045
return nil , nil
1045
1046
}
1046
1047
if err != nil {
@@ -1124,7 +1125,7 @@ func (p *processReader) Read(buf []byte) (int, error) {
1124
1125
1125
1126
func getProcessCommandLine (pid int32 ) (string , error ) {
1126
1127
h , err := windows .OpenProcess (processQueryInformation | windows .PROCESS_VM_READ , false , uint32 (pid ))
1127
- if err == windows .ERROR_ACCESS_DENIED || err == windows .ERROR_INVALID_PARAMETER {
1128
+ if errors . Is ( err , windows .ERROR_ACCESS_DENIED ) || errors . Is ( err , windows .ERROR_INVALID_PARAMETER ) {
1128
1129
return "" , nil
1129
1130
}
1130
1131
if err != nil {
0 commit comments