[PATCH] Don't leak file descriptors when spawning new workers
authorKevin Ottens <kevin.ottens@enioka.com>
Fri, 5 Jan 2024 10:51:49 +0000 (11:51 +0100)
committerAurélien COUDERC <coucouf@debian.org>
Thu, 23 May 2024 21:13:17 +0000 (23:13 +0200)
By default we inherit file descriptors from the parent in
the worker process. This is a leak of resources since the
worker won't be able to do anything with them. Also, in
the case of CIFS this causes locks which might lead to bad
surprises in the parent process.

Gbp-Pq: Name fix_cifs_file_locks.patch

src/kioslave/kioslave.cpp

index fcd15ef937cb17823d40fa7ed4a51c54e09251a6..6873003a9f7ce25a40f614a0f7c5cfcce2419e38 100644 (file)
 #include <QPluginLoader>
 #include <QString>
 
+#ifdef Q_OS_UNIX
+#include <sys/resource.h>
+#endif
+
 #ifdef Q_OS_WIN
 #include <QProcess>
 #include <QStandardPaths>
@@ -40,6 +44,17 @@ extern "C" KIO::AuthInfo *_kioslave_init_kio()
 
 int main(int argc, char **argv)
 {
+#ifdef Q_OS_UNIX
+    int max_fd = INT_MAX;
+    struct rlimit limit;
+    if (getrlimit(RLIMIT_NOFILE, &limit) == 0) {
+        max_fd = limit.rlim_cur;
+    }
+    for (int fd = STDERR_FILENO + 1; fd < max_fd; fd++) {
+        ::close(fd);
+    }
+#endif
+
     if (argc < 5) {
         fprintf(stderr, "Usage: kioslave5 <slave-lib> <protocol> <klauncher-socket> <app-socket>\n\nThis program is part of KDE.\n");
         return 1;