问题
I am trying to write a device driver and I need to use system()
function in the driver. To use system()
we need to include <stdlib.h>
, which dosnt seem to work from a driver.
It says no such file or directory found.
Is there an alternative to stdlib.h
for device drivers? Or an alternative to system()
?
回答1:
stdlib.h is a user space header.
User space is that set of memory locations in which user processes (i.e., everything other than the kernel) run. A process is an executing instance of a program. One of the roles of the kernel is to manage individual user processes within this space and to prevent them from interfering with each other.
Kernel space can be accessed by user processes only through the use of system calls. System calls are requests in a Unix-like operating system by an active process for a service performed by the kernel, such as input/output (I/O) or process creation. An active process is a process that is currently progressing in the CPU, as contrasted with a process that is waiting for its next turn in the CPU. I/O is any program, operation or device that transfers data to or from a CPU and to or from a peripheral device (such as disk drives, keyboards, mice and printers).
Please check KERNEL DIRECTORY/include folder for what headers can be used in kernel space.
There is no alternative to system command.
Once possible solution is that you can create a sys /proc entry from the kernel space to set a flag, from the user space you can check the flag and use system().
回答2:
The fact that you're attempting to #include stdlib.h and use system() from a driver shows that you need to become more educated about kernel-mode programming. So, before attempting any of this, you really ought to understand why that header doesn't exist in the kernel environment and why you can't use the libc system() function from there.
However, that being said, there is a kernel-mode analog that can be used when it makes sense to do so:
#include <linux.kmod.h>
static char *envv[] = {
"PATH=/sbin:/bin:/usr/sbin:/usr/bin",
"HOME=/",
NULL
};
static char *argv[] = {
"/sbin/your_program",
"arg1",
...
"argn",
NULL
};
int status = call_usermodehelper(argv[0], argv, envv, UMH_NO_WAIT);
There are other UMH_xxx flags that allow you to wait on the process to complete as system() would do. It would be well to understand what that means in the context of your driver (or any driver) before doing so.
来源:https://stackoverflow.com/questions/23150611/using-stdlib-h-within-a-device-driver