0

I am doing my OS assignments. The assignment requires us to write and compile a linux kernel module. I am required to use _do_fork in kernel/fork.c to fork a process. However, when I was trying to install my module into kernel space, the installation failed with the following error message:

ERROR: could not insert module program2.ko: Unknown symbol in module

and the message in kernel space is:

program2: Unknown symbol _do_fork (err 0)

Here comes my attempts to solve the problem: I have exported the symbol '_do_fork' in the source code of fork.c:

//fork.c
long _do_fork(unsigned long clone_flags,
          unsigned long stack_start,
          unsigned long stack_size,
          int __user *parent_tidptr,
          int __user *child_tidptr,
          unsigned long tls)
{
    (body of _do_fork function);
}
EXPORT_SYMBOL_GPL(_do_fork);//I also tried EXPORT_SYMBOL()

and recompile the kernel:

/my_linux_source_path/$me make bzImage

and reboot the system, and found the exported symbol within Module.symvers:

0x00000000 _do_fork vmlinux EXPORT_SYMBOL_GPL

and use extern to declare it in my own source file:

//program2.c
MODULE_LICENSE("GPL");
extern long _do_fork (unsigned long clone_flags,
    unsigned long stack_start,  
    unsigned long stack_size,
    int __user *parent_tidptr,
    int __user *child_tidptr,
    unsigned long tls);

int my_exec(void){
    printk("iamhere");
    return 0;
}
int my_fork(void *argc){

    //set default sigaction for current process
    int i;
    struct k_sigaction *k_action = &current->sighand->action[0];
    for(i=0;i<_NSIG;i++){
        k_action->sa.sa_handler = SIG_DFL;
        k_action->sa.sa_flags = 0;
        k_action->sa.sa_restorer = NULL;
        sigemptyset(&k_action->sa.sa_mask);
        k_action++;
    }
    
    /* fork a process using do_fork */
    int pid;
    
    pid=_do_fork(SIGCHLD,(unsigned long)&my_exec,0,NULL,NULL,0);
    printk("%d",pid);
    /* execute a test program in child process */
    
    /* wait until child process terminates */
    
    return 0;
}
//program init and exit; module init and exit;
...



But the same error still exist and I have no idea about the causes. Could anyone help me? The kernel I am using is Linux-4.10.14, gcc version is (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609

1 Answers1

0

i believe _do_fork is already exposed in shed.h, just put this at the start of your module and it should find it.

#include <linux/shed.h>

And if the assignment is a kernel module, i don't think you should recompile your kernel to complete it.

Capo80
  • 46
  • 3