923a60
From fc7fdb72096d2baeec3238a0ef324569a05da4ae Mon Sep 17 00:00:00 2001
923a60
From: Lennart Poettering <lennart@poettering.net>
923a60
Date: Fri, 13 Nov 2015 17:13:55 +0100
923a60
Subject: [PATCH] core: add new DefaultTasksMax= setting for system.conf
923a60
923a60
This allows initializing the TasksMax= setting of all units by default
923a60
to some fixed value, instead of leaving it at infinity as before.
923a60
923a60
Cherry-picked from: 0af20ea2ee2af2bcf2258e7a8e1a13181a6a75d6
923a60
Related: #1337244
923a60
---
923a60
 man/systemd-system.conf.xml           | 13 ++++++++++++-
923a60
 man/systemd.resource-control.xml      |  5 ++++-
923a60
 src/core/dbus-manager.c               |  1 +
923a60
 src/core/load-fragment-gperf.gperf.m4 |  2 +-
923a60
 src/core/load-fragment.c              |  7 ++++---
923a60
 src/core/main.c                       |  3 +++
923a60
 src/core/manager.h                    |  1 +
923a60
 src/core/system.conf                  |  2 ++
923a60
 src/core/unit.c                       |  3 +++
923a60
 src/shared/cgroup-util.c              |  3 ++-
923a60
 10 files changed, 33 insertions(+), 7 deletions(-)
923a60
923a60
diff --git a/man/systemd-system.conf.xml b/man/systemd-system.conf.xml
923a60
index d367ccd130..53e8ff665a 100644
923a60
--- a/man/systemd-system.conf.xml
923a60
+++ b/man/systemd-system.conf.xml
923a60
@@ -1,4 +1,4 @@
923a60
- 
923a60
+ 
923a60
 
923a60
   "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
923a60
 
923a60
@@ -319,6 +319,17 @@
923a60
         for details on the per-unit settings.</para></listitem>
923a60
       </varlistentry>
923a60
 
923a60
+      <varlistentry>
923a60
+        <term><varname>DefaultTasksMax=</varname></term>
923a60
+
923a60
+        <listitem><para>Configure the default value for the per-unit
923a60
+        <varname>TasksMax=</varname> setting. See
923a60
+        <citerefentry><refentrytitle>systemd.resource-control</refentrytitle><manvolnum>5</manvolnum></citerefentry>
923a60
+        for details. This setting applies to all unit types that
923a60
+        support resource control settings, with the exception of slice
923a60
+        units.</para></listitem>
923a60
+      </varlistentry>
923a60
+
923a60
       <varlistentry>
923a60
         <term><varname>DefaultLimitCPU=</varname></term>
923a60
         <term><varname>DefaultLimitFSIZE=</varname></term>
923a60
diff --git a/man/systemd.resource-control.xml b/man/systemd.resource-control.xml
923a60
index 6b9329bbee..217105ee5a 100644
923a60
--- a/man/systemd.resource-control.xml
923a60
+++ b/man/systemd.resource-control.xml
923a60
@@ -231,7 +231,10 @@
923a60
           see 
923a60
           url="https://www.kernel.org/doc/Documentation/cgroups/pids.txt">pids.txt</ulink>.</para>
923a60
 
923a60
-          <para>Implies <literal>TasksAccounting=true</literal>.</para>
923a60
+          <para>Implies <literal>TasksAccounting=true</literal>. The
923a60
+          system default for this setting may be controlled with
923a60
+          <varname>DefaultTasksMax=</varname> in
923a60
+          <citerefentry><refentrytitle>systemd-system.conf</refentrytitle><manvolnum>5</manvolnum></citerefentry>.</para>
923a60
         </listitem>
923a60
       </varlistentry>
923a60
 
923a60
diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c
923a60
index 7ba1b519ea..c92f8c6bfc 100644
923a60
--- a/src/core/dbus-manager.c
923a60
+++ b/src/core/dbus-manager.c
923a60
@@ -2039,6 +2039,7 @@ const sd_bus_vtable bus_manager_vtable[] = {
923a60
         SD_BUS_WRITABLE_PROPERTY("ShutdownWatchdogUSec", "t", bus_property_get_usec, bus_property_set_usec, offsetof(Manager, shutdown_watchdog), 0),
923a60
         SD_BUS_PROPERTY("ControlGroup", "s", NULL, offsetof(Manager, cgroup_root), 0),
923a60
         SD_BUS_PROPERTY("SystemState", "s", property_get_system_state, 0, 0),
923a60
+        SD_BUS_PROPERTY("DefaultTasksMax", "t", NULL, offsetof(Manager, default_tasks_max), SD_BUS_VTABLE_PROPERTY_CONST),
923a60
 
923a60
         SD_BUS_METHOD("GetUnit", "s", "o", method_get_unit, SD_BUS_VTABLE_UNPRIVILEGED),
923a60
         SD_BUS_METHOD("GetUnitByPID", "u", "o", method_get_unit_by_pid, SD_BUS_VTABLE_UNPRIVILEGED),
923a60
diff --git a/src/core/load-fragment-gperf.gperf.m4 b/src/core/load-fragment-gperf.gperf.m4
923a60
index 26e4c618ee..b2fe627af3 100644
923a60
--- a/src/core/load-fragment-gperf.gperf.m4
923a60
+++ b/src/core/load-fragment-gperf.gperf.m4
923a60
@@ -126,7 +126,7 @@ $1.BlockIODeviceWeight,          config_parse_blockio_device_weight, 0,
923a60
 $1.BlockIOReadBandwidth,         config_parse_blockio_bandwidth,     0,                             offsetof($1, cgroup_context)
923a60
 $1.BlockIOWriteBandwidth,        config_parse_blockio_bandwidth,     0,                             offsetof($1, cgroup_context)
923a60
 $1.TasksAccounting,              config_parse_bool,                  0,                             offsetof($1, cgroup_context.tasks_accounting)
923a60
-$1.TasksMax,                     config_parse_tasks_max,             0,                             offsetof($1, cgroup_context)
923a60
+$1.TasksMax,                     config_parse_tasks_max,             0,                             offsetof($1, cgroup_context.tasks_max)
923a60
 $1.Delegate,                     config_parse_bool,                  0,                             offsetof($1, cgroup_context.delegate)'
923a60
 )m4_dnl
923a60
 Unit.Description,                config_parse_unit_string_printf,    0,                             offsetof(Unit, description)
923a60
diff --git a/src/core/load-fragment.c b/src/core/load-fragment.c
923a60
index 7d2e737d05..c1ffee2c7e 100644
923a60
--- a/src/core/load-fragment.c
923a60
+++ b/src/core/load-fragment.c
923a60
@@ -3081,12 +3081,11 @@ int config_parse_tasks_max(
923a60
                 void *data,
923a60
                 void *userdata) {
923a60
 
923a60
-        CGroupContext *c = data;
923a60
-        uint64_t u;
923a60
+        uint64_t *tasks_max = data, u;
923a60
         int r;
923a60
 
923a60
         if (isempty(rvalue) || streq(rvalue, "infinity")) {
923a60
-                c->tasks_max = (uint64_t) -1;
923a60
+                *tasks_max = (uint64_t) -1;
923a60
                 return 0;
923a60
         }
923a60
 
923a60
@@ -3096,6 +3095,8 @@ int config_parse_tasks_max(
923a60
                 return 0;
923a60
         }
923a60
 
923a60
+        *tasks_max = u;
923a60
+
923a60
         return 0;
923a60
 }
923a60
 
923a60
diff --git a/src/core/main.c b/src/core/main.c
923a60
index aca05a5358..50c9714f78 100644
923a60
--- a/src/core/main.c
923a60
+++ b/src/core/main.c
923a60
@@ -118,6 +118,7 @@ static bool arg_default_blockio_accounting = false;
923a60
 static bool arg_default_memory_accounting = false;
923a60
 static EmergencyAction arg_cad_burst_action = EMERGENCY_ACTION_REBOOT_FORCE;
923a60
 static bool arg_default_tasks_accounting = false;
923a60
+static uint64_t arg_default_tasks_max = (uint64_t) -1;
923a60
 
923a60
 static void nop_handler(int sig) {}
923a60
 
923a60
@@ -678,6 +679,7 @@ static int parse_config_file(void) {
923a60
                 { "Manager", "DefaultMemoryAccounting",   config_parse_bool,             0, &arg_default_memory_accounting         },
923a60
                 { "Manager", "CtrlAltDelBurstAction",     config_parse_emergency_action, 0, &arg_cad_burst_action                  },
923a60
                 { "Manager", "DefaultTasksAccounting",    config_parse_bool,             0, &arg_default_tasks_accounting          },
923a60
+                { "Manager", "DefaultTasksMax",           config_parse_tasks_max,        0, &arg_default_tasks_max                 },
923a60
                 {}
923a60
         };
923a60
 
923a60
@@ -1688,6 +1690,7 @@ int main(int argc, char *argv[]) {
923a60
         m->default_blockio_accounting = arg_default_blockio_accounting;
923a60
         m->default_memory_accounting = arg_default_memory_accounting;
923a60
         m->default_tasks_accounting = arg_default_tasks_accounting;
923a60
+        m->default_tasks_max = arg_default_tasks_max;
923a60
         m->runtime_watchdog = arg_runtime_watchdog;
923a60
         m->shutdown_watchdog = arg_shutdown_watchdog;
923a60
 
923a60
diff --git a/src/core/manager.h b/src/core/manager.h
923a60
index 96dcd83dc0..e91e7bd8b3 100644
923a60
--- a/src/core/manager.h
923a60
+++ b/src/core/manager.h
923a60
@@ -263,6 +263,7 @@ struct Manager {
923a60
         bool default_blockio_accounting;
923a60
         bool default_tasks_accounting;
923a60
 
923a60
+        uint64_t default_tasks_max;
923a60
         usec_t default_timer_accuracy_usec;
923a60
 
923a60
         struct rlimit *rlimit[_RLIMIT_MAX];
923a60
diff --git a/src/core/system.conf b/src/core/system.conf
923a60
index a11f599038..91ef01cd0f 100644
923a60
--- a/src/core/system.conf
923a60
+++ b/src/core/system.conf
923a60
@@ -40,6 +40,8 @@
923a60
 #DefaultCPUAccounting=no
923a60
 #DefaultBlockIOAccounting=no
923a60
 #DefaultMemoryAccounting=no
923a60
+#DefaultTasksAccounting=no
923a60
+#DefaultTasksMax=
923a60
 #DefaultLimitCPU=
923a60
 #DefaultLimitFSIZE=
923a60
 #DefaultLimitDATA=
923a60
diff --git a/src/core/unit.c b/src/core/unit.c
923a60
index 2fcb4fbf07..6a2ad6ed38 100644
923a60
--- a/src/core/unit.c
923a60
+++ b/src/core/unit.c
923a60
@@ -127,6 +127,9 @@ static void unit_init(Unit *u) {
923a60
                 cc->blockio_accounting = u->manager->default_blockio_accounting;
923a60
                 cc->memory_accounting = u->manager->default_memory_accounting;
923a60
                 cc->tasks_accounting = u->manager->default_tasks_accounting;
923a60
+
923a60
+                if (u->type != UNIT_SLICE)
923a60
+                        cc->tasks_max = u->manager->default_tasks_max;
923a60
         }
923a60
 
923a60
         ec = unit_get_exec_context(u);
923a60
diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c
923a60
index cf757d2b23..c5d9e4bb58 100644
923a60
--- a/src/shared/cgroup-util.c
923a60
+++ b/src/shared/cgroup-util.c
923a60
@@ -1606,7 +1606,8 @@ static const char mask_names[] =
923a60
         "cpuacct\0"
923a60
         "blkio\0"
923a60
         "memory\0"
923a60
-        "devices\0";
923a60
+        "devices\0"
923a60
+        "pids\0";
923a60
 
923a60
 int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path) {
923a60
         CGroupControllerMask bit = 1;