1 // RUN: %clangxx_tsan -O1 %s -o %t && %run %t 2>&1 | FileCheck %s
8 // dup2(oldfd, newfd) races with read(newfd).
9 // This is not reported as race because:
10 // 1. Some software dups a closed pipe in place of a socket before closing
11 // the socket (to prevent races actually).
12 // 2. Some daemons dup /dev/null in place of stdin/stdout.
16 void *Thread(void *x
) {
18 int n
= read(fd
, &buf
, 1);
20 // This read can "legitimately" fail regardless of the fact that glibc claims
21 // that "there is no instant in the middle of calling dup2 at which new is
22 // closed and not yet a duplicate of old". Strace of the failing runs
25 // [pid 122196] open("/dev/urandom", O_RDONLY) = 3
26 // [pid 122196] open("/dev/urandom", O_RDONLY) = 4
27 // Process 122382 attached
28 // [pid 122382] read(3, <unfinished ...>
29 // [pid 122196] dup2(4, 3 <unfinished ...>
30 // [pid 122382] <... read resumed> 0x7fcd139960b7, 1) = -1 EBADF (Bad file descriptor)
31 // [pid 122196] <... dup2 resumed> ) = 3
32 // read failed: n=-1 errno=9
34 // The failing read does not interfere with what this test tests,
35 // so we just ignore the failure.
37 // exit(printf("read failed: n=%d errno=%d\n", n, errno));
43 fd
= open("/dev/urandom", O_RDONLY
);
44 int fd2
= open("/dev/urandom", O_RDONLY
);
45 if (fd
== -1 || fd2
== -1)
46 exit(printf("open failed\n"));
48 pthread_create(&th
, 0, Thread
, 0);
49 if (dup2(fd2
, fd
) == -1)
50 exit(printf("dup2 failed\n"));
53 exit(printf("close failed\n"));
55 exit(printf("close failed\n"));
56 fprintf(stderr
, "DONE\n");
59 // CHECK-NOT: WARNING: ThreadSanitizer: data race