[PATCH 17/19] agetty: replace gethostbyname() with getaddrinfo()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The gethostbyname() is legacy function which may be withdrawn in a
future.

Reference: http://pubs.opengroup.org/onlinepubs/009695399/functions/gethostbyname.html
Signed-off-by: Sami Kerola <kerolasa@xxxxxx>
---
 term-utils/agetty.8 |  2 +-
 term-utils/agetty.c | 20 ++++++++++++++++----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/term-utils/agetty.8 b/term-utils/agetty.8
index e400ec8..e51017d 100644
--- a/term-utils/agetty.8
+++ b/term-utils/agetty.8
@@ -215,7 +215,7 @@ no hostname at all will be shown.
 \-\-long\-hostname
 By default the hostname is only printed until the first dot.  With
 this option enabled, the full qualified hostname by gethostname()
-or if not found by gethostbyname() is shown.
+or if not found by getaddrinfo() is shown.
 .TP
 \-\-version
 Output version information and exit.
diff --git a/term-utils/agetty.c b/term-utils/agetty.c
index 244ea29..ac7c16c 100644
--- a/term-utils/agetty.c
+++ b/term-utils/agetty.c
@@ -1298,17 +1298,29 @@ static void do_prompt(struct options *op, struct termios *tp)
 		if (!hn)
 			log_err(_("failed to allocate memory: %m"));
 		if (gethostname(hn, sysconf(_SC_HOST_NAME_MAX)) == 0) {
-			struct hostent *ht;
 			char *dot = strchr(hn, '.');
 
 			if ((op->flags & F_LONGHNAME) == 0) {
 				if (dot)
 					*dot = '\0';
 				write_all(STDOUT_FILENO, hn, strlen(hn));
-			} else if (dot == NULL && (ht = gethostbyname(hn)))
-				write_all(STDOUT_FILENO, ht->h_name, strlen(ht->h_name));
-			else
+			} else if (dot == NULL) {
+				struct addrinfo *res, hints;
+				memset(&hints, 0, sizeof(hints));
+				hints.ai_flags = AI_CANONNAME;
+				if (!getaddrinfo(hn, NULL, &hints, &res)) {
+					write_all(STDOUT_FILENO,
+						  res->ai_canonname,
+						  strlen(res->ai_canonname));
+					freeaddrinfo(res);
+				} else {
+					freeaddrinfo(res);
+					goto printhn;
+				}
+			} else {
+ printhn:
 				write_all(STDOUT_FILENO, hn, strlen(hn));
+			}
 			write_all(STDOUT_FILENO, " ", 1);
 		}
 		free(hn);
-- 
1.7.12.2

--
To unsubscribe from this list: send the line "unsubscribe util-linux" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]

  Powered by Linux