summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2023-09-08 14:06:42 +0200
committerLennart Poettering <lennart@poettering.net>2023-09-08 15:25:05 +0200
commitbfb27b06da7b262b8323745703ebc4599cc3d485 (patch)
tree53513463a23ebd629639e62bfdc2dc081cc8b97f
parentmanager: move various fields that declare unit defaults into a new structure ... (diff)
downloadsystemd-bfb27b06da7b262b8323745703ebc4599cc3d485.tar.xz
systemd-bfb27b06da7b262b8323745703ebc4599cc3d485.zip
manager: add manager_set_unit_defaults() call that sets all unit defaults in one go
No change in behaviour. Just some refactoring.
-rw-r--r--src/basic/rlimit-util.c21
-rw-r--r--src/basic/rlimit-util.h2
-rw-r--r--src/core/main.c51
-rw-r--r--src/core/manager.c69
-rw-r--r--src/core/manager.h4
5 files changed, 93 insertions, 54 deletions
diff --git a/src/basic/rlimit-util.c b/src/basic/rlimit-util.c
index 259553b6f6..c1f0b2b974 100644
--- a/src/basic/rlimit-util.c
+++ b/src/basic/rlimit-util.c
@@ -362,6 +362,27 @@ void rlimit_free_all(struct rlimit **rl) {
free_many((void**) rl, _RLIMIT_MAX);
}
+int rlimit_copy_all(struct rlimit* target[static _RLIMIT_MAX], struct rlimit* const source[static _RLIMIT_MAX]) {
+ struct rlimit* copy[_RLIMIT_MAX] = {};
+
+ assert(target);
+ assert(source);
+
+ for (int i = 0; i < _RLIMIT_MAX; i++) {
+ if (!source[i])
+ continue;
+
+ copy[i] = newdup(struct rlimit, source[i], 1);
+ if (!copy[i]) {
+ rlimit_free_all(copy);
+ return -ENOMEM;
+ }
+ }
+
+ memcpy(target, copy, sizeof(struct rlimit*) * _RLIMIT_MAX);
+ return 0;
+}
+
int rlimit_nofile_bump(int limit) {
int r;
diff --git a/src/basic/rlimit-util.h b/src/basic/rlimit-util.h
index 59bc066798..202c3fdc44 100644
--- a/src/basic/rlimit-util.h
+++ b/src/basic/rlimit-util.h
@@ -4,6 +4,7 @@
#include <sys/resource.h>
#include "macro.h"
+#include "missing_resource.h"
const char *rlimit_to_string(int i) _const_;
int rlimit_from_string(const char *s) _pure_;
@@ -17,6 +18,7 @@ int rlimit_parse(int resource, const char *val, struct rlimit *ret);
int rlimit_format(const struct rlimit *rl, char **ret);
+int rlimit_copy_all(struct rlimit* target[static _RLIMIT_MAX], struct rlimit* const source[static _RLIMIT_MAX]);
void rlimit_free_all(struct rlimit **rl);
#define RLIMIT_MAKE_CONST(lim) ((struct rlimit) { lim, lim })
diff --git a/src/core/main.c b/src/core/main.c
index bd9e137871..54511be6d8 100644
--- a/src/core/main.c
+++ b/src/core/main.c
@@ -735,43 +735,26 @@ static int parse_config_file(void) {
}
static void set_manager_defaults(Manager *m) {
+ int r;
assert(m);
- /* Propagates the various default unit property settings into the manager object, i.e. properties that do not
- * affect the manager itself, but are just what newly allocated units will have set if they haven't set
- * anything else. (Also see set_manager_settings() for the settings that affect the manager's own behaviour) */
-
- m->defaults.timer_accuracy_usec = arg_defaults.timer_accuracy_usec;
- m->defaults.std_output = arg_defaults.std_output;
- m->defaults.std_error = arg_defaults.std_error;
- m->defaults.timeout_start_usec = arg_defaults.timeout_start_usec;
- m->defaults.timeout_stop_usec = arg_defaults.timeout_stop_usec;
- m->defaults.timeout_abort_usec = arg_defaults.timeout_abort_usec;
- m->defaults.timeout_abort_set = arg_defaults.timeout_abort_set;
- m->defaults.device_timeout_usec = arg_defaults.device_timeout_usec;
- m->defaults.restart_usec = arg_defaults.restart_usec;
- m->defaults.start_limit_interval = arg_defaults.start_limit_interval;
- m->defaults.start_limit_burst = arg_defaults.start_limit_burst;
- m->defaults.cpu_accounting = arg_defaults.cpu_accounting;
- m->defaults.io_accounting = arg_defaults.io_accounting;
- m->defaults.ip_accounting = arg_defaults.ip_accounting;
- m->defaults.blockio_accounting = arg_defaults.blockio_accounting;
- m->defaults.memory_accounting = arg_defaults.memory_accounting;
- m->defaults.tasks_accounting = arg_defaults.tasks_accounting;
- m->defaults.tasks_max = arg_defaults.tasks_max;
- m->defaults.memory_pressure_watch = arg_defaults.memory_pressure_watch;
- m->defaults.memory_pressure_threshold_usec = arg_defaults.memory_pressure_threshold_usec;
- m->defaults.oom_policy = arg_defaults.oom_policy;
- m->defaults.oom_score_adjust_set = arg_defaults.oom_score_adjust_set;
- m->defaults.oom_score_adjust = arg_defaults.oom_score_adjust;
-
- (void) manager_set_default_smack_process_label(m, arg_defaults.smack_process_label);
-
- (void) manager_set_default_rlimits(m, arg_defaults.rlimit);
-
- (void) manager_default_environment(m);
- (void) manager_transient_environment_add(m, arg_default_environment);
+ /* Propagates the various default unit property settings into the manager object, i.e. properties
+ * that do not affect the manager itself, but are just what newly allocated units will have set if
+ * they haven't set anything else. (Also see set_manager_settings() for the settings that affect the
+ * manager's own behaviour) */
+
+ r = manager_set_unit_defaults(m, &arg_defaults);
+ if (r < 0)
+ log_warning_errno(r, "Failed to set manager defaults, ignoring: %m");
+
+ r = manager_default_environment(m);
+ if (r < 0)
+ log_warning_errno(r, "Failed to set manager default environment, ignoring: %m");
+
+ r = manager_transient_environment_add(m, arg_default_environment);
+ if (r < 0)
+ log_warning_errno(r, "Failed to add to transient environment, ignoring: %m");
}
static void set_manager_settings(Manager *m) {
diff --git a/src/core/manager.c b/src/core/manager.c
index 45409ab6e4..c2c689f01f 100644
--- a/src/core/manager.c
+++ b/src/core/manager.c
@@ -4117,32 +4117,67 @@ int manager_get_effective_environment(Manager *m, char ***ret) {
return 0;
}
-int manager_set_default_smack_process_label(Manager *m, const char *label) {
+int manager_set_unit_defaults(Manager *m, const UnitDefaults *defaults) {
+ _cleanup_free_ char *label = NULL;
+ struct rlimit *rlimit[_RLIMIT_MAX];
+ int r;
+
assert(m);
+ assert(defaults);
+ if (streq_ptr(defaults->smack_process_label, "/"))
+ label = NULL;
+ else {
+ const char *l = defaults->smack_process_label;
#ifdef SMACK_DEFAULT_PROCESS_LABEL
- if (!label)
- return free_and_strdup(&m->defaults.smack_process_label, SMACK_DEFAULT_PROCESS_LABEL);
+ if (!l)
+ l = SMACK_DEFAULT_PROCESS_LABEL;
#endif
- if (streq_ptr(label, "/"))
- return free_and_strdup(&m->defaults.smack_process_label, NULL);
+ if (l) {
+ label = strdup(l);
+ if (!label)
+ return -ENOMEM;
+ } else
+ label = NULL;
+ }
- return free_and_strdup(&m->defaults.smack_process_label, label);
-}
+ r = rlimit_copy_all(rlimit, defaults->rlimit);
+ if (r < 0)
+ return r;
-int manager_set_default_rlimits(Manager *m, struct rlimit **default_rlimit) {
- assert(m);
+ m->defaults.std_output = defaults->std_output;
+ m->defaults.std_error = defaults->std_error;
- for (unsigned i = 0; i < _RLIMIT_MAX; i++) {
- m->defaults.rlimit[i] = mfree(m->defaults.rlimit[i]);
+ m->defaults.restart_usec = defaults->restart_usec;
+ m->defaults.timeout_start_usec = defaults->timeout_start_usec;
+ m->defaults.timeout_stop_usec = defaults->timeout_stop_usec;
+ m->defaults.timeout_abort_usec = defaults->timeout_abort_usec;
+ m->defaults.timeout_abort_set = defaults->timeout_abort_set;
+ m->defaults.device_timeout_usec = defaults->device_timeout_usec;
- if (!default_rlimit[i])
- continue;
+ m->defaults.start_limit_interval = defaults->start_limit_interval;
+ m->defaults.start_limit_burst = defaults->start_limit_burst;
- m->defaults.rlimit[i] = newdup(struct rlimit, default_rlimit[i], 1);
- if (!m->defaults.rlimit[i])
- return log_oom();
- }
+ m->defaults.cpu_accounting = defaults->cpu_accounting;
+ m->defaults.memory_accounting = defaults->memory_accounting;
+ m->defaults.io_accounting = defaults->io_accounting;
+ m->defaults.blockio_accounting = defaults->blockio_accounting;
+ m->defaults.tasks_accounting = defaults->tasks_accounting;
+ m->defaults.ip_accounting = defaults->ip_accounting;
+
+ m->defaults.tasks_max = defaults->tasks_max;
+ m->defaults.timer_accuracy_usec = defaults->timer_accuracy_usec;
+
+ m->defaults.oom_policy = defaults->oom_policy;
+ m->defaults.oom_score_adjust = defaults->oom_score_adjust;
+ m->defaults.oom_score_adjust_set = defaults->oom_score_adjust_set;
+
+ m->defaults.memory_pressure_watch = defaults->memory_pressure_watch;
+ m->defaults.memory_pressure_threshold_usec = defaults->memory_pressure_threshold_usec;
+
+ free_and_replace(m->defaults.smack_process_label, label);
+ rlimit_free_all(m->defaults.rlimit);
+ memcpy(m->defaults.rlimit, rlimit, sizeof(struct rlimit*) * _RLIMIT_MAX);
return 0;
}
diff --git a/src/core/manager.h b/src/core/manager.h
index 2309914a80..b0ba5c5062 100644
--- a/src/core/manager.h
+++ b/src/core/manager.h
@@ -537,9 +537,7 @@ int manager_transient_environment_add(Manager *m, char **plus);
int manager_client_environment_modify(Manager *m, char **minus, char **plus);
int manager_get_effective_environment(Manager *m, char ***ret);
-int manager_set_default_smack_process_label(Manager *m, const char *label);
-
-int manager_set_default_rlimits(Manager *m, struct rlimit **default_rlimit);
+int manager_set_unit_defaults(Manager *m, const UnitDefaults *defaults);
void manager_trigger_run_queue(Manager *m);