0

I accessing the current process's structure in order to get the process ID and the command name for the current process within a Linux module in kernel-space, but when I build my module with make and then insert it into the kernel, insmod returns Killed and then I cannot even remove my module from the kernel.

Is there something wrong here?

As you can see, I also tried to use task_struct as global, but I get the same results.

#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
// #include <linux/moduleparam.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("whoami");
MODULE_DESCRIPTION("Simple LKM");

// char *to_say = "Nothing";
// struct task_struct *current_process;
// module_param(to_say, charp, S_IRUGO);

static int __init initialization_function(void)
{
    struct task_struct *current_process;
    printk(KERN_INFO "Module: pid - %d, command name - %s\n", current_process->pid, current_process->comm);
    return 0;
}

static void __exit cleanup_funcion(void)
{
    printk(KERN_INFO "Module: Bye, World.");
}

module_init(initialization_function);
module_exit(cleanup_funcion);
$ make all
$ sudo insmod my_module.ko
Killed
$ tail -f /var/log/syslog | grep "Module:"

$ sudo rmmod my_module
cannot remove...

I fixed the error, I just initallized current_process with current.

struct task_struct *current_process = current;
printk(KERN_INFO "Module: pid - %d, command name - %s\n", current_process->pid, current_process->comm);
Dudu Faruk
  • 43
  • 1
  • 5
  • Try building with warnings enabled. In your init routine, the symbol `current_process` is used but not initialized; did you want `current` instead? After `insmod` also check for info in /var/log/messages – Milag Sep 11 '20 at 18:50
  • @Milag I heard somewhere that using `current` will break my module, but can't find the source. And `/var/log/syslog` is the replacement for `/var/log/messages` in new distros. – Dudu Faruk Sep 11 '20 at 18:57
  • @Milag I fixed it (: – Dudu Faruk Sep 11 '20 at 19:01
  • Why do you declare **additional variable** (`current_process`) when you can **directly** use `current`: `printk(KERN_INFO "Module: pid - %d, command name - %s\n", current->pid, current->comm);`? "I heard somewhere that using current will break my module" - You probably mean your [previous question](https://stackoverflow.com/questions/63798884/cant-access-task-struct-structure-from-another-header-file-in-kernel-code) where you have told to not **define** a variable with name `current`. But there is nothing wrong in **using existed** `current` variable-like macro. – Tsyvarev Sep 11 '20 at 22:15
  • @Tsyvarev Thanks, that works. I didn't know we can do that – Dudu Faruk Sep 12 '20 at 11:15

0 Answers0