|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path" |
| 7 | + "strconv" |
| 8 | + "unsafe" |
| 9 | + |
| 10 | + "golang.org/x/sys/unix" |
| 11 | +) |
| 12 | + |
| 13 | +type ServerProcess struct { |
| 14 | + Pid int |
| 15 | +} |
| 16 | + |
| 17 | +// linuxDirent64 is linux_dirent64 from linux/dirent.h |
| 18 | +type linuxDirent64 struct { |
| 19 | + DIno uint64 // u64 d_ino |
| 20 | + DOff int64 // s64 d_off |
| 21 | + DReclen uint16 // unsigned short d_reclen |
| 22 | + DType uint8 // unsigned char d_type |
| 23 | + DName byte // The first byte of flexible array member char d_name[] |
| 24 | +} |
| 25 | + |
| 26 | +func FindProcessesListeningToSocket(procDir string, socketInode uint64) ([]ServerProcess, error) { |
| 27 | + // Check what network namespace _we_ are in. |
| 28 | + selfNetNs, err := os.Readlink(path.Join(procDir, "self/ns/net")) |
| 29 | + if err != nil { |
| 30 | + return nil, fmt.Errorf("error reading %s/self/ns/net: %w", procDir, err) |
| 31 | + } |
| 32 | + |
| 33 | + // List out every process in /proc |
| 34 | + procEntries, err := os.ReadDir(procDir) |
| 35 | + if err != nil { |
| 36 | + return nil, fmt.Errorf("error reading dir %s: %w", procDir, err) |
| 37 | + } |
| 38 | + |
| 39 | + var result []ServerProcess |
| 40 | + |
| 41 | + for _, entry := range procEntries { |
| 42 | + // use an IIFE so that we can defer closing the directory FD. |
| 43 | + func(){ |
| 44 | + if !entry.IsDir() { |
| 45 | + return |
| 46 | + } |
| 47 | + pid, err := strconv.Atoi(entry.Name()) |
| 48 | + if err != nil { |
| 49 | + // Not a proc/$PID directory |
| 50 | + return |
| 51 | + } |
| 52 | + dirFD, err := os.OpenFile(path.Join(procDir, entry.Name()), os.O_RDONLY | unix.O_DIRECTORY, 0) |
| 53 | + if err != nil { |
| 54 | + // failed to open the directory - process may simply have died before we could open it. |
| 55 | + return |
| 56 | + } |
| 57 | + defer dirFD.Close() |
| 58 | + |
| 59 | + // Is this process in the same namespace as us? |
| 60 | + // format of this link is "net:[uint64 in base10]" so this buffer is always |
| 61 | + // going to be large enough |
| 62 | + linkBuffer := make([]byte, 64) |
| 63 | + n, err := unix.Readlinkat(int(dirFD.Fd()), "ns/net", linkBuffer) |
| 64 | + if err != nil { |
| 65 | + // Could mean a couple of things - this process might have exited, or we might not |
| 66 | + // be in the same (or parent) net namespace as the pid. We want to filter for processes |
| 67 | + // in the same network namespace anyway, so ignore this regardless. |
| 68 | + return |
| 69 | + } |
| 70 | + pidNetNs := string(linkBuffer[:n]) |
| 71 | + if selfNetNs != pidNetNs { |
| 72 | + // this process is in a different network namespace. Ignore. |
| 73 | + return |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + // See if it has an open file with the given socketInode. |
| 78 | + fdDirFD, err := unix.Openat(int(dirFD.Fd()), "fd", os.O_RDONLY | unix.O_DIRECTORY, 0) |
| 79 | + if err != nil { |
| 80 | + // could have exited |
| 81 | + return |
| 82 | + } |
| 83 | + defer unix.Close(fdDirFD) |
| 84 | + |
| 85 | + pidHasListenerSocketOpened := false |
| 86 | + // Gross-time. Golang has no binding for "read entries from a directory FD". This is important, |
| 87 | + // because pids can be recycled, so doing something like os.Readdir("/proc/$pid/fd") is racey. |
| 88 | + // Linux has the getdents64 syscall for this purpose; use the raw syscall API. |
| 89 | + dentBuf := make([]byte, 4096) |
| 90 | + for { |
| 91 | + nBytes, err := unix.Getdents(fdDirFD, dentBuf) |
| 92 | + if err != nil { |
| 93 | + return |
| 94 | + } |
| 95 | + if nBytes == 0 { |
| 96 | + break |
| 97 | + } |
| 98 | + offset := 0 |
| 99 | + for offset < nBytes { |
| 100 | + dentStruct := (*linuxDirent64)(unsafe.Pointer(&dentBuf[offset])) |
| 101 | + |
| 102 | + fdFileNameStartOffset := offset + int(unsafe.Offsetof(dentStruct.DName)) |
| 103 | + fdFileNameEndOffset := fdFileNameStartOffset |
| 104 | + for dentBuf[fdFileNameEndOffset] != 0 { |
| 105 | + fdFileNameEndOffset++ |
| 106 | + } |
| 107 | + fdFileName := string(dentBuf[fdFileNameStartOffset:fdFileNameEndOffset]) |
| 108 | + |
| 109 | + linkBytes, err := unix.Readlinkat(fdDirFD, fdFileName, linkBuffer) |
| 110 | + if err == nil && string(linkBuffer[:linkBytes]) == fmt.Sprintf("socket:[%d]", socketInode) { |
| 111 | + // This process has the listener socket opened. |
| 112 | + pidHasListenerSocketOpened = true |
| 113 | + } |
| 114 | + |
| 115 | + offset += int(dentStruct.DReclen) |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if pidHasListenerSocketOpened { |
| 120 | + result = append(result, ServerProcess{ |
| 121 | + Pid: pid, |
| 122 | + }) |
| 123 | + } |
| 124 | + }() |
| 125 | + } |
| 126 | + return result, nil |
| 127 | +} |
0 commit comments