-ENOBUFS example source.

Peter Enderborg (pme@ufh.se)
Tue, 24 Nov 1998 23:08:48 +0100


This is a multi-part message in MIME format.
--------------6823A08CC4E185E9A855B4A5
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is a exaple that shows we get errors on connect with out of
buffers.
Is this realy a well tuned kernel ?!

/* demo program ,shows a process get out of socket buffers Linux 2.1.129
*/
/* by Peter Enderborg */

#include <ctype.h>
#include <stdio.h>
#include <sys/time.h>
#include <sys/file.h>
#include <sys/errno.h>
extern int errno;
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#define NUM (700)

typedef struct foo
{
int sk;
} foobar;
int main ()
{
fd_set write_mask,workmask;
foobar bar[NUM];
char buf[30];
struct timeval timeout;
struct sockaddr_in local;
int loop;
int ret;

FD_ZERO(&write_mask);
FD_ZERO(&workmask);
local.sin_family = AF_INET;
local.sin_port = htons(13);

for (loop = 0 ; loop < NUM; loop++)
{
bar[loop].sk = socket(AF_INET,SOCK_STREAM,0);
if (bar[loop].sk == -1)
{
printf("Socket[%d]:%s[%d]\n",loop,strerror(errno),errno);
exit(1);
}
if (fcntl(bar[loop].sk,F_SETFL,O_NONBLOCK) == -1)
{
printf("Fcntl:%s[%d]\n",strerror(errno),errno);
exit(1);
}
sprintf(buf,"127.%d.%d.%d",random() % 254,random() %254,random() %
254);
local.sin_addr.s_addr = inet_addr(buf);
printf("%s\n",inet_ntoa(local.sin_addr));

if (connect(bar[loop].sk,(struct sockaddr*)&local,sizeof(struct
sockaddr_in)) == -1)
{
if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS))
{
printf("Connect:%s[%d]\n",strerror(errno),errno);
exit(1);

}
else
{
printf("Nonblock connect\n");
}

}
/* bar[loop].timeout = doubletime() + 100; */
FD_SET(bar[loop].sk,&workmask);
}
do
{
fd_set read_mask;
write_mask = workmask;
read_mask = workmask;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
ret = select(1000,&read_mask,&write_mask,(fd_set*)0,&timeout);
for (loop = 0; loop < NUM; loop++)
{
if (FD_ISSET(bar[loop].sk,&write_mask))
{
int val;
int valsize = sizeof(val);

if (getsockopt(bar[loop].sk,SOL_SOCKET,SO_ERROR,&val,&valsize) <
0)
printf("%s",strerror(val));

close(bar[loop].sk);
bar[loop].sk = socket(AF_INET,SOCK_STREAM,0);
if (bar[loop].sk == -1)
{
printf("Socket[%d]:%s[%d]\n",loop,strerror(errno),errno);
exit(1);
}
if (fcntl(bar[loop].sk,F_SETFL,O_NONBLOCK) == -1)
{
printf("Fcntl:%s[%d]\n",strerror(errno),errno);
exit(1);
}

sprintf(buf,"127.%d.%d.%d",random() % 254,random() %254,random()
% 254);
local.sin_addr.s_addr = inet_addr(buf);
printf("%s\n",inet_ntoa(local.sin_addr));

if (connect(bar[loop].sk,(struct sockaddr*)&local,sizeof(struct
sockaddr_in)) == -1)
{
if ((errno != EWOULDBLOCK) && (errno != EINPROGRESS))
{
printf("Connect:%s[%d]\n",strerror(errno),errno);
exit(1);

}
}
FD_SET(bar[loop].sk,&write_mask);
}
}
}

while(1);
return 0;
}

--
foo!

--------------6823A08CC4E185E9A855B4A5 Content-Type: text/x-vcard; charset=us-ascii; name="pme.vcf" Content-Transfer-Encoding: 7bit Content-Description: Card for Peter Enderborg Content-Disposition: attachment; filename="pme.vcf"

begin:vcard n:Enderborg;Peter x-mozilla-html:FALSE org:; adr:;;;;;; version:2.1 email;internet:pme@ufh.se x-mozilla-cpt:;0 fn:Peter Enderborg end:vcard

--------------6823A08CC4E185E9A855B4A5--

- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.rutgers.edu Please read the FAQ at http://www.tux.org/lkml/