[PATCH 2/2] allow system wide limits for services

Frederic Crozat fcrozat at suse.com
Wed Mar 21 10:03:40 PDT 2012


---
 src/main.c    |   28 ++++++++++++++++++++++++++++
 src/manager.c |    8 ++++++++
 src/manager.h |    2 ++
 src/service.c |    8 ++++++++
 4 files changed, 46 insertions(+), 0 deletions(-)

diff --git a/src/main.c b/src/main.c
index 7ae8841..f4295e4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -80,6 +80,7 @@ static char **arg_default_controllers = NULL;
 static char ***arg_join_controllers = NULL;
 static ExecOutput arg_default_std_output = EXEC_OUTPUT_JOURNAL;
 static ExecOutput arg_default_std_error = EXEC_OUTPUT_INHERIT;
+static struct rlimit *default_rlimit[RLIMIT_NLIMITS] = {};
 
 static FILE* serialization = NULL;
 
@@ -660,6 +661,22 @@ static int parse_config_file(void) {
                 { "Manager", "DefaultStandardOutput", config_parse_output,       0, &arg_default_std_output  },
                 { "Manager", "DefaultStandardError",  config_parse_output,       0, &arg_default_std_error   },
                 { "Manager", "JoinControllers",       config_parse_join_controllers, 0, &arg_join_controllers },
+                { "Manager", "LimitCPU",              config_parse_limit,        0, &default_rlimit[RLIMIT_CPU]},
+                { "Manager", "LimitFSIZE",            config_parse_limit,        0, &default_rlimit[RLIMIT_FSIZE]},
+                { "Manager", "LimitDATA",             config_parse_limit,        0, &default_rlimit[RLIMIT_DATA]},
+                { "Manager", "LimitSTACK",            config_parse_limit,        0, &default_rlimit[RLIMIT_STACK]},
+                { "Manager", "LimitCORE",             config_parse_limit,        0, &default_rlimit[RLIMIT_CORE]},
+                { "Manager", "LimitRSS",              config_parse_limit,        0, &default_rlimit[RLIMIT_RSS]},
+                { "Manager", "LimitNOFILE",           config_parse_limit,        0, &default_rlimit[RLIMIT_NOFILE]},
+                { "Manager", "LimitAS",               config_parse_limit,        0, &default_rlimit[RLIMIT_AS]},
+                { "Manager", "LimitNPROC",            config_parse_limit,        0, &default_rlimit[RLIMIT_NPROC]},
+                { "Manager", "LimitMEMLOCK",          config_parse_limit,        0, &default_rlimit[RLIMIT_MEMLOCK]},
+                { "Manager", "LimitLOCKS",            config_parse_limit,        0, &default_rlimit[RLIMIT_LOCKS]},
+                { "Manager", "LimitSIGPENDING",       config_parse_limit,        0, &default_rlimit[RLIMIT_SIGPENDING]},
+                { "Manager", "LimitMSGQUEUE",         config_parse_limit,        0, &default_rlimit[RLIMIT_MSGQUEUE]},
+                { "Manager", "LimitNICE",             config_parse_limit,        0, &default_rlimit[RLIMIT_NICE]},
+                { "Manager", "LimitRTPRIO",           config_parse_limit,        0, &default_rlimit[RLIMIT_RTPRIO]},
+                { "Manager", "LimitRTTIME",           config_parse_limit,        0, &default_rlimit[RLIMIT_RTTIME]},
                 { NULL, NULL, NULL, 0, NULL }
         };
 
@@ -1404,6 +1421,14 @@ int main(int argc, char *argv[]) {
         m->swap_auto = arg_swap_auto;
         m->default_std_output = arg_default_std_output;
         m->default_std_error = arg_default_std_error;
+        for (j = 0; j < RLIMIT_NLIMITS; j++) {
+                if (default_rlimit[j]) {
+                        newdup(m->rlimit[j],default_rlimit[j]);
+
+                        if (!m->rlimit[j])
+                                goto finish;
+                }
+        }
 
         if (dual_timestamp_is_set(&initrd_timestamp))
                 m->initrd_timestamp = initrd_timestamp;
@@ -1543,6 +1568,9 @@ finish:
         if (m)
                 manager_free(m);
 
+        for (j = 0; j < RLIMIT_NLIMITS; j++)
+                free (default_rlimit[j]);
+
         free(arg_default_unit);
         strv_free(arg_default_controllers);
         free_join_controllers();
diff --git a/src/manager.c b/src/manager.c
index 74bd740..da78901 100644
--- a/src/manager.c
+++ b/src/manager.c
@@ -456,6 +456,7 @@ static void manager_clear_jobs_and_units(Manager *m) {
 
 void manager_free(Manager *m) {
         UnitType c;
+        int i;
 
         assert(m);
 
@@ -501,6 +502,13 @@ void manager_free(Manager *m) {
         hashmap_free(m->cgroup_bondings);
         set_free_free(m->unit_path_cache);
 
+        for (i = 0; i < RLIMIT_NLIMITS; i++) {
+                if (m->rlimit[i]) {
+                        free (m->rlimit[i]);
+                        m->rlimit[i] = NULL;
+                }
+        }
+
         free(m);
 }
 
diff --git a/src/manager.h b/src/manager.h
index a9d08f0..5dc7e29 100644
--- a/src/manager.h
+++ b/src/manager.h
@@ -225,6 +225,8 @@ struct Manager {
 
         ExecOutput default_std_output, default_std_error;
 
+        struct rlimit *rlimit[RLIMIT_NLIMITS];
+
         /* non-zero if we are reloading or reexecuting, */
         int n_reloading;
 
diff --git a/src/service.c b/src/service.c
index 8b5c0b0..4f7ecd7 100644
--- a/src/service.c
+++ b/src/service.c
@@ -109,6 +109,7 @@ static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
 
 static void service_init(Unit *u) {
         Service *s = SERVICE(u);
+        int i;
 
         assert(u);
         assert(u->load_state == UNIT_STUB);
@@ -127,6 +128,13 @@ static void service_init(Unit *u) {
         s->guess_main_pid = true;
 
         exec_context_init(&s->exec_context);
+        for (i = 0; i < RLIMIT_NLIMITS; i++) {
+                if (UNIT(s)->manager->rlimit[i]) {
+                        newdup(s->exec_context.rlimit[i],UNIT(s)->manager->rlimit[i]);
+                        if (!s->exec_context.rlimit[i])
+                               return;
+                }
+        }
 
         RATELIMIT_INIT(s->start_limit, 10*USEC_PER_SEC, 5);
 
-- 
1.7.7


--=-VgnFJTuCW8NSvFo/Ahz8--



More information about the systemd-devel mailing list