@@ -392,7 +392,7 @@ func Connections(kind string) ([]ConnectionStat, error) {
392
392
}
393
393
394
394
func ConnectionsWithContext (ctx context.Context , kind string ) ([]ConnectionStat , error ) {
395
- return ConnectionsPid ( kind , 0 )
395
+ return ConnectionsPidWithContext ( ctx , kind , 0 )
396
396
}
397
397
398
398
// Return a list of network connections opened returning at most `max`
@@ -402,7 +402,7 @@ func ConnectionsMax(kind string, max int) ([]ConnectionStat, error) {
402
402
}
403
403
404
404
func ConnectionsMaxWithContext (ctx context.Context , kind string , max int ) ([]ConnectionStat , error ) {
405
- return ConnectionsPidMax ( kind , 0 , max )
405
+ return ConnectionsPidMaxWithContext ( ctx , kind , 0 , max )
406
406
}
407
407
408
408
// Return a list of network connections opened, omitting `Uids`.
@@ -463,7 +463,7 @@ func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, p
463
463
var err error
464
464
var inodes map [string ][]inodeMap
465
465
if pid == 0 {
466
- inodes , err = getProcInodesAll ( root , max )
466
+ inodes , err = getProcInodesAllWithContext ( ctx , root , max )
467
467
} else {
468
468
inodes , err = getProcInodes (root , pid , max )
469
469
if len (inodes ) == 0 {
@@ -474,10 +474,14 @@ func connectionsPidMaxWithoutUidsWithContext(ctx context.Context, kind string, p
474
474
if err != nil {
475
475
return nil , fmt .Errorf ("cound not get pid(s), %d: %w" , pid , err )
476
476
}
477
- return statsFromInodes ( root , pid , tmap , inodes , skipUids )
477
+ return statsFromInodesWithContext ( ctx , root , pid , tmap , inodes , skipUids )
478
478
}
479
479
480
480
func statsFromInodes (root string , pid int32 , tmap []netConnectionKindType , inodes map [string ][]inodeMap , skipUids bool ) ([]ConnectionStat , error ) {
481
+ return statsFromInodesWithContext (context .Background (), root , pid , tmap , inodes , skipUids )
482
+ }
483
+
484
+ func statsFromInodesWithContext (ctx context.Context , root string , pid int32 , tmap []netConnectionKindType , inodes map [string ][]inodeMap , skipUids bool ) ([]ConnectionStat , error ) {
481
485
dupCheckMap := make (map [string ]struct {})
482
486
var ret []ConnectionStat
483
487
@@ -493,7 +497,7 @@ func statsFromInodes(root string, pid int32, tmap []netConnectionKindType, inode
493
497
}
494
498
switch t .family {
495
499
case syscall .AF_INET , syscall .AF_INET6 :
496
- ls , err = processInet ( path , t , inodes , pid )
500
+ ls , err = processInetWithContext ( ctx , path , t , inodes , pid )
497
501
case syscall .AF_UNIX :
498
502
ls , err = processUnix (path , t , inodes , pid )
499
503
}
@@ -666,7 +670,11 @@ func (p *process) fillFromStatus() error {
666
670
}
667
671
668
672
func getProcInodesAll (root string , max int ) (map [string ][]inodeMap , error ) {
669
- pids , err := Pids ()
673
+ return getProcInodesAllWithContext (context .Background (), root , max )
674
+ }
675
+
676
+ func getProcInodesAllWithContext (ctx context.Context , root string , max int ) (map [string ][]inodeMap , error ) {
677
+ pids , err := PidsWithContext (ctx )
670
678
if err != nil {
671
679
return nil , err
672
680
}
@@ -695,6 +703,10 @@ func getProcInodesAll(root string, max int) (map[string][]inodeMap, error) {
695
703
// "0500000A:0016" -> "10.0.0.5", 22
696
704
// "0085002452100113070057A13F025401:0035" -> "2400:8500:1301:1052:a157:7:154:23f", 53
697
705
func decodeAddress (family uint32 , src string ) (Addr , error ) {
706
+ return decodeAddressWithContext (context .Background (), family , src )
707
+ }
708
+
709
+ func decodeAddressWithContext (ctx context.Context , family uint32 , src string ) (Addr , error ) {
698
710
t := strings .Split (src , ":" )
699
711
if len (t ) != 2 {
700
712
return Addr {}, fmt .Errorf ("does not contain port, %s" , src )
@@ -711,9 +723,9 @@ func decodeAddress(family uint32, src string) (Addr, error) {
711
723
var ip net.IP
712
724
// Assumes this is little_endian
713
725
if family == syscall .AF_INET {
714
- ip = net .IP (Reverse ( decoded ))
726
+ ip = net .IP (ReverseWithContext ( ctx , decoded ))
715
727
} else { // IPv6
716
- ip , err = parseIPv6HexString ( decoded )
728
+ ip , err = parseIPv6HexStringWithContext ( ctx , decoded )
717
729
if err != nil {
718
730
return Addr {}, err
719
731
}
@@ -738,19 +750,27 @@ func ReverseWithContext(ctx context.Context, s []byte) []byte {
738
750
739
751
// parseIPv6HexString parse array of bytes to IPv6 string
740
752
func parseIPv6HexString (src []byte ) (net.IP , error ) {
753
+ return parseIPv6HexStringWithContext (context .Background (), src )
754
+ }
755
+
756
+ func parseIPv6HexStringWithContext (ctx context.Context , src []byte ) (net.IP , error ) {
741
757
if len (src ) != 16 {
742
758
return nil , fmt .Errorf ("invalid IPv6 string" )
743
759
}
744
760
745
761
buf := make ([]byte , 0 , 16 )
746
762
for i := 0 ; i < len (src ); i += 4 {
747
- r := Reverse ( src [i : i + 4 ])
763
+ r := ReverseWithContext ( ctx , src [i : i + 4 ])
748
764
buf = append (buf , r ... )
749
765
}
750
766
return net .IP (buf ), nil
751
767
}
752
768
753
769
func processInet (file string , kind netConnectionKindType , inodes map [string ][]inodeMap , filterPid int32 ) ([]connTmp , error ) {
770
+ return processInetWithContext (context .Background (), file , kind , inodes , filterPid )
771
+ }
772
+
773
+ func processInetWithContext (ctx context.Context , file string , kind netConnectionKindType , inodes map [string ][]inodeMap , filterPid int32 ) ([]connTmp , error ) {
754
774
if strings .HasSuffix (file , "6" ) && ! common .PathExists (file ) {
755
775
// IPv6 not supported, return empty.
756
776
return []connTmp {}, nil
@@ -793,11 +813,11 @@ func processInet(file string, kind netConnectionKindType, inodes map[string][]in
793
813
} else {
794
814
status = "NONE"
795
815
}
796
- la , err := decodeAddress ( kind .family , laddr )
816
+ la , err := decodeAddressWithContext ( ctx , kind .family , laddr )
797
817
if err != nil {
798
818
continue
799
819
}
800
- ra , err := decodeAddress ( kind .family , raddr )
820
+ ra , err := decodeAddressWithContext ( ctx , kind .family , raddr )
801
821
if err != nil {
802
822
continue
803
823
}
0 commit comments