diff mbox series

common/rc: use getent when available

Message ID 52747027-2915-4c92-a6e9-b0dce1cbe198@redhat.com (mailing list archive)
State New
Headers show
Series common/rc: use getent when available | expand

Commit Message

Eric Sandeen March 10, 2025, 5:55 p.m. UTC
Zorro noticed that on systems using nss-altfiles, some tests failed when
trying to parse /etc/passwd directly. The "getent" command does the
right thing in this case, so let's use it as long as it's available.

Signed-off-by: Eric Sandeen <sandeen@redhat.com>
---

Comments

Christoph Hellwig March 11, 2025, 7:42 a.m. UTC | #1
On Mon, Mar 10, 2025 at 12:55:51PM -0500, Eric Sandeen wrote:
> Zorro noticed that on systems using nss-altfiles, some tests failed when
> trying to parse /etc/passwd directly. The "getent" command does the
> right thing in this case, so let's use it as long as it's available.

Are the reevant systyems where it does not exist?  It seems to come
with glibc and apparenly has for a long time.
Eric Sandeen March 11, 2025, 1:38 p.m. UTC | #2
On 3/11/25 2:42 AM, Christoph Hellwig wrote:
> On Mon, Mar 10, 2025 at 12:55:51PM -0500, Eric Sandeen wrote:
>> Zorro noticed that on systems using nss-altfiles, some tests failed when
>> trying to parse /etc/passwd directly. The "getent" command does the
>> right thing in this case, so let's use it as long as it's available.
> 
> Are the reevant systyems where it does not exist?  It seems to come
> with glibc and apparenly has for a long time.

It's impossible for me to know whether any systems exist that don't have
it, so I erred on the safe side to not break things for anyone.

(i.e. it could depend on packaging etc, and I simply have no idea.)

-Eric
Zorro Lang March 11, 2025, 2:18 p.m. UTC | #3
On Mon, Mar 10, 2025 at 12:55:51PM -0500, Eric Sandeen wrote:
> Zorro noticed that on systems using nss-altfiles, some tests failed when
> trying to parse /etc/passwd directly. The "getent" command does the
> right thing in this case, so let's use it as long as it's available.
> 
> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
> ---

Hahah, you're so fast:) This's similar with what I'm doing, so it's good to me.

Reviewed-by: Zorro Lang <zlang@redhat.com>

I tend to use getent too, if anyone has any better idea, please feel free to
share.

Thanks,
Zorro

> 
> diff --git a/common/rc b/common/rc
> index 6592c835..50312331 100644
> --- a/common/rc
> +++ b/common/rc
> @@ -2621,7 +2621,11 @@ _yp_active()
>  _cat_passwd()
>  {
>  	[ $(_yp_active) -eq 0 ] && ypcat passwd
> -	cat /etc/passwd
> +	if type getent &>/dev/null; then
> +		getent passwd
> +	else
> +		cat /etc/passwd
> +	fi
>  }
>  
>  # cat the group file
> @@ -2629,7 +2633,11 @@ _cat_passwd()
>  _cat_group()
>  {
>  	[ $(_yp_active) -eq 0 ] && ypcat group
> -	cat /etc/group
> +	if type getent &>/dev/null; then
> +		getent group
> +	else
> +		cat /etc/group
> +	fi
>  }
>  
>  # check if a user exists in the system
> 
>
Darrick J. Wong March 11, 2025, 3:30 p.m. UTC | #4
On Mon, Mar 10, 2025 at 12:55:51PM -0500, Eric Sandeen wrote:
> Zorro noticed that on systems using nss-altfiles, some tests failed when
> trying to parse /etc/passwd directly. The "getent" command does the
> right thing in this case, so let's use it as long as it's available.
> 
> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
> ---
> 
> diff --git a/common/rc b/common/rc
> index 6592c835..50312331 100644
> --- a/common/rc
> +++ b/common/rc
> @@ -2621,7 +2621,11 @@ _yp_active()
>  _cat_passwd()
>  {
>  	[ $(_yp_active) -eq 0 ] && ypcat passwd
> -	cat /etc/passwd
> +	if type getent &>/dev/null; then

If you want to be really nitpicky, 'type' isn't defined for posix
shells; you're supposed to use command[1]:

	if command -v getent &>/dev/null; then

I don't care that much since fstests is wholly dependent on bashisms so:
Reviewed-by: "Darrick J. Wong" <djwong@kernel.org>

if you decide to change it or not.

--D

[1] https://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html

> +		getent passwd
> +	else
> +		cat /etc/passwd
> +	fi
>  }
>  
>  # cat the group file
> @@ -2629,7 +2633,11 @@ _cat_passwd()
>  _cat_group()
>  {
>  	[ $(_yp_active) -eq 0 ] && ypcat group
> -	cat /etc/group
> +	if type getent &>/dev/null; then
> +		getent group
> +	else
> +		cat /etc/group
> +	fi
>  }
>  
>  # check if a user exists in the system
> 
>
Eric Sandeen March 11, 2025, 3:44 p.m. UTC | #5
On 3/11/25 10:30 AM, Darrick J. Wong wrote:
> On Mon, Mar 10, 2025 at 12:55:51PM -0500, Eric Sandeen wrote:
>> Zorro noticed that on systems using nss-altfiles, some tests failed when
>> trying to parse /etc/passwd directly. The "getent" command does the
>> right thing in this case, so let's use it as long as it's available.
>>
>> Signed-off-by: Eric Sandeen <sandeen@redhat.com>
>> ---
>>
>> diff --git a/common/rc b/common/rc
>> index 6592c835..50312331 100644
>> --- a/common/rc
>> +++ b/common/rc
>> @@ -2621,7 +2621,11 @@ _yp_active()
>>  _cat_passwd()
>>  {
>>  	[ $(_yp_active) -eq 0 ] && ypcat passwd
>> -	cat /etc/passwd
>> +	if type getent &>/dev/null; then
> 
> If you want to be really nitpicky, 'type' isn't defined for posix
> shells; you're supposed to use command[1]:
> 
> 	if command -v getent &>/dev/null; then
> 
> I don't care that much since fstests is wholly dependent on bashisms so:

Yup, I started with "command -v," realized it wasn't used anywhere else
in fstests, and that we always used "type" - so when in Rome, etc.

> Reviewed-by: "Darrick J. Wong" <djwong@kernel.org>

Thanks,
-Eric
 
> if you decide to change it or not.
> 
> --D
diff mbox series

Patch

diff --git a/common/rc b/common/rc
index 6592c835..50312331 100644
--- a/common/rc
+++ b/common/rc
@@ -2621,7 +2621,11 @@  _yp_active()
 _cat_passwd()
 {
 	[ $(_yp_active) -eq 0 ] && ypcat passwd
-	cat /etc/passwd
+	if type getent &>/dev/null; then
+		getent passwd
+	else
+		cat /etc/passwd
+	fi
 }
 
 # cat the group file
@@ -2629,7 +2633,11 @@  _cat_passwd()
 _cat_group()
 {
 	[ $(_yp_active) -eq 0 ] && ypcat group
-	cat /etc/group
+	if type getent &>/dev/null; then
+		getent group
+	else
+		cat /etc/group
+	fi
 }
 
 # check if a user exists in the system