Search code examples
linuxassemblysystem-callsstat

What is the precise definition of the structure passed to the STAT system call?


Where can I find the precise definition of the structure that the STAT family of system calls expects?

Note that I'm referring to the raw system calls one would call in assembly, (System calls number 4, 5, and 6 on x86_64), and not the wrappers that are normally provided by libc.

The man page, stat(2), and what I could manage dig out of both the linux and glibc source code gave confusing and contradicting results (different structure field ordering, Extra fields, padding).

I'm certain that I'm at fault for not looking where I should, but I can't seem to find the information I'm after. Which led me to post this question.


Clarification: What I seek is the exact definition of the stat structure as returned by the system call on any given architecture. I am aware that I can determine this information experimentally. Experimentation is limited to my specific architecture. Furthermore, I expect something as crucial as a data structure used in Linux's ABI to be documented somewhere. I want to know where.

The question here does not have the information requested in this post. Please unmark this post as a duplicate.


Solution

  • struct stat definition strictly depends on your architecture. E.g. for x86_64 you can find it in arch/x86/include/uapi/asm/stat.h.

    In user-space you can find the same structure in /usr/include/asm/stat.h file.

    Here is the definition for x86_64:

    struct stat {
        __kernel_ulong_t    st_dev;
        __kernel_ulong_t    st_ino;
        __kernel_ulong_t    st_nlink;
    
        unsigned int        st_mode;
        unsigned int        st_uid;
        unsigned int        st_gid;
        unsigned int        __pad0;
        __kernel_ulong_t    st_rdev;
        __kernel_long_t     st_size;
        __kernel_long_t     st_blksize;
        __kernel_long_t     st_blocks;  /* Number 512-byte blocks allocated. */
    
        __kernel_ulong_t    st_atime;
        __kernel_ulong_t    st_atime_nsec;
        __kernel_ulong_t    st_mtime;
        __kernel_ulong_t    st_mtime_nsec;
        __kernel_ulong_t    st_ctime;
        __kernel_ulong_t    st_ctime_nsec;
        __kernel_long_t     __unused[3];
    };