System Information
******************

This chapter describes functions that return information about the particular machine that is in use---the type of hardware, the type of software, and the individual machine's name.

Host Identification
===================

This section explains how to identify the particular machine that your program is running on.  The identification of a machine consists of its Internet host name and Internet address; see Internet Namespace. The host name should always be a fully qualified domain name, like crispy-wheats-n-chicken.ai.mit.edu, not a simple name like just crispy-wheats-n-chicken.

Prototypes for these functions appear in unistd.h.  The shell commands hostname and hostid work by calling them.

 -- Function: int gethostname (char *NAME, size_t SIZE)
     This function returns the name of the host machine in the array NAME.  The SIZE argument specifies the size of this array, in bytes.

     The return value is 0 on success and -1 on failure.  In the GNU C library, gethostname fails if SIZE is not large enough; then you can try again with a larger array.  The following errno error condition is defined for this function:

     ENAMETOOLONG
          The SIZE argument is less than the size of the host name plus one.

     On some systems, there is a symbol for the maximum possible host name length: MAXHOSTNAMELEN.  It is defined in sys/param.h. But you can't count on this to exist, so it is cleaner to handle failure and try again.

     gethostname stores the beginning of the host name in NAME even if the host name won't entirely fit.  For some purposes, a truncated host name is good enough.  If it is, you can ignore the error code.

 -- Function: int sethostname (const char *NAME, size_t LENGTH)
     The sethostname function sets the name of the host machine to NAME, a string with length LENGTH.  Only privileged processes are allowed to do this.  Usually it happens just once, at system boot time.

     The return value is 0 on success and -1 on failure. The following errno error condition is defined for this function:

     EPERM
          This process cannot set the host name because it is not privileged.

 -- Function: long int gethostid (void)
     This function returns the ``host ID'' of the machine the program is running on.  By convention, this is usually the primary Internet address of that machine, converted to a long int.  However, some systems it is a meaningless but unique number which is hard-coded for each machine.

 -- Function: int sethostid (long int ID)
     The sethostid function sets the ``host ID'' of the host machine to ID.  Only privileged processes are allowed to do this.  Usually it happens just once, at system boot time.

     The return value is 0 on success and -1 on failure. The following errno error condition is defined for this function:

     EPERM
          This process cannot set the host name because it is not privileged.

     ENOSYS
          The operating system does not support setting the host ID.  On some systems, the host ID is a meaningless but unique number hard-coded for each machine.

Hardware/Software Type Identification
=====================================

You can use the uname function to find out some information about the type of computer your program is running on.  This function and the associated data type are declared in the header file sys/utsname.h.

 -- Data Type: struct utsname
     The utsname structure is used to hold information returned by the uname function.  It has the following members:

     char sysname[]
          This is the name of the operating system in use.

     char nodename[]
          This is the network name of this particular computer.  In the GNU library, the value is the same as that returned by gethostname; see Host Identification.

     char release[]
          This is the current release level of the operating system implementation.

     char version[]
          This is the current version level within the release of the operating system.

     char machine[]
          This is a description of the type of hardware that is in use.

          Some systems provide a mechanism to interrogate the kernel directly for this information.  On systems without such a mechanism, the GNU C library fills in this field based on the configuration name that was specified when building and installing the library.

          GNU uses a three-part name to describe a system configuration; the three parts are CPU, MANUFACTURER and SYSTEM-TYPE, and they are separated with dashes.  Any possible combination of three names is potentially meaningful, but most such combinations are meaningless in practice and even the meaningful ones are not necessarily supported by any particular GNU program.

          Since the value in machine is supposed to describe just the hardware, it consists of the first two parts of the configuration name: CPU-MANUFACTURER.  For example, it might be one of these:

               "sparc-sun", "i386-ANYTHING", "m68k-hp", "m68k-sony", "m68k-sun", "mips-dec"

 -- Function: int uname (struct utsname *INFO)
     The uname function fills in the structure pointed to by INFO with information about the operating system and host machine. A non-negative value indicates that the data was successfully stored.

     -1 as the value indicates an error.  The only error possible is EFAULT, which we normally don't mention as it is always a possibility.

Which filesystems are mounted and/or available?
===============================================

The Unix concept of Everything is a file is based on the possibility to mount filesystems or other things into the filesystem.  For some programs it is desirable and necessary to access the information whether and, if yes, where a certain filesystem is mounted or simply to get lists of all the available filesystems.  The GNU libc provides some functions to retrieve this information portably.

Traditionally Unix systems have a file named /etc/fstab which describes all possibly mounted filesystems.  The mount program uses this file to mount at startup time of the system all the necessary filesystems.  The information about all the filesystems actually mounted is normally kept in a file named /etc/mtab.  Both files share the same syntax and it is crucial that this syntax is followed all the time.  Therefore it is best to never directly write the files.  The functions described in this section can do this and they also provide the functionality to convert the external textual representation to the internal representation.

