Topic : Accessing User Information
Author : LUPG
Page : << Previous 5  
Go to page :

systems have the ut_type field in a utmp record.
String fields (user name, host name) are terminated by a null character ('\0'), unless their contents are as large as the field's size. In such a case, we must add the missing null character before treating the value as a string.
The code showed here is buggy. just think what happens if in the middle of scanning the file a new user is logged in. To avoid this, some file locking is necessary (e.g. using the fcntl). However, this should not be done for a long period of time, or else no user will be able to log in (the login program will get blocked when it tries to update the utmp file) - so try this only at home - not on a production system.
The same user may have several records in the utmp file - one for each session the user currently has. If we wanted to only show the user names, we should remove these duplicates.
On some systems, there might be a non-standard API for reading from (and possibly writing to) the the utmp file. It is better to use that API then to use this code, thought it will be even less portable.

Who Has Been Using My System Lately?
Much like the utmp file stores records of currently logged in users, the wtmp file stores historic records of login and logout operations. This can be used to find out when a user was last logged in, or how many hours they have spent on the system in the past week. The wtmp file contains records that are appended by the various login programs. Their format is exactly like the format of utmp record, with two exceptions:

A null user name in a record indicates a logout record. To find the matching login record, scan the wtmp file backwards, until the first record with the same device name field (ut_line);
A record with the value '~' in its terminal name, and a value of "shutdown" or "reboot" in its user name field, indicates a shutdown operation or a boot operation, respectively. In such a case, there might be missing logout entries for users that were logged on during the shutdown, and programs that process the wtmp file should take this into account.
Some other record types might exist - read the manual page about wtmp for info about how your system handles this file.
We will leave it up to you to write a program that parses the wtmp file. Note that this file might grow to a very large size on busy systems, so such a program should be very efficient. An example of such a program is the "last" program, that shows you the list of logins to the system from the most recent - backwards.

References To Other System Databases
There are many other files on Unix systems that serve as databases. For example, the "/etc/shells" file defines the list of valid user shells, and is consulted by the "chsh" (change shell) program that allows a user to set their login shell. There are databases to hold mapping of host names into IP addresses ("/etc/hosts"), and so on. Some of these database files are standard on most Unix systems. Some are specific to classes of Unix systems. Often, a specific section of the on-line manual (section 4 or section 5, depending on the system type) documents the structure of these files. You might want to take a look at the files found in directory "/usr/man/man5" or "/usr/man/cat5" to get a clue of what database files exist on your system.

Page : << Previous 5