How to use statx syscall?
Asked Answered
M

1

6

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?

Murmansk answered 17/12, 2018 at 7:59 Comment(5)
From the NOTES section of this manual page: "libc does not (yet) provide a wrapper for the statx() system call; call it using syscall(2)."Janina
@Someprogrammerdude Can I use struct statx definition defined in linux/stat.h? I thought headers from linux/xxx.h were sort of kernel private so I'm not really sure.Murmansk
the linux/xx.h includes is a part of kernel API or ABI - it is the interface to access linux things from userspace things. For common features across systems programs can use glibc or musl or other standard C library and POSIX (and other standards) implementations. Currently glibc library does not implement statx call, so you have to use kernel api.Ashmead
@Ashmead Can you please expand? I thought the only way to access kernel from userspace was syscalls or ioctls on device drivers. What do you mean by the interface to access linux things from userspace things?Murmansk
It's an API. The kernel provides an API (structures, syscall numbers, etc.) defined in kernel/xxx.h. Then glibc is build upon that API and provides it's own API. glibc translates printf("...") in number of syscall(__NR_write, ...) then kernel/xxx.h tells glibc what the number behind __NR_write is. You may not use glibc and call syscall yourself. As glibc does not provide a wrapper for statx, you have to call syscall yourself.Ashmead
A
12

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

Ashmead answered 17/12, 2018 at 8:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.