Просмотр исходного кода

feat: add universal process runtime service

This change introduces the `lwp_runtime.c` component, which provides the
necessary runtime environment for the init process, including boot scripts,
shutdown, and poweroff functionalities. The initialization logic has been
moved from `lwp.c` to this new file, enhancing modularity and flexibility
in handling LWP runtime tasks.

Changes:
- Moved the `lwp_startup` function from `lwp.c` to `lwp_runtime.c` to handle
  system initialization and runtime environment setup.
- Added `lwp_teardown` placeholder for system shutdown and cleanup tasks in
  the future (though not yet implemented).
- Introduced the `LWP_USING_RUNTIME` configuration option in `Kconfig` to
  conditionally enable the runtime environment.
- Updated the `SConscript` to conditionally include `lwp_runtime.c` based on
  the `LWP_USING_RUNTIME` configuration.
- Removed the old `lwp_startup` code from `lwp.c`, simplifying the file.

Signed-off-by: Shell <smokewood@qq.com>
Shell 5 месяцев назад
Родитель
Сommit
972931c991
4 измененных файлов с 124 добавлено и 82 удалено
  1. 9 1
      components/lwp/Kconfig
  2. 4 0
      components/lwp/SConscript
  3. 0 81
      components/lwp/lwp.c
  4. 111 0
      components/lwp/lwp_runtime.c

+ 9 - 1
components/lwp/Kconfig

@@ -14,9 +14,17 @@ if RT_USING_LWP
         config LWP_DEBUG_INIT
             select RT_USING_HOOKLIST
             bool "Enable debug mode of init process"
-            default n
+            depends on LWP_USING_RUNTIME
+            default y
     endif
 
+    config LWP_USING_RUNTIME
+        bool "Using processes runtime environment (INIT process)"
+        default y
+        help
+            Runtime environment provide by init process including boot scripts,
+            poweroff, shutdown, reboot, etc.
+
     config RT_LWP_MAX_NR
         int "The max number of light-weight process"
         default 30

+ 4 - 0
components/lwp/SConscript

@@ -45,6 +45,10 @@ for item in termios_path:
     src += Glob(item + '*.c')
 CPPPATH += ['./terminal/']
 
+# Remove optional sources
+if not GetDepend(['LWP_USING_RUNTIME']):
+    SrcRemove(src, 'lwp_runtime.c')
+
 group = DefineGroup('lwP', src, depend = ['RT_USING_SMART'], CPPPATH = CPPPATH)
 
 group = group + SConscript(os.path.join('vdso', 'SConscript'))

+ 0 - 81
components/lwp/lwp.c

@@ -83,87 +83,6 @@ static int lwp_component_init(void)
 }
 INIT_COMPONENT_EXPORT(lwp_component_init);
 
