0

I am unable to compile the following kernel module. I am trying to implement a litte syscall with SYSCALL_DEFINE0 which does pretty much nothing. Here is the kernel module:

#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/syscalls.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/uaccess.h>
#include <linux/kthread.h>
#include <linux/wait.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/slab.h>

MODULE_LICENSE("GPL");

#ifndef __NR_TESTSYSCALL
#define __NR_TESTSYSCALL 436
#endif

char *addr;

SYSCALL_DEFINE0(testsyscall) {
        printk(KERN_INFO "Test\n");
        return 0;
}

int init_module() {
        addr = kmalloc(32, GFP_KERNEL);
        return 0;
}

void cleanup_module() {
        printk(KERN_INFO "Module removed\n");
}

I already checked the syscall number 436 is not used. However I can't compile this. I get this error:

make -C /lib/modules/5.4.0-72-generic/build M=/root/beginner modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-72-generic'
  CC [M]  /root/beginner/testme.o
  Building modules, stage 2.
  MODPOST 1 modules
ERROR: "enter_syscall_print_funcs" [/root/beginner/testme.ko] undefined!
ERROR: "event_class_syscall_enter" [/root/beginner/testme.ko] undefined!
ERROR: "exit_syscall_print_funcs" [/root/beginner/testme.ko] undefined!
ERROR: "event_class_syscall_exit" [/root/beginner/testme.ko] undefined!
make[2]: *** [scripts/Makefile.modpost:94: __modpost] Error 1
make[1]: *** [Makefile:1667: modules] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-72-generic'
make: *** [Makefile:4: all] Error 2

I used this script to compile:

obj-m += testme.o

all:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
        make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

Can someone help me?

BitFriends
  • 379
  • 5
  • 18
  • 7
    I don't think you can add a system call in a module, since the system call table is static. See https://stackoverflow.com/questions/2394985/linux-kernel-add-system-call-dynamically-through-module I suspect the link error is that the `SYSCALL_DEFINE0` macro calls functions that are not exported to modules, precisely because modules are not supposed to try to define system calls. – Nate Eldredge Apr 30 '21 at 20:22
  • as @NateEldredge said, system call cannot be added as a module , since system call table is static. If you really want to add a custom system call, you have to recompile your kernel with the custom system call. you can refer this link https://brennan.io/2016/11/14/kernel-dev-ep3/ – Vijay May 04 '21 at 05:22

0 Answers0