803fb7
From a3b8feb9320f745f960fe8f7006183137f4969b1 Mon Sep 17 00:00:00 2001
803fb7
From: Lennart Poettering <lennart@poettering.net>
803fb7
Date: Mon, 2 Nov 2015 09:34:05 +0100
803fb7
Subject: [PATCH] core: bump net.unix.max_dgram_qlen really early during boot
803fb7
803fb7
Only that way it actually has an effect on all our sockets, including
803fb7
$NOTIFY_SOCKET.
803fb7
803fb7
(cherry picked from commit 19854865a877a3a4fa3d04550c15a99c0e1187ff)
803fb7
Related: #1267707
803fb7
---
803fb7
 src/core/main.c  | 36 ++++++++++++++++++++++++++++++++++++
803fb7
 src/shared/def.h |  3 +++
803fb7
 2 files changed, 39 insertions(+)
803fb7
803fb7
diff --git a/src/core/main.c b/src/core/main.c
803fb7
index 60ea36c3c..c9d8ce4a4 100644
803fb7
--- a/src/core/main.c
803fb7
+++ b/src/core/main.c
803fb7
@@ -1198,6 +1198,7 @@ static int status_welcome(void) {
803fb7
 
803fb7
 static int write_container_id(void) {
803fb7
         const char *c;
803fb7
+        int r;
803fb7
 
803fb7
         c = getenv("container");
803fb7
         if (isempty(c))
803fb7
@@ -1206,6 +1207,40 @@ static int write_container_id(void) {
803fb7
         return write_string_file("/run/systemd/container", c);
803fb7
 }
803fb7
 
803fb7
+static int bump_unix_max_dgram_qlen(void) {
803fb7
+        _cleanup_free_ char *qlen = NULL;
803fb7
+        unsigned long v;
803fb7
+        int r;
803fb7
+
803fb7
+        /* Let's bump the net.unix.max_dgram_qlen sysctl. The kernel
803fb7
+         * default of 16 is simply too low. We set the value really
803fb7
+         * really early during boot, so that it is actually applied to
803fb7
+         * all our sockets, including the $NOTIFY_SOCKET one. */
803fb7
+
803fb7
+        r = read_one_line_file("/proc/sys/net/unix/max_dgram_qlen", &qlen);
803fb7
+        if (r < 0)
803fb7
+                return log_warning_errno(r, "Failed to read AF_UNIX datagram queue length, ignoring: %m");
803fb7
+
803fb7
+        r = safe_atolu(qlen, &v);
803fb7
+        if (r < 0)
803fb7
+                return log_warning_errno(r, "Failed to parse AF_UNIX datagram queue length, ignoring: %m");
803fb7
+
803fb7
+        if (v >= DEFAULT_UNIX_MAX_DGRAM_QLEN)
803fb7
+                return 0;
803fb7
+
803fb7
+        free(qlen);
803fb7
+        qlen = NULL;
803fb7
+        if (asprintf(&qlen, "%lu\n", DEFAULT_UNIX_MAX_DGRAM_QLEN) < 0)
803fb7
+                return log_oom();
803fb7
+
803fb7
+        r = write_string_file("/proc/sys/net/unix/max_dgram_qlen", qlen);
803fb7
+        if (r < 0)
803fb7
+                return log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r,
803fb7
+                                      "Failed to bump AF_UNIX datagram queue length, ignoring: %m");
803fb7
+
803fb7
+        return 1;
803fb7
+}
803fb7
+
803fb7
 int main(int argc, char *argv[]) {
803fb7
         Manager *m = NULL;
803fb7
         int r, retval = EXIT_FAILURE;
803fb7
@@ -1571,6 +1606,7 @@ int main(int argc, char *argv[]) {
803fb7
                 hostname_setup();
803fb7
                 machine_id_setup(NULL);
803fb7
                 loopback_setup();
803fb7
+                bump_unix_max_dgram_qlen();
803fb7
 
803fb7
                 test_mtab();
803fb7
                 test_usr();
803fb7
diff --git a/src/shared/def.h b/src/shared/def.h
803fb7
index a3d9fcf38..76daf012d 100644
803fb7
--- a/src/shared/def.h
803fb7
+++ b/src/shared/def.h
803fb7
@@ -35,6 +35,9 @@
803fb7
  * the watchdog pings will keep the loop busy. */
803fb7
 #define DEFAULT_EXIT_USEC (30*USEC_PER_SEC)
803fb7
 
803fb7
+/* The default value for the net.unix.max_dgram_qlen sysctl */
803fb7
+#define DEFAULT_UNIX_MAX_DGRAM_QLEN 512UL
803fb7
+
803fb7
 #define SYSTEMD_CGROUP_CONTROLLER "name=systemd"
803fb7
 
803fb7
 #define SIGNALS_CRASH_HANDLER SIGSEGV,SIGILL,SIGFPE,SIGBUS,SIGQUIT,SIGABRT