Imported Debian patch 1.6.8p9-2
[debian/sudo] / closefrom.c
1 /*
2  * Copyright (c) 2004 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "config.h"
18
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #ifdef STDC_HEADERS
24 # include <stdlib.h>
25 # include <stddef.h>
26 #else
27 # ifdef HAVE_STDLIB_H
28 #  include <stdlib.h>
29 # endif
30 #endif /* STDC_HEADERS */
31 #ifdef HAVE_DIRENT_H
32 # include <dirent.h>
33 # define NAMLEN(dirent) strlen((dirent)->d_name)
34 #else
35 # define dirent direct
36 # define NAMLEN(dirent) (dirent)->d_namlen
37 # ifdef HAVE_SYS_NDIR_H
38 #  include <sys/ndir.h>
39 # endif
40 # ifdef HAVE_SYS_DIR_H
41 #  include <sys/dir.h>
42 # endif
43 # ifdef HAVE_NDIR_H
44 #  include <ndir.h>
45 # endif
46 #endif
47
48 #include "sudo.h"
49
50 #ifndef lint
51 static const char rcsid[] = "$Sudo: closefrom.c,v 1.6 2004/06/01 20:51:56 millert Exp $";
52 #endif /* lint */
53
54 /*
55  * Close all file descriptors greater than or equal to lowfd.
56  */
57 void
58 closefrom(lowfd)
59     int lowfd;
60 {
61     long fd, maxfd;
62 #ifdef HAVE_DIRFD
63     char fdpath[PATH_MAX], *endp;
64     struct dirent *dent;
65     DIR *dirp;
66     int len;
67
68     /* Check for a /proc/$$/fd directory. */
69     len = snprintf(fdpath, sizeof(fdpath), "/proc/%ld/fd", (long)getpid());
70     if (len != -1 && len <= sizeof(fdpath) && (dirp = opendir(fdpath))) {
71         while ((dent = readdir(dirp)) != NULL) {
72             fd = strtol(dent->d_name, &endp, 10);
73             if (dent->d_name != endp && *endp == '\0' &&
74                 fd >= 0 && fd < INT_MAX && fd >= lowfd && fd != dirfd(dirp))
75                 (void) close((int) fd);
76         }
77         (void) closedir(dirp);
78     } else
79 #endif
80     {
81         /*
82          * Fall back on sysconf() or getdtablesize().  We avoid checking
83          * resource limits since it is possible to open a file descriptor
84          * and then drop the rlimit such that it is below the open fd.
85          */
86 #ifdef HAVE_SYSCONF
87         maxfd = sysconf(_SC_OPEN_MAX);
88 #else
89         maxfd = getdtablesize();
90 #endif /* HAVE_SYSCONF */
91         if (maxfd < 0)
92             maxfd = OPEN_MAX;
93
94         for (fd = lowfd; fd < maxfd; fd++)
95             (void) close((int) fd);
96     }
97 }