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