[glibc] nss: Add verbose flag to getent tool

Adhemerval Zanella via Glibc-cvs <[email protected]>
Newsgroups gmane.comp.lib.glibc.cvs
Message-ID <[email protected]>
https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=d0e06e8254a4aa93ab3f612a206a5ca0116c8cc6

commit d0e06e8254a4aa93ab3f612a206a5ca0116c8cc6
Author: Petr Menšík <[email protected]>
Date:   Thu Oct 16 16:18:21 2025 +0200

    nss: Add verbose flag to getent tool
    
    Unlike older hosts database served by gethostbyname, getaddrinfo call
    can return varying return codes. Those codes can be vital for providing
    reason why name resolution on the system did not return address. Even
    when getent tool is usually present on every small container image,
    there is often no helpful tool to show getaddrinfo errors.
    
    This simple change adds verbosity flag to getent. With that it can
    provide more details about the reason of the failure. It can help to
    obtain information whether the name queried exists or does not have
    address of requested types only.
    
    The only database where this will help is ahosts* variants. I have not
    found any kind of test to expand with new verbose flag. But I think this
    would be very useful on various limited system, where bind-utils is not
    installed by default. Besides, sometimes getaddrinfo call can return
    different information than DNS protocol itself.
    
    Example of usage:
    
    nss/getent -v ahosts com.
    
    This will tell you the name exists, but has no address.
    
    Signed-off-by: Petr Menšík <[email protected]>
    Reviewed-by: Adhemerval Zanella  <[email protected]>

Diff:
---
 nss/getent.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/nss/getent.c b/nss/getent.c
index a288afe9da..7e78edabdf 100644
--- a/nss/getent.c
+++ b/nss/getent.c
@@ -58,6 +58,7 @@ static const struct argp_option args_options[] =
   {
     { "service", 's', N_("CONFIG"), 0, N_("Service configuration to be used") },
     { "no-idn", 'i', NULL, 0, N_("disable IDN encoding") },
+    { "verbose", 'v', NULL, 0, N_("print getaddrinfo error details") },
     { "no-addrconfig", 'A', NULL, 0,
       N_("do not filter out unsupported IPv4/IPv6 addresses (with ahosts*)") },
     { NULL, 0, NULL, 0, NULL },
@@ -84,6 +85,9 @@ static int idn_flags = AI_IDN | AI_CANONIDN;
 /* Set to 0 by --no-addrconfig.  */
 static int addrconfig_flags = AI_ADDRCONFIG;
 
+/* Set to 1 by -v. */
+static bool gai_verbose = false;
+
 /* Print the version information.  */
 static void
 print_version (FILE *stream, struct argp_state *state)
@@ -358,9 +362,17 @@ ahosts_keys_int (int af, int xflags, int number, char *key[])
   for (i = 0; i < number; ++i)
     {
       struct addrinfo *res;
+      int gai_result;
 
-      if (getaddrinfo (key[i], NULL, &hint, &res) != 0)
-	result = 2;
+      gai_result = getaddrinfo (key[i], NULL, &hint, &res);
+
+      if (gai_result != 0)
+	{
+	  result = 2;
+	  if (gai_verbose)
+	    fprintf (stderr, _("Host %s failed: %s\n"), key[i],
+			    gai_strerror (gai_result));
+	}
       else
 	{
 	  struct addrinfo *runp = res;
@@ -910,6 +922,10 @@ parse_option (int key, char *arg, struct argp_state *state)
       idn_flags = 0;
       break;
 
+    case 'v':
+      gai_verbose = true;
+      break;
+
     case 'A':
       addrconfig_flags = 0;
       break;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.