I'm taking an OS course and the first assigment was to add a few system calls to the kernel (Redhat, kernel version 2.4).
I've added everything that I thought that needs to be added and it still doesn't work.
I'm trying to run tests and I noticed the wrappers (I've added an example for one below and the sys_call code for it) aren't linked the system calls. I know this because I put a printk inside the system calls and they never appear (and the test itself doesn't give the result I'm expecting).
Now from what I understand the development process for adding a system call is:
- Create a wrappers like the example wrapper function in the instructions (lets call it aservice״).
- Write the sys_aservice in a .c file which'll go to one of the many files in the linux-2.4.18-14custom folder. The file it goes to depends on the type of service I want to add ( e.g if it were a file system service I'd put it in fs).
- Add the entry for the system call in the table at
/usr/src/linuxlinux-2.4.18-14custom/arch/i386/kernel/ entry.S
This is what I did at first but it doesn't seem to be enough (because it doesn't seem to work).
I looked it up online (and read: How to add a system call) and I read that I need to add/change the following things inorder for the linking to work.
Add a line in unistd.h for the system call. It'll look something like
define __NR_aservice
Add the aservice to the makefile. So for example if I put the aservice.c in the fs folder I'd add aservice.c to the makefile in fs.
Create a aservice.h file which'll go to linux-2.4.18-14custom/include/linux (where all the h files go apparently?). This .h file will include something called a "stub" which is automatically generated from a macro in unistd.h (which the .h file will have to include) so that a user program can use your system call. The stub's declaration looks as follows: _syscallN(return type, function name, arg1 type, arg1 name ...)" where "N" is the number of parameters.
Prepend to every sys_X function in aservice.c the keyword "asmlinkage" (which requires inclusion of linkage.h). So function declarations look like: asmlinkage int sys_aservice(...){..} Afterward include the .h file in the .c file.
Is all this correct? Because even after doing the following it still seems as if the system call isn't called.
Here is an example wrapper and system call implementation:
int stop_monitor(int pid){
long __res;
__asm__ volatile (
"movl $244, %%eax;" << 244 is the entry in the sys_call table in entry.S
"movl %1, %%ebx;"
"int $0x80;"
"movl %%eax, %0"
: "=m" (__res)
: "m" (pid)
: "%eax", "%ebx"
);
if((unsigned long)(__res) >= (unsigned long)(-125)) {
errno = -(__res);
__res = -1;
}
return (int)(__res);
}
asmlinkage int sys_stop_monitor(int pid){
task_t* task = find_task_by_pid(pid);
if(!task)
return -ESRCH;
if(task->flagMonitor == 0)
return -EPERM;
task->flagMonitor = 0;
return 0;
}
What am I missing?