Linux Kernel Modification – Adding New System Calls

When the need arises to create a powerful and reliable Linux-based system (whether it is to maintain a system processes or the web hosting, etc.), then it becomes necessary to configure the system kernel in such a way that the entire system works more efficiently and reliably.

Moreover, the process of configuring Linux kernel is simplified by the availability of Linux code so anyone can download the source code of the Linux kernel and put to their use.

In this post, we are going to help you understand some general aspects of configuring the Linux kernel and how to to add a new system call. Let’s take a look.

29 Basic Linux Commands For Web Developers

29 Basic Linux Commands For Web Developers

Linux distributions support various GUIs (graphical user interfaces) but the old command-line interface (bash) still proves to be... Read more

Configurable kernel parameters

The Linux system kernel has been developed in such a way that it’s quite easy to customize it to the required operating conditions and hardware environment (pretty much like everything else in UNIX and Linux systems).

Moreover, the flexibility of Linux allows you to configure its kernel so that it would be possible for system administrators to change the parameters at any time.

To accomplish this task, there is a special interface that supports data channels between the kernel and user-level programs. It is through these channels that the directions are sent to set values for kernel parameters.

1. Installing necessary packages

First, you need to make sure that your system has all the packages required to build the kernel. If not, then you need to install the missing ones.

To do this, run the command:

sudo apt install libncurses-dev libncurses dwarves build-essential gcc bc bison flex libssl-dev libelf-dev
2. Obtaining the kernel sources

The best sources are taken from the site of your distribution kit (if they are available) or from the official site of the kernel: kernel.org. So, select a version, go to kernel.org, and download the required tarball sources.

The archive obtained from the official website must be unpacked. To do this, go to your downloads folder and run the unpack command :

cd ~ / Downloads(the folder you downloaded the archive to) / tar xvf linux *

Then you need to go to the folder with the unpacked kernel sources. For version 5.14.14, the command will look like this:

cd linux-5.13.7/
3. Current core configuration

Take the current kernel configuration and use it as a base to build a new one. You can retrieve such a configuration using the command:

zcat /proc/config.gz > .config

The kernel configuration is located in the /boot folder, in a file called config and the kernel version. In order to copy the configuration file to the source folder, run the following command:

cp /boot/config-(version)-generic .config
4. Automatic configuration

The resulting configuration must be updated to the state of the current kernel. Newer versions of the kernel usually add new options that are not yet available in your distribution's kernel configuration.

The localmodulesconfig command can be used to optimize this process as it is the easiest way to build a Linux kernel for your hardware. It verifies the kernel modules that are currently loaded and leaves only those modules enabled, while keeping all others disabled.

To run the localmodulesconfig script, use the following command:

make localmodulesconfig

Creating a system call

So, open a command terminal and, using the nano editor, create a new type C file like this:

nano exp.c 

Now in the editor, write all the following C codes:

#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
int main(int argc, char *argv[]) {
printf("PID of somefile.r = %d\n", getpid());
Char *args[] = {"First", "Second", "Third", Null};
execv("./justfile", args);
return 0;
}

Once this is done, your main function will be created. Here, Printf shows the string data and PID of the process of the file somefile.r.

Then we have an array of args[] character types with some values in it. The exec system call is used to take the filename and the one-line array above as an argument. Create another c file, justfile.r, using the nano editor.

Enter the code into it:

#include<stdio.h> 
#include<unistd.h>
#include<stdlib.h>
int main(int argc, char *argv[]) {
printf("Pid of justfile.r = %d\n", getpid());
return 0;
}

Now let's compile both files using the GNU Compiler Collection:

gcc –o somefile somefile.r
gcc –o justfile justfile.r

When we execute the somefile.r file, it outputs the first print statement from somefile.r file and both print lines from justfile.r as you would be able to see below:

./somefile
PID of somefile.r = 2602
PID of justfile.r = 2602
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail