簡體   English   中英

調用open時如何調用sys_open而不是sys_openat

[英]how to call sys_open rather than sys_openat when calling open

我寫了一段代碼來生成系統調用

void open_test(int fd, const char *filepath) {
    if (fd == -1) {
        printf("Open \"%s\" Failed!\n", filepath);
    } else {
        printf("Successfully Open \"%s\"!\n", filepath);
        write(fd, "successfully open!", sizeof("successfully open!") - 1);
        close(fd);
    }
    fflush(stdout);
}

int main(int argc, char const *argv[]) {
    const char fp1[] = "whatever.txt", fp2[] = "./not-exist.txt";
    int fd1 = open(fp1, O_CREAT | O_WRONLY | O_TRUNC, S_IRWXU);
    int fd2 = open(fp2, O_WRONLY | O_TRUNC, S_IRWXU);
    open_test(fd1, fp1);
    open_test(fd2, fp2);
    return 0;
}

和另一個程序(細節省略)來捕捉系統調用,但后來我發現所有的open()結果都是調用 sys_openat 而不是 sys_open。

以下文本是程序的輸出:

Detect system call open, %rax is 257, Addr is 0x00007fefef78aec8, Pathname is /etc/ld.so.cache
Detect system call open, %rax is 257, Addr is 0x00007fefef78aec8, Pathname is /etc/ld.so.cache
Detect system call open, %rax is 257, Addr is 0x00007fefef993dd0, Pathname is /lib/x86_64-linux-gnu/libc.so.6
Detect system call open, %rax is 257, Addr is 0x00007fefef993dd0, Pathname is /lib/x86_64-linux-gnu/libc.so.6
Detect system call open, %rax is 257, Addr is 0x00007fffd44e38e3, Pathname is whatever.txt
Detect system call open, %rax is 257, Addr is 0x00007fffd44e38e3, Pathname is whatever.txt
Detect system call open, %rax is 257, Addr is 0x00007fffd44e38f0, Pathname is ./not-exist.txt
Detect system call open, %rax is 257, Addr is 0x00007fffd44e38f0, Pathname is ./not-exist.txt
Successfully Open "whatever.txt"!
Open "./not-exist.txt" Failed!

這里 rax=257 表示調用了 sys_openat(對於 sys_open,rax=2)

您通過syscall(2)包裝器syscall(SYS_open, ...)syscall(SYS_open, ...)

#define _GNU_SOURCE
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#include <sys/syscall.h>

int main(void){
        char *path = "whatever.txt";
        int fd = syscall(SYS_open, path, O_RDONLY, 0);
        if(fd == -1) err(1, "SYS_open %s", path);
}

但何必呢? SYS_openat現在是規范的系統調用, open(2)只是一個 API,保留SYS_open系統調用條目只是為了向后二進制兼容。

在較新的體系結構上,可能根本沒有實際的SYS_open系統調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM