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

* Re: [PATCH] kernel-shark: Retrieve the home of root from "/etc/passwd"
  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)
  0 siblings, 2 replies; 6+ messages in thread
From: Slavomir Kaslev @ 2019-11-28 14:27 UTC (permalink / raw)
  To: Yordan Karadzhov (VMware); +Cc: Steven Rostedt, linux-trace-devel

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

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

* Re: [PATCH] kernel-shark: Retrieve the home of root from "/etc/passwd"
  2019-11-28 14:27 ` Slavomir Kaslev
@ 2019-11-29 18:44   ` Steven Rostedt
  2019-12-12 13:30   ` Yordan Karadzhov (VMware)
  1 sibling, 0 replies; 6+ messages in thread
From: Steven Rostedt @ 2019-11-29 18:44 UTC (permalink / raw)
  To: Slavomir Kaslev; +Cc: Yordan Karadzhov (VMware), linux-trace-devel

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

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

* Re: [PATCH] kernel-shark: Retrieve the home of root from "/etc/passwd"
  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
  1 sibling, 1 reply; 6+ messages in thread
From: Yordan Karadzhov (VMware) @ 2019-12-12 13:30 UTC (permalink / raw)
  To: Slavomir Kaslev; +Cc: Steven Rostedt, linux-trace-devel



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
> 

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

* Re: [PATCH] kernel-shark: Retrieve the home of root from "/etc/passwd"
  2019-12-12 13:30   ` Yordan Karadzhov (VMware)
@ 2019-12-12 15:18     ` Slavomir Kaslev
  2019-12-13 11:43       ` Yordan Karadzhov (VMware)
  0 siblings, 1 reply; 6+ messages in thread
From: Slavomir Kaslev @ 2019-12-12 15:18 UTC (permalink / raw)
  To: Yordan Karadzhov (VMware); +Cc: Steven Rostedt, linux-trace-devel

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.

-- 
Slavomir Kaslev

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

* Re: [PATCH] kernel-shark: Retrieve the home of root from "/etc/passwd"
  2019-12-12 15:18     ` Slavomir Kaslev
@ 2019-12-13 11:43       ` Yordan Karadzhov (VMware)
  0 siblings, 0 replies; 6+ messages in thread
From: Yordan Karadzhov (VMware) @ 2019-12-13 11:43 UTC (permalink / raw)
  To: Slavomir Kaslev; +Cc: Steven Rostedt, linux-trace-devel



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.

^ permalink raw reply	[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).