From 1191d1548470a7ed1049f1d78de6f17b77e0d36d Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sat, 6 Jan 2024 17:51:41 -0600 Subject: purge years of old test code Signed-off-by: Jeff Carr --- linuxstatus/proc.go | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 linuxstatus/proc.go (limited to 'linuxstatus/proc.go') diff --git a/linuxstatus/proc.go b/linuxstatus/proc.go new file mode 100644 index 0000000..b7720e0 --- /dev/null +++ b/linuxstatus/proc.go @@ -0,0 +1,101 @@ +package linuxstatus + +import ( + "io/ioutil" + "os" + "path/filepath" + "strconv" + "strings" + + "go.wit.com/log" +) + +func GetProcessNameByPort(port int) string { + // Convert port to hex string + portHex := strconv.FormatInt(int64(port), 16) + + // Function to search /proc/net/tcp or /proc/net/udp + searchProcNet := func(file string) string { + data, err := ioutil.ReadFile(file) + if err != nil { + return "" + } + // log.Log(PROC, "searchProcNet() data:", string(data)) + + lines := strings.Split(string(data), "\n") + for _, line := range lines { + fields := strings.Fields(line) + log.Log(PROC, "searchProcNet() portHex:", portHex) + if (len(fields) > 9) { + log.Log(PROC, "searchProcNet() fields[9]", fields[9]) + } + log.Log(PROC, "searchProcNet() lines:", line) + if len(fields) > 1 { + parts := strings.Split(fields[1], ":") + if len(parts) > 1 { + // Convert the hexadecimal string to an integer + value, _ := strconv.ParseInt(parts[1], 16, 64) + log.Log(PROC, "searchProcNet() value, port =", value, port, "parts[1] =", parts[1]) + if (port == int(value)) { + log.Log(PROC, "searchProcNet() THIS IS THE LINE:", fields) + return fields[9] + } + } + } + } + + return "" + } + + // Search TCP and then UDP + inode := searchProcNet("/proc/net/tcp") + if inode == "" { + inode = searchProcNet("/proc/net/udp") + } + log.Log(PROC, "searchProcNet() inode =", inode) + + // Search for process with the inode + procs, _ := ioutil.ReadDir("/proc") + for _, proc := range procs { + if !proc.IsDir() { + continue + } + + fdPath := filepath.Join("/proc", proc.Name(), "fd") + fds, err := ioutil.ReadDir(fdPath) + if err != nil { + continue // Process might have exited; skip it + } + + for _, fd := range fds { + fdLink, _ := os.Readlink(filepath.Join(fdPath, fd.Name())) + var s string + s = "socket:["+inode+"]" + if strings.Contains(fdLink, "socket:[") { + log.Log(PROC, "searchProcNet() fdLink has socket:", fdLink) + log.Log(PROC, "searchProcNet() proc.Name() =", proc.Name(), "s =", s) + } + if strings.Contains(fdLink, "socket:[35452]") { + log.Log(PROC, "searchProcNet() found proc.Name() =", proc.Name(), fdLink) + return proc.Name() + } + if strings.Contains(fdLink, "socket:[35450]") { + log.Log(PROC, "searchProcNet() found proc.Name() =", proc.Name(), fdLink) + return proc.Name() + } + if strings.Contains(fdLink, "socket:[35440]") { + log.Log(PROC, "searchProcNet() found proc.Name() =", proc.Name(), fdLink) + return proc.Name() + } + if strings.Contains(fdLink, "socket:[21303]") { + log.Log(PROC, "searchProcNet() found proc.Name() =", proc.Name(), fdLink) + // return proc.Name() + } + if strings.Contains(fdLink, "socket:["+inode+"]") { + return proc.Name() + } + } + } + + return "" +} -- cgit v1.2.3