1 /* $OpenBSD: closefrom.c,v 1.1 2004/01/15 22:22:52 marc Exp $ */
2
3 /* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */
4
5 #include <errno.h>
6 #include <pthread.h>
7 #include <unistd.h>
8
9 #include "test.h"
10
11 static void *
dummy_thread(void * arg)12 dummy_thread(void* arg)
13 {
14 /* just exit */
15 return NULL;
16 }
17
18 /*
19 * Test that closefrom does the right thing in a threaded programs,
20 * specifically that it doesn't kill the thread kernel signal pipe.
21 */
22 int
main(int argc,char * argv[])23 main(int argc, char *argv[])
24 {
25 pthread_t thread;
26 void *status;
27 int fd;
28 int result;
29
30 /* close files above stderr. The kernel pipes shouldn't be touched */
31 fd = STDERR_FILENO + 1;
32 result = closefrom(fd);
33 printf("closefrom(%d) == %d/%d\n", fd, result, errno);
34
35 /* do it again: make sure that the result is -1/EBADF */
36 result = closefrom(fd);
37 printf("closefrom(%d) == %d/%d\n", fd, result, errno);
38 ASSERT(result == -1 && errno == EBADF);
39
40 /* start a thread to verify the thread kernel is working */
41 CHECKr(pthread_create(&thread, NULL, dummy_thread, NULL));
42 CHECKr(pthread_join(thread, &status));
43 printf("dummy thread exited with status %p\n", status);
44
45 SUCCEED;
46 return 0;
47 }
48