我最近在做一些内核编码,我发现 2 unistd.h
文件。
第一个的位置是/usr/include/asm/unistd.h
.第二个来自内核源码:linux/include/uapi/asm-generic/unistd.h
.
源代码的版本和我内核的一样,但是两个头文件
彼此不同。/usr/include/asm/unistd.h
(来自我的电脑):
#define __NR_read 0
#define __NR_write 1
#define __NR_open 2
#define __NR_close 3
#define __NR_stat 4
#define __NR_fstat 5
#define __NR_lstat 6
#define __NR_poll 7
#define __NR_lseek 8
#define __NR_mmap 9
linux/include/uapi/asm-generic/unistd.h
(来自来源):#define __NR_io_setup 0
__SC_COMP(__NR_io_setup, sys_io_setup, compat_sys_io_setup)
#define __NR_io_destroy 1
__SYSCALL(__NR_io_destroy, sys_io_destroy)
#define __NR_io_submit 2
__SC_COMP(__NR_io_submit, sys_io_submit, compat_sys_io_submit)
#define __NR_io_cancel 3
__SYSCALL(__NR_io_cancel, sys_io_cancel)
#define __NR_io_getevents 4
__SC_COMP(__NR_io_getevents, sys_io_getevents, compat_sys_io_getevents)
/* fs/xattr.c */
#define __NR_setxattr 5
__SYSCALL(__NR_setxattr, sys_setxattr)
#define __NR_lsetxattr 6
__SYSCALL(__NR_lsetxattr, sys_lsetxattr)
#define __NR_fsetxattr 7
__SYSCALL(__NR_fsetxattr, sys_fsetxattr)
#define __NR_getxattr 8
__SYSCALL(__NR_getxattr, sys_getxattr)
#define __NR_lgetxattr 9
有什么不同?我应该使用哪一个来索引
sys_call_table
?
最佳答案
asm-generic
一个是模板版本,如果您正在为内核开发新架构,可以使用它。相信你会发现unistd.h
其实有很多版本在内核源代码中,因为系统调用的顺序(实际上是系统调用的存在)因架构而异。从内核源代码层次结构的根目录尝试此操作:
find . -name 'unistd*.h'
特别是对于 x86,
uapi
版本是在构建内核时生成的。见 Makefile
和各种 *.tbl
arch/x86/entry/syscalls/
中的文件目录。这最终会生成文件:arch/x86/include/generated/uapi/asm/unistd_64.h
arch/x86/include/generated/uapi/asm/unistd_32.h
arch/x86/include/generated/uapi/asm/unistd_x32.h
(所有这些都是
#include
d 来自 stub unistd.h
文件)。最终,Linux“发行版”的创建是非常特定于体系结构的,因此由发行版创建者复制正确的
unistd.h
文件放入 /usr/include
中的某个适当位置等级制度。 (当然,您的 libc
也需要针对正确的版本进行编译,以便普通的 libc
系统调用正常工作。)综上所述,
/usr/include/asm
中的版本最好匹配您正在运行的内核,否则将无法从系统上的用户进程正确生成临时系统调用,但您不应在内核源代码层次结构中使用该系统调用,因为绝对是内核源代码层次结构从不依赖于用户空间 header 。在内核源代码中,索引该表的机制依赖于体系结构,因为表的布局和排序本身是依赖于体系结构的,并且只有特定于体系结构的代码(系统调用入口代码)通常访问该表,因此只有代码“需要知道”正确的索引。现在,如果您要创建一个新的系统调用,则需要在所有
unistd.h
中定义其编号。您希望它出现在的所有架构的文件。
关于c - 哪个系统调用号的头文件是正确的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53122539/