summaryrefslogtreecommitdiff
path: root/dbus
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2011-01-31 15:22:14 -0500
committerColin Walters <walters@verbum.org>2011-02-03 13:25:34 -0500
commit66a09fa7c3c8e4232b4225c49d01d9efb97458c9 (patch)
treefc1d6d281bd48532ccc9af33dd6c76a74af5b14f /dbus
parenta0e8ae3b77e5ec33e5661f82320cdef4466a9f9d (diff)
bus: Raise file descriptor limit to match configuration
The default configuration has hardcoded 2048 complete connections, and 64 incomplete. We need at least that number of file descriptors, plus some for internal use. In the bus, attempt to call setrlimit() before we drop privileges. Practically speaking for this means the system bus gets it, the session bus doesn't. http://bugs.freedesktop.org/show_bug.cgi?id=33474 Reviewed-By: Simon McVittie <simon.mcvittie@collabora.co.uk>
Diffstat (limited to 'dbus')
-rw-r--r--dbus/dbus-sysdeps-util-unix.c53
-rw-r--r--dbus/dbus-sysdeps-util-win.c5
-rw-r--r--dbus/dbus-sysdeps.h2
3 files changed, 60 insertions, 0 deletions
diff --git a/dbus/dbus-sysdeps-util-unix.c b/dbus/dbus-sysdeps-util-unix.c
index 6e092458..02954d5c 100644
--- a/dbus/dbus-sysdeps-util-unix.c
+++ b/dbus/dbus-sysdeps-util-unix.c
@@ -42,6 +42,9 @@
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
+#ifdef HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#endif
#include <grp.h>
#include <sys/socket.h>
#include <dirent.h>
@@ -369,6 +372,56 @@ _dbus_change_to_daemon_user (const char *user,
}
#endif /* !HAVE_LIBAUDIT */
+
+/**
+ * Attempt to ensure that the current process can open
+ * at least @limit file descriptors.
+ *
+ * If @limit is lower than the current, it will not be
+ * lowered. No error is returned if the request can
+ * not be satisfied.
+ *
+ * @limit Number of file descriptors
+ */
+void
+_dbus_request_file_descriptor_limit (unsigned int limit)
+{
+#ifdef HAVE_SETRLIMIT
+ struct rlimit lim;
+ struct rlimit target_lim;
+ unsigned int current_limit;
+
+ /* No point to doing this practically speaking
+ * if we're not uid 0. We expect the system
+ * bus to use this before we change UID, and
+ * the session bus takes the Linux default
+ * of 1024 for both cur and max.
+ */
+ if (getuid () != 0)
+ return;
+
+ if (getrlimit (RLIMIT_NOFILE, &lim) < 0)
+ return;
+
+ if (lim.rlim_cur >= limit)
+ return;
+
+ /* Ignore "maximum limit", assume we have the "superuser"
+ * privileges. On Linux this is CAP_SYS_RESOURCE.
+ */
+ target_lim.rlim_cur = target_lim.rlim_max = limit;
+ /* Also ignore errors; if we fail, we will at least work
+ * up to whatever limit we had, which seems better than
+ * just outright aborting.
+ *
+ * However, in the future we should probably log this so OS builders
+ * have a chance to notice any misconfiguration like dbus-daemon
+ * being started without CAP_SYS_RESOURCE.
+ */
+ setrlimit (RLIMIT_NOFILE, &target_lim);
+#endif
+}
+
void
_dbus_init_system_log (void)
{
diff --git a/dbus/dbus-sysdeps-util-win.c b/dbus/dbus-sysdeps-util-win.c
index 2f214092..f10100b6 100644
--- a/dbus/dbus-sysdeps-util-win.c
+++ b/dbus/dbus-sysdeps-util-win.c
@@ -257,6 +257,11 @@ _dbus_change_to_daemon_user (const char *user,
}
void
+_dbus_request_file_descriptor_limit (unsigned int limit)
+{
+}
+
+void
_dbus_init_system_log (void)
{
// FIXME!
diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h
index 3955d829..22d7969e 100644
--- a/dbus/dbus-sysdeps.h
+++ b/dbus/dbus-sysdeps.h
@@ -517,6 +517,8 @@ dbus_bool_t _dbus_change_to_daemon_user (const char *user,
void _dbus_flush_caches (void);
+void _dbus_request_file_descriptor_limit (unsigned int limit);
+
/*
* replaces the term DBUS_PREFIX in configure_time_path by the
* current dbus installation directory. On unix this function is a noop