Richard hesiduPosted on
Paste a post I wrote. It is purely for discussion, so that no one may say that I infringe on intellectual property rights.
# Include <Linux/init. h>
# Include <Linux/module. h>
# Include <Linux/proc_fs.h> // proc_fs
# Include <Linux/seq_file.h> // seq_file
# Include <Linux/fs. h> // struct file, struct inode
# Include <Linux/sched. h> // next_task ()
Module_author ("xunil @ bmy ");
Module_license ("GPL ");
Module_description ("A Test Module utilise the seq_file mechanic ");
Static void * ps_seq_start (struct seq_file * s, loff_t * POS ){
Struct task_struct * task;
Seq_printf (S, "% s \ t % s \ n", "PID", "ppid", "uid ", "gid", "Comm ");
If (* POS> 0)
Return NULL;
Else {
Task = next_task (current );
Return task;
}
}
Static void * ps_seq_next (struct seq_file * s, void * V, loff_t * POS ){
Struct task_struct * task = (struct task_struct *) V;
+ + * Pos;
If (Task-> pid = Current-> PID ){
Return NULL;
} Else {
Task = next_task (task );
Return task;
}
}
Static void ps_seq_stop (struct seq_file * s, void * V ){}
Static int ps_seq_show (struct seq_file * s, void * V ){
Rwlock_t lock = rw_lock_unlocked;
Struct task_struct * task = (struct task_struct *) V;
Read_lock (& lock );
Seq_printf (S, "% d \ t % s \ n", task-> PID, task-> parent-> PID, task-> uid, task-> GID, task-> comm );
Read_unlock (& lock );
Return 0;
}
Static struct seq_operations ps_seq_ops = {
. Start = ps_seq_start,
. Next = ps_seq_next,
. Stop = ps_seq_stop,
. Show = ps_seq_show
};
Static int ps_open (struct inode * inode, struct file * file ){
Return seq_open (file, & ps_seq_ops );
}
Static struct file_operations ps_file_ops = {
. Owner = this_module,
. Open = ps_open,
. Read = seq_read,
. Llseek = seq_lseek,
. Release = seq_release
};
Static int _ init ps_init (void ){
Struct proc_dir_entry * entry;
Entry = create_proc_entry ("myps", 0, null );
If (entry)
Entry-> proc_fops = & ps_file_ops;
Return 0;
}
Static void _ exit ps_exit (void ){
Remove_proc_entry ("myps", null );
}
Module_init (ps_init );
Module_exit (ps_exit );