]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/service: drop redundant flush_n_restarts indicator
authorMike Yuan <me@yhndnzj.com>
Sat, 3 Aug 2024 19:46:51 +0000 (21:46 +0200)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 4 Aug 2024 00:37:59 +0000 (09:37 +0900)
Now that we track auto-restarts with a dedicated state,
there's no need for a separate variable for this.

I also took the chance to reorder some struct members.

src/core/service.c
src/core/service.h

index 49ed3b088a749357af2fd05c062867f2ef5114c6..ca6040a0557cb8dfbf8870d1bafe8df0c12090f6 100644 (file)
@@ -2074,14 +2074,12 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart)
                 log_unit_debug(UNIT(s), "Next restart interval calculated as: %s", FORMAT_TIMESPAN(restart_usec_next, 0));
 
                 service_set_state(s, SERVICE_AUTO_RESTART);
-        } else {
+        } else
+                /* If we shan't restart, the restart counter would be flushed out. But rather than doing that
+                 * immediately here, this is delegated to service_start(), i.e. next start, so that the user
+                 * can still introspect the counter. */
                 service_set_state(s, end_state);
 
-                /* If we shan't restart, then flush out the restart counter. But don't do that immediately, so that the
-                 * user can still introspect the counter. Do so on the next start. */
-                s->flush_n_restarts = true;
-        }
-
         /* The new state is in effect, let's decrease the fd store ref counter again. Let's also re-add us to the GC
          * queue, so that the fd store is possibly gc'ed again */
         unit_add_to_gc_queue(UNIT(s));
@@ -2595,7 +2593,6 @@ static void service_enter_restart(Service *s, bool shortcut) {
          * fully stopped, i.e. as long as it remains up or remains in auto-start states. The user can reset
          * the counter explicitly however via the usual "systemctl reset-failure" logic. */
         s->n_restarts++;
-        s->flush_n_restarts = false;
 
         log_unit_struct(UNIT(s), LOG_INFO,
                         "MESSAGE_ID=" SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR,
@@ -2799,6 +2796,10 @@ static int service_start(Unit *u) {
         s->main_pid_alien = false;
         s->forbid_restart = false;
 
+        /* This is not an automatic restart? Flush the restart counter then. */
+        if (s->state != SERVICE_AUTO_RESTART_QUEUED)
+                s->n_restarts = 0;
+
         s->status_text = mfree(s->status_text);
         s->status_errno = 0;
         s->status_bus_error = mfree(s->status_bus_error);
@@ -2814,12 +2815,6 @@ static int service_start(Unit *u) {
         exec_command_reset_status_list_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
         exec_status_reset(&s->main_exec_status);
 
-        /* This is not an automatic restart? Flush the restart counter then */
-        if (s->flush_n_restarts) {
-                s->n_restarts = 0;
-                s->flush_n_restarts = false;
-        }
-
         CGroupRuntime *crt = unit_get_cgroup_runtime(u);
         if (crt)
                 crt->reset_accounting = true;
@@ -3005,7 +3000,6 @@ static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
         (void) serialize_bool(f, "bus-name-good", s->bus_name_good);
 
         (void) serialize_item_format(f, "n-restarts", "%u", s->n_restarts);
-        (void) serialize_bool(f, "flush-n-restarts", s->flush_n_restarts);
         (void) serialize_bool(f, "forbid-restart", s->forbid_restart);
 
         service_serialize_exec_command(u, f, s->control_command);
@@ -3348,12 +3342,6 @@ static int service_deserialize_item(Unit *u, const char *key, const char *value,
                 if (r < 0)
                         log_unit_debug_errno(u, r, "Failed to parse serialized restart counter '%s': %m", value);
 
-        } else if (streq(key, "flush-n-restarts")) {
-                r = parse_boolean(value);
-                if (r < 0)
-                        log_unit_debug_errno(u, r, "Failed to parse serialized flush restart counter setting '%s': %m", value);
-                else
-                        s->flush_n_restarts = r;
         } else if (streq(key, "forbid-restart")) {
                 r = parse_boolean(value);
                 if (r < 0)
@@ -4875,7 +4863,6 @@ static void service_reset_failed(Unit *u) {
         s->reload_result = SERVICE_SUCCESS;
         s->clean_result = SERVICE_SUCCESS;
         s->n_restarts = 0;
-        s->flush_n_restarts = false;
 }
 
 static PidRef* service_main_pid(Unit *u, bool *ret_is_alien) {
index 1d67d13fda4cfbd7fefcfe6e102f2a3129529b9a..8984be37b4385c19b7f8351136d412e78c1d07db 100644 (file)
@@ -124,8 +124,9 @@ struct Service {
         /* If set we'll read the main daemon PID from this file */
         char *pid_file;
 
-        usec_t restart_usec;
+        unsigned n_restarts;
         unsigned restart_steps;
+        usec_t restart_usec;
         usec_t restart_max_delay_usec;
         usec_t timeout_start_usec;
         usec_t timeout_stop_usec;
@@ -143,8 +144,6 @@ struct Service {
         bool watchdog_override_enable;
         sd_event_source *watchdog_event_source;
 
-        ExecCommand* exec_command[_SERVICE_EXEC_COMMAND_MAX];
-
         ExecContext exec_context;
         KillContext kill_context;
         CGroupContext cgroup_context;
@@ -154,14 +153,15 @@ struct Service {
         /* The exit status of the real main process */
         ExecStatus main_exec_status;
 
-        /* The currently executed control process */
-        ExecCommand *control_command;
+        ExecCommand *exec_command[_SERVICE_EXEC_COMMAND_MAX];
 
-        /* The currently executed main process, which may be NULL if
-         * the main process got started via forking mode and not by
-         * us */
+        /* The currently executed main process, which may be NULL if the main process got started via
+         * forking mode and not by us */
         ExecCommand *main_command;
 
+        /* The currently executed control process */
+        ExecCommand *control_command;
+
         /* The ID of the control command currently being executed */
         ServiceExecCommand control_command_id;
 
@@ -225,9 +225,6 @@ struct Service {
         int stdout_fd;
         int stderr_fd;
 
-        unsigned n_restarts;
-        bool flush_n_restarts;
-
         OOMPolicy oom_policy;
 
         LIST_HEAD(OpenFile, open_files);