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):
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]);
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