summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/gen/closefrom.c
blob: 334a03de5b277d44a845ee0d50a75f9d767b3605 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#pragma weak closefrom = _closefrom
#pragma weak fdwalk = _fdwalk

#include "synonyms.h"
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <limits.h>
#include <errno.h>
#include <alloca.h>
#include <sys/resource.h>

/* Initial size of the open file descriptor array */
#define	FDS_SZ	(1024 * sizeof (int))

/*
 * Iterate over all open file descriptors, calling 'func' on each one.
 * Terminate the iteration when 'func' returns non-zero or when all
 * open file descriptors have been processed.  Return the value of
 * the last non-zero return from 'func' or zero.
 */
int
fdwalk(int (*func)(void *, int), void *cd)
{
	int err = errno;
	int rv = 0;
	int max_fds = INT_MAX;
	struct rlimit rl;
	DIR *dirp;
	struct dirent64 *dp;
	int *fds;
	size_t fds_sz;
	int nfds;
	int i;

	nfds = 0;
	fds = alloca(FDS_SZ);
	fds_sz = FDS_SZ;
	if ((dirp = opendir("/proc/self/fd")) != NULL) {
		/*
		 * Collect all of the open file descriptors and close
		 * the directory before calling 'func' on any of them.
		 */
		while ((dp = readdir64(dirp)) != NULL) {
			/* skip '.', '..' and the opendir() fd */
			if (!isdigit(dp->d_name[0]) ||
			    (i = atoi(dp->d_name)) == dirp->dd_fd)
				continue;
			if (fds_sz <= nfds * sizeof (int)) {
				fds = memcpy(alloca(fds_sz * 2), fds, fds_sz);
				fds_sz *= 2;
			}
			fds[nfds++] = i;
		}
		(void) closedir(dirp);
	} else {
		/*
		 * We could not open the /proc file descriptor directory.
		 * We have to do it the hard way.
		 */
		if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
			max_fds = (rl.rlim_max == RLIM_INFINITY)?
				INT_MAX : rl.rlim_max;
		for (i = 0; i < max_fds; i++) {
			if (fcntl(i, F_GETFD) < 0)
				continue;
			if (fds_sz <= nfds * sizeof (int)) {
				fds = memcpy(alloca(fds_sz * 2), fds, fds_sz);
				fds_sz *= 2;
			}
			fds[nfds++] = i;
		}
	}

	/*
	 * Restore the original value of errno so that
	 * the caller sees only the value of errno set
	 * by the callback function.
	 */
	errno = err;

	/*
	 * Perform the callbacks on all of the open files.
	 */
	for (i = 0; i < nfds; i++)
		if ((rv = func(cd, fds[i])) != 0)
			break;

	return (rv);
}

/*
 * Call-back function for closefrom(), below.
 */
static int
void_close(void *lowp, int fd)
{
	if (fd >= *(int *)lowp)
		(void) close(fd);
	return (0);
}

/*
 * Close all open file descriptors greater than or equal to lowfd.
 */
void
closefrom(int lowfd)
{
	int low = (lowfd < 0)? 0 : lowfd;

	/*
	 * Close lowfd right away as a hedge against failing
	 * to open the /proc file descriptor directory due
	 * all file descriptors being currently used up.
	 */
	(void) close(low++);
	(void) fdwalk(void_close, &low);
}