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?

Some Name
  • 8,555
  • 5
  • 27
  • 77
  • 3
    From [the NOTES section](http://man7.org/linux/man-pages/man2/statx.2.html#NOTES) of [this manual page](http://man7.org/linux/man-pages/man2/statx.2.html): "libc does not (yet) provide a wrapper for the `statx()` system call; call it using [`syscall(2)`](http://man7.org/linux/man-pages/man2/syscall.2.html)." – Some programmer dude Dec 17 '18 at 08:03
  • @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. – Some Name Dec 17 '18 at 08:06
  • 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. – KamilCuk Dec 17 '18 at 08:10
  • @KamilCuk Can you please expand? I thought the only way to access kernel from userspace was `syscall`s or `ioctl`s on device drivers. What do you mean by _the interface to access linux things from userspace things_? – Some Name Dec 17 '18 at 08:12
  • 1
    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. – KamilCuk Dec 17 '18 at 08:17

1 Answers1

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

KamilCuk
  • 120,984
  • 8
  • 59
  • 111