将 linux 系统调用与较旧的 glibc 一起使用

Using linux syscall with older glibc

我想在运行 2.6.39 内核的嵌入式设备解决方案中使用 timerfd,但交叉编译器使用了太旧的 glibc (2.5)。 Timerfd 从 2.6.25 开始存在,但仅从 glibc 2.8

开始存在

当我尝试使用较新的 glibc 中的头文件时,编译器抱怨找不到 extern timerfd_create (undefined reference to 'timerfd_create'),但我无法在glibc.

我现在的问题是,无论旧的 glibc 版本如何,我如何使用 timerfd?我必须手动调用系统调用吗?如果是,我该怎么做?

I can't find the implementation anywhere in the glibc.

这是找到它的技巧:安装了 libc6-dbg:

gdb -q  /lib/x86_64-linux-gnu/libc.so.6
(gdb) list timerfd_create
61  ../sysdeps/unix/syscall-template.S: No such file or directory.

这告诉你实现只是通过系统调用将参数直接传递给内核。

您的实施可以是:

#include <unistd.h>
#include <syscall.h>

#ifndef __NR_timerfd_create
#define __NR_timerfd_create 283
int timerfd_create(int clockid, int flags)
{
  return syscall(__NR_timerfd_create, clockid, flags);
}
#endif