Do not assume that root is always at "/root". Instead read the "/etc/passwd" file and searches for the user id of 0. Return the home path for that user. On any error just quietly default back to "/root". Suggested-by: Steven Rostedt (VMware) <rostedt@xxxxxxxxxxx> Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@xxxxxxxxx> --- kernel-shark/src/KsMainWindow.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/kernel-shark/src/KsMainWindow.cpp b/kernel-shark/src/KsMainWindow.cpp index bd6c338..fa887fc 100644 --- a/kernel-shark/src/KsMainWindow.cpp +++ b/kernel-shark/src/KsMainWindow.cpp @@ -422,6 +422,30 @@ QString KsMainWindow::_getCacheDir() QDir().mkpath(dir); }; + auto lamRootHome = [] () { + QFile fPswd("/etc/passwd"); + QString home("/root"); + QStringList userInfo; + + fPswd.open(QIODevice::ReadOnly); + if (!fPswd.isOpen()) + return home; + + QTextStream s(&fPswd); + while (!s.atEnd()) { + userInfo = s.readLine().split(':'); + + /* Check the User Id. */ + if (userInfo[2].toInt() == 0) { + home = userInfo[5]; + break; + } + } + + fPswd.close(); + return home; + }; + dir = getenv("KS_USER_CACHE_DIR"); if (!dir.isEmpty()) { if (!QDir(dir).exists()) @@ -432,7 +456,7 @@ QString KsMainWindow::_getCacheDir() dir += "/kernelshark"; if (geteuid() == 0) - dir.replace(QDir::homePath(), "/root"); + dir.replace(QDir::homePath(), lamRootHome()); if (!QDir(dir).exists()) lamMakePath(false); -- 2.20.1