//------------------------------------------------------------------- // codesize.c (solution for Question VI, Midterm Exam I) // // This module creates a pseudo-file (named '/proc/codesize') // which reports the total amount of userspace memory devoted // to the Virtual Memory Areas that contain 'executable' code // for any task presently belonging to the kernel's tasklist. // // NOTE: Written and tested with Linux kernel version 2.6.10. // // programmer: ALLAN CRUSE // written on: 15 FEB 2005 //------------------------------------------------------------------- #include // for init_module() #include // for create_proc_info_entry() #include // for 'vm_area_struct' static char modname[] = "codesize"; static int my_get_info( char *buf, char **start, off_t off, int count ) { struct task_struct *task; struct vm_area_struct *vma; unsigned int areasize, codesize, totalcode; int len = 0; // show the explanatory title len += sprintf( buf+len, "\nList of userspace code-footprints " ); len += sprintf( buf+len, "(by task)\n\n" ); // outer-loop iterates over all the 'task_struct' objects totalcode = 0; task = &init_task; do { if ( task->mm != NULL ) // can omit if a 'kernel-thread' { // inner-loop iterates over all the 'vm_area_struct' // objects that are associated with a specific task codesize = 0; vma = task->mm->mmap; while ( vma != NULL ) { areasize = vma->vm_end - vma->vm_start; if ( vma->vm_flags & VM_EXEC ) codesize += areasize; vma = vma->vm_next; } totalcode += codesize; len += sprintf( buf+len, " pid=%-5d ", task->pid ); len += sprintf( buf+len, " codesize=%08X ", codesize ); len += sprintf( buf+len, " %-15s \n", task->comm ); } task = next_task( task ); } while ( task != &init_task ); // show the total size of userspace code len += sprintf( buf+len, "\nTotal size of userspace code " ); len += sprintf( buf+len, "is 0x%08X bytes \n\n", totalcode ); return len; } int init_module( void ) { printk( "<1>\nInstalling \'%s\' module\n", modname ); create_proc_info_entry( modname, 0, NULL, my_get_info ); return 0; //SUCCESS } void cleanup_module( void ) { remove_proc_entry( modname, NULL ); printk( "<1>Removing \'%s\' module\n", modname ); } MODULE_LICENSE("GPL");