Move valid_fd_012() from string.c to fd.c.
[paraslash.git] / fd.c
diff --git a/fd.c b/fd.c
index 58851723978fb3c9856f87aa35d65a7141c162d7..8fc1abad19057bd724f26a25dca2ddb3ba5003ee 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -426,3 +426,24 @@ again:
                goto again;
        return ret;
 }
+
+/**
+ * Ensure that file descriptors 0, 1, and 2 are valid.
+ *
+ * Common approach that opens /dev/null until it gets a file descriptor greater
+ * than two.
+ *
+ * \sa okir's Black Hats Manual.
+ */
+void valid_fd_012(void)
+{
+       while (1) {
+               int fd = open("/dev/null", O_RDWR);
+               if (fd < 0)
+                       exit(EXIT_FAILURE);
+               if (fd > 2) {
+                       close(fd);
+                       break;
+               }
+       }
+}