0

What information do I need to add to either the Makefile or the source file in order for the current symbol to not cause compilation issues.

PWD = /home/user/dev/kernel-sandbox ARCH = x86_64 GNU/Linux

Output of make:

user@dev:~/dev/kernel-sandbox$ make
sudo make -C /lib/modules/4.10.0-35-generic/build M=/home/user/dev/kernel-sandbox modules
make: Entering directory '/usr/src/linux-headers-4.10.0-35-generic'
  CC [M]  /home/user/dev/kernel-sandbox/lkm.o
In file included from ./arch/x86/include/asm/preempt.h:6:0,
                 from ./include/linux/preempt.h:59,
                 from ./include/linux/spinlock.h:50,
                 from ./include/linux/seqlock.h:35,
                 from ./include/linux/time.h:5,
                 from ./include/linux/stat.h:18,
                 from ./include/linux/module.h:10,
                 from /home/user/dev/kernel-sandbox/lkm.c:3:
/home/user/dev/kernel-sandbox/lkm.c: In function ‘hello_init’:
./include/linux/thread_info.h:21:54: error: ‘current’ undeclared (first use in this function)
 #define current_thread_info() ((struct thread_info *)current)
                                                      ^
./include/asm-generic/current.h:6:24: note: in expansion of macro ‘current_thread_info’
 #define get_current() (current_thread_info()->task)
                        ^
./include/asm-generic/current.h:7:17: note: in expansion of macro ‘get_current’
 #define current get_current()
                 ^
/home/user/dev/kernel-sandbox/lkm.c:13:17: note: in expansion of macro ‘current’
                 current->comm, current->pid);
                 ^
./include/linux/thread_info.h:21:54: note: each undeclared identifier is reported only once for each function it appears in
 #define current_thread_info() ((struct thread_info *)current)
                                                      ^
./include/asm-generic/current.h:6:24: note: in expansion of macro ‘current_thread_info’
 #define get_current() (current_thread_info()->task)
                        ^
./include/asm-generic/current.h:7:17: note: in expansion of macro ‘get_current’
 #define current get_current()
                 ^
/home/user/dev/kernel-sandbox/lkm.c:13:17: note: in expansion of macro ‘current’
                 current->comm, current->pid);
                 ^
scripts/Makefile.build:301: recipe for target '/home/user/dev/kernel-sandbox/lkm.o' failed
make[1]: *** [/home/user/dev/kernel-sandbox/lkm.o] Error 1
Makefile:1524: recipe for target '_module_/home/user/dev/kernel-sandbox' failed
make: *** [_module_/home/user/dev/kernel-sandbox] Error 2
make: Leaving directory '/usr/src/linux-headers-4.10.0-35-generic'
Makefile:3: recipe for target 'all' failed
make: *** [all] Error 2

[/home/user/dev/kernel-sandbox/lkm.c]

#include <linux/init.h>
#include <linux/module.h>
#include <asm-generic/current.h>
MODULE_LICENSE("Dual BSD/GPL");

static int hello_init(void)
{
    printk(KERN_ALERT "Hello, world\n");

    printk(KERN_INFO "The process is \"%s\" (pid %i)\n",current->comm, current->pid);
    return 0;
}

static void hello_exit(void)
{
    printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

[/home/user/dev/kernel-sandbox/Makefile]

obj-m += lkm.o
all:
    sudo make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
    sudo make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

I also have another current.h header file which I tried to include but was not sure how to specify the #include as I am not sure exactly how kbuild is looking for paths.. /usr/src/linux-headers-4.10.0-35-generic/arch/ia64/include/asm/current.h

Extra Info: If I remove #include <asm-generic/current.h> and printk(KERN_INFO "The process is \"%s\" (pid %i)\n", current->comm, current->pid); it builds fine.

  • **Never** include `asm` and `asm-generic` headers unless you know what you are doing. Definition of most of the structures can be obtained via including asm-independent headers. In case of `task_struct`, this is `linux/sched.h` header. – Tsyvarev Oct 29 '17 at 13:12
  • adding, #include fixed it –  Oct 29 '17 at 18:23

0 Answers0