{"id":58521,"date":"2022-06-27T18:01:10","date_gmt":"2022-06-27T10:01:10","guid":{"rendered":"https:\/\/www.hongkiat.com\/blog\/?p=58521"},"modified":"2022-06-24T18:57:45","modified_gmt":"2022-06-24T10:57:45","slug":"linux-kernal-add-new-system-calls","status":"publish","type":"post","link":"https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/","title":{"rendered":"Linux Kernel Modification &#8211; Adding New System Calls"},"content":{"rendered":"<p>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.<\/p>\n<p>Moreover, the process of configuring <a target=\"_blank\" href=\"https:\/\/www.hongkiat.com\/blog\/most-asked-questions-linux\/\" rel=\"noopener\">Linux kernel<\/a> is simplified by the availability of Linux code so anyone can download the source code of the Linux kernel and put to their use.<\/p>\n<p>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\u2019s take a look.<\/p>\n<div class=\"ref-block ref-block--post\" id=\"ref-post-1\">\n\t\t\t\t\t<a href=\"https:\/\/www.hongkiat.com\/blog\/basic-linux-commands\/\" class=\"ref-block__link\" title=\"Read More: 29 Basic Linux Commands For Web Developers\" rel=\"bookmark\"><span class=\"screen-reader-text\">29 Basic Linux Commands For Web Developers<\/span><\/a>\n<div class=\"ref-block__thumbnail img-thumb img-thumb--jumbo\" data-img='{ \"src\" : \"https:\/\/assets.hongkiat.com\/uploads\/thumbs\/250x160\/basic-linux-commands.jpg\" }'>\n\t\t\t\t\t\t\t<noscript>\n<style>.no-js #ref-block-post-24173 .ref-block__thumbnail { background-image: url(\"https:\/\/assets.hongkiat.com\/uploads\/thumbs\/250x160\/basic-linux-commands.jpg\"); }<\/style>\n<\/noscript>\n\t\t\t\t\t\t<\/div>\n<div class=\"ref-block__summary\">\n<h4 class=\"ref-title\">29 Basic Linux Commands For Web Developers<\/h4>\n<p class=\"ref-description\">\n\t\t\t\t\t\tLinux distributions support various GUIs (graphical user interfaces) but the old command-line interface (bash) still proves to be...\t\t\t\t\t\t<span>Read more<\/span><\/p>\n<\/div>\n<\/div>\n<h2>Configurable kernel parameters<\/h2>\n<p>The Linux system kernel has been developed in such a way that it\u2019s quite easy to customize it to the required operating conditions and hardware environment (pretty much like everything else in UNIX and Linux systems).<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<h3>1. Installing necessary packages <\/h3>\n<p>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.<\/p>\n<p>To do this, run the command:<\/p>\n<pre>sudo apt install libncurses-dev libncurses dwarves build-essential gcc bc bison flex libssl-dev libelf-dev<\/pre>\n<h3>2. Obtaining the kernel sources<\/h3>\n<p>The best sources are taken from the site of your distribution kit (if they are available) or from the official site of the kernel: <a target=\"_blank\" href=\"https:\/\/www.kernel.org\/\" rel=\"noopener\">kernel.org<\/a>. So, select a version, go to kernel.org, and download the required tarball sources.<\/p>\n<p>The archive obtained from the official website must be unpacked. To do this, go to your <strong>downloads folder and run the unpack command<\/strong> :<\/p>\n<pre>cd ~ \/ Downloads(the folder you downloaded the archive to) \/ tar xvf linux *<\/pre>\n<p>Then you need to <strong>go to the folder with the unpacked kernel sources<\/strong>. For version 5.14.14, the command will look like this:<\/p>\n<pre>cd linux-5.13.7\/<\/pre>\n<h3>3. Current core configuration<\/h3>\n<p>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:<\/p>\n<pre>zcat \/proc\/config.gz &gt; .config<\/pre>\n<p>The kernel configuration is <strong>located in the <code>\/boot<\/code> folder, in a file called <em>config<\/em><\/strong> and the kernel version. In order to copy the configuration file to the source folder, run the following command:<\/p>\n<pre>cp \/boot\/config-(version)-generic .config<\/pre>\n<h3>4. Automatic configuration<\/h3>\n<p>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.<\/p>\n<p>The <strong><code>localmodulesconfig<\/code> command can be used to optimize this process<\/strong> 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.<\/p>\n<p>To run the <code>localmodulesconfig<\/code> script, use the following command:<\/p>\n<pre>make localmodulesconfig<\/pre>\n<h2>Creating a system call<\/h2>\n<p>So, open a command terminal and, using the <strong>nano editor, create a new type C file<\/strong> like this:<\/p>\n<pre>nano exp.c <\/pre>\n<p>Now in the editor, write all the following C codes:<\/p>\n<pre>#include&lt;stdio.h&gt;\r\n#include&lt;unistd.h&gt;\r\n#include&lt;stdlib.h&gt;\r\nint main(int argc, char *argv[]) {\r\nprintf(\"PID of somefile.r = %d\\n\", getpid());\r\nChar *args[] = {\"First\", \"Second\", \"Third\", Null};\r\nexecv(\".\/justfile\", args);\r\nreturn 0;\r\n}<\/pre>\n<p>Once this is done, your main function will be created. Here, <code>Printf<\/code> shows the string data and PID of the process of the file <em>somefile.r<\/em>.<\/p>\n<p>Then we have an array of <code>args[]<\/code> 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, <em>justfile.r<\/em>, using the nano editor.<\/p>\n<p>Enter the code into it:<\/p>\n<pre>#include&lt;stdio.h&gt; \r\n#include&lt;unistd.h&gt;\r\n#include&lt;stdlib.h&gt;\r\nint main(int argc, char *argv[]) {\r\nprintf(\"Pid of justfile.r = %d\\n\", getpid());\r\nreturn 0;\r\n}<\/pre>\n<p>Now let's compile both files using the GNU Compiler Collection:<\/p>\n<pre>gcc \u2013o somefile somefile.r\r\ngcc \u2013o justfile justfile.r<\/pre>\n<p>When we execute the <em>somefile.r <\/em>file, it outputs the first print statement from <em>somefile.r<\/em> file and both print lines from <em>justfile.r<\/em> as you would be able to see below:<\/p>\n<pre>.\/somefile\r\nPID of somefile.r = 2602\r\nPID of justfile.r = 2602<\/pre>","protected":false},"excerpt":{"rendered":"<p>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&hellip;<\/p>\n","protected":false},"author":387,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3397],"tags":[888],"topic":[],"class_list":["entry-content","is-maxi"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.8 (Yoast SEO v27.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Linux Kernel Modification - Adding New System Calls - Hongkiat<\/title>\n<meta name=\"description\" content=\"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\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linux Kernel Modification - Adding New System Calls\" \/>\n<meta property=\"og:description\" content=\"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\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/\" \/>\n<meta property=\"og:site_name\" content=\"Hongkiat\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/hongkiatcom\" \/>\n<meta property=\"article:published_time\" content=\"2022-06-27T10:01:10+00:00\" \/>\n<meta name=\"author\" content=\"Nikita\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:site\" content=\"@hongkiat\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nikita\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/linux-kernal-add-new-system-calls\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/linux-kernal-add-new-system-calls\\\/\"},\"author\":{\"name\":\"Nikita\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/84c1af9448851225e4e9f9e466a520fb\"},\"headline\":\"Linux Kernel Modification &#8211; Adding New System Calls\",\"datePublished\":\"2022-06-27T10:01:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/linux-kernal-add-new-system-calls\\\/\"},\"wordCount\":644,\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"keywords\":[\"Linux\"],\"articleSection\":[\"Desktop\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/linux-kernal-add-new-system-calls\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/linux-kernal-add-new-system-calls\\\/\",\"name\":\"Linux Kernel Modification - Adding New System Calls - Hongkiat\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\"},\"datePublished\":\"2022-06-27T10:01:10+00:00\",\"description\":\"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\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/linux-kernal-add-new-system-calls\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/linux-kernal-add-new-system-calls\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/linux-kernal-add-new-system-calls\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux Kernel Modification &#8211; Adding New System Calls\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"name\":\"Hongkiat\",\"description\":\"Tech and Design Tips\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#organization\",\"name\":\"Hongkiat.com\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"contentUrl\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/wp-content\\\/uploads\\\/hkdc-logo-rect-yoast.jpg\",\"width\":1200,\"height\":799,\"caption\":\"Hongkiat.com\"},\"image\":{\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/hongkiatcom\",\"https:\\\/\\\/x.com\\\/hongkiat\",\"https:\\\/\\\/www.pinterest.com\\\/hongkiat\\\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/#\\\/schema\\\/person\\\/84c1af9448851225e4e9f9e466a520fb\",\"name\":\"Nikita\",\"description\":\"Nikita is an enthusiastic writer who is fond of IT programming and technology. In his free time from writing, he reads books and, most likely, drinks coffee at this time. But he also does not forget about performing physical exercises, which give him a massive boost of energy and pleasure.\",\"sameAs\":[\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/\"],\"url\":\"https:\\\/\\\/www.hongkiat.com\\\/blog\\\/author\\\/nikita\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Linux Kernel Modification - Adding New System Calls - Hongkiat","description":"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","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/","og_locale":"en_US","og_type":"article","og_title":"Linux Kernel Modification - Adding New System Calls","og_description":"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","og_url":"https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/","og_site_name":"Hongkiat","article_publisher":"https:\/\/www.facebook.com\/hongkiatcom","article_published_time":"2022-06-27T10:01:10+00:00","author":"Nikita","twitter_card":"summary_large_image","twitter_creator":"@hongkiat","twitter_site":"@hongkiat","twitter_misc":{"Written by":"Nikita","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/#article","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/"},"author":{"name":"Nikita","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/84c1af9448851225e4e9f9e466a520fb"},"headline":"Linux Kernel Modification &#8211; Adding New System Calls","datePublished":"2022-06-27T10:01:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/"},"wordCount":644,"publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"keywords":["Linux"],"articleSection":["Desktop"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/","url":"https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/","name":"Linux Kernel Modification - Adding New System Calls - Hongkiat","isPartOf":{"@id":"https:\/\/www.hongkiat.com\/blog\/#website"},"datePublished":"2022-06-27T10:01:10+00:00","description":"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","breadcrumb":{"@id":"https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.hongkiat.com\/blog\/linux-kernal-add-new-system-calls\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.hongkiat.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Linux Kernel Modification &#8211; Adding New System Calls"}]},{"@type":"WebSite","@id":"https:\/\/www.hongkiat.com\/blog\/#website","url":"https:\/\/www.hongkiat.com\/blog\/","name":"Hongkiat","description":"Tech and Design Tips","publisher":{"@id":"https:\/\/www.hongkiat.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.hongkiat.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.hongkiat.com\/blog\/#organization","name":"Hongkiat.com","url":"https:\/\/www.hongkiat.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","contentUrl":"https:\/\/www.hongkiat.com\/blog\/wp-content\/uploads\/hkdc-logo-rect-yoast.jpg","width":1200,"height":799,"caption":"Hongkiat.com"},"image":{"@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/hongkiatcom","https:\/\/x.com\/hongkiat","https:\/\/www.pinterest.com\/hongkiat\/"]},{"@type":"Person","@id":"https:\/\/www.hongkiat.com\/blog\/#\/schema\/person\/84c1af9448851225e4e9f9e466a520fb","name":"Nikita","description":"Nikita is an enthusiastic writer who is fond of IT programming and technology. In his free time from writing, he reads books and, most likely, drinks coffee at this time. But he also does not forget about performing physical exercises, which give him a massive boost of energy and pleasure.","sameAs":["https:\/\/www.hongkiat.com\/blog\/"],"url":"https:\/\/www.hongkiat.com\/blog\/author\/nikita\/"}]}},"jetpack_featured_media_url":"https:\/\/","jetpack_shortlink":"https:\/\/wp.me\/p4uxU-fdT","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/58521","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/users\/387"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/comments?post=58521"}],"version-history":[{"count":1,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/58521\/revisions"}],"predecessor-version":[{"id":58522,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/posts\/58521\/revisions\/58522"}],"wp:attachment":[{"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/media?parent=58521"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/categories?post=58521"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/tags?post=58521"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.hongkiat.com\/blog\/wp-json\/wp\/v2\/topic?post=58521"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}