struct utmp{ char ut_line[LEN]; // Terminal line: ttyh0, ttyd0, ttyp0, etc. char ut_name[LEN]; // login name long ut_time; // seconds since Epoch };
When logging in, the login program fills in this type of structure and writes it to the utmp and wtmp files. On logout, the init process erases the corresponding record in the utmp file (filling each byte with a null byte) and appends a new record to the wtmp file. In the logout record of the utmp file, the ut_name field is cleared to 0. When the system is restarted, and before and after changing the system time and date, a special entry is appended to the wtmp file. The who command reads the utmp file and prints its contents in a readable format; the last command reads the records of the wtmp file.
POSIX.1 defines the uname function, which returns information about the host and operating system.
#include <sys/utsname.h> int uname(struct utsname *name); /* Return value: if successful, return a non-negative value; otherwise, return -1 */ struct utsname{ char sysname[LEN1]; // name of the operating system char nodename[LEN2]; // name of this node char release[LEN3]; // current release of operating system char version[LEN4]; // current version of this release char machine[LEN5]; // name of hardware type /* may have other fields */ };
POSIX.1 only defines the minimum required fields in the utsname structure, and they are all character arrays, and the length of each array is determined by the implementation. The information in this structure can usually be printed with the uname command.
Historically, BSD-derived systems have provided the gethostname function, which simply returns the hostname, which is usually the name of the host on the TCP/IP network.
#include <unistd.h> int gethostname(char *name, int namelen); /* Return value: if successful, return 0; otherwise, return -1 */
The namelen parameter specifies the name buffer length. If enough space is provided, the string returned by name is null-terminated; otherwise, it is not specified whether the string returned by name is null-terminated. It is specified in POSIX.1 that the maximum hostname length is HOST_NAME_MAX. If the host machine is connected to a TCP/IP network, this hostname is usually the full domain name of the host machine.
The hostname command can be used to get and set the hostname (superuser has a similar function sethostname to set the hostname). The hostname is usually set when the system is booted, and is taken from a startup file by /etc/rc or init.