Team BBL
Previous Page Next Page

6.9. System Identification

POSIX.1 defines the uname function to return information on the current host and operating system.

#include <sys/utsname.h>

int uname(struct utsname *name);

Returns: non-negative value if OK, 1 on error


We pass the address of a utsname structure, and the function fills it in. POSIX.1 defines only the minimum fields in the structure, which are all character arrays, and it's up to each implementation to set the size of each array. Some implementations provide additional fields in the structure.

   struct utsname {
     char  sysname[];    /* name of the operating system */
     char  nodename[];   /* name of this node */
     char  release[];    /* current release of operating system */
     char  version[];    /* current version of this release */
     char  machine[];    /* name of hardware type */
   };

Each string is null-terminated. The maximum name lengths supported by the four platforms discussed in this book are listed in Figure 6.7. The information in the utsname structure can usually be printed with the uname(1) command.

POSIX.1 warns that the nodename element may not be adequate to reference the host on a communications network. This function is from System V, and in older days, the nodename element was adequate for referencing the host on a UUCP network.

Realize also that the information in this structure does not give any information on the POSIX.1 level. This should be obtained using _POSIX_VERSION, as described in Section 2.6.

Finally, this function gives us a way only to fetch the information in the structure; there is nothing specified by POSIX.1 about initializing this information.

Historically, BSD-derived systems provide the gethostname function to return only the name of the host. This name is usually the name of the host on a TCP/IP network.

#include <unistd.h>

int gethostname(char *name, int namelen);

Returns: 0 if OK, 1 on error


The namelen argument specifies the size of the name buffer. If enough space is provided, the string returned through name is null terminated. If insufficient room is provided, however, it is unspecified whether the string is null terminated.

The gethostname function, now defined as part of POSIX.1, specifies that the maximum host name length is HOST_NAME_MAX. The maximum name lengths supported by the four implementations covered in this book are summarized in Figure 6.7.

Figure 6.7. System identification name limits

Interface

Maximum name length

FreeBSD 5.2.1

Linux 2.4.22

Mac OS X 10.3

Solaris 9

uname

256

65

256

257

gethostname

256

64

256

256


If the host is connected to a TCP/IP network, the host name is normally the fully qualified domain name of the host.

There is also a hostname(1) command that can fetch or set the host name. (The host name is set by the superuser using a similar function, sethostname.) The host name is normally set at bootstrap time from one of the start-up files invoked by /etc/rc or init.

    Team BBL
    Previous Page Next Page