在Linux中构建系统调用。内核> 3.x.x

时间:2013-09-18 13:00:44

标签: c linux gcc linux-kernel glibc

我最初尝试在此question

的帮助下构建系统调用

我的发布信息:Linux linux-epq2.site 3.7.10-1.16-default #1 SMP Fri May 31 20:21:23 UTC 2013 (97c14ba) x86_64 x86_64 x86_64 GNU/Linux

在我的程序的当前版本中,这将允许我嵌入这个作为系统调用确实有一个主要(愚蠢甚至说,但只是为了做事情 更明确)。

在我目前的计划中:

它接收来自用户的两个输入,并进行一些计算并将输出作为图形和一些数据。

我最初的尝试是通过execlp库中提供的unistd来调用该程序,如下所示:

#include<linux/linkage.h>
#include<linux/kernel.h>
#include<unistd.h>

asmlinkage long graph(const char* granularity, char* application)
{
        pid_t child;
        child = fork();
        if (!child) {
                execlp("./system-call", granularity, application, NULL);
        }
        sleep(0.2);
        return 0;
}

但是当我尝试编译内核(注意:相同的内核版本)和旧的配置文件时(如果需要我还会上传配置文件)。 我收到以下错误:

linux-3.7.10 % make             
make[1]: Nothing to be done for `all'.
make[1]: Nothing to be done for `relocs'.
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CALL    scripts/checksyscalls.sh
  CHK     include/generated/compile.h
make[3]: `arch/x86/realmode/rm/realmode.bin' is up to date.
  CHK     kernel/config_data.h
  CC      test/graph.o
test/graph.c:10:19: fatal error: unistd.h: No such file or directory
compilation terminated.
make[1]: *** [test/graph.o] Error 1
make: *** [test] Error 2
make  4.50s user 1.27s system 75% cpu 7.626 total`

我检查了glibc是否安装了,我看到所有内核头文件都可用。

zypper search glibc      
Loading repository data...
Reading installed packages...

S | Name                     | Summary                                               | Type   
--+--------------------------+-------------------------------------------------------+--------
i | glibc                    | Standard Shared Libraries (from the GNU C Library)    | package
i | glibc-32bit              | Standard Shared Libraries (from the GNU C Library)    | package
i | glibc-devel              | Include Files and Libraries Mandatory for Development | package
i | glibc-devel-32bit        | Include Files and Libraries Mandatory for Development | package
i | glibc-devel-static       | C library static libraries for -static linking        | package
i | glibc-devel-static-32bit | C library static libraries for -static linking        | package
i | glibc-extra              | Extra binaries from GNU C Library                     | package
i | glibc-info               | Info Files for the GNU C Library                      | package
i | glibc-locale             | Locale Data for Localized Programs                    | package
i | glibc-locale-32bit       | Locale Data for Localized Programs                    | package
i | glibc-utils              | Development utilities from GNU C library              | package
i | linux-glibc-devel        | Linux headers for userspace development               | package

我检查了它是否在新的内核转储中可用,并且它不可用。

unistd.h/usr/include/unistd.h复制到我想要编译的新内核转储是否安全?

还有另外一种方法吗?

以下是更新:编辑

我必须将其从#include<unistd.h>更改为#include<asm/unistd.h> 但我仍然得到错误

error: implicit declaration of function ‘fork’ [-Werror=implicit-function-declaration]
error: implicit declaration of function ‘execlp’ [-Werror=implicit-function-declaration]
warning: incompatible implicit declaration of built-in function ‘execlp’ [enabled by default]

真的不确定问题是什么。

1 个答案:

答案 0 :(得分:1)

on fork()/ exec()错误:您无法从内核模式调用库代码。考虑一下你要做什么,fork产生一个新的用户进程作为当前进程的克隆,exec用一个新进程替换该进程。 fork和exec本身就是系统调用(虽然你通常调用的函数是一个围绕调用本身的libc包装器),所以尝试通过调用系统调用来创建一个系统调用是有点落后的。

实现graph():你不能从内核模式运行用户空间程序(比如你的'graph'程序),至少不容易。你需要将图形程序的代码编译到某个地方的内核中(这里的模块会很好,但是这样做比将你的.c文件压缩到源代码树并编译它们更难)。

可以直接从内核模式调用fork和exec,但这是而不是你想要创建graph()系统调用的目的。