Add Book to My BookshelfPurchase This Book Online

Chapter 8 - Users and Groups

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

The Lastlog File
On Solaris 2.x systems, the /var/adm/lastlog file is used to record the last login time of each user. This file is maintained by the login command. (Note that users who log in by using rsh to start a window system terminal emulator such as xterm do not pass through the login command, and hence do not appear in this file.) The file is indexed by user ID number, and contains one structure for each user.
On IRIX 5.x systems, there is an individual file for each user called /var/adm/lastlog/username which contains a single structure for that user.
This functionality is not provided in HP-UX 10.x.
The struct lastlog structure is defined in the include file lastlog.h:
    struct lastlog {
        time_t  ll_time;
        char    ll_line[8];
        char    ll_host[16];
    };
The fields are:
ll_time
The time the user last logged in.
ll_line
The name of the terminal device the user last logged in on.
ll_host
The name of the host the user logged in from, if she logged in via the network. This field is 257 bytes long in IRIX 5.x.
Example 8-4 shows a program that prints the last login time for each user named on its command line. This version is for Solaris 2.x.
Example 8-4:  lastlog for Solaris
#include <sys/types.h>
#include <sys/time.h>
#include <lastlog.h>
#include <stdio.h>
#include <pwd.h>
int
main(int argc, char **argv)
{
    FILE *fp;
    struct lastlog ll;
    struct passwd *pwd;
    /*
     * Open the lastlog file.
     */
    if ((fp = fopen("/var/adm/lastlog", "r")) == NULL) {
        perror("/var/adm/lastlog");
        exit(1);
    }
    /*
     * For each user named on the command line...
     */
    while (--argc) {
        /*
         * Look up the user's user-id number.
         */
        if ((pwd = getpwnam(*++argv)) == NULL) {
            fprintf(stderr, "unknown user: %s\n", *argv);
            continue;
        }
        /*
         * Read the right structure.
         */
        fseek(fp, pwd->pw_uid * sizeof(struct lastlog), 0);
        fread(&ll, sizeof(struct lastlog), 1, fp);
        /*
         * Print it out.
         */
        printf("%-8.8s %-8.8s %-16.16s %s", *argv, ll.ll_line, ll.ll_host,
               ctime(&ll.ll_time));
    }
    fclose(fp);
    exit(0);
}
    % lastlog davy root cathy
    davy     pts/3                     Sun May 29 15:28:18 1994
    root     console                   Sun May 22 17:11:38 1994
    cathy    pts/2    big.school.edu   Thu May  5 12:16:32 1994
Example 8-5 shows the same program as it would be written on an IRIX 5.x system.
Example 8-5:  lastlog for IRIX
#include <sys/types.h>
#include <sys/time.h>
#include <lastlog.h>
#include <stdio.h>
int
main(int argc, char **argv)
{
    FILE *fp;
    struct lastlog ll;
    char lastlogfile[1024];
    /*
     * For each user named on the command line...
     */
    while (--argc) {
        /*
         * Open the lastlog file.
         */
        sprintf(lastlogfile, "/var/adm/lastlog/%s", *++argv);
        if ((fp = fopen(lastlogfile, "r")) == NULL) {
            perror(lastlogfile);
            continue;
        }
        /*
         * Read the structure.
         */
        fread(&ll, sizeof(struct lastlog), 1, fp);
        /*
         * Print it out.
         */
        printf("%-8.8s %-8.8s %-16.16s %s", *argv, ll.ll_line, ll.ll_host,
               ctime(&ll.ll_time));
        fclose(fp);
    }
    exit(0);
}
    % lastlog davy root cathy
    davy     pts/3                     Sun May 29 15:28:18 1994
    root     console                   Sun May 22 17:11:38 1994
    cathy    pts/2    big.school.edu   Thu May  5 12:16:32 1994

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