From: Clark Dorman <clark@s3i.com>
Newsgroups: comp.parallel.pvm
Subject: Re: gethostname() ???
Date: 03 Sep 1998 12:39:57 -0400
Organization: PSINet
Message-Id: <dww7l2lr6.fsf@s3i.com>
References: <35EDD414.736F5744@nichols.com>
    <35EE87AF.E1AD1D89@eecs.rwth-aachen.de>


Juergen Kuersch <kue.s.pam@eecs.rwth-aachen.de> writes:
> Eric George wrote:
> > 
> > Where is this function defined?  It is used in the hello_other.c source
> > file for the hello world example.  I've compiled these programs before
> > with no problem.
> > But I've been away from it for awhile and have obviously forgotten
> > something :-(
> > 
> > I can't seem to find anything in the documentation or header files, and
> > when I try to compile I get an "unresolved symbol" error.
> > I'm sure this is very obvious, but I can't seem to come up with it.
> > Eric
> 
> Gethostname is not a PVM function. On SPARC Solaris, it is contained
> in the C library. What platform are you on ?

Well, I'm on a Sparc 20, Solaris 5.5.1.  The man gethostname says: 

----------------------------------------------------------------------
gethostname(3C)        C Library Functions        gethostname(3C)

NAME
     gethostname, sethostname - get/set name of current host

SYNOPSIS
     int gethostname(char *name, int namelen);

     int sethostname(char *name, int namelen);

----------------------------------------------------------------------

which is really weird, because for most C Library functions, they have
a line that says which header to include.  For example, for getopt, it
says:

----------------------------------------------------------------------
getopt(3C)             C Library Functions             getopt(3C)

NAME
     getopt - get option letter from argument vector

SYNOPSIS
     #include <stdlib.h>

     int getopt(int argc, char * const *argv,
          const char *optstring);

----------------------------------------------------------------------

A grep in /usr/include does not produce any declarations of the
gethostname function either.  

If you do an 'ar -t /usr/lib/libc.a | grep gethost', on 5.5.1, it
says:

gethostid.o
gethostname.o

Which means that you can use them, and you either have to declare your
own extern gethostname someplace or live with the compiler warning.
I'm actually doing C++, so if I need it, I say (in my .cc file):

//----------------------------------------------------------------------
// These are not defined in the .h files. 
extern "C"
{
   char *pvmgetpvmd();     // Should be in pvm3.h, but is really an internal
   int gethostname();      // Should be in some system include file, but isnt
}
//----------------------------------------------------------------------

Note that I'm also having to declare pvmgetpvmd since it is supposed
to be an internal function.  I needed it for something, so I just
declared it myself.  Of course, if it's internal, you have to keep in
mind that the maintainers of PVM could change it and you wouldn't have
a right to complain, since it is not part of the public interface (to
use C++ terminology).


_However_, on Red Hat Linux and on Silicon Graphics Irix 5.3 (yes, I
know it's ancient, but I'm stuck with it), there is a gethostname
prototype in unistd.h.  Why is it not in unistd.h on Solaris?  Because
Solaris headers suck.  Just try finding rstat in either
/usr/include/rpc or /usr/include/rpcsvc.  It's not there, even though
if you do a man rstat, it tells that it is. (BOO HISS!!)  And of
course, rstat is _exactly_ the sort of thing that you want to do in a
parallel environment, to figure out how busy the other computers are.

Note that it is possible that gethostname just does not exist on your
system.  As it turns out, there is no vanilla rstat in Red Hat Linux
(BOO HISS), so I'm having to rethink things with regards to getting
information about the other computers, with a more complicated call
that is consistently in rpcsvc.

There is another way around this problem, and that is to find out if
there is another way to get the information about the computer name.
For example, you might be able to use some function in
<sys/sysinfo.h>, or <sys/utsname.h>, or use getenv to get the
information from the user's environment.  Or as a last resort, you can
do a system call to uname and try to parse the output.

And all this is why POSIX is a good thing.

-- 
Clark Dorman				"Evolution is cleverer than you are."
http://cns-web.bu.edu/pub/dorman/D.html                -Francis Crick

