Download presentation
Presentation is loading. Please wait.
Published byStephen Higgins Modified over 9 years ago
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 #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, ¤t_record, reclen) == reclen) show_info(¤t_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”); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.