Hi Minh, ack with some comments, (on top of AndersW comments).

/Thanks HansN


On 03/15/2018 07:50 AM, Minh Chau wrote:
---
  src/base/os_defs.c | 25 ++++++++++++++++++++-----
  1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/src/base/os_defs.c b/src/base/os_defs.c
index 6f9ec52..e914011 100644
--- a/src/base/os_defs.c
+++ b/src/base/os_defs.c
@@ -1052,14 +1052,29 @@ uint32_t 
ncs_os_process_execute_timed(NCS_OS_PROC_EXECUTE_TIMED_INFO *req)
                 * child */
                if (getenv("OPENSAF_KEEP_FD_OPEN_AFTER_FORK") == NULL) {
                        /* Close all inherited file descriptors */
-                       int i = sysconf(_SC_OPEN_MAX);
-                       if (i == -1) {
+                       int fd_max = sysconf(_SC_OPEN_MAX);
+
+                       if (fd_max == -1) {
                                syslog(LOG_ERR, "%s: sysconf failed - %s",
-                                      __FUNCTION__, strerror(errno));
+                                       __FUNCTION__, strerror(errno));
                                exit(EXIT_FAILURE);
                        }
-                       for (i--; i >= 0; --i)
-                               (void)close(i); /* close all descriptors */
+                       struct dirent *pentry = NULL;
+                       DIR *dir = opendir("/proc/self/fd");
+
+                       if (dir != NULL) {
+                               while ((pentry = readdir(dir)) != NULL) {
[HansN] readdir will return not only 0, 1, 2 etc. but also the current directory '.' and '..' this should be handled here.
[HansN] perhaps we should use readdir_r instead?
+                                       int fd = strtoimax(pentry->d_name, 
NULL, 10);
+                                       if (fd > INT_MIN && fd < fd_max) 
(void)close(fd);
+                               }
+                               (void)closedir(dir);
+                       } else {
+                               /* fall back, close all possible descriptors */
+                               syslog(LOG_ERR, "%s: opendir failed - %s",
+                                       __FUNCTION__, strerror(errno));
+                               for (fd_max--; fd_max >= 0; --fd_max)
+                                       (void)close(fd_max);
+                       }
/* Redirect standard files to /dev/null */
                        if (freopen("/dev/null", "r", stdin) == NULL)


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Opensaf-devel mailing list
Opensaf-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/opensaf-devel

Reply via email to