diff mbox series

kernel-shark: Retrieve the home of root from "/etc/passwd"

Message ID 20191128114500.6289-1-y.karadz@gmail.com (mailing list archive)
State Superseded
Headers show
Series kernel-shark: Retrieve the home of root from "/etc/passwd" | expand

Commit Message

Yordan Karadzhov Nov. 28, 2019, 11:45 a.m. UTC
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(-)

Comments

Slavomir Kaslev Nov. 28, 2019, 2:27 p.m. UTC | #1
On Thu, Nov 28, 2019 at 1:46 PM Yordan Karadzhov (VMware)
<y.karadz@gmail.com> wrote:
[...]
> +       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;
> +                       }
> +               }
> +

Another option is to use getpwent(3) [0] instead of parsing /etc/passwd by hand.

Cheers,

-- Slavi

[0] http://man7.org/linux/man-pages/man3/getpwent.3.html
Steven Rostedt Nov. 29, 2019, 6:44 p.m. UTC | #2
On Thu, 28 Nov 2019 16:27:11 +0200
Slavomir Kaslev <slavomir.kaslev@gmail.com> wrote:

> On Thu, Nov 28, 2019 at 1:46 PM Yordan Karadzhov (VMware)
> <y.karadz@gmail.com> wrote:
> [...]
> > +       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;
> > +                       }
> > +               }
> > +  
> 
> Another option is to use getpwent(3) [0] instead of parsing /etc/passwd by hand.

Thank you! My google fu is starting to show its age, as I did a bit of
searching looking for a library function to parse passwd, knowing one had
to be there, but came up empty. This is exactly what I was looking for.

Yes, Yordan, I think using this would be the better option as if I had
found it, it would have been what I would have suggested in the first
place.

-- Steve
Yordan Karadzhov Dec. 12, 2019, 1:30 p.m. UTC | #3
On 28.11.19 г. 16:27 ч., Slavomir Kaslev wrote:
> On Thu, Nov 28, 2019 at 1:46 PM Yordan Karadzhov (VMware)
> <y.karadz@gmail.com> wrote:
> [...]
>> +       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;
>> +                       }
>> +               }
>> +
> 
> Another option is to use getpwent(3) [0] instead of parsing /etc/passwd by hand.
> 

Hi Slavi,

Thanks for pointing out this!
This simplifies the patch a lot. I am sending a new version.

cheers,
Yordan

> Cheers,
> 
> -- Slavi
> 
> [0] http://man7.org/linux/man-pages/man3/getpwent.3.html
>
Slavomir Kaslev Dec. 12, 2019, 3:18 p.m. UTC | #4
On Thu, Dec 12, 2019 at 3:30 PM Yordan Karadzhov (VMware)
<y.karadz@gmail.com> wrote:
[...]

> Thanks for pointing out this!
> This simplifies the patch a lot. I am sending a new version.

I just saw the patch and I think I mislead you (sorry about that) by
referring you to getpwent(3), since getpwuid(3) is a better match for
this use-case and will make the patch even smaller.
Yordan Karadzhov Dec. 13, 2019, 11:43 a.m. UTC | #5
On 12.12.19 г. 17:18 ч., Slavomir Kaslev wrote:
> On Thu, Dec 12, 2019 at 3:30 PM Yordan Karadzhov (VMware)
> <y.karadz@gmail.com> wrote:
> [...]
> 
>> Thanks for pointing out this!
>> This simplifies the patch a lot. I am sending a new version.
> 
> I just saw the patch and I think I mislead you (sorry about that) by
> referring you to getpwent(3), since getpwuid(3) is a better match for
> this use-case and will make the patch even smaller.

Yes, getpwuid(3) is perfect. Thanks a lot!
Sending v3.

Y.
diff mbox series

Patch

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);