-rt_weak int lwp_startup_debug_request(void)
-{
-    return 0;
-}
-
-#define LATENCY_TIMES (3)
-#define LATENCY_IN_MSEC (128)
-#define LWP_CONSOLE_PATH "CONSOLE=/dev/console"
-const char *init_search_path[] = {
-    "/sbin/init",
-    "/bin/init",
-};
-
-/**
- * Startup process 0 and do the essential works
- * This is the "Hello World" point of RT-Smart
- */
-static int lwp_startup(void)
-{
-    int error;
-
-    const char *init_path;
-    char *argv[] = {0, "&"};
-    char *envp[] = {LWP_CONSOLE_PATH, 0};
-
-#ifdef LWP_DEBUG_INIT
-    int command;
-    int countdown = LATENCY_TIMES;
-    while (countdown)
-    {
-        command = lwp_startup_debug_request();
-        if (command)
-        {
-            return 0;
-        }
-        rt_kprintf("Press any key to stop init process startup ... %d\n", countdown);
-        countdown -= 1;
-        rt_thread_mdelay(LATENCY_IN_MSEC);
-    }
-    rt_kprintf("Starting init ...\n");
-#endif /* LWP_DEBUG_INIT */
-
-    for (size_t i = 0; i < sizeof(init_search_path)/sizeof(init_search_path[0]); i++)
-    {
-        struct stat s;
-        init_path = init_search_path[i];
-        error = stat(init_path, &s);
-        if (error == 0)
-        {
-            argv[0] = (void *)init_path;
-            error = lwp_execve((void *)init_path, 0, sizeof(argv)/sizeof(argv[0]), argv, envp);
-            if (error < 0)
-            {
-                LOG_E("%s: failed to startup process 0 (init)\n"
-                    "Switching to legacy mode...", __func__);
-            }
-            else if (error != 1)
-            {
-                LOG_E("%s: pid 1 is already allocated", __func__);
-                error = -EBUSY;
-            }
-            else
-            {
-                rt_lwp_t p = lwp_from_pid_locked(1);
-                p->sig_protected = 1;
-
-                error = 0;
-            }
-            break;
-        }
-    }
-
-    if (error)
-    {
-        LOG_D("%s: init program not found\n"
-            "Switching to legacy mode...", __func__);
-    }
-    return error;
-}
-INIT_APP_EXPORT(lwp_startup);
-
 void lwp_setcwd(char *buf)
 {
     struct rt_lwp *lwp = RT_NULL;

+ 111 - 0
components/lwp/lwp_runtime.c

@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2006-2024, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2024-11-11     Shell        moved lwp_startup() from lwp.c;
+ *                             added lwp_teardown()
+ */
+
+#define DBG_TAG "lwp"
+#define DBG_LVL DBG_INFO
+#include <rtdbg.h>
+
+#include "lwp_internal.h"
+
+#include <rthw.h>
+#include <rtthread.h>
+
+#include <dfs_file.h>
+#include <dfs_mnt.h>
+#include <dfs_fs.h>
+
+/**
+ * lwp_runtime:
+ * Runtime environment provide by init process including boot scripts,
+ * poweroff, shutdown, reboot, service management etc. In the kernel, lwp will
+ * provide the underlying software bootstrap and cleanup for the init proc.
+ *
+ */
+
+rt_weak int lwp_startup_debug_request(void)
+{
+    return 0;
+}
+
+#define LATENCY_TIMES (3)
+#define LATENCY_IN_MSEC (128)
+#define LWP_CONSOLE_PATH "CONSOLE=/dev/console"
+const char *init_search_path[] = {
+    "/sbin/init",
+    "/bin/init",
+};
+
+/**
+ * Startup process 1 and do the essential works
+ */
+static int lwp_startup(void)
+{
+    int error;
+
+    const char *init_path;
+    char *argv[] = {0, "&"};
+    char *envp[] = {LWP_CONSOLE_PATH, 0};
+
+#ifdef LWP_DEBUG_INIT
+    int command;
+    int countdown = LATENCY_TIMES;
+    while (countdown)
+    {
+        command = lwp_startup_debug_request();
+        if (command)
+        {
+            return 0;
+        }
+        rt_kprintf("Press any key to stop init process startup ... %d\n", countdown);
+        countdown -= 1;
+        rt_thread_mdelay(LATENCY_IN_MSEC);
+    }
+    rt_kprintf("Starting init ...\n");
+#endif /* LWP_DEBUG_INIT */
+
+    for (size_t i = 0; i < sizeof(init_search_path)/sizeof(init_search_path[0]); i++)
+    {
+        struct stat s;
+        init_path = init_search_path[i];
+        error = stat(init_path, &s);
+        if (error == 0)
+        {
+            argv[0] = (void *)init_path;
+            error = lwp_execve((void *)init_path, 0, sizeof(argv)/sizeof(argv[0]), argv, envp);
+            if (error < 0)
+            {
+                LOG_W("%s: failed to setup runtime environment\b"
+                      "\tlwp_execve() failed with code %d", __func__, error);
+            }
+            else if (error != 1)
+            {
+                LOG_W("%s: pid 1 is already allocated", __func__);
+                error = -EBUSY;
+            }
+            else
+            {
+                rt_lwp_t p = lwp_from_pid_locked(1);
+                p->sig_protected = 1;
+
+                error = 0;
+            }
+            break;
+        }
+    }
+
+    if (error)
+    {
+        LOG_D("%s: failed to setup runtime environment\b"
+              "\tinit program not found", __func__);
+    }
+    return error;
+}
+INIT_APP_EXPORT(lwp_startup);