首页 > 解决方案 > 如何使用 statx 系统调用?

问题描述

Ubuntu 18.04

我正在尝试使用statxLinux Kernel 中引入的系统调用4.11。有手动输入:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>           /* Definition of AT_* constants */

int statx(int dirfd, const char *pathname, int flags,
             unsigned int mask, struct statx *statxbuf);

所以我试着自己写一个例子:

const char *dir_path = NULL;
const char *file_path = NULL;
//read from command line arguments
int dir_fd = open(dir_path, O_DIRECTORY);

struct statx st; //<--------------------------- compile error
statx(dir_fd, file_path, 0, &statx);

但它根本无法编译。错误是sizeof(statx)未知的。实际上它没有定义在 中sys/stat.h,但linux/stat.h不包含在其中sys/stat.h。但是在包括linux/stat.h问题之后没有定义

int statx(int dirfd, const char *pathname, int flags,
             unsigned int mask, struct statx *statxbuf);

我预计自从

$ uname -r
4.15.0-39-generic

和 4.15.0-39-generic 比 4.11 更新我可以使用它。

怎么了?

标签: clinuxstat

解决方案


目前,由于 glibc 没有为statx调用提供包装器,因此您必须使用内核定义。所以要么从你的内核复制statx结构定义,要么只从 linux 内核提供的 API 中使用它。当前struct statx在 中定义linux/stat.h

linux在此处statx提供了一个可用的示例调用。

在 glibc 2.28 中添加了 @update 库支持


推荐阅读