Add Book to My BookshelfPurchase This Book Online

Chapter 9 - System Configuration and Resource Limits

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

General System Information
Each system maintains a number of general information parameters, including the host name, operating system name, operating system release number, hardware serial number, and machine architecture. The system call to obtain this information is uname:
    #include <sys/utsname.h>
    int uname(struct utsname *name);
This function places system configuration information in the structure pointed to by name and returns a non-negative value on success. If a failure occurs, -1 is returned and the external integer  errno is set to the error number.
The struct utsname structure has the following members:
    struct utsname {
        char    sysname[SYS_NMLN];
        char    nodename[SYS_NMLN];
        char    release[SYS_NMLN];
        char    version[SYS_NMLN];
        char    machine[SYS_NMLN];
    };
sysname
A null-terminated string naming the current operating system
nodename
A null-terminated string containing the name the system is known by on a communications network (its host name)
release
A null-terminated string identifying the operating system release
version
A null-terminated string identifying the operating system version
machine
A null-terminated string identifying the type of hardware the operating system is running on (the machine architecture)
The uname call is specified by the POSIX standard, which adopted it from versions of System V UNIX. SVR4 also provides another call, sysinfo, that performs a similar function, but can provide some additional information:
    #include <sys/systeminfo.h>
    long sysinfo(int command, char *buf, long count);
The sysinfo function copies information about the operating system, as requested by  command, into  buf. The  count parameter specifies the length of buf; it should be at least 257 bytes in size. Upon successful completion, sysinfo returns the number of bytes in  buf required to hold the return value and the terminating null character. If this value is less than or equal to count, the whole value was copied, otherwise, count-1 bytes plus a terminating null character were copied. If an error occurs, -1 is returned and the reason for failure is stored in  errno.
The legal values for command, defined in sys/systeminfo.h, are:
SI_SYSNAME
Returns the operating system name. This is the same value returned by uname in the sysname field.
SI_HOSTNAME
Returns the name of the current host, as it is known on a communications network. This is the same value returned by uname in the nodename field.
SI_SET_HOSTNAME
Sets the system host name to the value contained in  buf. This command is restricted to the superuser.
SI_RELEASE
Returns the operating system release. This is the same value returned by uname in the release field.
SI_VERSION
Returns the operating system version. This is the same value returned by uname in the version field.
SI_MACHINE
Returns the machine type. This is the same value returned by uname in the machine field.
SI_ARCHITECTURE
Returns the hardware instruction set architecture.
SI_HW_PROVIDER
Returns the name of the hardware manufacturer.
SI_HW_SERIAL
Returns the ASCII representation of the hardware-specific serial number of the physical machine. In common usage, this number is usually called the hostid, and does not necessarily represent the true serial number of the machine. However, it is assumed that when the two strings returned by SI_HW_PROVIDER and SI_HW_SERIAL are combined, the result will be unique. This value may contain non-numeric characters. Note that on Sun systems, this value is usually represented as a hexadecimal number, but sysinfo returns it as a decimal number.
SI_SRPC_DOMAIN
Returns the Secure Remote Procedure Call domain name.
SI_SET_SRPC_DOMAIN
Sets the Secure Remote Procedure Call domain name to the value contained in buf.
The sysinfo function is not available in HP-UX 10.x.
Example 9-1 shows a program that prints out the information obtained by uname and sysinfo.
Example 9-1:  systeminfo
#include <sys/systeminfo.h>
#include <sys/utsname.h>
#include <stdio.h>
typedef struct {
    int     command;
    char    *string;
} Info;
Info info[] = {
    SI_SYSNAME,         "SI_SYSNAME",
    SI_HOSTNAME,        "SI_HOSTNAME",
    SI_RELEASE,         "SI_RELEASE",
    SI_VERSION,         "SI_VERSION",
    SI_MACHINE,         "SI_MACHINE",
    SI_ARCHITECTURE,    "SI_ARCHITECTURE",
    SI_HW_PROVIDER,     "SI_HW_PROVIDER",
    SI_HW_SERIAL,       "SI_HW_SERIAL",
    SI_SRPC_DOMAIN,     "SI_SRPC_DOMAIN",
    0,                  NULL
};
int
main(void)
{
    Info *ip;
    char buf[BUFSIZ];
    struct utsname name;
    /*
     * Request uname information.
     */
    if (uname(&name) < 0) {
        perror("uname");
        exit(1);
    }
    /*
     * Print it out.
     */
    printf("Uname information:\n");
    printf("\t sysname: %s\n", name.sysname);
    printf("\tnodename: %s\n", name.nodename);
    printf("\t release: %s\n", name.release);
    printf("\t version: %s\n", name.version);
    printf("\t machine: %s\n", name.machine);
    /*
     * Request and print system information.
     */
    printf("\nSysinfo information:\n");
    for (ip = info; ip->string != NULL; ip++) {
        if (sysinfo(ip->command, buf, sizeof(buf)) < 0) {
            perror("sysinfo");
            exit(1);
        }
        printf("%16s: %s\n", ip->string, buf);
    }
    exit(0);
}
    % systeminfo
    Uname information:
             sysname: SunOS
            nodename: msw
             release: 5.3
             version: Generic
             machine: sun4m
    Sysinfo information:
          SI_SYSNAME: SunOS
         SI_HOSTNAME: msw
          SI_RELEASE: 5.3
          SI_VERSION: Generic
          SI_MACHINE: sun4m
     SI_ARCHITECTURE: sparc
      SI_HW_PROVIDER: Sun_Microsystems
        SI_HW_SERIAL: 2147630684
      SI_SRPC_DOMAIN:
Porting Notes
Most systems based on a version of System V offer the uname system call, but not sysinfo. Versions based on BSD, however, offer two different calls that obtain only some parts of the information described above:
    int gethostname(char *name, int len);
    int sethostname(char *name, int len);
    long gethostid(void);
The gethostname function copies the current name of the host, as it is known on a communications network, into the character array pointed to by  name, which is  len characters long. The sethostname function sets the current host name to the value contained in  name. This call is restricted to the superuser. The gethostid function returns a 32-bit identifier for the system, which should be unique across all hosts. This value is equivalent to the one returned by the SI_HW_SERIAL command to the sysinfo function.  (On early BSD systems such as the VAX, where the serial number was not available through software, this value was equal to the system's IP address.)

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback