Skip to content

Commit

Permalink
Improve procfs scanner
Browse files Browse the repository at this point in the history
  • Loading branch information
omris94 committed Oct 9, 2024
1 parent e0e9550 commit 1192cee
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/sniffer/pkg/utils/procfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,31 @@ func ScanProcDirProcesses(callback ProcessScanCallback) error {
}

func ExtractProcessHostname(pDir string) (string, error) {
hostname, found, err := extractProcessHostnameUsingEtcHostname(pDir)
if err != nil {
return "", errors.Wrap(err)
}
if found {
return hostname, nil
}
return extractProcessHostnameUsingEnviron(pDir)

}

func extractProcessHostnameUsingEtcHostname(pDir string) (string, bool, error) {
// Read the environment variables from the proc filesystem
data, err := os.ReadFile(fmt.Sprintf("%s/root/etc/hostname", pDir))
if os.IsNotExist(err) {
return "", false, nil
}
if err != nil {
return "", false, errors.Wrap(err)
}

return strings.TrimSpace(string(data)), true, nil
}

func extractProcessHostnameUsingEnviron(pDir string) (string, error) {
// Read the environment variables from the proc filesystem
data, err := os.ReadFile(fmt.Sprintf("%s/environ", pDir))
if err != nil {
Expand All @@ -58,7 +83,6 @@ func ExtractProcessHostname(pDir string) (string, error) {
}

return "", errors.Errorf("couldn't find hostname in %s/environ", pDir)

}

func ExtractProcessIPAddr(pDir string) (string, error) {
Expand Down

0 comments on commit 1192cee

Please sign in to comment.