Monday, January 28, 2008

What is the Maximum Length for Host Name in Solaris

My colleague was asking me what is the maximum length of hostname in Solaris. Sorry, I don't know, but I can find out.

The command '/bin/hostname' is used to print or set the hostname. However, /bin/hostname is actually a shell script that wrap around the 'uname' command.

With Solaris being open-sourced, we can just browse the source code of 'uname' from OpenSolaris to find out if there is a limit imposed. Below shows the code snippet:

    219  if (Sflg) {
    220   int len = strlen(nodename);
    221 
    222   if (len > SYS_NMLN - 1) {
    223    (void) fprintf(stderr, gettext(
    224     "uname: name must be <= %d letters\n"),
    225     SYS_NMLN-1);
    226    exit(1);
    227   }
    228   if (sysinfo(SI_SET_HOSTNAME, nodename, len) < 0) {
    229    int err = errno;
    230    (void) fprintf(stderr, gettext(
    231     "uname: error in setting name: %s\n"),
    232     strerror(err));
    233    exit(1);
    234   }
    235   return (0);
    236  }
There you go, the limit is SYS_NMLN. SYS_NMLN is defined in <sys/utsname.h> which is set as 257 (256 + string terminate character 'null'). BTW, the 'man uname' documented this too.

Another way to find out is to do a brute-force approach. Below script will try to set the hostname starting with 1 character at a time until it throws exception. A trap is setup to reset the hostname and print out the maximum length of the hostname before it exit gracefully.

#! /bin/sh

orig=`hostname`
name=""
count=0


trap 'hostname $orig; echo Maximum Length for host name is $count; exit' 0 1 2 9 15


while :
do
        name="a$name"
        hostname $name > /dev/null 2>&1
        if [ $? -ne 0 ]; then
                exit
        fi
        count=`expr $count + 1`
done

Labels: ,

0 Comments:

Post a Comment

<< Home