When developing a kernel module in Linux, using the C standard library isn't allowed. However, in case I need to use some common functionality like system()
, how do I implement this?

- 63,369
- 21
- 118
- 128

- 7
- 2
-
4Accessing `system` in low-level underlying kernel code sounds paradoxical. – Govind Parmar Mar 04 '21 at 14:28
-
system does a fork and executes a shell to run the command, neither of which makes sense in the kernel. – stark Mar 04 '21 at 14:42
-
1What are you _actually_ trying to achieve. This sounds like an [XY Problem](https://xyproblem.info/) – Jabberwocky Mar 04 '21 at 14:44
-
1If you really want to run user mode software through the kernel, you could look at how core dumping to a process, or usermode driver support work, using `call_usermodehelper` and friends. – Hasturkun Mar 04 '21 at 14:47
-
Actually I am trying to use a kernel module for displaying the information regarding the kernel using the system call but unable to do so. Is there any other way i can show info using c program i,e. kernal module only? – Suyash25 Mar 04 '21 at 15:32
-
1The Linux kernel produces output to user-space through its log `printk`, `dmesg`, virtual files like those in `/proc` and `/sys` and device ioctls (IO Control, the `ioctl` system call). Oh, and `NETLINK` sockets. – Zan Lynx Mar 04 '21 at 15:47
-
Does this answer your question? [How to execute shell command in kernel programming?](https://stackoverflow.com/questions/15676667/how-to-execute-shell-command-in-kernel-programming) – Tsyvarev Mar 04 '21 at 21:31
1 Answers
The answer is: you don't. There are very, very few instances in which you would need to do something similar to system()
, that is to call a user-space application from kernel space. In those special cases, there is the usermode-helper API available, which allows launching (and possibly waiting for) arbitrary user space programs from kernel space.
However, it must be noted that when designing a module you should really avoid being dependent on the output/execution of other user space programs. In the best case scenario, this slows the system down, while in the worst case it can also break kernel/user space isolation and introduce critical vulnerabilities. The existing instances of usage of the call_usermodehelper()
function in modern kernel versions can almost be counted on the tips of your hands. You will basically never need to do such a thing when writing a kernel module. If you think you do, you should re-think about it twice first.

- 63,369
- 21
- 118
- 128
-
Actually I am trying to use a kernel module for displaying the information regarding the kernel using the system call but unable to do so. Is there any other way i can show info using c program i,e. kernal module only? – Suyash25 Mar 04 '21 at 15:33
-
1@Suyash25 that is a very broad and unclear situation. You should detail what you are trying to accomplish as much as possible, then post another question showing your attempt along with the rest of the information. – Marco Bonelli Mar 04 '21 at 15:35
-
Sounds like the kernel should offer the info through /sys and a user space program should read it. – stark Mar 07 '21 at 12:25