Skip to content

Commit

Permalink
top: Add printing option for security attributes
Browse files Browse the repository at this point in the history
Signed-off-by: iipeace <[email protected]>
  • Loading branch information
iipeace committed Jan 29, 2024
1 parent e8bce25 commit 248ac09
Showing 1 changed file with 49 additions and 17 deletions.
66 changes: 49 additions & 17 deletions guider/guider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__credits__ = "Peace Lee"
__license__ = "GPLv2"
__version__ = "3.9.8"
__revision__ = "240127"
__revision__ = "240129"
__maintainer__ = "Peace Lee"
__email__ = "[email protected]"
__repository__ = "https://github.com/iipeace/guider"
Expand Down Expand Up @@ -25942,6 +25942,7 @@ class SysMgr(object):

# flag #
affinityEnable = False
attrEnable = False
barGraphEnable = None
binderEnable = False
blockEnable = False
Expand Down Expand Up @@ -33372,7 +33373,7 @@ def isValidEnableOption(options):
if not options:
return False

optionList = "BCDEFGHILMNOPRSUTWXYabcdefghijklmnopqrrstuvwxy"
optionList = "ABCDEFGHILMNOPRSUTWXYabcdefghijklmnopqrrstuvwxy"
for opt in options:
if not opt in optionList:
return False
Expand Down Expand Up @@ -33999,24 +34000,25 @@ def readSchedFeatures():
return []

@staticmethod
def readProcData(tid, path, num=-1):
def readProcData(tid, path, num=-1, verb=True):
path = "%s/%s/%s" % (SysMgr.procPath, tid, path)

try:
f = open(path, "r")

if num == -1:
return f.readlines()
elif num == 0:
return f.readline().replace("\n", "")
else:
return f.readline().replace("\n", "").split()[num - 1]
except SystemExit:
sys.exit(0)
except:
SysMgr.printOpenErr(path)
if verb:
SysMgr.printOpenErr(path)
return None

if num == -1:
return f.readlines()
elif num == 0:
return f.readline().replace("\n", "")
else:
return f.readline().replace("\n", "").split()[num - 1]

