Opened 13 years ago

Closed 12 years ago

#1250 closed bug (fixed)

hostname not fully implemented

Reported by: jonas.kirilla Owned by: axeld
Priority: normal Milestone: R1
Component: System/ Version: R1/pre-alpha1
Keywords: Cc:
Blocked By: Blocking:
Has a Patch: no Platform: All


Shell command 'hostname' returns 'beast'. Shell command 'uname --nodename' returns 'unknown'.

Both are hardcoded values. /etc/hostname is ignored.

Setting a hostname using 'hostname foo' does not work.

Implementation ideas:


  • call gethostname(), copy this to utsname->nodename


  • implement reading of /etc/hostname if present, in gethostname()
  • implement writing of /etc/hostname, in sethostname()

The supplied patch makes uname.c consult gethostname() so both shell commands agree that (for now) the name of the host is 'beast'.

I will try to also make a patch for hostname.c.

Attachments (1)

uname.c.diff (1010 bytes ) - added by jonas.kirilla 13 years ago.

Download all attachments as: .zip

Change History (4)

by jonas.kirilla, 13 years ago

Attachment: uname.c.diff added

comment:1 by jonas.kirilla, 13 years ago

  1. Looking at it some more I may have to rewrite the patch for uname.c, as gethostname() can return errors.
  1. hostname -> nodename truncation okay?

Utsname.nodename is limited by _SYS_NAMELEN to 32 bytes, whereas MAXHOSTNAMELEN is defined as 256 in sys/param.h. Calling gethostname() with a too short string would of course truncate it. I don't know if this truncation of the hostname is a problem for uname or not.

FreeBSD appears to use a 32 bytes field length for utsname in the kernel, and 256 when in user space.

  1. HOST_NAME_MAX appears to be the better choice over MAXHOSTNAMELEN

The FreeBSD man page on GETHOSTNAME(3) has this to say:

STANDARDS The gethostname() function conforms to IEEE Std 1003.1-2001 (POSIX.1). Callers should be aware that {HOST_NAME_MAX} may be variable or infinite, but is guaranteed to be no less than {_POSIX_HOST_NAME_MAX}. On older systems, this limit was defined in the non-standard header <sys/param.h> as MAXHOSTNAMELEN, and counted the terminating null. The sethostname() function and the error returns for gethostname() are not standardized.

So far I haven't figured out whether HOST_NAME_MAX is used in Linux. I've only found a lot of MAXHOSTNAMELEN in the sources.

  1. Would things such as these be better discussed on the development mailinglist?

comment:2 by jonas.kirilla, 13 years ago

FWIW, in glibc gethostname() is implemented with a call to uname(), the other way around from what my patch does.

comment:3 by axeld, 12 years ago

Resolution: fixed
Status: newclosed

Thanks for your work! It's fixed in hrev22492.

Note: See TracTickets for help on using tickets.