The filenames given above should never be used directly.  The portable way to handle these file is to use the macros _PATH_FSTAB, defined in fstab.h and _PATH_MNTTAB, defined in mntent.h, respectively.  There are also two alternate macro names FSTAB and _PATH_MOUNTED defined but both names are deprecated and kept only for backward compatibility.  The two former names should always be used.

The internal representation for entries of the file is struct fstab, defined in fstab.h.

 -- Data Type: struct fstab
     This structure is used with the getfsent, getfsspec, and getfsfile functions.

     char *fs_spec
          This element describes the device from which the filesystem is mounted. Normally this is the name of a special device, such as a hard disk partition, but it could also be a more or less generic string.  For NFS it would be a hostname and directory name combination.

          Even though the element is not declared const it shouldn't be modified.  The missing const has historic reasons, since this function predates ISO C.  The same is true for the other string elements of this structure.

     char *fs_file
          This describes the mount point on the local system.  I.e., accessing any file in this filesystem has implicitly or explicitly this string as a prefix.

     char *fs_vfstype
          This is the type of the filesystem.  Depending on what the underlying kernel understands it can be any string.

     char *fs_mntops
          This is a string containing options passed to the kernel with the mount call.  Again, this can be almost anything.  There can be more than one option, separated from the others by a comma.  Each option consists of a name and an optional value part, introduced by an = character.

          If the value of this element must be processed it should best happen using the getsubopt function; see Suboptions.

     const char *fs_type
          This name is poorly chosen.  This element points to a string (possibly in the fs_mntops string) which describes the modes with which the filesystem is mounted.  fstab defines five macros to describe the possible values:

          FSTAB_RW
               The filesystems gets mounted with read and write enabled.

          FSTAB_RQ
               The filesystems gets mounted with read and write enabled.  Write access is restricted by quotas.

          FSTAB_RO
               The filesystem gets mounted read-only.

          FSTAB_SW
               This is not a real filesystem, it is a swap device.

          FSTAB_XX
               This entry from the fstab file is totally ignored.

          Testing for equality with these value must happen using strcmp since these are all strings.  Comparing the pointer will probably always fail.

     int fs_freq
          This element describes the dump frequency in days.

     int fs_passno
          This element describes the pass number on parallel dumps.  It is closely related to the dump utility used on Unix systems.

To read the entire content of the of the fstab file the GNU libc contains a set of three functions which are designed in the usual way.

 -- Function: int setfsent (void)
     This function makes sure that the internal read pointer for the fstab file is at the beginning of the file.  This is done by either opening the file or resetting the read pointer.

     Since the file handle is internal to the libc this function is not thread-safe.

     This function returns a non-zero value if the operation was successful and the getfs* functions can be used to read the entries of the file.

 -- Function: void endfsent (void)
     This function makes sure that all resources acquired by a prior call to setfsent (explicitly or implicitly by calling getfsent) are freed.

 -- Function: struct fstab * getfsent (void)
     This function returns the next entry of the fstab file.  If this is the first call to any of the functions handling fstab since program start or the last call of endfsent, the file will be opened.

     The function returns a pointer to an variable of type struct fstab.  This variable is shared by all threads and therefore this function is not thread-safe.  If an error occurred getfsent returns a NULL pointer.

 -- Function: struct fstab * getfsspec (const char *NAME)
     This function returns the next entry of the fstab file which has a string equal to NAME pointed to by the fs_spec element. Since there is normally exactly one entry for each special device it makes no sense to call this function more than once for the same argument.  If this is the first call to any of the functions handling fstab since program start or the last call of endfsent, the file will be opened.

     The function returns a pointer to an variable of type struct fstab.  This variable is shared by all threads and therefore this function is not thread-safe.  If an error occurred getfsent returns a NULL pointer.

 -- Function: struct fstab * getfsfile (const char *NAME)
     This function returns the next entry of the fstab file which has a string equal to NAME pointed to by the fs_file element. Since there is normally exactly one entry for each mount point it makes no sense to call this function more than once for the same argument.  If this is the first call to any of the functions handling fstab since program start or the last call of endfsent, the file will be opened.

     The function returns a pointer to an variable of type struct fstab.  This variable is shared by all threads and therefore this function is not thread-safe.  If an error occurred getfsent returns a NULL pointer.

