lib: Use close_low_fd in close_low_fds
authorVolker Lendecke <vl@samba.org>
Tue, 29 Jul 2014 18:42:18 +0000 (20:42 +0200)
committerJeremy Allison <jra@samba.org>
Fri, 1 Aug 2014 20:11:46 +0000 (22:11 +0200)
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/become_daemon.c

index eba0cae579e9580e6ffd22b3baa0352484b0e1eb..d940cd7d33eb7021ef8cf65a0ad77c75dd927100 100644 (file)
@@ -77,42 +77,28 @@ _PUBLIC_ int close_low_fd(int fd)
 
 _PUBLIC_ void close_low_fds(bool stdin_too, bool stdout_too, bool stderr_too)
 {
-#ifndef VALGRIND
-       int fd;
-       int i;
-
-       if (stdin_too)
-               close(0);
-       if (stdout_too)
-               close(1);
-
-       if (stderr_too)
-               close(2);
-
-       /* try and use up these file descriptors, so silly
-               library routines writing to stdout etc won't cause havoc */
-       for (i=0;i<3;i++) {
-               if (i == 0 && !stdin_too)
-                       continue;
-               if (i == 1 && !stdout_too)
-                       continue;
-               if (i == 2 && !stderr_too)
-                       continue;
-
-               fd = open("/dev/null",O_RDWR,0);
-               if (fd < 0)
-                       fd = open("/dev/null",O_WRONLY,0);
-               if (fd < 0) {
-                       DEBUG(0,("Can't open /dev/null\n"));
-                       return;
+
+       if (stdin_too) {
+               int ret = close_low_fd(0);
+               if (ret != 0) {
+                       DEBUG(0, ("%s: close_low_fd(0) failed: %s\n",
+                                 __func__, strerror(ret)));
                }
-               if (fd != i) {
-                       DEBUG(0,("Didn't get file descriptor %d\n",i));
-                       close(fd);
-                       return;
+       }
+       if (stdout_too) {
+               int ret = close_low_fd(1);
+               if (ret != 0) {
+                       DEBUG(0, ("%s: close_low_fd(1) failed: %s\n",
+                                 __func__, strerror(ret)));
+               }
+       }
+       if (stderr_too) {
+               int ret = close_low_fd(2);
+               if (ret != 0) {
+                       DEBUG(0, ("%s: close_low_fd(2) failed: %s\n",
+                                 __func__, strerror(ret)));
                }
        }
-#endif
 }
 
 /****************************************************************************