@staticmethod
def hasMainArg(dash=False):
if len(sys.argv) <= 2 or (not dash and sys.argv[2].startswith("-")):
Expand Down Expand Up @@ -34566,9 +34568,9 @@ def printHelp(force=False, isExit=True):
topSubStr = """
Options:
-e <CHARACTER> enable options
[ a:affinity | b:block | B:bar | c:cpu
C:compress | d:disk | D:DWARF | e:encode
E:exec | f:float | F:wfc | G:cgroup
[ a:affinity | A:secAttr | b:block | B:bar
c:cpu | C:compress | d:disk | D:DWARF
e:encode | E:exec | f:float | F:wfc | G:cgroup
h:sigHandler | H:sched | i:irq | I:elastic
k:peak | l:threshold | L:cmdline | m:mem
M:min | n:net | N:namespace | o:oomScore
Expand Down Expand Up @@ -45249,6 +45251,7 @@ def _setOpt(items, val, name):

_setOpt(items, SysMgr.affinityEnable, "AFNT ")
_setOpt(items, SysMgr.andlogEnable, "ANDL ")
_setOpt(items, SysMgr.attrEnable, "ATTR ")
_setOpt(items, SysMgr.barGraphEnable, "BAR ")
_setOpt(items, SysMgr.blockEnable, "BLOCK ")
_setOpt(items, SysMgr.cmdlineEnable, "CMD ")
Expand Down Expand Up @@ -48210,7 +48213,7 @@ def printPipe(line="", newline=True, flush=False, pager=True, trim=True):
# defopt = '-FRSXMQi'
defopt = "-FRXMQi"

# set SIGCHLD #
# receive SIGCHLD #
signal.signal(signal.SIGCHLD, signal.SIG_DFL)

# verify pager option support #
Expand Down Expand Up @@ -50016,6 +50019,8 @@ def parseAnalOption(option=None):
TaskAnalyzer.setLastField("wchan")
elif "h" in options:
TaskAnalyzer.setLastField("signal")
elif "A" in options:
TaskAnalyzer.setLastField("attr")

if "f" in options:
SysMgr.floatEnable = True
Expand Down Expand Up @@ -55603,7 +55608,7 @@ def _enableSigPipe():
myEnv = deepcopy(os.environ)
myEnv["REMOTERUN"] = "True"

# set SIGCHLD #
# receive SIGCHLD #
signal.signal(signal.SIGCHLD, signal.SIG_DFL)

# set print flag #
Expand Down Expand Up @@ -58822,6 +58827,10 @@ def _getMemUsage(pid):
buf = buf[pos:].split("\n")
info = buf.pop(0)
pid = info.split("(", 1)[1].split(")", 1)[0]
if not SysMgr.isAlive(pid):
procInfo = info[info.find(":") + 2 :].split("]", 1)[0]
SysMgr.printErr("%s is not alive" % procInfo)
continue
pids[pid] = None
bufLen = len(buf)
pos = 0
Expand Down Expand Up @@ -59095,6 +59104,12 @@ def _getMemUsage(pid):
for call in callList:
memsProc.setdefault(pid, [])
memsProc[pid].append(call[:2])

# check target process list #
if not pids:
SysMgr.printErr("no target process alive")
return

elif SysMgr.hasMainArg():
targetList = SysMgr.getMainArgs(False)
elif SysMgr.filterGroup:
Expand Down Expand Up @@ -59151,6 +59166,8 @@ def _getMemUsage(pid):
try:
dbgObj = Debugger(pid)
dbgObj.initValues()
except SystemExit:
sys.exit(0)
except:
continue

Expand Down Expand Up @@ -66342,7 +66359,7 @@ def _cputask(idx, load):
signal.signal(signal.SIGALRM, SysMgr.onAlarm)
signal.alarm(SysMgr.intervalEnable)

# ignore SIGCHLD #
# receive SIGCHLD #
signal.signal(signal.SIGCHLD, signal.SIG_DFL)
except SystemExit:
sys.exit(0)
Expand Down Expand Up @@ -85555,6 +85572,9 @@ def attach(self, pid=None, verb=False, cont=False):
)
return -1

# recover SIGCHLD #
signal.signal(signal.SIGCHLD, signal.SIG_DFL)

doExit = False

while 1:
Expand Down Expand Up @@ -119845,6 +119865,7 @@ def readTraceData(fname):
@staticmethod
def setLastField(option):
SysMgr.affinityEnable = False
SysMgr.attrEnable = False
SysMgr.wchanEnable = False
SysMgr.sigHandlerEnable = False
SysMgr.oomEnable = False
Expand All @@ -119860,6 +119881,8 @@ def setLastField(option):
SysMgr.sigHandlerEnable = True
elif option == "oom":
SysMgr.oomEnable = True
elif option == "attr":
SysMgr.attrEnable = True
else:
SysMgr.printErr("failed to set '%s' as a last field" % option)

Expand Down Expand Up @@ -135406,6 +135429,8 @@ def _getTypes():
etc = "SignalHandler"
elif SysMgr.nsEnable:
etc = "Namespace[%s]" % SysMgr.nsType
elif SysMgr.attrEnable:
etc = "SecurityAttr"
elif SysMgr.processEnable:
etc = "Parent"
else:
Expand Down Expand Up @@ -135902,6 +135927,11 @@ def _memFactorPG(stat):
if SysMgr.wchanEnable:
self.saveProcWchanData(value["taskPath"], idx)

# save attr info #
if SysMgr.attrEnable:
vattr = SysMgr.readProcData(idx, "attr/current", verb=False)
value["attr"] = "%22s" % vattr[0].strip() if vattr else "None"

# save fdinfo #
if "DMABUF" in SysMgr.environList:
self.saveFdinfoData(idx)
Expand Down Expand Up @@ -136106,6 +136136,8 @@ def _memFactorPG(stat):
nsid = os.readlink("%s/ns/%s" % (value["taskPath"], ns))
nsid = nsid[len(ns) + 2 : -1]
etc = self.nsProcData[ns][nsid]
elif SysMgr.attrEnable:
etc = value["attr"]
else:
pgid = procData[idx]["stat"][self.ppidIdx]
if pgid == "0":
Expand Down

0 comments on commit 248ac09

Please sign in to comment.