(ctx context.Context, pid int)
| 28 | ) |
| 29 | |
| 30 | func InspectNetNS(ctx context.Context, pid int) (*native.NetNS, error) { |
| 31 | nsPath := fmt.Sprintf("/proc/%d/ns/net", pid) |
| 32 | res := &native.NetNS{} |
| 33 | fn := func(_ ns.NetNS) error { |
| 34 | intf, err := net.Interfaces() |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | res.Interfaces = make([]native.NetInterface, len(intf)) |
| 39 | for i, f := range intf { |
| 40 | x := native.NetInterface{ |
| 41 | Interface: f, |
| 42 | } |
| 43 | if f.HardwareAddr != nil { |
| 44 | x.HardwareAddr = f.HardwareAddr.String() |
| 45 | } |
| 46 | if x.Interface.Flags.String() != "0" { |
| 47 | x.Flags = strings.Split(x.Interface.Flags.String(), "|") |
| 48 | } |
| 49 | if addrs, err := x.Interface.Addrs(); err == nil { |
| 50 | x.Addrs = make([]string, len(addrs)) |
| 51 | for j, a := range addrs { |
| 52 | x.Addrs[j] = a.String() |
| 53 | } |
| 54 | } |
| 55 | res.Interfaces[i] = x |
| 56 | } |
| 57 | res.PrimaryInterface = determinePrimaryInterface(res.Interfaces) |
| 58 | return nil |
| 59 | } |
| 60 | if err := ns.WithNetNSPath(nsPath, fn); err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | return res, nil |
| 64 | } |
| 65 | |
| 66 | // determinePrimaryInterface returns a net.Interface.Index value, not a slice index. |
| 67 | // Zero means no primary interface was detected. |
no test coverage detected
searching dependent graphs…