Possible lack of thread safety in dup,fdopen

Mark Pizzolato list-cygwin@subscriptions.pizzolato.net
Mon Mar 7 22:45:00 GMT 2005


I've been using clamav's clamd under cygwin and today noticed an issue.

Clamd is a multi-threaded program.

The following code sequence encountered an error today (ERROR calling fdopen 
on fd 11):

int i, fd;
FILE *f, *tmp;
 tmp = fopen("somefile", "wb+");
 if (NULL == tmp) return;
 fd = fileno(tmp);
{write some stuff to fd }
 lseek(fd, 0, SEEK_SET);
 i = dup(fd);
 if ((f - fdopen(i, "rb")) == NULL) {
     fprintf(stderr, "ERROR calling fdopen on  fd %d", i);

This is happening in one thread while other threads are merrily open and 
closing files and sockets.

I've got some log output suggesting that fd 11 might have also been used by 
another thread at "around" the same time.  It would seem that this could 
only happen if something lost track of the bookeeping for fd's or there was 
a race managing that bookeeping.

I tried to look at the code for fopen(),dup(), and fdopen() myself before 
reporting this, but I can't find the implementations of these system calls 
in the source package for cygwin-1.5.13-1

Can someone point me to where I can look at the source code?

Thanks.

- Mark Pizzolato







--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/



More information about the Cygwin mailing list