From: Michael Tremer Date: Thu, 4 Aug 2022 15:33:11 +0000 (+0000) Subject: tests: jail: Add check if we are leaking any file descriptors X-Git-Tag: 0.9.28~586 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ba357d0875ca7bf05435b407a5e980bfe438b98a;p=pakfire.git tests: jail: Add check if we are leaking any file descriptors Signed-off-by: Michael Tremer --- diff --git a/tests/python/jail.py b/tests/python/jail.py index 6f7555918..8428a0d45 100755 --- a/tests/python/jail.py +++ b/tests/python/jail.py @@ -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): diff --git a/tests/stub/command.c b/tests/stub/command.c index c37f73495..e09809521 100644 --- a/tests/stub/command.c +++ b/tests/stub/command.c @@ -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