From: Kevin Layer <layer@--you-know-what-to-remove--.franz.com>
Subject: Tru64 5.1A (and B) socket bug
Date: 23 Jan 2003 15:12:00 -0800 [thread overview]
Message-ID: <mky95bihq7.fsf@--you-know-what-to-remove--.franz.com> (raw)
Anyone else run into this problem? We upgraded to 5.1B to see if it
was fixed, and it is not.
We're writing from socket A to socket B, then shutting down the output
side of socket A. Then we write from socket B to socket A and close
socket B completely.
At this point the operating system realizes that it need only deliver
to the application what's left in A's buffer. So it shuts down the
network connection and sits waiting for reads from socket A. The
reads work up to and including the partial read that delivers the last
bits of information in A's buffer. At this point the operating system
gets ahead of itself. It realizes that it's delivered the last bit of
information in the buffer and it prematurely shuts down the network
socket, and maybe puts some data structs back in the memory pool.
Thus when the next read comes in, the one that should return 0
indicating EOF, what the OS finds is that the file descriptor no
longer points to anything reasonable in the kernel so the 'invalid
argument' error is returned (meaning that the file descriptor is
bogus).
A C program to demonstrate the problem is below.
delta% uname -a
OSF1 delta V5.1 2650 alpha
delta% cc -o bug bug.c
delta% ./bug
Wanted to see eof on read, got -1 instead.. errno = 22
delta%
*******************************************************************************
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#if !defined(m_sun4) || defined(SVR4)
#include <netdb.h>
#endif
/* #include <stdio.h> */
#include <errno.h>
/*
* demonstrate bug in Tru64 V5.x
* (this works correctly in Digital UNIX V4.0E (Rev. 1091)
*
* cc -o socketbug socketbug.c
* ./aocketbug
*
* when the error occurs you see
* Wanted to see eof on read, got -1 instead.. errno = 22
*
* when it works correctly no message is printed
*/
#define BufSize 50000
main()
{
char buff[BufSize];
int port, passive_sock, socketa, socketb;
struct sockaddr_in sck;
int socklen = sizeof(sck);
passive_sock = make_passive_socket(&port);
socketa = connect_to_socket(port);
socketb = accept(passive_sock, (struct sockaddr *) &sck, &socklen);
close(passive_sock);
write_bytes(socketa,buff,BufSize);
shutdown(socketa,SHUT_WR);
read_bytes(socketb,buff, BufSize);
write_bytes(socketb, buff, BufSize);
close(socketb);
read_bytes(socketa,buff, BufSize);
close(socketa);
}
int
make_passive_socket(int *port)
{
struct sockaddr_in sck;
int socklen = sizeof(sck);
int s;
if ((s=socket(PF_INET, SOCK_STREAM, 0)) < 0) errnodie();
sck.sin_family = AF_INET;
sck.sin_port = htons((short)0);
sck.sin_addr.s_addr = htonl((127 << 24) + 1);
if (bind(s,(struct sockaddr *) &sck, sizeof(sck)) < 0)
errnodie();
listen(s,10);
/* get port number */
if (getsockname(s, (struct sockaddr *)&sck, &socklen) < 0) errnodie();
*port = ntohs(sck.sin_port);
return s;
}
int
connect_to_socket(port)
{
/* connect to the given port on localhost */
int s;
struct sockaddr_in sck;
if ((s=socket(PF_INET, SOCK_STREAM, 0)) < 0) errnodie();
memset(&sck, 0, sizeof(sck));
sck.sin_family = AF_INET;
sck.sin_addr.s_addr = htonl((127 << 24) + 1);
sck.sin_port = htons((short)port);
if (connect(s,(struct sockaddr *) &sck, sizeof(sck)) < 0) errnodie();
return s;
}
int
write_bytes(int s, char *buffer, int len)
{
int i = 0;
int towrite;
while (len > 0) {
towrite = (len > 4096 ? 4096 : len);
if (write(s,buffer+i,towrite) != towrite)
fail("didn't write as much as it should have");
len -= towrite;
i += towrite;
}
return 0;
}
int
read_bytes(int s, char *buffer, int len)
{
int i=0;
int toread;
int got;
while (len > 0)
{
toread = (len > 4096? 4096 : len);
if ((got= read(s,buffer+i,toread)) != toread) {
printf("Wanted to read %d, got %d, errno = %d\n",
toread, got, errno);
exit(1);
}
len -= toread;
i += toread;
}
/* now get eof */
if ((got=read(s,buffer,4096)) != 0) {
printf("Wanted to see eof on read, got %d instead.. errno = %d\n",
got, errno);
}
return 0;
}
int
errnodie()
{
printf("Failed with errno %d\n", errno);
exit(1);
return 0;
}
int fail(char *message)
{
printf(message);
exit(1);
return 0;
}
next reply other threads:[~2003-01-23 23:12 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-01-23 23:12 Kevin Layer [this message]
2003-01-24 18:48 ` Tru64 5.1A (and B) socket bug Brian Haley
2003-01-24 22:48 ` Kevin Layer
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox