[PATCH v2] nss: Add verbose flag to getent tool
Petr Menšík
pemensik@redhat.com
Wed Oct 8 14:56:03 GMT 2025
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 <pemensik@redhat.com>
---
nss/getent.c | 20 ++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/nss/getent.c b/nss/getent.c
index f63f6892b7..137636771a 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 int 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;
--
2.51.0
More information about the Libc-alpha
mailing list