util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: kerolasa@iki.fi
Subject: [PATCH 04/19] agetty: stop using MAXHOSTNAMELEN
Date: Sun, 14 Oct 2012 21:20:55 +0100	[thread overview]
Message-ID: <1350246070-10544-5-git-send-email-kerolasa@iki.fi> (raw)
In-Reply-To: <1350246070-10544-1-git-send-email-kerolasa@iki.fi>

Use the sysconf(_SC_HOST_NAME_MAX) to determine maximum length of a
hostname.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 term-utils/agetty.c | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/term-utils/agetty.c b/term-utils/agetty.c
index 4e23135..754a43f 100644
--- a/term-utils/agetty.c
+++ b/term-utils/agetty.c
@@ -1293,12 +1293,14 @@ static void do_prompt(struct options *op, struct termios *tp)
 	}
 #endif /* KDGKBLED */
 	if ((op->flags & F_NOHOSTNAME) == 0) {
-		char hn[MAXHOSTNAMELEN + 1];
-		if (gethostname(hn, sizeof(hn)) == 0) {
+		char *hn;
+		hn = malloc(sizeof(char) * (sysconf(_SC_HOST_NAME_MAX) + 1));
+		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, '.');
 
-			hn[MAXHOSTNAMELEN] = '\0';
 			if ((op->flags & F_LONGHNAME) == 0) {
 				if (dot)
 					*dot = '\0';
@@ -1309,6 +1311,7 @@ static void do_prompt(struct options *op, struct termios *tp)
 				write_all(STDOUT_FILENO, hn, strlen(hn));
 			write_all(STDOUT_FILENO, " ", 1);
 		}
+		free(hn);
 	}
 	if (op->autolog == (char*)0) {
 		/* Always show login prompt. */
@@ -1732,7 +1735,7 @@ static void output_iface_ip(struct ifaddrs *addrs, const char *iface, sa_family_
 
 static void output_ip(sa_family_t family)
 {
-	char host[MAXHOSTNAMELEN + 1];
+	char *host;
 	struct addrinfo hints, *info = NULL;
 
 	memset(&hints, 0, sizeof(hints));
@@ -1740,7 +1743,10 @@ static void output_ip(sa_family_t family)
 	if (family == AF_INET6)
 		hints.ai_flags = AI_V4MAPPED;
 
-	if (gethostname(host, sizeof(host)) == 0
+	host = malloc(sizeof(char) * (sysconf(_SC_HOST_NAME_MAX) + 1));
+	if (!host)
+		log_err(_("failed to allocate memory: %m"));
+	if (gethostname(host, sysconf(_SC_HOST_NAME_MAX)) == 0
 	    && getaddrinfo(host, NULL, &hints, &info) == 0
 	    && info) {
 
@@ -1760,6 +1766,7 @@ static void output_ip(sa_family_t family)
 
 		freeaddrinfo(info);
 	}
+	free(host);
 }
 
 /*
@@ -1814,25 +1821,30 @@ static void output_special_char(unsigned char c, struct options *op,
 		break;
 	case 'o':
 	{
-		char domainname[MAXHOSTNAMELEN+1];
+		char *domainname;
+		domainname = malloc(sizeof(char) * (sysconf(_SC_HOST_NAME_MAX) + 1));
+		if (!domainname)
+			log_err(_("failed to allocate memory: %m"));
 #ifdef HAVE_GETDOMAINNAME
-		if (getdomainname(domainname, sizeof(domainname)))
+		if (getdomainname(domainname, sysconf(_SC_HOST_NAME_MAX)))
 #endif
 		strcpy(domainname, "unknown_domain");
-		domainname[sizeof(domainname)-1] = '\0';
 		printf("%s", domainname);
 		break;
 	}
 	case 'O':
 	{
 		char *dom = "unknown_domain";
-		char host[MAXHOSTNAMELEN+1];
+		char *host;
 		struct addrinfo hints, *info = NULL;
 
 		memset(&hints, 0, sizeof(hints));
 		hints.ai_flags = AI_CANONNAME;
 
-		if (gethostname(host, sizeof(host)) ||
+		host = malloc(sizeof(char) * (sysconf(_SC_HOST_NAME_MAX) + 1));
+		if (!host)
+			log_err(_("failed to allocate memory: %m"));
+		if (gethostname(host, sysconf(_SC_HOST_NAME_MAX)) ||
 		    getaddrinfo(host, NULL, &hints, &info) ||
 		    info == NULL)
 			fputs(dom, stdout);
@@ -1844,6 +1856,7 @@ static void output_special_char(unsigned char c, struct options *op,
 			fputs(dom, stdout);
 			freeaddrinfo(info);
 		}
+		free(host);
 		break;
 	}
 	case 'd':
-- 
1.7.12.2


  parent reply	other threads:[~2012-10-14 20:21 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-10-14 20:20 [PATCH 00/19] compliancy fixes Sami Kerola
2012-10-14 20:20 ` [PATCH 01/19] last: stop using MAXHOSTNAMELEN Sami Kerola
2012-10-15  1:46   ` Mike Frysinger
2012-10-14 20:20 ` [PATCH 02/19] login: " Sami Kerola
2012-10-14 20:20 ` [PATCH 03/19] write: " Sami Kerola
2012-10-15  2:12   ` Mike Frysinger
     [not found]     ` <20121015152558.GK18377@x2.net.home>
2012-10-18 20:06       ` Sami Kerola
2012-10-22  6:01         ` Mike Frysinger
2012-10-22  8:06           ` Karel Zak
2012-10-22  8:09             ` Karel Zak
2012-10-22 20:03             ` Mike Frysinger
2012-10-22 20:22               ` Karel Zak
2012-10-14 20:20 ` Sami Kerola [this message]
2012-10-14 20:20 ` [PATCH 05/19] c.h: remove unnecessary MAXHOSTNAMELEN fallback definition Sami Kerola
2012-10-14 20:20 ` [PATCH 06/19] docs: add line breaks to whereis.1 Sami Kerola
2012-10-14 20:20 ` [PATCH 07/19] sd-daemon: fix cppcheck warnings Sami Kerola
2012-10-14 22:10   ` Dave Reisner
2012-10-15  8:32     ` Sami Kerola
2012-10-14 20:20 ` [PATCH 08/19] libmount: replace usleep with nanosleep Sami Kerola
2012-10-15  2:14   ` Mike Frysinger
2012-10-14 20:21 ` [PATCH 09/19] include/all-io: " Sami Kerola
2012-10-14 20:21 ` [PATCH 10/19] hwclock: " Sami Kerola
2012-10-14 20:21 ` [PATCH 11/19] rtcwake: " Sami Kerola
2012-10-14 20:21 ` [PATCH 12/19] agetty: " Sami Kerola
2012-10-14 20:21 ` [PATCH 13/19] tailf: " Sami Kerola
2012-10-14 20:21 ` [PATCH 14/19] include/usleep: remove remaining references to usleep Sami Kerola
2012-10-14 20:21 ` [PATCH 15/19] libmount, eject: replace index() and rindex() with strrch() or strrchr() Sami Kerola
2012-10-15  2:14   ` Mike Frysinger
2012-10-14 20:21 ` [PATCH 16/19] logger: replace gethostbyname() with getaddrinfo() Sami Kerola
2012-10-14 20:21 ` [PATCH 17/19] agetty: " Sami Kerola
2012-10-14 20:21 ` [PATCH 18/19] build-sys: remove gethostbyname() check Sami Kerola
2012-10-14 20:21 ` [PATCH 19/19] fsck.cramfs: replace utime() with utimensat() Sami Kerola
2012-10-15  2:17   ` Mike Frysinger
2012-10-15  8:36     ` Sami Kerola
2012-10-15 17:39       ` Mike Frysinger
2012-10-22  9:07 ` [PATCH 00/19] compliancy fixes Karel Zak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1350246070-10544-5-git-send-email-kerolasa@iki.fi \
    --to=kerolasa@iki.fi \
    --cc=util-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).