[systemd-devel] [PATCH v2 3/3] WIP: service: add StartLimitInterval/StartLimitBurst/StartLimitAction

Michael Olbrich m.olbrich at pengutronix.de
Wed Feb 8 01:10:36 PST 2012


Signed-off-by: Michael Olbrich <m.olbrich at pengutronix.de>
---
changes in v2:
 - Directly set ratelimit.interval/ratelimit.burst
   There are some issues with that:
   The names are now inconsistent: ratelimit vs. start_limit, maybe we
   should call the variable ratelimit_action?
   setting StartLimitBurst=0 will trigger an assert. Where would I add
   range checking?

 src/dbus-service.c               |    7 +++++++
 src/load-fragment-gperf.gperf.m4 |    3 +++
 src/load-fragment.c              |    1 +
 src/load-fragment.h              |    1 +
 src/service.c                    |   37 ++++++++++++++++++++++++++++++++++++-
 src/service.h                    |   14 ++++++++++++++
 6 files changed, 62 insertions(+), 1 deletions(-)

diff --git a/src/dbus-service.c b/src/dbus-service.c
index fedfc1d..4e694b4 100644
--- a/src/dbus-service.c
+++ b/src/dbus-service.c
@@ -46,6 +46,9 @@
         "  <property name=\"WatchdogUSec\" type=\"t\" access=\"read\"/>\n" \
         "  <property name=\"WatchdogTimestamp\" type=\"t\" access=\"read\"/>\n" \
         "  <property name=\"WatchdogTimestampMonotonic\" type=\"t\" access=\"read\"/>\n" \
+        "  <property name=\"StartLimitInterval\" type=\"t\" access=\"read\"/>\n" \
+        "  <property name=\"StartLimitBurst\" type=\"u\" access=\"read\"/>\n" \
+        "  <property name=\"StartLimitAction\" type=\"s\" access=\"read\"/>\n" \
         BUS_EXEC_COMMAND_INTERFACE("ExecStartPre")                      \
         BUS_EXEC_COMMAND_INTERFACE("ExecStart")                         \
         BUS_EXEC_COMMAND_INTERFACE("ExecStartPost")                     \
@@ -101,6 +104,7 @@ static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_service_append_type, service_type, Se
 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_service_append_restart, service_restart, ServiceRestart);
 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_service_append_notify_access, notify_access, NotifyAccess);
 static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_service_append_service_result, service_result, ServiceResult);
