//------------------------------------------------------------------- // dumptcb.cpp // // This program shows the contects of its own 'task_struct' in // hexadecimal format (if executed with sufficient privilege). // It presumes our 'tcb.o' module is in the current directory. // // programmer: ALLAN CRUSE // written on: 18 SEP 2004 //------------------------------------------------------------------- #include // for printf(), perror() #include // for open() #include // for system(), exit() #include // for read(), close() char filename[] = "/proc/tcb"; int main( int argc, char **argv ) { // install module that creates the pseudo-file int retval = system( "/sbin/insmod tcb.o" ); if ( retval < 0 ) { perror( "system" ); exit(1); } // open the file int fd = open( filename, O_RDONLY ); if ( fd < 0 ) { perror( filename ); exit(1); } // get file's length (then reposition file's pointer) int filesize = lseek( fd, 0, SEEK_END ); lseek( fd, 0, SEEK_SET ); // rewind the file // allocate storage for the file's contents char *buffer = (char*)malloc( filesize ); if ( buffer == NULL ) { perror( "malloc" ); exit(1); } // read the current contents of the pseudo-file int nbytes = read( fd, buffer, filesize ); printf( "\nread %d bytes from \'%s\' \n", nbytes, filename ); // display the 'task_struct' in hexadecimal format unsigned long *lp = (unsigned long*)buffer; for (int i = 0; i < nbytes/4; i++) { if ( ( i % 8 ) == 0 ) printf( "\n%03X: ", i<<2 ); printf( "%08X ", lp[i] ); } printf( "\n\n" ); // release storage, close file, and remove the module free( buffer ); close( fd ); system( "/sbin/rmmod tcb"); }