From 2448f336379d1cba610d809b9220e54838af24d6 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Fri, 19 Oct 2012 16:27:46 +0200 Subject: agetty: stop using MAXHOSTNAMELEN Signed-off-by: Karel Zak --- term-utils/agetty.c | 78 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 23 deletions(-) (limited to 'term-utils/agetty.c') diff --git a/term-utils/agetty.c b/term-utils/agetty.c index 4e2313592..9bb0fd753 100644 --- a/term-utils/agetty.c +++ b/term-utils/agetty.c @@ -1220,6 +1220,41 @@ static void auto_baud(struct termios *tp) tcsetattr(STDIN_FILENO, TCSANOW, tp); } +static char *xgethostname(void) +{ + char *name; + size_t sz = get_hostname_max() + 1; + + name = malloc(sizeof(char) * sz); + if (!name) + log_err(_("failed to allocate memory: %m")); + + if (gethostname(name, sz) != 0) + return NULL; + + name[sz - 1] = '\0'; + return name; +} + +static char *xgetdomainname(void) +{ +#ifdef HAVE_GETDOMAINNAME + char *name; + size_t sz = get_hostname_max() + 1; + + name = malloc(sizeof(char) * sz); + if (!name) + log_err(_("failed to allocate memory: %m")); + + if (getdomainname(name, sz) != 0) + return NULL; + + name[sz - 1] = '\0'; + return name; +#endif + return NULL; +} + /* Show login prompt, optionally preceded by /etc/issue contents. */ static void do_prompt(struct options *op, struct termios *tp) { @@ -1293,12 +1328,12 @@ 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 = xgethostname(); + + if (hn) { struct hostent *ht; char *dot = strchr(hn, '.'); - hn[MAXHOSTNAMELEN] = '\0'; if ((op->flags & F_LONGHNAME) == 0) { if (dot) *dot = '\0'; @@ -1308,6 +1343,7 @@ static void do_prompt(struct options *op, struct termios *tp) else write_all(STDOUT_FILENO, hn, strlen(hn)); write_all(STDOUT_FILENO, " ", 1); + free(hn); } } if (op->autolog == (char*)0) { @@ -1732,7 +1768,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,9 +1776,8 @@ static void output_ip(sa_family_t family) if (family == AF_INET6) hints.ai_flags = AI_V4MAPPED; - if (gethostname(host, sizeof(host)) == 0 - && getaddrinfo(host, NULL, &hints, &info) == 0 - && info) { + host = xgethostname(); + if (host && getaddrinfo(host, NULL, &hints, &info) == 0 && info) { void *addr = NULL; char buff[INET6_ADDRSTRLEN + 1]; @@ -1760,6 +1795,7 @@ static void output_ip(sa_family_t family) freeaddrinfo(info); } + free(host); } /* @@ -1814,36 +1850,32 @@ static void output_special_char(unsigned char c, struct options *op, break; case 'o': { - char domainname[MAXHOSTNAMELEN+1]; -#ifdef HAVE_GETDOMAINNAME - if (getdomainname(domainname, sizeof(domainname))) -#endif - strcpy(domainname, "unknown_domain"); - domainname[sizeof(domainname)-1] = '\0'; - printf("%s", domainname); + char *dom = xgetdomainname(); + + fputs(dom ? dom : "unknown_domain", stdout); + free(dom); break; } case 'O': { - char *dom = "unknown_domain"; - char host[MAXHOSTNAMELEN+1]; + char *dom = NULL; + char *host = xgethostname(); struct addrinfo hints, *info = NULL; memset(&hints, 0, sizeof(hints)); hints.ai_flags = AI_CANONNAME; - if (gethostname(host, sizeof(host)) || - getaddrinfo(host, NULL, &hints, &info) || - info == NULL) - fputs(dom, stdout); - else { + if (host && getaddrinfo(host, NULL, &hints, &info) == 0 && info) { char *canon; + if (info->ai_canonname && (canon = strchr(info->ai_canonname, '.'))) dom = canon + 1; - fputs(dom, stdout); - freeaddrinfo(info); } + fputs(dom ? dom : "unknown_domain", stdout); + if (info) + freeaddrinfo(info); + free(host); break; } case 'd': -- cgit v1.2.3-55-g7522