summaryrefslogtreecommitdiff
path: root/regress/lib/libpthread/closefrom/closefrom.c
blob: 3568e41a8a7b43ace30281759ae4e2e5a89d6721 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* $OpenBSD: closefrom.c,v 1.1 2004/01/15 22:22:52 marc Exp $ */

/* PUBLIC DOMAIN: No Rights Reserved. Marco S Hyman <marc@snafu.org> */

#include <errno.h>
#include <pthread.h>
#include <unistd.h>

#include "test.h"

static void *
dummy_thread(void* arg)
{
	/* just exit */
	return NULL;
}

/*
 * Test that closefrom does the right thing in a threaded programs,
 * specifically that it doesn't kill the thread kernel signal pipe.
 */
int
main(int argc, char *argv[])
{
	pthread_t thread;
	void *status;
	int fd;
	int result;

	/* close files above stderr.   The kernel pipes shouldn't be touched */
	fd = STDERR_FILENO + 1;
	result = closefrom(fd);
	printf("closefrom(%d) == %d/%d\n", fd, result, errno);

	/* do it again: make sure that the result is -1/EBADF */
	result = closefrom(fd);
	printf("closefrom(%d) == %d/%d\n", fd, result, errno);
	ASSERT(result == -1 && errno == EBADF);

	/* start a thread to verify the thread kernel is working */
	CHECKr(pthread_create(&thread, NULL, dummy_thread, NULL));
	CHECKr(pthread_join(thread, &status));
	printf("dummy thread exited with status %p\n", status);

	SUCCEED;
	return 0;
}