summaryrefslogtreecommitdiff
path: root/lib/libpthread/uthread/uthread_closefrom.c
blob: d80c0cff21ddbe42008e60cc317baa0bae3b7777 (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
48
49
/* $OpenBSD: uthread_closefrom.c,v 1.2 2006/09/22 19:04:33 kurt Exp $ */

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

#include <sys/stat.h>

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

#include "pthread_private.h"

int
closefrom(int fd)
{
	int ret;
	int safe_fd;
	int lock_fd;

	_thread_enter_cancellation_point();
	
	if (fd < 0 || fd >= _thread_dtablesize) {
		errno = EBADF;
		ret = -1;
	} else {
		safe_fd = _thread_kern_pipe[0] > _thread_kern_pipe[1] ?
			_thread_kern_pipe[0] : _thread_kern_pipe[1];

		/*
		 * close individual files until we get past the pipe
		 * fds.  Attempting to close a pipe fd is a no-op.
		 */
		for (safe_fd++; fd < safe_fd; fd++)
			close(fd);

		/* Reset flags and clean up the fd table for fds to close */
		for (lock_fd = fd; lock_fd < _thread_dtablesize; lock_fd++)
			if (_thread_fd_table[lock_fd] != NULL)
				_thread_fd_table_remove(lock_fd);

		/* Now let the system do its thing */
		ret = _thread_sys_closefrom(fd);
	}

	_thread_leave_cancellation_point();

	return (ret);

}