【问题标题】:Building system calls in Linux. Kernel > 3.x.x在 Linux 中构建系统调用。内核 > 3.x.x
【发布时间】:2023-03-15 17:35:02
【问题描述】:

我最初尝试在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

在我的程序的当前版本中,这将允许我将其嵌入为系统调用确实有一个 main (即使这样说也很愚蠢,但只是为了做事 更明确)。

在我当前的程序中:

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

我最初的尝试是通过 execlpunistd 库中调用该程序,如下所示:

#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&lt;unistd.h&gt; 更改为 #include&lt;asm/unistd.h&gt; 但我仍然得到错误

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]

真的不知道是什么问题。

【问题讨论】:

  • 内核空间不是用户空间。不要期望像为用户空间开发一样为内核开发。
  • @tangrs:当然可以,但我看不出有什么错误 :-) 是链接文件的问题还是其他问题?
  • 使用#include &lt;linux/unistd.h&gt;
  • 同样的错误仍然存​​在。
  • 你想在图形函数中做什么?让我们知道,以便我们可以为您提供更多帮助。同时发布你的模块代码。

标签: c linux gcc linux-kernel glibc


【解决方案1】:

关于 fork()/exec() 错误:您不能从内核模式调用库代码。想想你要做什么,fork 生成一个新的 user 进程作为当前进程的克隆,然后 exec 用一个新进程替换该进程。 fork 和 exec 本身就是系统调用(尽管您通常调用的函数是调用本身的 libc 包装器),因此尝试通过调用系统调用来创建系统调用有点落后。

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

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

【讨论】:

  • 确实如此。但是,内核应该有一种方法产生子进程,对吧?你知道那个函数调用到底在哪里吗?
  • @pistal - 如果您想生成新线程,请阅读 kthreads。
  • 你可以看看usermode-helper API。
猜你喜欢
  • 2012-01-07
  • 2010-09-20
  • 1970-01-01
  • 2012-11-18
  • 1970-01-01
  • 2013-07-13
  • 2013-04-25
  • 2011-01-07
  • 2018-09-15
相关资源
最近更新 更多