49 RESTARTABLE(dup2(fd_from, fd_to), err);
50 return err;
51 }
52
53 int
54 closeSafely(int fd)
55 {
56 return (fd == -1) ? 0 : close(fd);
57 }
58
59 int
60 isAsciiDigit(char c)
61 {
62 return c >= '0' && c <= '9';
63 }
64
65 #ifdef _ALLBSD_SOURCE
66 #define FD_DIR "/dev/fd"
67 #define dirent64 dirent
68 #define readdir64 readdir
69 #else
70 #define FD_DIR "/proc/self/fd"
71 #endif
72
73 int
74 closeDescriptors(void)
75 {
76 DIR *dp;
77 struct dirent64 *dirp;
78 int from_fd = FAIL_FILENO + 1;
79
80 /* We're trying to close all file descriptors, but opendir() might
81 * itself be implemented using a file descriptor, and we certainly
82 * don't want to close that while it's in use. We assume that if
83 * opendir() is implemented using a file descriptor, then it uses
84 * the lowest numbered file descriptor, just like open(). So we
85 * close a couple explicitly. */
86
87 close(from_fd); /* for possible use by opendir() */
88 close(from_fd + 1); /* another one for good luck */
89
90 if ((dp = opendir(FD_DIR)) == NULL)
91 return 0;
92
93 /* We use readdir64 instead of readdir to work around Solaris bug
94 * 6395699: /proc/self/fd fails to report file descriptors >= 1024 on Solaris 9
95 */
96 while ((dirp = readdir64(dp)) != NULL) {
97 int fd;
98 if (isAsciiDigit(dirp->d_name[0]) &&
99 (fd = strtol(dirp->d_name, NULL, 10)) >= from_fd + 2)
100 close(fd);
101 }
102
103 closedir(dp);
104
105 return 1;
106 }
107
108 int
|
49 RESTARTABLE(dup2(fd_from, fd_to), err);
50 return err;
51 }
52
53 int
54 closeSafely(int fd)
55 {
56 return (fd == -1) ? 0 : close(fd);
57 }
58
59 int
60 isAsciiDigit(char c)
61 {
62 return c >= '0' && c <= '9';
63 }
64
65 #ifdef _ALLBSD_SOURCE
66 #define FD_DIR "/dev/fd"
67 #define dirent64 dirent
68 #define readdir64 readdir
69 #elif defined(_AIX)
70 /* AIX does not understand '/proc/self' - it requires the real process ID */
71 #define FD_DIR aix_fd_dir
72 #else
73 #define FD_DIR "/proc/self/fd"
74 #endif
75
76 int
77 closeDescriptors(void)
78 {
79 DIR *dp;
80 struct dirent64 *dirp;
81 int from_fd = FAIL_FILENO + 1;
82
83 /* We're trying to close all file descriptors, but opendir() might
84 * itself be implemented using a file descriptor, and we certainly
85 * don't want to close that while it's in use. We assume that if
86 * opendir() is implemented using a file descriptor, then it uses
87 * the lowest numbered file descriptor, just like open(). So we
88 * close a couple explicitly. */
89
90 close(from_fd); /* for possible use by opendir() */
91 close(from_fd + 1); /* another one for good luck */
92
93 #if defined(_AIX)
94 /* AIX does not understand '/proc/self' - it requires the real process ID */
95 char aix_fd_dir[32]; /* the pid has at most 19 digits */
96 snprintf(aix_fd_dir, 32, "/proc/%d/fd", getpid());
97 #endif
98
99 if ((dp = opendir(FD_DIR)) == NULL)
100 return 0;
101
102 /* We use readdir64 instead of readdir to work around Solaris bug
103 * 6395699: /proc/self/fd fails to report file descriptors >= 1024 on Solaris 9
104 */
105 while ((dirp = readdir64(dp)) != NULL) {
106 int fd;
107 if (isAsciiDigit(dirp->d_name[0]) &&
108 (fd = strtol(dirp->d_name, NULL, 10)) >= from_fd + 2)
109 close(fd);
110 }
111
112 closedir(dp);
113
114 return 1;
115 }
116
117 int
|