]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: introduce unit_defaults_init() common initialization helper 29130/head
authorLennart Poettering <lennart@poettering.net>
Fri, 8 Sep 2023 12:26:17 +0000 (14:26 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 8 Sep 2023 13:25:05 +0000 (15:25 +0200)
THis adds a helper for initializing UnitDefaults to our default values.
Previously we'd do that differently in two different locations. Let's
unify this in one, and apply the exact same settings at both as
defaults.

src/core/main.c
src/core/manager.c
src/core/manager.h

index 54511be6d8a3fa1f01ca0995cb43fb9519e4ddb9..d10e6d4833b183ae25e5ad0b7ea6c3800d29fbc3 100644 (file)
 #include <sanitizer/lsan_interface.h>
 #endif
 
-#define DEFAULT_TASKS_MAX ((TasksMax) { 15U, 100U }) /* 15% */
-
 static enum {
         ACTION_RUN,
         ACTION_HELP,
@@ -2517,37 +2515,8 @@ static void reset_arguments(void) {
         arg_service_watchdogs = true;
 
         unit_defaults_done(&arg_defaults);
+        unit_defaults_init(&arg_defaults, arg_runtime_scope);
 
-        arg_defaults = (UnitDefaults) {
-                .std_output = EXEC_OUTPUT_JOURNAL,
-                .std_error = EXEC_OUTPUT_INHERIT,
-                .restart_usec = DEFAULT_RESTART_USEC,
-                .timeout_start_usec = manager_default_timeout(arg_runtime_scope),
-                .timeout_stop_usec = manager_default_timeout(arg_runtime_scope),
-                .timeout_abort_usec = manager_default_timeout(arg_runtime_scope),
-                .timeout_abort_set = false,
-                .device_timeout_usec = manager_default_timeout(arg_runtime_scope),
-                .start_limit_interval = DEFAULT_START_LIMIT_INTERVAL,
-                .start_limit_burst = DEFAULT_START_LIMIT_BURST,
-
-                /* On 4.15+ with unified hierarchy, CPU accounting is essentially free as it doesn't require the CPU
-                 * controller to be enabled, so the default is to enable it unless we got told otherwise. */
-                .cpu_accounting = cpu_accounting_is_cheap(),
-                .memory_accounting = MEMORY_ACCOUNTING_DEFAULT,
-                .io_accounting = false,
-                .blockio_accounting = false,
-                .tasks_accounting = true,
-                .ip_accounting = false,
-
-                .tasks_max = DEFAULT_TASKS_MAX,
-                .timer_accuracy_usec = 1 * USEC_PER_MINUTE,
-
-                .memory_pressure_watch = CGROUP_PRESSURE_WATCH_AUTO,
-                .memory_pressure_threshold_usec = MEMORY_PRESSURE_DEFAULT_THRESHOLD_USEC,
-
-                .oom_policy = OOM_STOP,
-                .oom_score_adjust_set = false,
-        };
         arg_runtime_watchdog = 0;
         arg_reboot_watchdog = 10 * USEC_PER_MINUTE;
         arg_kexec_watchdog = 0;
index c2c689f01fce72391ac09b24468ff3ae265abf87..3bd27cba9ce191ef794fc793e9f4221ea6b9b78d 100644 (file)
 /* How many units and jobs to process of the bus queue before returning to the event loop. */
 #define MANAGER_BUS_MESSAGE_BUDGET 100U
 
+#define DEFAULT_TASKS_MAX ((TasksMax) { 15U, 100U }) /* 15% */
+
 static int manager_dispatch_notify_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
 static int manager_dispatch_cgroups_agent_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
 static int manager_dispatch_signal_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata);
@@ -872,20 +874,6 @@ int manager_new(RuntimeScope runtime_scope, ManagerTestRunFlags test_run_flags,
 
                 .status_unit_format = STATUS_UNIT_FORMAT_DEFAULT,
 
-                .defaults = {
-                        .timer_accuracy_usec = USEC_PER_MINUTE,
-                        .memory_accounting = MEMORY_ACCOUNTING_DEFAULT,
-                        .tasks_accounting = true,
-                        .tasks_max = TASKS_MAX_UNSET,
-                        .timeout_start_usec = manager_default_timeout(runtime_scope),
-                        .timeout_stop_usec = manager_default_timeout(runtime_scope),
-                        .restart_usec = DEFAULT_RESTART_USEC,
-                        .device_timeout_usec = manager_default_timeout(runtime_scope),
-                        .oom_policy = OOM_STOP,
-                        .memory_pressure_watch = CGROUP_PRESSURE_WATCH_AUTO,
-                        .memory_pressure_threshold_usec = USEC_INFINITY,
-                },
-
                 .original_log_level = -1,
                 .original_log_target = _LOG_TARGET_INVALID,
 
@@ -920,6 +908,8 @@ int manager_new(RuntimeScope runtime_scope, ManagerTestRunFlags test_run_flags,
                 },
         };
 
+        unit_defaults_init(&m->defaults, runtime_scope);
+
 #if ENABLE_EFI
         if (MANAGER_IS_SYSTEM(m) && detect_container() <= 0)
                 boot_timestamps(m->timestamps + MANAGER_TIMESTAMP_USERSPACE,
@@ -4886,6 +4876,43 @@ ManagerTimestamp manager_timestamp_initrd_mangle(ManagerTimestamp s) {
         return s;
 }
 
+void unit_defaults_init(UnitDefaults *defaults, RuntimeScope scope) {
+        assert(defaults);
+        assert(scope >= 0);
+        assert(scope < _RUNTIME_SCOPE_MAX);
+
+        *defaults = (UnitDefaults) {
+                .std_output = EXEC_OUTPUT_JOURNAL,
+                .std_error = EXEC_OUTPUT_INHERIT,
+                .restart_usec = DEFAULT_RESTART_USEC,
+                .timeout_start_usec = manager_default_timeout(scope),
+                .timeout_stop_usec = manager_default_timeout(scope),
+                .timeout_abort_usec = manager_default_timeout(scope),
+                .timeout_abort_set = false,
+                .device_timeout_usec = manager_default_timeout(scope),
+                .start_limit_interval = DEFAULT_START_LIMIT_INTERVAL,
+                .start_limit_burst = DEFAULT_START_LIMIT_BURST,
+
+                /* On 4.15+ with unified hierarchy, CPU accounting is essentially free as it doesn't require the CPU
+                 * controller to be enabled, so the default is to enable it unless we got told otherwise. */
+                .cpu_accounting = cpu_accounting_is_cheap(),
+                .memory_accounting = MEMORY_ACCOUNTING_DEFAULT,
+                .io_accounting = false,
+                .blockio_accounting = false,
+                .tasks_accounting = true,
+                .ip_accounting = false,
+
+                .tasks_max = DEFAULT_TASKS_MAX,
+                .timer_accuracy_usec = 1 * USEC_PER_MINUTE,
+
+                .memory_pressure_watch = CGROUP_PRESSURE_WATCH_AUTO,
+                .memory_pressure_threshold_usec = MEMORY_PRESSURE_DEFAULT_THRESHOLD_USEC,
+
+                .oom_policy = OOM_STOP,
+                .oom_score_adjust_set = false,
+        };
+}
+
 void unit_defaults_done(UnitDefaults *defaults) {
         assert(defaults);
 
index b0ba5c5062e64ae79813413b352ff63838f14f2a..354af5a8be362df1bb08349337919da872f469cd 100644 (file)
@@ -613,4 +613,5 @@ int manager_override_watchdog_pretimeout_governor(Manager *m, const char *govern
 const char* oom_policy_to_string(OOMPolicy i) _const_;
 OOMPolicy oom_policy_from_string(const char *s) _pure_;
 
+void unit_defaults_init(UnitDefaults *defaults, RuntimeScope scope);
 void unit_defaults_done(UnitDefaults *defaults);