1 / 9

Utmp.h

Utmp.h. The utmp file records information about who is currently using the system The file is a sequence of utmp entries, as defined in struct utmp in the utmp.h file

lael
Download Presentation

Utmp.h

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Utmp.h • The utmp file records information about who is currently using the system • The file is a sequence of utmp entries, as defined in struct utmp in the utmp.h file • The utmp gives the name of the special file associated with the user’s terminal, user’s login name, and the time of the login time • /usr/include/utmp.h • /var/adm/utmp • $more /usr/include/utmp.h

  2. #define UTMP_FILE “var/adm/utmp” • #define WTMP_FILE “var/adm/wtmp” • Wtmp file records all logins and logouts. • #define ut_name ut_user • Struct utmp{ • char ut_user[32]; // login name • char ut_id[14]; • char ut_line[32]; // device name • char ut_type; // type of entry • pid_t ut_pid; // process id • struct exit_status{ • short e_termination; // process termination status • short e_exit; // process exit status • } ut_exit; • time_t ut_time; // time entry was made • char ut_host[64]; // host name • };

  3. Writing who • We have three system calls we need to connect to a file, extract chunks of data from a file, and close a file. • #include <stdio.h> • #include<utmp.h> • #include<fcntl.h> • #include<unistd.h> • #define SHOWHOST // include remote machine on output

  4. int main() • { • struct utmp current_record; • int utmpfd; • int reclen = sizeof(current_record); • if( (utmpfd = open(UTMP_FILE,O_RDONLY)) == -1){ • perror(UTMP_FILE); • exit(1); • } • while( read(utmpfd, &current_record, reclen) == reclen) • show_info(&current_record); • close(utmpfd); • return 0; • }

  5. Show_info() – displays contents of the utmp struct • show_info( struct utmp *utbufp) • { • printf(“%-8.8s”, utbufp->ut_name); • printf(“ “); • printf(“%-8.8s”, utbufp->ut_line); • printf(“ “); • printf(“%10ld”, utbufp->ut_time); • printf(“ “); • #ifdef SHOWHOST • printf(“(%s)”, utbufp->ut_host); • #endif • printf(“ “); • }

  6. Blank record • Who command lists username of users logged into the system. The above program lists what it finds in the utmp file. • The utmp file seems to have records for all terminals, even unused ones • Modify the program to skip the entries with blank user names • In utmp.h, there are following definition for ut_type • #define EMPTY 0 • #define RUN_LVL 1 • #define BOOT_TIME 2 • #define OLD_TIME 3 • #define NEW_TIME 4 • #define INIT_PROCESS 5 • #define LOGIN_PROCESS 6 • #define USER_PROCESS 7 • #define DEAD_PROCESS 8

  7. Display login time in Human-Readable form • Time_t data type – unix stores times as the number of seconds since midnight, Jan. 1,1970. • Time_t data type is an integer • Ut_time in utmp records represents the login time as the number of seconds since the beginning of the Epoch

  8. Making a time_t readable: ctime • ctime() is function that converts a number of seconds since the start of Unix time into a sensible format. • char *ctime( const time_t *timep); • It converts the calendar time into a string in the form • “Wed Jun 30 21:49:08 1993” • We have time_t value in the utmp records. • We want a string looks like • Jun 30 21:49 • Wed Jun 30 21:49:08 1993 • Pick 12 characters from position 4 • Printf(“%12.12s”, cp+4);

  9. Rewrite show_info • void show_info(struct utmp *utbufp) • { • char * cp; • if( utbufo->ut_type != USER_PROCESS) • return; • printf(“%-8.8s”, utbufp->tu_name); • printf(“ “); • printf(“%-8.8s”, utbufp->ut_line); • cp = ctime(&utbuf->ut_time); • printf(“12.12s”, cp+4); • #ifdef SHOWHOST • if( utbufp->ut_host[0] != ‘\0’) • printf(“ (%s)”, utbufp->ut_host); • #endif • printf(“\n”); • }

More Related