Skip to content

Commit

Permalink
refactor(techStackGeneric): improve getProcNames (#250)
Browse files Browse the repository at this point in the history
The `getProcNames` procedure was doing some strange things:

- It was trying to read files at `/proc/foo/status`, where `foo` is the
  name of a file (not directory) in `/proc`.

- It scanned each file at `/proc/[pid]/status` once per digit of its
  pid.

This wasn't producing any incorrect results because:

- It caught and ignored the exceptions from opening nonexistent files.

- The names are added to a `HashSet[string]`, which deduplicates them.

It was also doing some further unnecessary work: it kept scanning lines
of `/proc/[pid]/status` even after it found the `Name` value (which is
specified to be on the first line).

This commit resolves those issues.

Refs: #249
  • Loading branch information
ee7 authored Mar 21, 2024
1 parent 8f00241 commit 7cf3029
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/plugins/techStackGeneric.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
## (see https://crashoverride.com/docs/chalk)
##

import std/[hashes, re, sequtils, sets, tables]
import std/[hashes, re, sequtils, sets, strscans, tables]
import pkg/nimutils
import ".."/[config, plugin_api, util]

const FT_ANY = "*"
Expand Down Expand Up @@ -123,20 +124,17 @@ proc scanFile(filePath: string, category: string, subcategory: string) =
scanFileStream(stream)

proc getProcNames(): HashSet[string] =
## Returns every Name value in files at `/proc/[0-9]+/status`.
## This is the filename of each executable, truncated to 15 characters.
result = initHashSet[string]()
for kind, path in walkDir("/proc/"):
for ch in path.splitPath().tail:
try:
if ch notin "0123456789":
continue
let p_path = path / "status"
var data = p_path.readFile()
for line in data.split("\n"):
if "Name:" in line:
var name = line.split("Name:")[1].strip()
result.incl(name)
except:
continue
if kind == pcDir and path.lastPathPart().allIt(it in {'0'..'9'}):
let data = tryToLoadFile(path / "status")
for line in data.splitLines():
let (isMatch, name) = line.scanTuple("Name:$s$+")
if isMatch:
result.incl(name)
break

# The current host based detection simply checks for the
# presence of configuration files, therefore we don't need
Expand Down

0 comments on commit 7cf3029

Please sign in to comment.