open a named pipe on two channels, write a message on the first channel, fstat of the second channel should return the message length in st_size. Reproducible: Always Steps to Reproduce: -- sample program follows -- /********************************************************************** @(#) pt - fstat test for named pipe **********************************************************************/ #include <errno.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> static char msg[] = "This is a test message"; int main(argc,argv,envp) int argc; char **argv; char **envp; { int ifd; int ofd; int i; int msgsiz; int insiz; char inbuf[33]; char pipefile[] = "PIPE"; struct stat statb; ifd = open( pipefile, O_RDWR, 0 ); if ( ifd < 0 ) goto badopen; ofd = open( pipefile, O_RDWR, 0 ); if ( ofd < 0 ) goto badopen; msgsiz = sizeof( msg ); write( ofd, msg, msgsiz ); if ( (i = fstat( ifd, &statb )) ) goto badstat; if ( statb.st_size != msgsiz ) goto badssiz; i = read( ifd, inbuf, sizeof( inbuf ) ); if ( i != msgsiz ) goto badrsiz; for( i=0; i<msgsiz; i++) if ( msg[i] != inbuf[i] ) goto baddata; close(ofd); close(ifd); printf(" Successfully transfered %d byte message\n",msgsiz); exit(0); badopen: printf("Can not open PIPE file (errno = %d)\n",errno); exit(1); badstat: printf("Can not stat PIPE file (errno = %d)\n",errno); exit(1); badssiz: printf("Expected st_size is %d, actual is %d\n",msgsiz,statb.st_size); exit(1); badrsiz: printf("Expected read size is %d, actual is %d\n",msgsiz,i); exit(1); baddata: printf("Expected data is '%s'\n", msg); printf("Actual data is '%s'\n", inbuf); exit(1); } /********************************************************************** Instructions 1. make a named pipe using mknod PIPE p 2. compile program using cc -o pt -g pt.c 3. execute program using ./pt Expected output is Successfully transfered 23 byte message **********************************************************************/ Actual Results: fstat always returns 0 Expected Results: returned length of data in the pipe tested on x86 32 bit and AMD 64 (uname -sro: Linux 2.4.22-gentoo-r7 GNU/Linux)
This is not a bug. fstat will always return 0 for the size of a pipe.
for reference: http://sources.redhat.com/ml/bug-glibc/2003-09/msg00098.html Found with via google: glibc fstat pipe Some BSD systems and Solaris do provide !0 for st_size.