Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 40. Login Accounting > Updating the utmp and wtmp Files for a Login Ses...

40.6. Updating the utmp and wtmp Files for a Login Session

When writing an application that creates a login session (in the manner of, say, login or sshd), we should update the utmp and wtmp files as follows:

  • On login, a record should be written to the utmp file to indicate that this user logged in. The application must check whether a record for this terminal already exists in the utmp file. If a previous record exists, it is overwritten; otherwise, a new record is appended to the file. Often, calling pututxline() (described shortly) is enough to ensure that these steps are correctly performed (see Example 40-3 for an example). The output utmpx record should have at least the ut_type, ut_user, ut_tv, ut_pid, ut_id, and ut_line fields filled in. The ut_type field should be set to USER_PROCESS. The ut_id field should contain the suffix of the name of the device (i.e., the terminal or pseudoterminal) on which the user is logging in, and the ut_line field should contain the name of the login device, with the leading /dev/ string removed. (Examples of the contents of these two fields are shown in the sample runs of the program in Example 40-2.) A record containing exactly the same information is appended to the wtmp file.


    Note:

    The terminal name acts (via the ut_line and ut_id fields) as a unique key for records in the utmp file.


  • On logout, the record previously written to the utmp file should be erased. This is done by creating a record with ut_type set to DEAD_PROCESS, and with the same ut_id and ut_line values as the record written during login, but with the ut_user field zeroed out. This record is written over the earlier record. A copy of the same record is appended to the wtmp file.


    Note:

    If we fail to clean up the utmp record on logout, perhaps because of a program crash, then, on the next reboot, init automatically cleans up the record, setting its ut_type to DEAD_PROCESS and zeroing out various other fields of the record.



  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial