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