Hi folks,
i'm writting a piece of code, and i need to write data to temporary files created by mkstemp function.
The problem occurs at the second and later calls.
Here is the strace output of a sample.c :
sample.c
Code:
#include <stdio.h>
char tn[]="/tmp/TEST-XXXXXX";
int main(int argc, char *argv[]){
int i,tmpfd;
for(i=0;i<5;i++) {
tmpfd=mkstemp(tn);
write(tmpfd,"HI\n",3);
close(tmpfd);
}
return 0;
}
The strace output:
Code:
open("/tmp/TEST-uvSflv", O_RDWR|O_CREAT|O_EXCL, 0600) = 3
write(3, "OI\n", 3) = 3
close(3) = 0
write(-1, "OI\n", 3) = -1 EBADF (Bad file descriptor)
close(-1) = -1 EBADF (Bad file descriptor)
write(-1, "OI\n", 3) = -1 EBADF (Bad file descriptor)
close(-1) = -1 EBADF (Bad file descriptor)
write(-1, "OI\n", 3) = -1 EBADF (Bad file descriptor)
close(-1) = -1 EBADF (Bad file descriptor)
write(-1, "OI\n", 3) = -1 EBADF (Bad file descriptor)
close(-1) = -1 EBADF (Bad file descriptor)
exit_group(0) = ?
So the mkstemp fail to open a new fd every new call inside a loop.
Well, according to the manpage the mkstemp function create and open the file, so we expect a new file (fd) every time right?
somebody know why this happen and how fix this ?
thanks a lot!