Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use statx syscall?

Tags:

c

linux

stat

Ubuntu 18.04

I'm trying to use statx syscall introduced in the Linux Kernel 4.11. There is a manual entry:

#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);

So I tried to write an example by myself:

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);

But it simply does not compile. The error is the sizeof(statx) is unknown. And actually it is not defined in sys/stat.h, but in linux/stat.h which is not included by sys/stat.h. But after including linux/stat.h the problem is there is no definition for

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

I expected that since

$ uname -r
4.15.0-39-generic

and 4.15.0-39-generic newer than 4.11 I can use it.

What's wrong?

like image 785
Some Name Avatar asked Jan 27 '23 00:01

Some Name


1 Answers

Currently as the glibc does not provide a wrapper for the statx call, you have to use your kernels definitions. So either copy the statx structure definition from your kernel or just use it from the API the linux kernel provides. The struct statx is currently defined in linux/stat.h.

linux provides a example call to statx available here.

@update library support was added in glibc 2.28

like image 58
KamilCuk Avatar answered Feb 04 '23 15:02

KamilCuk