diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/dbus-execute.c | 16 | ||||
-rw-r--r-- | src/dbus-execute.h | 6 | ||||
-rw-r--r-- | src/execute.c | 30 | ||||
-rw-r--r-- | src/execute.h | 4 | ||||
-rw-r--r-- | src/load-fragment.c | 16 | ||||
-rw-r--r-- | src/missing.h | 23 |
6 files changed, 65 insertions, 30 deletions
diff --git a/src/dbus-execute.c b/src/dbus-execute.c index 4f8a3da0d1..cdc15b25ce 100644 --- a/src/dbus-execute.c +++ b/src/dbus-execute.c @@ -33,7 +33,7 @@ DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_kill_mode, kill_mode, KillMod DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_input, exec_input, ExecInput); DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_execute_append_output, exec_output, ExecOutput); -int bus_execute_append_oom_adjust(Manager *m, DBusMessageIter *i, const char *property, void *data) { +int bus_execute_append_oom_score_adjust(Manager *m, DBusMessageIter *i, const char *property, void *data) { ExecContext *c = data; int32_t n; @@ -42,15 +42,23 @@ int bus_execute_append_oom_adjust(Manager *m, DBusMessageIter *i, const char *pr assert(property); assert(c); - if (c->oom_adjust_set) - n = c->oom_adjust; + if (c->oom_score_adjust_set) + n = c->oom_score_adjust; else { char *t; n = 0; - if (read_one_line_file("/proc/self/oom_adj", &t) >= 0) { + if (read_one_line_file("/proc/self/oom_score_adj", &t) >= 0) { safe_atoi(t, &n); free(t); + } else if (read_one_line_file("/proc/self/oom_adj", &t) >= 0) { + safe_atoi(t, &n); + free(t); + + if (n == OOM_ADJUST_MAX) + n = OOM_SCORE_ADJ_MAX; + else + n = (n * OOM_SCORE_ADJ_MAX) / -OOM_DISABLE; } } diff --git a/src/dbus-execute.h b/src/dbus-execute.h index 03a474ede2..f1814315f7 100644 --- a/src/dbus-execute.h +++ b/src/dbus-execute.h @@ -54,7 +54,7 @@ " <property name=\"LimitRTTIME\" type=\"t\" access=\"read\"/>\n" \ " <property name=\"WorkingDirectory\" type=\"s\" access=\"read\"/>\n" \ " <property name=\"RootDirectory\" type=\"s\" access=\"read\"/>\n" \ - " <property name=\"OOMAdjust\" type=\"i\" access=\"read\"/>\n" \ + " <property name=\"OOMScoreAdjust\" type=\"i\" access=\"read\"/>\n" \ " <property name=\"Nice\" type=\"i\" access=\"read\"/>\n" \ " <property name=\"IOScheduling\" type=\"i\" access=\"read\"/>\n" \ " <property name=\"CPUSchedulingPolicy\" type=\"i\" access=\"read\"/>\n" \ @@ -111,7 +111,7 @@ { interface, "LimitRTTIME", bus_execute_append_rlimits, "t", &(context) }, \ { interface, "WorkingDirectory", bus_property_append_string, "s", (context).working_directory }, \ { interface, "RootDirectory", bus_property_append_string, "s", (context).root_directory }, \ - { interface, "OOMAdjust", bus_execute_append_oom_adjust, "i", &(context) }, \ + { interface, "OOMScoreAdjust", bus_execute_append_oom_score_adjust, "i", &(context) }, \ { interface, "Nice", bus_execute_append_nice, "i", &(context) }, \ { interface, "IOScheduling", bus_execute_append_ioprio, "i", &(context) }, \ { interface, "CPUSchedulingPolicy", bus_execute_append_cpu_sched_policy, "i", &(context) }, \ @@ -156,7 +156,7 @@ int bus_execute_append_output(Manager *m, DBusMessageIter *i, const char *property, void *data); int bus_execute_append_input(Manager *m, DBusMessageIter *i, const char *property, void *data); -int bus_execute_append_oom_adjust(Manager *m, DBusMessageIter *i, const char *property, void *data); +int bus_execute_append_oom_score_adjust(Manager *m, DBusMessageIter *i, const char *property, void *data); int bus_execute_append_nice(Manager *m, DBusMessageIter *i, const char *property, void *data); int bus_execute_append_ioprio(Manager *m, DBusMessageIter *i, const char *property, void *data); int bus_execute_append_cpu_sched_policy(Manager *m, DBusMessageIter *i, const char *property, void *data); diff --git a/src/execute.c b/src/execute.c index 2443d9c74d..755b4700fc 100644 --- a/src/execute.c +++ b/src/execute.c @@ -36,6 +36,7 @@ #include <pwd.h> #include <sys/mount.h> #include <linux/fs.h> +#include <linux/oom.h> #ifdef HAVE_PAM #include <security/pam_appl.h> @@ -52,6 +53,7 @@ #include "namespace.h" #include "tcpwrap.h" #include "exit-status.h" +#include "missing.h" /* This assumes there is a 'tty' group */ #define TTY_MODE 0620 @@ -1066,15 +1068,27 @@ int exec_spawn(ExecCommand *command, goto fail; } - if (context->oom_adjust_set) { + if (context->oom_score_adjust_set) { char t[16]; - snprintf(t, sizeof(t), "%i", context->oom_adjust); + snprintf(t, sizeof(t), "%i", context->oom_score_adjust); char_array_0(t); - if (write_one_line_file("/proc/self/oom_adj", t) < 0) { - r = EXIT_OOM_ADJUST; - goto fail; + if (write_one_line_file("/proc/self/oom_score_adj", t) < 0) { + /* Compatibility with Linux <= 2.6.35 */ + + int adj; + + adj = (context->oom_score_adjust * -OOM_DISABLE) / OOM_SCORE_ADJ_MAX; + adj = CLAMP(adj, OOM_DISABLE, OOM_ADJUST_MAX); + + snprintf(t, sizeof(t), "%i", adj); + char_array_0(t); + + if (write_one_line_file("/proc/self/oom_adj", t) < 0) { + r = EXIT_OOM_ADJUST; + goto fail; + } } } @@ -1461,10 +1475,10 @@ void exec_context_dump(ExecContext *c, FILE* f, const char *prefix) { "%sNice: %i\n", prefix, c->nice); - if (c->oom_adjust_set) + if (c->oom_score_adjust_set) fprintf(f, - "%sOOMAdjust: %i\n", - prefix, c->oom_adjust); + "%sOOMScoreAdjust: %i\n", + prefix, c->oom_score_adjust); for (i = 0; i < RLIM_NLIMITS; i++) if (c->rlimit[i]) diff --git a/src/execute.h b/src/execute.h index 0dc5a1d252..31cce5d7d2 100644 --- a/src/execute.h +++ b/src/execute.h @@ -97,7 +97,7 @@ struct ExecContext { char *working_directory, *root_directory; mode_t umask; - int oom_adjust; + int oom_score_adjust; int nice; int ioprio; int cpu_sched_policy; @@ -153,7 +153,7 @@ struct ExecContext { * don't enter a trigger loop. */ bool same_pgrp; - bool oom_adjust_set:1; + bool oom_score_adjust_set:1; bool nice_set:1; bool ioprio_set:1; bool cpu_sched_set:1; diff --git a/src/load-fragment.c b/src/load-fragment.c index c9ed6798ae..636de8d103 100644 --- a/src/load-fragment.c +++ b/src/load-fragment.c @@ -299,7 +299,7 @@ static int config_parse_nice( return 0; } -static int config_parse_oom_adjust( +static int config_parse_oom_score_adjust( const char *filename, unsigned line, const char *section, @@ -317,17 +317,17 @@ static int config_parse_oom_adjust( assert(data); if ((r = safe_atoi(rvalue, &oa)) < 0) { - log_error("[%s:%u] Failed to parse OOM adjust value, ignoring: %s", filename, line, rvalue); + log_error("[%s:%u] Failed to parse the OOM score adjust value, ignoring: %s", filename, line, rvalue); return 0; } - if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) { - log_error("[%s:%u] OOM adjust value out of range, ignoring: %s", filename, line, rvalue); + if (oa < OOM_SCORE_ADJ_MIN || oa > OOM_SCORE_ADJ_MAX) { + log_error("[%s:%u] OOM score adjust value out of range, ignoring: %s", filename, line, rvalue); return 0; } - c->oom_adjust = oa; - c->oom_adjust_set = true; + c->oom_score_adjust = oa; + c->oom_score_adjust_set = true; return 0; } @@ -1425,7 +1425,7 @@ static void dump_items(FILE *f, const ConfigItem *items) { { config_parse_path, "PATH" }, { config_parse_strv, "STRING [...]" }, { config_parse_nice, "NICE" }, - { config_parse_oom_adjust, "OOMADJUST" }, + { config_parse_oom_score_adjust, "OOMSCOREADJUST" }, { config_parse_io_class, "IOCLASS" }, { config_parse_io_priority, "IOPRIORITY" }, { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" }, @@ -1515,7 +1515,7 @@ static int load_from_path(Unit *u, const char *path) { { "Group", config_parse_string_printf, &(context).group, section }, \ { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \ { "Nice", config_parse_nice, &(context), section }, \ - { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \ + { "OOMScoreAdjust", config_parse_oom_score_adjust,&(context), section }, \ { "IOSchedulingClass", config_parse_io_class, &(context), section }, \ { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \ { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \ diff --git a/src/missing.h b/src/missing.h index eaeeeab19f..418dbb8c0d 100644 --- a/src/missing.h +++ b/src/missing.h @@ -28,6 +28,11 @@ #include <sys/syscall.h> #include <fcntl.h> #include <unistd.h> +#include <linux/oom.h> + +#ifdef HAVE_AUDIT +#include <libaudit.h> +#endif #include "macro.h" @@ -51,16 +56,24 @@ #define IP_FREEBIND 15 #endif -static inline int pivot_root(const char *new_root, const char *put_old) { - return syscall(SYS_pivot_root, new_root, put_old); -} +#ifndef OOM_SCORE_ADJ_MIN +#define OOM_SCORE_ADJ_MIN (-1000) +#endif + +#ifndef OOM_SCORE_ADJ_MAX +#define OOM_SCORE_ADJ_MAX 1000 +#endif #ifndef AUDIT_SERVICE_START -#define AUDIT_SERVICE_START 1130 /* Service (daemon) start */ +#define AUDIT_SERVICE_START 1130 /* Service (daemon) start */ #endif #ifndef AUDIT_SERVICE_STOP -#define AUDIT_SERVICE_STOP 1131 /* Service (daemon) stop */ +#define AUDIT_SERVICE_STOP 1131 /* Service (daemon) stop */ #endif +static inline int pivot_root(const char *new_root, const char *put_old) { + return syscall(SYS_pivot_root, new_root, put_old); +} + #endif |