[4587] in linux-net channel archive
gethostbyname() and gethostname() problems
daemon@ATHENA.MIT.EDU (Tony Nugent)
Mon Sep 30 08:44:55 1996
Date: Mon, 30 Sep 1996 10:24:18 +1000
To: linux-gcc@vger.rutgers.edu, linux-net@vger.rutgers.edu
From: Tony Nugent <tony@trishul.sci.gu.edu.au>
[posted to both linux-gcc and linux-net]
It seems I have a problem with the gethostname() and gethostbyname()
functions in my libc. This is either a bug, or a local configuration
problem.
I have here:
linux-2.0.20, libc-5.3.12, gcc-2.7.2, totally revamped slackware
2.2/2.3 installation (which I'm sorry I ever used - one day I'll
install RedHat onto a clean partition).
What has drawn my attention to this is syslogd's behaviour since I
upgraded it (twice). Initially my localhost name in my syslogs was
identified as `tashi' (see below), but then it changed to `sctnugen'
when I upgraded to sysklogd-1.3, about the same time that I upgraded
my system to cope with the linux-2.0.x kernels (which included moving
to libc-5.3.12). The recent upgrade patch to sysklogd-1.3-3 did not
seem to touch the gethost{by}name() functions.
But now it has again changed to `(none)' since I applied the 1.3.3
beta patches to those sources (I did that because syslogd was not
being successully started as a daemon from my rc scripts on bootup).
An example entry from /var/adm/syslog...
Sep 20 10:14:06 (none) login: ROOT LOGIN ON tty6
^^^^^^
Yuk... ugly :(
(Strange thing is that if I run syslogd in debug mode, my hostname
_is_ found; killing it and running it again without debug and it still
knows my hostname - until net time I reboot. Weird).
I notice that while libc-5.3.12 is still current in
sunsite.unc.edu/pub/Linux/GCC, there is libc-5.4.5 in
/pub/Linux/GCC/private/tofu/ - will this make any difference?
I've been in contact with Greg Wettstein (current maintainer of
sysklogd) about this. He says that I'm the only person who has
reported this problem, and I certainly can't figure out why this is
happening.
It appears that the value obtained for the localhost name by syslogd
is the one returned by the gethostname() and gethostbyname()
functions. Also, `man gethostname' says that:
Under Linux, gethostname is implemented at the library level by
calling uname(2).
So I wrote a quick'n'dirty program to test all this.
========8<--cut--\/--\/--\/--\/--\/--\/--\/--\/--\/--\/--\/--
#include <stdio.h>
#include <netdb.h>
#include <sys/param.h>
#include <unistd.h>
#include <sys/utsname.h>
char LocalHostName[MAXHOSTNAMELEN+1];
struct hostent *hent;
char name[MAXHOSTNAMELEN+1];
size_t len;
struct utsname myuname;
int main() {
gethostname(name, len);
if (len)
printf("gethostname = %s", name);
else
printf("no gethostname returned\n");
hent = gethostbyname(LocalHostName);
if (hent)
printf("hostname is %s\n", hent->h_name);
else
printf("no gethostbyname returned\n");
uname(&myuname);
printf( "utsname.nodename = %s\nutsname.domainname = %s\n",
myuname.nodename, myuname.domainname);
return 0;
}
========8<--cut--/\--/\--/\--/\--/\--/\--/\--/\--/\--/\--/\--
% gcc -O2 gethname.c -o gethname
% gethname
no gethostname returned
no gethostbyname returned
nodename = tashi
domainname = (none)
Hmmm. This is not looking good.
<sys/utsname.h> has this:
========8<--cut--\/--\/--\/--\/--\/--\/--\/--\/--\/--\/--\/--
#define _SYS_NMLN 257
struct utsname {
char sysname[_SYS_NMLN];
char nodename[_SYS_NMLN];
char release[_SYS_NMLN];
char version[_SYS_NMLN];
char machine[_SYS_NMLN-65];
char domainname[65]; /* Kludge to allow linux domain name to work
with SVR4 ABI. */
};
========8<--cut--/\--/\--/\--/\--/\--/\--/\--/\--/\--/\--/\--
Hmm, kludge indeed :-(
Other relevant info:
% gcc -v
Reading specs from /usr/lib/gcc-lib/i486-linux/2.7.2/specs
gcc version 2.7.2
% uname -a
Linux tashi 2.0.20 #1 Wed Sep 18 22:55:22 EST 1996 i386
% cat /etc/HOSTNAME
tashi
% grep tashi /etc/hosts
132.234.192.80 sctnugen.ppp.gu.edu.au tashi sctnugen
% grep -v "#" /etc/host.conf
order hosts, bind
multi on
% grep -v "#" /etc/host.conf
domain ppp.gu.edu.au
nameserver 132.234.1.1
nameserver 132.234.5.17
% /bin/hostname
tashi
% /bin/hostname -a
tashi
% /bin/hostname -i
132.234.192.80
% /bin/hostname -f
sctnugen.ppp.gu.edu.au
% /bin/hostname -d
ppp.gu.edu.au
% /bin/hostname -y
(none)
% /bin/hostname --nis
(none)
% /bin/hostname -v
gethostname()=`tashi'
tashi
% /bin/hostname -v -f
gethostname()=`tashi'
Resolving `tashi' ...
Result: h_name=`sctnugen.ppp.gu.edu.au'
Result: h_aliases=`tashi'
Result: h_addr_list=`132.234.192.80'
sctnugen.ppp.gu.edu.au
% /bin/hostname -V
Linux NET-3 Base Utilities
Source: net-tools 1.32-alpha net-tools@lina.inka.de (Bernd Eckenfels)
Kernelsource: 2.0.0
hostname 1.96 (1996-02-18)
% /bin/dnsdomainname
ppp.gu.edu.au
% /bin/nisdomainname
(none)
% /bin/ypdomainname
(none)
Very strange... hostname(1) *can* return my correct "gethostname()"!!
Why can't the actual libc system call do this?
What is the *correct* way to get the localhost's name?
Thanks for any clues about what's going on.
Cheers
Tony