c - 为系统调用编写 glibc api

标签 c linux unix glibc system-calls

<分区>

Possible Duplicate:
Need help with glibc source

我了解如何在 linux 内核中实现我们自己的系统调用。我知道我们可以在 c 程序中使用 syscall()_asm() 调用它。

  1. 但我想了解如何为这个新的系统调用编写 glibc api?

  2. open()read() glibc 函数调用如何映射到内核中的系统调用?

    char      message[ ] = "Hello!\n";
    
    int main( void )
    {
            write( 1, message, 7 );
            exit( 0 );
    }
    

当我将上面的程序转换为程序集时,它给出了

main:
    leal    4(%esp), %ecx
    andl    $-16, %esp
    pushl   -4(%ecx)
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %ecx
    subl    $20, %esp
    movl    $7, 8(%esp)
    movl    $message, 4(%esp)
    movl    $1, (%esp)
    call    write
    movl    $0, (%esp)
    call    exit
    .size   main, .-main
    .ident  "GCC: (Debian 4.3.2-1.1) 4.3.2"
    .section        .note.GNU-stack,"",@progbits

~

3、在“call write”中我认为write是glibc调用这里?。之后会发生什么?它如何将 glibc 调用映射到系统调用?

最佳答案

参见例如this answerthat answer到类似的问题。另请阅读更多关于 syscalls 的信息, linux kernel ,概述linux syscalls , 和 assembly howto

来自 glibc 的 write 函数不是真正的系统调用。它是一个包装器(通过例如 sysenter 机器指令执行 sycall,可能使用 VDSO 并设置 errno)。 您可以使用 strace 了解某些程序完成的系统调用。

例如,MUSL libc有这个 write.c write 的实现。对于 GNU libc,请查看其 write.c .

关于c - 为系统调用编写 glibc api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12563121/

相关文章:

linux - Linux Mint 上的 Qutip

bash - 从一行中提取列的值(变量)

c++需要有关如何使用回调函数的帮助

c - Linux内核中.mod.c文件中版本信息的含义

c - 单精度 float 乘以 2

bash - 查找字符串中所有子字符串的个数

unix - 将 Unix 纪元作为字符串转换为 time.Time on Go

c - fork 进程的正确方法

linux - 使用 ls 以位为单位的文件大小?

c - 使用 fork() 进行拆分过程-程序有什么问题