On Sun, 07 May 2006 17:48:45 +0200
Per Jessen
Jerry Feldman wrote:
I would think that it is unlikely that open(2) will give you a duplicate file descriptor to one that is already open. I think it is more likely that you have a a bug in your program.
Completely agree, but this strace seems to indicate something's wrong:
28333 and 28315 are both pthreads: ----
28315 write(7, "250 Ok\r\n", 8
28333 waitpid(28360, here 28333 is just cleaning up after a fork()ed process.
28315 <... write resumed> ) = 8 28333 <... waitpid resumed> [{WIFEXITED(s) && WEXITSTATUS(s) == 0}], 0) = 28360 28315 write(2, "[01] state 1; right event RIGHT_"..., 85) = 85 28315 open("/tmp/ibwd.1", O_WRONLY|O_CREAT|O_TRUNC, 0666) = 13
so, 28315 gets fd13 for /tmp/ibwd.1
28313 <... time resumed> NULL) = 1147013625 28362 <... execve resumed> ) = 0 28318 <... write resumed> ) = 2 28333 stat64("28360.stderr",
28315 write(2, "[01] using fd 13 for /tmp/ibwd.1"..., 33 28318 write(14, "Adobe Acrobat 7 Professional Ret"..., 61 28333 <... stat64 resumed> {st_mode=S_IFREG|0600, st_size=0, ...}) = 0 28315 <... write resumed> ) = 33 28318 <... write resumed> ) = 61 28333 unlink("28360.stderr" removing any stderr output from the fork()ed process.
28315 write(7, "354 End data with <CR><LF>.<CR><"..., 37
28318 write(14, "\r\n", 2 28333 <... unlink resumed> ) = 0 28315 <... write resumed> ) = 37 28318 <... write resumed> ) = 2 28333 close(13 and here 28333 is closing fd 13 (which was one end of a pipe() for the fork()ed process) although fd 13 was opened by 28315 ...
Everything is compiled using -fstack-protector-all, which I think should give me a good chance of catching anyone walking right over my stackspace. The issue here is with open(2) (and related calls that return a new file descriptor). The trace does not show that 2 different open(2) calls return 13.
I suggest that you add some diagnostic code whenever an open(2) or
related call returns a file descriptor.
fprintf(stderr, "Thread %ld receives fd %d\n", pthread_self(), fd);
As I mentioned, GDB is not the best tool to trace threads.
If you want to log the data:
Create a log file in the parent thread.
Create a mutex
void logger(int fd)
{
pthread_mutex_lock(&lock_mutex);
fprintf(logstream, "Thread %ld receives fd %d\n",
pthread_self(), fd);
fflush(logstream);
pthread_mutex_unlock(&lock_miutex);
}
--
Jerry Feldman