]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core/service: use switch for service_enter_start
authorMike Yuan <me@yhndnzj.com>
Tue, 7 May 2024 11:08:31 +0000 (19:08 +0800)
committerMike Yuan <me@yhndnzj.com>
Tue, 7 May 2024 16:17:54 +0000 (00:17 +0800)
Plus, add an assertion to ensure we're actually
using the start timestamp for the correct pid.

src/core/service.c

index 36671ae1c0f72f50c4394f87bc0c2967c8d47990..9c30067e922a8ad514bb83528b5168140abebd12 100644 (file)
@@ -2413,35 +2413,38 @@ static void service_enter_start(Service *s) {
                 goto fail;
         }
 
-        if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE)) {
-                /* For simple services we immediately start
-                 * the START_POST binaries. */
+        assert(pidref.pid == c->exec_status.pid);
 
-                (void) service_set_main_pidref(s, TAKE_PIDREF(pidref), &c->exec_status.start_timestamp);
-                service_enter_start_post(s);
-
-        } else  if (s->type == SERVICE_FORKING) {
+        switch (s->type) {
 
-                /* For forking services we wait until the start
-                 * process exited. */
+        case SERVICE_SIMPLE:
+        case SERVICE_IDLE:
+                /* For simple services we immediately start the START_POST binaries. */
+                (void) service_set_main_pidref(s, TAKE_PIDREF(pidref), &c->exec_status.start_timestamp);
+                return service_enter_start_post(s);
 
+        case SERVICE_FORKING:
+                /* For forking services we wait until the start process exited. */
                 pidref_done(&s->control_pid);
                 s->control_pid = TAKE_PIDREF(pidref);
-                service_set_state(s, SERVICE_START);
-
-        } else if (IN_SET(s->type, SERVICE_ONESHOT, SERVICE_DBUS, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD, SERVICE_EXEC)) {
-
-                /* For oneshot services we wait until the start process exited, too, but it is our main process. */
-
-                /* For D-Bus services we know the main pid right away, but wait for the bus name to appear on the
-                 * bus. 'notify' and 'exec' services are similar. */
-
+                return service_set_state(s, SERVICE_START);
+
+        case SERVICE_ONESHOT: /* For oneshot services we wait until the start process exited, too, but it is our main process. */
+        case SERVICE_EXEC:
+        case SERVICE_DBUS:
+        case SERVICE_NOTIFY:
+        case SERVICE_NOTIFY_RELOAD:
+                /* For D-Bus services we know the main pid right away, but wait for the bus name to appear
+                 * on the bus. 'notify' and 'exec' services wait for readiness notification and EOF
+                 * on exec_fd, respectively. */
                 (void) service_set_main_pidref(s, TAKE_PIDREF(pidref), &c->exec_status.start_timestamp);
-                service_set_state(s, SERVICE_START);
-        } else
-                assert_not_reached();
+                return service_set_state(s, SERVICE_START);
 
-        return;
+        default:
+                ;
+        }
+
+        assert_not_reached();
 
 fail:
         service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);