init.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package ebpftracer
  2. import (
  3. "strings"
  4. "github.com/coroot/coroot-node-agent/proc"
  5. klog "github.com/sirupsen/logrus"
  6. )
  7. type file struct {
  8. pid uint32
  9. fd uint64
  10. }
  11. type sock struct {
  12. pid uint32
  13. fd uint64
  14. proc.Sock
  15. }
  16. func readFds(pids []uint32) (files []file, socks []sock) {
  17. nss := map[string]map[string]sock{}
  18. for _, pid := range pids {
  19. ns, err := proc.GetNetNs(pid)
  20. if err != nil {
  21. continue
  22. }
  23. nsId := ns.UniqueId()
  24. sockets, ok := nss[nsId]
  25. _ = ns.Close()
  26. if !ok {
  27. sockets = map[string]sock{}
  28. nss[nsId] = sockets
  29. if ss, err := proc.GetSockets(pid); err != nil {
  30. klog.Warningln(err)
  31. } else {
  32. for _, s := range ss {
  33. sockets[s.Inode] = sock{Sock: s}
  34. }
  35. }
  36. }
  37. fds, err := proc.ReadFds(pid)
  38. if err != nil {
  39. continue
  40. }
  41. for _, fd := range fds {
  42. switch {
  43. case fd.SocketInode != "":
  44. if s, ok := sockets[fd.SocketInode]; ok {
  45. s.fd = fd.Fd
  46. s.pid = pid
  47. socks = append(socks, s)
  48. }
  49. case strings.HasPrefix(fd.Dest, "/"):
  50. files = append(files, file{pid: pid, fd: fd.Fd})
  51. }
  52. }
  53. }
  54. return
  55. }