+static DEFINE_BUS_PROPERTY_APPEND_ENUM(bus_service_append_start_limit_action, start_limit_action, StartLimitAction);
 
 static const BusProperty bus_exec_main_status_properties[] = {
         { "ExecMainStartTimestamp",         bus_property_append_usec, "t", offsetof(ExecStatus, start_timestamp.realtime)  },
@@ -123,6 +127,9 @@ static const BusProperty bus_service_properties[] = {
         { "WatchdogUSec",           bus_property_append_usec,         "t", offsetof(Service, watchdog_usec)                },
         { "WatchdogTimestamp",      bus_property_append_usec,         "t", offsetof(Service, watchdog_timestamp.realtime)  },
         { "WatchdogTimestampMonotonic",bus_property_append_usec,      "t", offsetof(Service, watchdog_timestamp.monotonic) },
+        { "StartLimitInterval",     bus_property_append_usec,         "t", offsetof(Service, ratelimit.interval)           },
+        { "StartLimitBurst",        bus_property_append_uint32,       "u", offsetof(Service, ratelimit.burst)              },
+        { "StartLimitAction",       bus_service_append_start_limit_action,"s", offsetof(Service, start_limit_action)       },
         BUS_EXEC_COMMAND_PROPERTY("ExecStartPre",  offsetof(Service, exec_command[SERVICE_EXEC_START_PRE]),  true ),
         BUS_EXEC_COMMAND_PROPERTY("ExecStart",     offsetof(Service, exec_command[SERVICE_EXEC_START]),      true ),
         BUS_EXEC_COMMAND_PROPERTY("ExecStartPost", offsetof(Service, exec_command[SERVICE_EXEC_START_POST]), true ),
diff --git a/src/load-fragment-gperf.gperf.m4 b/src/load-fragment-gperf.gperf.m4
index 9191f90..2d55f8e 100644
--- a/src/load-fragment-gperf.gperf.m4
+++ b/src/load-fragment-gperf.gperf.m4
@@ -135,6 +135,9 @@ Service.ExecStopPost,            config_parse_exec,                  SERVICE_EXE
 Service.RestartSec,              config_parse_usec,                  0,                             offsetof(Service, restart_usec)
 Service.TimeoutSec,              config_parse_usec,                  0,                             offsetof(Service, timeout_usec)
 Service.WatchdogSec,             config_parse_usec,                  0,                             offsetof(Service, watchdog_usec)
+Service.StartLimitInterval,      config_parse_usec,                  0,                             offsetof(Service, ratelimit.interval)
+Service.StartLimitBurst,         config_parse_unsigned,              0,                             offsetof(Service, ratelimit.burst)
+Service.StartLimitAction,        config_parse_start_limit_action,    0,                             offsetof(Service, start_limit_action)
 Service.Type,                    config_parse_service_type,          0,                             offsetof(Service, type)
 Service.Restart,                 config_parse_service_restart,       0,                             offsetof(Service, restart)
 Service.PermissionsStartOnly,    config_parse_bool,                  0,                             offsetof(Service, permissions_start_only)
diff --git a/src/load-fragment.c b/src/load-fragment.c
index b2d43fb..b963d7f 100644
--- a/src/load-fragment.c
+++ b/src/load-fragment.c
@@ -1648,6 +1648,7 @@ int config_parse_unit_condition_null(
 }
 
 DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
+DEFINE_CONFIG_PARSE_ENUM(config_parse_start_limit_action, start_limit_action, StartLimitAction, "Failed to parse start limit action specifier");
 
 int config_parse_unit_cgroup_attr(
                 const char *filename,
diff --git a/src/load-fragment.h b/src/load-fragment.h
index fbb31f9..79fc76d 100644
--- a/src/load-fragment.h
+++ b/src/load-fragment.h
@@ -76,6 +76,7 @@ int config_parse_unit_condition_string(const char *filename, unsigned line, cons
 int config_parse_unit_condition_null(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_kill_mode(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_notify_access(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
+int config_parse_start_limit_action(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_unit_cgroup_attr(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_unit_cpu_shares(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
 int config_parse_unit_memory_limit(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
diff --git a/src/service.c b/src/service.c
index a190a73..552407e 100644
--- a/src/service.c
+++ b/src/service.c
@@ -23,7 +23,9 @@
 #include <signal.h>
 #include <dirent.h>
 #include <unistd.h>
+#include <sys/reboot.h>
 
+#include "manager.h"
 #include "unit.h"
 #include "service.h"
 #include "load-fragment.h"
@@ -2348,7 +2350,32 @@ static int service_start(Unit *u) {
 
         /* Make sure we don't enter a busy loop of some kind. */
         if (!ratelimit_test(&s->ratelimit)) {
-                log_warning("%s start request repeated too quickly, refusing to start.", u->id);
+                switch (s->start_limit_action) {
+                case SERVICE_START_LIMIT_NONE:
+                        log_warning("%s start request repeated too quickly, refusing to start.", u->id);
+                        break;
+                case SERVICE_START_LIMIT_REBOOT: {
+                        DBusError error;
+                        int r;
+
+                        log_warning("%s start request repeated too quickly, rebooting.", u->id);
+                        dbus_error_init(&error);
+                        r = manager_add_job_by_name(u->manager, JOB_START, SPECIAL_REBOOT_TARGET, JOB_REPLACE, true, &error, NULL);
+                        if (r < 0)
+                                log_error("Failed to reboot: %s.", bus_error(&error, r));
+                        break;
+                }
+                case SERVICE_START_LIMIT_REBOOT_FORCE:
+                        log_warning("%s start request repeated too quickly, force rebooting.", u->id);
+                        u->manager->exit_code = MANAGER_REBOOT;
+                        break;
+                case SERVICE_START_LIMIT_REBOOT_IMMEDIATE:
+                        reboot(RB_AUTOBOOT);
+                        break;
+                default:
+                        log_error("start limit action=%i", s->start_limit_action);
+                        assert_not_reached("Unknown StartLimitAction.");
+                }
                 return -ECANCELED;
         }
 
@@ -3664,6 +3691,14 @@ static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
 
 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
 
+static const char* const start_limit_action_table[_SERVICE_START_LIMIT_MAX] = {
+        [SERVICE_START_LIMIT_NONE] = "none",
+        [SERVICE_START_LIMIT_REBOOT] = "reboot",
+        [SERVICE_START_LIMIT_REBOOT_FORCE] = "reboot-force",
+        [SERVICE_START_LIMIT_REBOOT_IMMEDIATE] = "reboot-immediate"
+};
+DEFINE_STRING_TABLE_LOOKUP(start_limit_action, StartLimitAction);
+
 const UnitVTable service_vtable = {
         .suffix = ".service",
         .object_size = sizeof(Service),
diff --git a/src/service.h b/src/service.h
index 02726ef..3399413 100644
--- a/src/service.h
+++ b/src/service.h
@@ -100,6 +100,15 @@ typedef enum ServiceResult {
         _SERVICE_RESULT_INVALID = -1
 } ServiceResult;
 
+typedef enum StartLimitAction {
+        SERVICE_START_LIMIT_NONE,
+        SERVICE_START_LIMIT_REBOOT,
+        SERVICE_START_LIMIT_REBOOT_FORCE,
+        SERVICE_START_LIMIT_REBOOT_IMMEDIATE,
+        _SERVICE_START_LIMIT_MAX,
+        _SERVICE_START_LIMIT_INVALID = -1
+} StartLimitAction;
+
 struct Service {
         Unit meta;
 
@@ -116,6 +125,8 @@ struct Service {
         usec_t watchdog_usec;
         Watch watchdog_watch;
 
+        StartLimitAction start_limit_action;
+
         ExecCommand* exec_command[_SERVICE_EXEC_COMMAND_MAX];
         ExecContext exec_context;
 
@@ -203,4 +214,7 @@ NotifyAccess notify_access_from_string(const char *s);
 const char* service_result_to_string(ServiceResult i);
 ServiceResult service_result_from_string(const char *s);
 
+const char* start_limit_action_to_string(StartLimitAction i);
+StartLimitAction start_limit_action_from_string(const char *s);
+
 #endif
-- 
1.7.7.3



More information about the systemd-devel mailing list