To access the mtab file there is a different set of functions and also a different structure to describe the results.

 -- Data Type: struct mntent
     This structure is used with the getmntent, getmntent_t, addmntent, and hasmntopt functions.

     char *mnt_fsname
          This element contains a pointer to a string describing the name of the special device from which the filesystem is mounted.  It corresponds to the fs_spec element in struct fstab.

     char *mnt_dir
          This element points to a string describing the mount point of the filesystem.  It corresponds to the fs_file element in struct fstab.

     char *mnt_type
          mnt_type describes the filesystem type and is therefore equivalent to fs_vfstype in struct fstab.  mntent.h defines a few symbolic names for some of the value this string can have. But since the kernel can support an arbitrary filesystems it does not make much sense to give them symbolic names.  If one knows the symbol name one also knows the filesystem name.  Nevertheless here follows the list of the symbol provided in mntent.h.

          MNTTYPE_IGNORE
               This symbol expands to "ignore".  The value is sometime used in fstab files to make sure entries are not used without removing them.

          MNTTYPE_NFS
               Expands to "nfs".  Using this macro sometimes could make sense since it names the default NFS implementation, in case both version 2 and 3 are supported.

          MNTTYPE_SWAP
               This symbol expands to "swap".  It names the special fstab entry which names one of the possibly multiple swap partitions.

     char *mnt_opts
          The element contains a string describing the options used while mounting the filesystem.  As for the equivalent element fs_mntops of struct fstab it is best to use the function getsubopt (see Suboptions) to access the parts of this string.

          The mntent.h file defines a number of macros with string values which correspond to some of the options understood by the kernel.  There might be many more options which are possible so it makes not much sense to rely on these macros but to be consistent here is the list:

          MNTOPT_DEFAULTS
               Expands to "defaults".  This option should be used alone since it indicates all values for the customizable values are chosen to be the default.

          MNTOPT_RO
               Expands to "ro".  See the FSTAB_RO value, it means the filesystem is mounted read-only.

          MNTOPT_RW
               Expand to "rw".  See the FSTAB_RW value, it means the filesystem is mounted with read and write permissions.

          MNTOPT_SUID
               Expands to "suid".  This means that the SUID bit (see How Change Persona) is respected when a program from the filesystem is started.

          MNTOPT_NOSUID
               Expands to "nosuid".  This is the opposite of MNTOPT_SUID, the SUID bit for all files from the filesystem is ignored.

          MNTOPT_NOAUTO
               Expands to "noauto".  At startup time the mount program will ignore this entry if it is started with the -a option to mount all filesystems mentioned in the fstab file.

          As for the FSTAB_* entries introduced above it is important to use strcmp to check for equality.

     mnt_freq
          This elements corresponds to fs_freq and also specifies the frequency in days in which dumps are made.

     mnt_passno
          This element is equivalent to fs_passno with the same meaning which is uninteresting for all programs beside dump.

For accessing the mtab file there is again a set of three functions to access all entries in a row.  Unlike the functions to handle fstab these functions do not access a fixed file and there is even a thread safe variant of the get function.  Beside this the GNU libc contains functions to alter the file and test for specific options.

 -- Function: FILE * setmntent (const char *FILE, const char *MODE)
     The setmntent function prepares the file named FILE which must be in the format of a fstab and mtab file for the upcoming processing through the other functions of the family.  The MODE parameter can be chosen in the way the OPENTYPE parameter for fopen (see Opening Streams) can be chosen.  If the file is opened for writing the file is also allowed to be empty.

     If the file was successfully opened setmntent returns a file descriptor for future use.  Otherwise the return value is NULL and errno is set accordingly.

 -- Function: int endmntent (FILE *STREAM)
     This function takes for the STREAM parameter a file handle which previously was returned from the setmntent call. endmntent closes the stream and frees all resources.

     The return value is 1 unless an error occurred in which case it is 0.

 -- Function: struct mntent * getmntent (FILE *STREAM)
     The getmntent function takes as the parameter a file handle previously returned by successful call to setmntent.  It returns a pointer to a static variable of type struct mntent which is filled with the information from the next entry from the file currently read.

     If there was an error or the end of the file is reached the return value is NULL.

     This function is not thread-safe since all calls to this function return a pointer to the same static variable.  getmntent_r should be used in situations where multiple threads access the file.

 -- Function: struct mntent * getmntent_r (FILE *STREAM, struct mentent *RESULT, char *BUFFER, int BUFSIZE)
     The getmntent_r function is the reentrant variant of getmntent.  It also returns the next entry from the file and returns a pointer.  The actual variable the values are stored in is not static, though.  Instead the function stores the values in the variable pointed to by the RESULT parameter.  Additional information (e.g., the strings pointed to by the elements of the result) are kept in the buffer of size BUFSIZE pointed to by BUFFER.

     The function returns a NULL pointer in error cases.  Errors could be:

       * error while reading the file,

       * end of file reached,

       * BUFSIZE is too small for reading a complete new entry.

 -- Function: int addmntent (FILE *STREAM, const struct mntent *MNT)
     The addmntent function allows to add a new entry to the file previously opened with setmntent.  The new entries are always appended.  I.e., even if the position of the file descriptor is not at the end of the file this function does not overwrite an existing entry following the current position.

     The implication of this is that to remove an entry from a file one has to create a new file while leaving out the entry to be removed and after closing the file remove the old one and rename the new file to the chosen name.

     This function returns 0 in case the operation was successful. Otherwise the return value is 1 and errno is set appropriately.

 -- Function: char * hasmntopt (const struct mntent *MNT, const char *OPT)
     This function can be used to check whether the string pointed to by the mnt_opts element of the variable pointed to by MNT contains the option OPT.  If this is true a pointer to the beginning of the option in the mnt_opts element is returned.  If no such option exists the function returns NULL.

     This function is useful to test whether a specific option is present but when all options have to be processed one is better off with using the getsubopt function to iterate over all options in the string.