Skip to contentSkip to conversation

kernel-shark: Retrieve the home of root using getpwent()

PATCH Superseded Yordan Karadzhov (VMware) 1 participant 1 message Dec 12, 2019

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 kernel-shark: Retrieve the home of root using getpwent()

Do not assume that root is always at "/root". Instead use getpwent(void) and searche for user id of 0. Return the home path for that user. On any error just quietly default back to "/root".

---
 kernel-shark/src/KsMainWindow.cpp | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
kernel-shark/src/KsMainWindow.cpp +15 -1
index bd6c338..31224ed 100644
@@ -422,6 +422,20 @@ QString KsMainWindow::_getCacheDir()
 		QDir().mkpath(dir);
 	};
 
+	auto lamRootHome = [] () {
+		QString home("/root");
+		struct passwd *pwd;
+
+		while ((pwd = getpwent()) != NULL) {
+			if (pwd->pw_uid == 0) {
+				home = pwd->pw_dir;
+				break;
+			}
+		}
+
+		return home;
+	};
+
 	dir = getenv("KS_USER_CACHE_DIR");
 	if (!dir.isEmpty()) {
 		if (!QDir(dir).exists())
@@ -432,7 +446,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);
Signature · 1 line
2.20.1