Description: Optimize file descriptor closing Closing all the fds is a severe performance issue in containers when ulimit is high. We fixed that in APT a long time ago, time to apply the same fix here. Author: Julian Andres Klode --- a/faked.c +++ b/faked.c @@ -107,6 +107,7 @@ #ifdef HAVE_SYS_XATTR_H #include #endif +#include #include #include #include @@ -1502,16 +1503,32 @@ int main(int argc, char **argv){ /* literally copied from the linux klogd code, go to background */ if ((pid=fork()) == 0){ int fl; - int num_fds = getdtablesize(); + DIR *dir; - fflush(stdout); + dir = opendir("/proc/self/fd"); + if (dir != NULL) { + struct dirent *ent; + int dfd = dirfd(dir); + while (ent = readdir(dir)) { + fl = atoi(ent->d_name); + if (fl >= 0 && fl != dfd) +#ifdef FAKEROOT_FAKENET + if (fl != sd) +#endif /* FAKEROOT_FAKENET */ + close(fl); + } + closedir(dir); + } + else { + int num_fds = getdtablesize(); - /* This is the child closing its file descriptors. */ - for (fl= 0; fl <= num_fds; ++fl) + /* This is the child closing its file descriptors. */ + for (fl= 0; fl <= num_fds; ++fl) #ifdef FAKEROOT_FAKENET - if (fl != sd) + if (fl != sd) #endif /* FAKEROOT_FAKENET */ - close(fl); + close(fl); + } setsid(); } else { printf("%li:%i\n",(long)FAKE_KEY,pid);