Hi I have a class assignment that requires me to intercept an open call then read the file and edit the output without editing the file itself this is all done in a loadable module in the kernel space. When I say editing I mean like changing the word she to _he replacing the s with an underscore it changes every instance of the word she to _he well that's the desired result. I have looked online for days trying to figure this out I thought I found a suitable example but it kept giving me an error. As soon as I enter the module into the kernel it instantly says killed then it says I cannot remove it because it is in use when it isn't this forces me to restart my virtual machine. Below is the code. Any help would be appreciated thank you.
#include <linux/module.h> // Needed by all modules
#include <linux/kernel.h> // Needed for KERN_INFO
#include <linux/fs.h> // Needed by filp
#include <asm/uaccess.h> // Needed by segment descriptors
int init_module(void)
{
// Create variables
struct file *f;
char buf[128];
mm_segment_t fs;
int i;
// Init the buffer with 0
for(i=0;i<128;i++)
buf[i] = 0;
// To see in /var/log/messages that the module is operating
printk(KERN_INFO "My module is loaded\n");
// I am using Fedora and for the test I have chosen following file
// Obviously it is much smaller than the 128 bytes, but hell with it =)
f = filp_open("/etc/fedora-release", O_RDONLY, 0);
if(f == NULL)
printk(KERN_ALERT "filp_open error!!.\n");
else{
// Get current segment descriptor
fs = get_fs();
// Set segment descriptor associated to kernel space
set_fs(get_ds());
// Read the file
f->f_op->read(f, buf, 128, &f->f_pos);
// Restore segment descriptor
set_fs(fs);
// See what we read from file
printk(KERN_INFO "buf:%s\n",buf);
}
filp_close(f,NULL);
return 0;
}
void cleanup_module(void)
{
printk(KERN_INFO "My module is unloaded\n");
}
module_init(init_module);
module_exit(cleanup_module);