linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] kernel-shark: Retrieve the home of root from "/etc/passwd"
@ 2019-11-28 11:45 Yordan Karadzhov (VMware)
  2019-11-28 14:27 ` Slavomir Kaslev
  0 siblings, 1 reply; 6+ messages in thread
From: Yordan Karadzhov (VMware) @ 2019-11-28 11:45 UTC (permalink / raw)
  To: rostedt; +Cc: linux-trace-devel, Yordan Karadzhov (VMware)

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@goodmis.org>
Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-12-13 20:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-28 11:45 [PATCH] kernel-shark: Retrieve the home of root from "/etc/passwd" Yordan Karadzhov (VMware)
2019-11-28 14:27 ` Slavomir Kaslev
2019-11-29 18:44   ` Steven Rostedt
2019-12-12 13:30   ` Yordan Karadzhov (VMware)
2019-12-12 15:18     ` Slavomir Kaslev
2019-12-13 11:43       ` Yordan Karadzhov (VMware)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).