LINUX.ORG.RU

Не могу поймать fstat


0

0

Помогите, люди добрые: не могу поимать fstat. Ни как не могу найти ошибку:
[me]$ cat fakestat.c
/* fakestat.c*/
#include <sys/stat.h>
#define BUFFERLEN 256
int fstat (int fd, struct stat *statbuf);
int fstat (int fd, struct stat *statbuf){
return 10;
}
[me]$ cat stattest.c
#include <fcntl.h>
/*stattest.c*/
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <dlfcn.h>
int main(int argc, char *argv[])
{
char fileName[120];
struct stat statbuf, realstatbuf;
char timeBuf[50];
int a, statRet;

strcpy(fileName, "/etc/passwd");
a=open (fileName,O_RDONLY );
if (statRet=fstat(a, &statbuf)){
printf("could not fstat %s: %s\n", fileName, strerror(errno));
}
printf ("stat returned %d\n",statRet);
printf("passwd was accessed on %s\n",ctime(&statbuf.st_atime));
return 0;
}
[me]$ cat Makefile
#Makefile
CC = gcc
all: lib testprog test
lib: fakestat.c
${CC} -nostartfiles -shared -fPIC -Wl,-soname,libfakestat.so.1 -o libfakestat.so.1 fakestat.c -ldl -lm

testprog: stattest.c
${CC} -o stattest stattest.c

test: lib testprog
@echo "Running the test program with no faked stat specified"
@LD_PRELOAD=./libfakestat.so.1 ./stattest
@echo
@echo "Running the test program with absolute date 2003-01-01 10:00:05 specified"
@LD_PRELOAD=./libfakestat.so.1 FAKETIME="2003-01-01 10:00:05" ./stattest[me]$ make
gcc -nostartfiles -shared -fPIC -Wl,-soname,libfakestat.so.1 -o libfakestat.so.1 fakestat.c -ldl -lm
gcc -o stattest stattest.c
Running the test program with no faked stat specified
stat returned 0
passwd was accessed on Wed Feb 25 01:34:21 2004


Running the test program with absolute date 2003-01-01 10:00:05 specified
stat returned 0
passwd was accessed on Wed Feb 25 01:34:21 2004

★★

И не поймаешь! Дело в том, что fstat всегда линкуется статически (при использовании gcc она вообще объявлена как inline). Это такой специфический хак, чтобы можно было менять layout структуры stat, не теряя совместимости (cм. sys/stat.h для подробностей)

aa5779
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.