Reputation: 87
I'm written a module to try and change the address of the exported symbol 'do_fork' to point to my function first before calling the original do_fork address. So far I can't seem to change the address as it gives me the error 'lvalue required as left operand of assignment.'
I'm not sure how to change the pointer to do_fork() to my function fake_fork();
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/sched.h>
#include <linux/module.h>
int c=0;
long fake_fork(unsigned long a, unsigned long b, unsigned long c, int __user *d, int __user *e)
{
++c;
return do_fork(a, b, c, d, e);
}
EXPORT_SYMBOL(fake_fork);
static int fork_proc_show(struct seq_file *m, void *v)
{
seq_printf(m, "System Call fork called: %d times.\n", c);
return 0;
}
static int fork_proc_open(struct inode *inode, struct file *file)
{
return single_open(file, fork_proc_show, NULL);
}
static const struct file_operations fork_proc_fops = {
.open = fork_proc_open,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static int __init proc_fork_init(void)
{
do_fork = fake_fork; // <-- Not working
printk("init proc forkcounter\n");
proc_create("forkcounter", 0, NULL, &fork_proc_fops);
return 0;
}
static void __exit cleanup_fork_module(void)
{
remove_proc_entry("forkcounter",NULL);
printk("cleanup proc forkcounter\n");
}
module_init(proc_fork_init);
module_exit(cleanup_fork_module);
Upvotes: 0
Views: 840
Reputation: 1668
You cannot change do_fork
, it is a constant at run time. It is the address of do_fork()
function.
You cannot assign anything to a function. It is because name of a function is not a variable. It is a constant pointer.
It is the same as
5 = 2 + 2;
You would get the same error message.
I assume you want your function to be called every time do_fork()
is called. It it going to be more complicated to implement. I would start for example with this link.
Upvotes: 3