Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions jtop/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ def cat(path):
return f.readline().rstrip('\x00')


def cat_multiline(path, max_length=16384):
with open(path, 'r') as f:
return f.read(max_length)


def locate_commands(name, commands):
for cmd in commands:
if os.path.exists(cmd):
Expand Down
10 changes: 7 additions & 3 deletions jtop/core/processes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
import re
import os
import pwd
from .common import cat
from .common import cat, cat_multiline
# Logging
import logging
# Create logger
logger = logging.getLogger(__name__)


PROCESS_UID_REG = re.compile(r'^Uid:\s+(\d+)', re.MULTILINE)
MEM_TABLE_REG = re.compile(r'^(?P<user>\w+)\s+(?P<process>[^ ]+)\s+(?P<PID>\d+)\s+(?P<size>\d+)(?P<unit>\w)\n')
TOT_TABLE_REG = re.compile(r'total\s+(?P<size>\d+)(?P<unit>\w)')

Expand Down Expand Up @@ -70,7 +72,7 @@ def read_process_table(path_table):
class ProcessService(object):

def __init__(self):
self.usernames = {4294967295: "root"}
self.usernames = {4294967295: "root", -1: "<unknown>"}
# board type
self._root_path = "/sys/kernel"
if os.getenv('JTOP_TESTING', False):
Expand All @@ -91,7 +93,9 @@ def get_process_info(self, pid, gpu_mem_usage, process_name, uptime):
# https://man7.org/linux/man-pages/man5/proc.5.html
stat = cat(os.path.join('/proc', pid, 'stat')).split()
# Decode uid and find username
uid = int(cat(os.path.join('/proc', pid, 'loginuid')))
proc_status = cat_multiline(os.path.join('/proc', pid, 'status'))
uid_matches = PROCESS_UID_REG.findall(proc_status)
uid = int(uid_matches[0]) if uid_matches else int(-1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): We've found these issues:

if uid not in self.usernames:
self.usernames[uid] = pwd.getpwuid(uid).pw_name
# Read memory process
Expand Down
Loading