1 /*
2  * Reproducer for bug #323905. See also
3  * http://bugs.kde.org/show_bug.cgi?id=323905.
4  */
5 
6 #include <climits>    /* PTHREAD_STACK_MIN */
7 #include <cstdio>     /* fprintf() */
8 #include <fcntl.h>    /* O_RDONLY */
9 #include <pthread.h>
10 #include <unistd.h>   /* close() */
11 
12 /* Happens with two threads also */
13 #define THREAD_COUNT 256
14 
15 void* thread(void*)
16 {
17   int fd;
18 
19   /* Happens with any file, not just /dev/null */
20   fd = open("/dev/null", O_RDONLY);
21   if (fd >= 0)
22     close(fd);
23   else
24     fprintf(stderr, "Failed to open /dev/null\n");
25   return 0;
26 }
27 
28 int main()
29 {
30   int i, r;
31   pthread_attr_t attr;
32   pthread_t threads[THREAD_COUNT];
33 
34   pthread_attr_init(&attr);
35   pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN);
36   for (i = 0; i < THREAD_COUNT; ++i) {
37     r = pthread_create(&threads[i], &attr, thread, 0);
38     if (r != 0) {
39       fprintf(stderr, "Failed to create thread %d\n", i);
40       return 1;
41     }
42   }
43   pthread_attr_destroy(&attr);
44   for (i = 0; i < THREAD_COUNT; ++i) {
45     r = pthread_join(threads[i], 0);
46     if (r != 0) {
47       fprintf(stderr, "Failed to join thread %d\n", i);
48       return 1;
49     }
50   }
51   fprintf(stderr, "Done.\n");
52   return 0;
53 }
54