chiark / gitweb /
when shortcutting states do not skip state transitions
authorLennart Poettering <lennart@poettering.net>
Thu, 28 Jan 2010 00:59:41 +0000 (01:59 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 28 Jan 2010 00:59:41 +0000 (01:59 +0100)
service.c
socket.c

index 2001b9f..1cbfb3d 100644 (file)
--- a/service.c
+++ b/service.c
@@ -30,7 +30,7 @@ static const char* const state_string_table[_SERVICE_STATE_MAX] = {
         [SERVICE_DEAD] = "dead",
         [SERVICE_START_PRE] = "start-pre",
         [SERVICE_START] = "start",
-        [SERVICE_START_POST] = "post",
+        [SERVICE_START_POST] = "start-post",
         [SERVICE_RUNNING] = "running",
         [SERVICE_RELOAD] = "reload",
         [SERVICE_STOP] = "stop",
@@ -330,7 +330,7 @@ static void service_set_state(Service *s, ServiceState state) {
             state == SERVICE_AUTO_RESTART)
                 service_notify_sockets(s);
 
-        log_debug("%s changing %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
+        log_debug("%s changed %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
 
         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
 }
@@ -471,13 +471,14 @@ static void service_enter_stop_post(Service *s, bool success) {
         if (!success)
                 s->failure = true;
 
-        if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
-
+        if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]))
                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
                         goto fail;
 
-                service_set_state(s, SERVICE_STOP_POST);
-        } else
+
+        service_set_state(s, SERVICE_STOP_POST);
+
+        if (!s->control_command)
                 service_enter_dead(s, true, true);
 
         return;
@@ -518,9 +519,11 @@ static void service_enter_signal(Service *s, ServiceState state, bool success) {
 
                 if (r < 0)
                         goto fail;
+        }
 
-                service_set_state(s, state);
-        } else
+        service_set_state(s, state);
+
+        if (s->main_pid <= 0 && s->control_pid <= 0)
                 service_enter_dead(s, true, true);
 
         return;
@@ -544,13 +547,13 @@ static void service_enter_stop(Service *s, bool success) {
         if (!success)
                 s->failure = true;
 
-        if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
-
+        if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP]))
                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
                         goto fail;
 
-                service_set_state(s, SERVICE_STOP);
-        } else
+        service_set_state(s, SERVICE_STOP);
+
+        if (!s->control_command)
                 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
 
         return;
@@ -564,13 +567,14 @@ static void service_enter_start_post(Service *s) {
         int r;
         assert(s);
 
-        if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
-
+        if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST]))
                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
                         goto fail;
 
-                service_set_state(s, SERVICE_START_POST);
-        } else
+
+        service_set_state(s, SERVICE_START_POST);
+
+        if (!s->control_command)
                 service_set_state(s, SERVICE_RUNNING);
 
         return;
@@ -592,6 +596,8 @@ static void service_enter_start(Service *s) {
         if ((r = service_spawn(s, s->exec_command[SERVICE_EXEC_START], s->type == SERVICE_FORKING, true, &pid)) < 0)
                 goto fail;
 
+        service_set_state(s, SERVICE_START);
+
         if (s->type == SERVICE_SIMPLE) {
                 /* For simple services we immediately start
                  * the START_POST binaries. */
@@ -607,7 +613,6 @@ static void service_enter_start(Service *s) {
 
                 s->control_pid = pid;
                 s->control_command = s->exec_command[SERVICE_EXEC_START];
-                service_set_state(s, SERVICE_START);
         } else
                 assert_not_reached("Unknown service type");
 
@@ -623,13 +628,13 @@ static void service_enter_start_pre(Service *s) {
 
         assert(s);
 
-        if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
-
+        if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]))
                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
                         goto fail;
 
-                service_set_state(s, SERVICE_START_PRE);
-        } else
+        service_set_state(s, SERVICE_START_PRE);
+
+        if (!s->control_command)
                 service_enter_start(s);
 
         return;
@@ -661,13 +666,13 @@ static void service_enter_reload(Service *s) {
 
         assert(s);
 
-        if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
-
+        if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]))
                 if ((r = service_spawn(s, s->control_command, true, false, &s->control_pid)) < 0)
                         goto fail;
 
-                service_set_state(s, SERVICE_RELOAD);
-        } else
+        service_set_state(s, SERVICE_RELOAD);
+
+        if (!s->control_command)
                 service_set_state(s, SERVICE_RUNNING);
 
         return;
index 68a4d81..7bcd112 100644 (file)
--- a/socket.c
+++ b/socket.c
@@ -340,7 +340,7 @@ static void socket_set_state(Socket *s, SocketState state) {
         if (state != SOCKET_LISTENING)
                 socket_unwatch_fds(s);
 
-        log_debug("%s changing %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
+        log_debug("%s changed %s → %s", unit_id(UNIT(s)), state_string_table[old_state], state_string_table[state]);
 
         unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
 }
@@ -393,13 +393,13 @@ static void socket_enter_stop_post(Socket *s, bool success) {
         if (!success)
                 s->failure = true;
 
-        if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST])) {
-
+        if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_POST]))
                 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
                         goto fail;
 
-                socket_set_state(s, SOCKET_STOP_POST);
-        } else
+        socket_set_state(s, SOCKET_STOP_POST);
+
+        if (!s->control_command)
                 socket_enter_dead(s, true);
 
         return;
@@ -426,9 +426,11 @@ static void socket_enter_signal(Socket *s, SocketState state, bool success) {
                         r = -errno;
                         goto fail;
                 }
+        }
 
-                socket_set_state(s, state);
-        } else
+        socket_set_state(s, state);
+
+        if (s->control_pid <= 0)
                 socket_enter_dead(s, true);
 
         return;
@@ -449,13 +451,13 @@ static void socket_enter_stop_pre(Socket *s, bool success) {
         if (!success)
                 s->failure = true;
 
-        if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE])) {
-
+        if ((s->control_command = s->exec_command[SOCKET_EXEC_STOP_PRE]))
                 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
                         goto fail;
 
-                socket_set_state(s, SOCKET_STOP_PRE);
-        } else
+        socket_set_state(s, SOCKET_STOP_PRE);
+
+        if (!s->control_command)
                 socket_enter_stop_post(s, true);
 
         return;
@@ -490,15 +492,15 @@ static void socket_enter_start_post(Socket *s) {
                 goto fail;
         }
 
-        if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST])) {
-
+        if ((s->control_command = s->exec_command[SOCKET_EXEC_START_POST]))
                 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0) {
                         log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
                         goto fail;
                 }
 
-                socket_set_state(s, SOCKET_START_POST);
-        } else
+        socket_set_state(s, SOCKET_START_POST);
+
+        if (!s->control_command)
                 socket_enter_listening(s);
 
         return;
@@ -511,13 +513,13 @@ static void socket_enter_start_pre(Socket *s) {
         int r;
         assert(s);
 
-        if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE])) {
-
+        if ((s->control_command = s->exec_command[SOCKET_EXEC_START_PRE]))
                 if ((r = socket_spawn(s, s->control_command, true, &s->control_pid)) < 0)
                         goto fail;
 
-                socket_set_state(s, SOCKET_START_PRE);
-        } else
+        socket_set_state(s, SOCKET_START_PRE);
+
+        if (!s->control_command)
                 socket_enter_start_post(s);
 
         return;