diff options
author | Karel Zak | 2012-10-19 16:27:46 +0200 |
---|---|---|
committer | Karel Zak | 2012-10-19 16:27:46 +0200 |
commit | 2448f336379d1cba610d809b9220e54838af24d6 (patch) | |
tree | 67d8ff3f755d822f70f17cb61fc61c844eb0ac5a /term-utils | |
parent | login: stop using MAXHOSTNAMELEN (diff) | |
download | kernel-qcow2-util-linux-2448f336379d1cba610d809b9220e54838af24d6.tar.gz kernel-qcow2-util-linux-2448f336379d1cba610d809b9220e54838af24d6.tar.xz kernel-qcow2-util-linux-2448f336379d1cba610d809b9220e54838af24d6.zip |
agetty: stop using MAXHOSTNAMELEN
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'term-utils')
-rw-r--r-- | term-utils/agetty.c | 78 |
1 files changed, 55 insertions, 23 deletions
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': |