Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System call stat() converted to stat64() without any cpp option

Tags:

c

linux

gcc

gnu

libc

I am running 32-bit SUSE Linux with kernel level 3.0.76.

I can see a stat() call in my code translate to stat64() in the strace output, without me specifying any CPP options like _LARGEFILE64_SOURCE or _FILE_OFFSET_BITS=64.

#include<stdio.h>
#include<sys/stat.h>

int main ( int argc, char * argv[] )
{
    char * path = "nofile";
    struct stat b;
    if (stat(path, &b) != 0) {
    }
}

I compiled this file with gcc with no compiler options/flags.

On running the program, relevant strace output is:

    munmap(0xb770a000, 200704)              = 0
    stat64("nofile", 0xbfb17834)            = -1 ENOENT (No such file or directory)
    exit_group(-1)   

Can anyone please tell me how the stat() was converted to stat64() ?

Thanks in advance!

like image 371
pushkarnk Avatar asked Feb 02 '26 04:02

pushkarnk


1 Answers

The answer seems to be found in the stat man page

Over time, increases in the size of the stat structure have led to three successive versions of stat(): sys_stat() (slot __NR_oldstat), sys_newstat() (slot __NR_stat), and sys_stat64() (new in kernel 2.4; slot __NR_stat64). The glibc stat() wrapper function hides these details from applications, invoking the most recent version of the system call provided by the kernel, and repacking the returned information if required for old binaries. Similar remarks apply for fstat() and lstat().

Basically, glibc always call stat64.

If you add a printf("%zu\n", sizeof b); , the struct stat sizes are likely different depending on whether you use _FILE_OFFSET_BITS=64 or not, and glibc converts the struct stat between the kernel and your code.

like image 186
nos Avatar answered Feb 04 '26 23:02

nos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!