]> git.ipfire.org Git - pakfire.git/commitdiff
tests: jail: Add check if we are leaking any file descriptors
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Aug 2022 15:33:11 +0000 (15:33 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 4 Aug 2022 15:33:11 +0000 (15:33 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/python/jail.py
tests/stub/command.c

index 6f755591836259f22b0b13a1c0792d4a16d6ac19..8428a0d453636f716a3f604267d4c4714640ebb5 100755 (executable)
@@ -116,6 +116,12 @@ class JailTests(unittest.TestCase):
                with self.assertRaises(OSError):
                        self.pakfire.execute(["/command", "print-nice"], nice=100)
 
+       def test_check_open_file_descriptors(self):
+               """
+                       Since we are spawning child processes, it might happen that we leak file
+                       descriptors to the child process.
+               """
+               self.pakfire.execute(["/command", "check-open-file-descriptors"])
 
        # This is an interactive test which cannot be performed automatically
        #def test_shell(self):
index c37f734954246a93066af116fddbc50957851bce..e0980952130152fdc1ef2d2298002e1284261acd 100644 (file)
@@ -47,6 +47,33 @@ static int random_string(char* s, size_t length) {
        return 0;
 }
 
+static int check_open_file_descriptors(int argc, char* argv[]) {
+       struct rlimit limits;
+       int r;
+
+       // Determine the maximum amount of files that could be open
+       r = getrlimit(RLIMIT_NOFILE, &limits);
+       if (r) {
+               fprintf(stderr, "getrlimit() failed: %m\n");
+               return EXIT_FAILURE;
+       }
+
+       // Count any open file descriptors
+       unsigned int open_fds = 0;
+
+       for (unsigned int fd = 3; fd < limits.rlim_max; fd++) {
+               r = close(fd);
+               if (r < 0)
+                       continue;
+
+               fprintf(stderr, "FD %u is open\n", fd);
+               open_fds++;
+       }
+
+       // Return failure if we have found any open file descriptors
+       return (open_fds > 0) ? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
 static int echo(int argc, char* argv[]) {
        for (int i = 0; i < argc; i++) {
                printf("%s\n", argv[i]);
@@ -184,8 +211,11 @@ int main(int argc, char* argv[]) {
        int (*callback)(int argc, char* argv[]) = NULL;
        const char* command = argv[1];
 
+       if (strcmp(command, "check-open-file-descriptors") == 0)
+               callback = check_open_file_descriptors;
+
        // Echo
-       if (strcmp(command, "echo") == 0)
+       else if (strcmp(command, "echo") == 0)
                callback = echo;
 
        // Echo Environment