Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 57329 - fstat of a named pipe returns st_size=0
Summary: fstat of a named pipe returns st_size=0
Status: RESOLVED INVALID
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Core system (show other bugs)
Hardware: x86 Linux
: High normal (vote)
Assignee: Gentoo Toolchain Maintainers
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-07-16 14:01 UTC by Charles Gilman
Modified: 2004-12-23 07:07 UTC (History)
0 users

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Charles Gilman 2004-07-16 14:01:37 UTC
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)
Comment 1 rob holland (RETIRED) gentoo-dev 2004-12-23 07:00:54 UTC
This is not a bug. fstat will always return 0 for the size of a pipe.
Comment 2 rob holland (RETIRED) gentoo-dev 2004-12-23 07:07:24 UTC
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.