//------------------------------------------------------------------- // tasklist.c // // This module creates an entry in the '/proc' filesystem that // allows the user to display a list of all the current tasks. // // NOTE: Developed and tested with Linux kernel version 2.4.26 // // programmer: ALLAN CRUSE // written on: 09 SEP 2004 //------------------------------------------------------------------- #include // for init_module() #include // for create_proc_read_entry() static char modname[] = "tasklist"; static char report[ 8192 ]; static int my_read_proc( char *page, char **start, off_t offset, int count, int *eof, void *data ) { struct task_struct *tsk; static int len = 0; int tasks, nbytes, i; if ( offset == 0 ) { //-------------------------------------------- // count the number of tasks on the task-list //-------------------------------------------- tsk = &init_task; tasks = 0; do { ++tasks; tsk = tsk->next_task; } while ( tsk != &init_task ); len = sprintf( report, "\nNumber of tasks = %d \n", tasks ); //----------------------------------- // print information about each task //----------------------------------- tsk = &init_task; for (i = 0; i < tasks; i++) { char ch = ( ( i % 3 ) == 0 ) ? '\n' : ' '; char st = tsk->state | '0'; len += sprintf( report+len, "%c", ch ); len += sprintf( report+len, "%5d ", tsk->pid ); len += sprintf( report+len, "%c ", st ); len += sprintf( report+len, "%-15s ", tsk->comm ); tsk = tsk->next_task; } len += sprintf( report+len, "\n\n" ); } //-------------------------------------------------- // must not allow reading beyond the end-of-message //-------------------------------------------------- if ( offset >= len ) { *eof = 1; return 0; } //-------------------------------------------------- // must not deliver more bytes than 'page' can hold //-------------------------------------------------- if ( offset + count < len ) nbytes = count; else nbytes = len - offset; //--------------------------------------------------- // transfer at most 'count' bytes into 'page' buffer //--------------------------------------------------- memcpy( page, report+offset, nbytes ); *start = page; // tell kernel where to find our data return nbytes; // and how many bytes we put there } int init_module( void ) { printk( "<1>\nInstalling \'%s\' module\n", modname ); create_proc_read_entry( modname, 0, NULL, my_read_proc, NULL ); return 0; //SUCCESS; } void cleanup_module( void ) { printk( "<1>Removing \'%s\' module\n", modname ); remove_proc_entry( modname, NULL ); } MODULE_LICENSE("GPL");