xref: /onnv-gate/usr/src/lib/libc/port/gen/closefrom.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53636Sraf  * Common Development and Distribution License (the "License").
63636Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213636Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
29*6812Sraf #pragma weak _closefrom = closefrom
30*6812Sraf #pragma weak _fdwalk = fdwalk
310Sstevel@tonic-gate 
32*6812Sraf #include "lint.h"
330Sstevel@tonic-gate #include <ctype.h>
340Sstevel@tonic-gate #include <stdlib.h>
350Sstevel@tonic-gate #include <unistd.h>
360Sstevel@tonic-gate #include <string.h>
370Sstevel@tonic-gate #include <fcntl.h>
380Sstevel@tonic-gate #include <dirent.h>
390Sstevel@tonic-gate #include <limits.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <alloca.h>
420Sstevel@tonic-gate #include <sys/resource.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /* Initial size of the open file descriptor array */
450Sstevel@tonic-gate #define	FDS_SZ	(1024 * sizeof (int))
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate  * Iterate over all open file descriptors, calling 'func' on each one.
490Sstevel@tonic-gate  * Terminate the iteration when 'func' returns non-zero or when all
500Sstevel@tonic-gate  * open file descriptors have been processed.  Return the value of
510Sstevel@tonic-gate  * the last non-zero return from 'func' or zero.
520Sstevel@tonic-gate  */
530Sstevel@tonic-gate int
fdwalk(int (* func)(void *,int),void * cd)540Sstevel@tonic-gate fdwalk(int (*func)(void *, int), void *cd)
550Sstevel@tonic-gate {
560Sstevel@tonic-gate 	int err = errno;
570Sstevel@tonic-gate 	int rv = 0;
580Sstevel@tonic-gate 	int max_fds = INT_MAX;
590Sstevel@tonic-gate 	struct rlimit rl;
600Sstevel@tonic-gate 	DIR *dirp;
613636Sraf 	struct dirent64 *dp;
620Sstevel@tonic-gate 	int *fds;
630Sstevel@tonic-gate 	size_t fds_sz;
640Sstevel@tonic-gate 	int nfds;
650Sstevel@tonic-gate 	int i;
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	nfds = 0;
680Sstevel@tonic-gate 	fds = alloca(FDS_SZ);
690Sstevel@tonic-gate 	fds_sz = FDS_SZ;
700Sstevel@tonic-gate 	if ((dirp = opendir("/proc/self/fd")) != NULL) {
710Sstevel@tonic-gate 		/*
720Sstevel@tonic-gate 		 * Collect all of the open file descriptors and close
730Sstevel@tonic-gate 		 * the directory before calling 'func' on any of them.
740Sstevel@tonic-gate 		 */
753636Sraf 		while ((dp = readdir64(dirp)) != NULL) {
760Sstevel@tonic-gate 			/* skip '.', '..' and the opendir() fd */
770Sstevel@tonic-gate 			if (!isdigit(dp->d_name[0]) ||
780Sstevel@tonic-gate 			    (i = atoi(dp->d_name)) == dirp->dd_fd)
790Sstevel@tonic-gate 				continue;
800Sstevel@tonic-gate 			if (fds_sz <= nfds * sizeof (int)) {
810Sstevel@tonic-gate 				fds = memcpy(alloca(fds_sz * 2), fds, fds_sz);
820Sstevel@tonic-gate 				fds_sz *= 2;
830Sstevel@tonic-gate 			}
840Sstevel@tonic-gate 			fds[nfds++] = i;
850Sstevel@tonic-gate 		}
860Sstevel@tonic-gate 		(void) closedir(dirp);
870Sstevel@tonic-gate 	} else {
880Sstevel@tonic-gate 		/*
890Sstevel@tonic-gate 		 * We could not open the /proc file descriptor directory.
900Sstevel@tonic-gate 		 * We have to do it the hard way.
910Sstevel@tonic-gate 		 */
920Sstevel@tonic-gate 		if (getrlimit(RLIMIT_NOFILE, &rl) == 0)
930Sstevel@tonic-gate 			max_fds = (rl.rlim_max == RLIM_INFINITY)?
94*6812Sraf 			    INT_MAX : rl.rlim_max;
950Sstevel@tonic-gate 		for (i = 0; i < max_fds; i++) {
960Sstevel@tonic-gate 			if (fcntl(i, F_GETFD) < 0)
970Sstevel@tonic-gate 				continue;
980Sstevel@tonic-gate 			if (fds_sz <= nfds * sizeof (int)) {
990Sstevel@tonic-gate 				fds = memcpy(alloca(fds_sz * 2), fds, fds_sz);
1000Sstevel@tonic-gate 				fds_sz *= 2;
1010Sstevel@tonic-gate 			}
1020Sstevel@tonic-gate 			fds[nfds++] = i;
1030Sstevel@tonic-gate 		}
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	/*
1070Sstevel@tonic-gate 	 * Restore the original value of errno so that
1080Sstevel@tonic-gate 	 * the caller sees only the value of errno set
1090Sstevel@tonic-gate 	 * by the callback function.
1100Sstevel@tonic-gate 	 */
1110Sstevel@tonic-gate 	errno = err;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	/*
1140Sstevel@tonic-gate 	 * Perform the callbacks on all of the open files.
1150Sstevel@tonic-gate 	 */
1160Sstevel@tonic-gate 	for (i = 0; i < nfds; i++)
1170Sstevel@tonic-gate 		if ((rv = func(cd, fds[i])) != 0)
1180Sstevel@tonic-gate 			break;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	return (rv);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * Call-back function for closefrom(), below.
1250Sstevel@tonic-gate  */
1260Sstevel@tonic-gate static int
void_close(void * lowp,int fd)1270Sstevel@tonic-gate void_close(void *lowp, int fd)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	if (fd >= *(int *)lowp)
1300Sstevel@tonic-gate 		(void) close(fd);
1310Sstevel@tonic-gate 	return (0);
1320Sstevel@tonic-gate }
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * Close all open file descriptors greater than or equal to lowfd.
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate void
closefrom(int lowfd)1380Sstevel@tonic-gate closefrom(int lowfd)
1390Sstevel@tonic-gate {
1400Sstevel@tonic-gate 	int low = (lowfd < 0)? 0 : lowfd;
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	/*
1430Sstevel@tonic-gate 	 * Close lowfd right away as a hedge against failing
1440Sstevel@tonic-gate 	 * to open the /proc file descriptor directory due
1450Sstevel@tonic-gate 	 * all file descriptors being currently used up.
1460Sstevel@tonic-gate 	 */
1470Sstevel@tonic-gate 	(void) close(low++);
1480Sstevel@tonic-gate 	(void) fdwalk(void_close, &low);
1490Sstevel@tonic-gate }
150