Commit 2933df58 authored by Rune Juhl Jacobsen's avatar Rune Juhl Jacobsen

Handle unknown user in cachetop

If running on a system that uses cgroups the process owner uid may not exist on
the host system and `pwd.getpwuid` throws a KeyError.

This patch defaults to a uid of `UNKNOWN(id)` when we're unable to look up the
username.
parent ee433d4e
......@@ -222,11 +222,20 @@ def handle_loop(stdscr, args):
)
(height, width) = stdscr.getmaxyx()
for i, stat in enumerate(process_stats):
uid = int(stat[1])
try:
username = pwd.getpwuid(uid)[0]
except KeyError as ex:
# `pwd` throws a KeyError if the user cannot be found. This can
# happen e.g. when the process is running in a cgroup that has
# different users from the host.
username = 'UNKNOWN({})'.format(uid)
stdscr.addstr(
i + 2, 0,
"{0:8} {username:8.8} {2:16} {3:8} {4:8} "
"{5:8} {6:9.1f}% {7:9.1f}%".format(
*stat, username=pwd.getpwuid(int(stat[1]))[0]
*stat, username=username
)
)
if i > height - 4:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment