Re: select() call corrupts stack

Byron Stanoszek (gandalf@winds.org)
Thu, 28 Feb 2002 11:33:05 -0500 (EST)


On Wed, 27 Feb 2002, Artiom Morozov wrote:

> Hello,
>
> Here's a sample program. Try running it and open about 2k of
> connections to port 5222 (you'll need ulimit -n 10000 or like that). It
> will segfault. Simple asm like this

> ps: it's okay for 1k of connections or so

printf("%d\n", sizeof(fd_set));

You'll see that it's 128, which is equivalent to 1024 bits, or 1024 possible
open connections that can be held in that variable. I would consider using a
dynamically-resizable memory space like in the code below to do what you want:

struct timeval timeout;
static fd_set *input_set, *output_set;
static int fdset_size;

extern int top_desc;

/* dynamically grow file descriptor sets when needed (for >1024 FDs) */
if(!input_set || (fdset_size-8)*8 <= top_desc) {
free(input_set);
free(output_set);
fdset_size=(top_desc/64+2)*8;
input_set=malloc(fdset_size);
output_set=malloc(fdset_size);
}

/* clear input/output file descriptor sets */
memset(input_set, 0, fdset_size);
memset(output_set, 0, fdset_size);

[... insert code to set input_set/output_set bits using FD_SET ...]

select(top_desc+1, input_set, output_set, NULL, &timeout);

-Byron

-- 
Byron Stanoszek                         Ph: (330) 644-3059
Systems Programmer                      Fax: (330) 644-8110
Commercial Timesharing Inc.             Email: byron@comtime.com

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