aur/fakeroot/optimize-fd-closing.patch

56 lines
1.5 KiB
Diff
Raw Normal View History

2023-03-22 08:59:27 +01:00
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 <juliank@ubuntu.com>
--- a/faked.c
+++ b/faked.c
@@ -107,6 +107,7 @@
#ifdef HAVE_SYS_XATTR_H
#include <sys/xattr.h>
#endif
+#include <dirent.h>
#include <fcntl.h>
#include <ctype.h>
#include <stdio.h>
@@ -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);