Skip to content

Commit 972931c

Browse files
polarvidRbb666
authored andcommitted
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 <[email protected]>
1 parent dc3270f commit 972931c

File tree

4 files changed

+124
-82
lines changed

4 files changed

+124
-82
lines changed

components/lwp/Kconfig

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@ if RT_USING_LWP
1414
config LWP_DEBUG_INIT
1515
select RT_USING_HOOKLIST
1616
bool "Enable debug mode of init process"
17-
default n
17+
depends on LWP_USING_RUNTIME
18+
default y
1819
endif
1920

21+
config LWP_USING_RUNTIME
22+
bool "Using processes runtime environment (INIT process)"
23+
default y
24+
help
25+
Runtime environment provide by init process including boot scripts,
26+
poweroff, shutdown, reboot, etc.
27+
2028
config RT_LWP_MAX_NR
2129
int "The max number of light-weight process"
2230
default 30

components/lwp/SConscript

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ for item in termios_path:
4545
src += Glob(item + '*.c')
4646
CPPPATH += ['./terminal/']
4747

48+
# Remove optional sources
49+
if not GetDepend(['LWP_USING_RUNTIME']):
50+
SrcRemove(src, 'lwp_runtime.c')
51+
4852
group = DefineGroup('lwP', src, depend = ['RT_USING_SMART'], CPPPATH = CPPPATH)
4953

5054
group = group + SConscript(os.path.join('vdso', 'SConscript'))

components/lwp/lwp.c

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -83,87 +83,6 @@ static int lwp_component_init(void)
8383
}
8484
INIT_COMPONENT_EXPORT(lwp_component_init);
8585

86-
rt_weak int lwp_startup_debug_request(void)
87-
{
88-
return 0;
89-
}
90-
91-
#define LATENCY_TIMES (3)
92-
#define LATENCY_IN_MSEC (128)
93-
#define LWP_CONSOLE_PATH "CONSOLE=/dev/console"
94-
const char *init_search_path[] = {
95-
"/sbin/init",
96-
"/bin/init",
97-
};
98-
99-
/**
100-
* Startup process 0 and do the essential works
101-
* This is the "Hello World" point of RT-Smart
102-
*/
103-
static int lwp_startup(void)
104-
{
105-
int error;
106-
107-
const char *init_path;
108-
char *argv[] = {0, "&"};
109-
char *envp[] = {LWP_CONSOLE_PATH, 0};
110-
111-
#ifdef LWP_DEBUG_INIT
112-
int command;
113-
int countdown = LATENCY_TIMES;
114-
while (countdown)
115-
{
116-
command = lwp_startup_debug_request();
117-
if (command)
118-
{
119-
return 0;
120-
}
121-
rt_kprintf("Press any key to stop init process startup ... %d\n", countdown);
122-
countdown -= 1;
123-
rt_thread_mdelay(LATENCY_IN_MSEC);
124-
}
125-
rt_kprintf("Starting init ...\n");
126-
#endif /* LWP_DEBUG_INIT */
127-
128-
for (size_t i = 0; i < sizeof(init_search_path)/sizeof(init_search_path[0]); i++)
129-
{
130-
struct stat s;
131-
init_path = init_search_path[i];
132-
error = stat(init_path, &s);
133-
if (error == 0)
134-
{
135-
argv[0] = (void *)init_path;
136-
error = lwp_execve((void *)init_path, 0, sizeof(argv)/sizeof(argv[0]), argv, envp);
137-
if (error < 0)
138-
{
139-
LOG_E("%s: failed to startup process 0 (init)\n"
140-
"Switching to legacy mode...", __func__);
141-
}
142-
else if (error != 1)
143-
{
144-
LOG_E("%s: pid 1 is already allocated", __func__);
145-
error = -EBUSY;
146-
}
147-
else
148-
{
149-
rt_lwp_t p = lwp_from_pid_locked(1);
150-
p->sig_protected = 1;
151-
152-
error = 0;
153-
}
154-
break;
155-
}
156-
}
157-
158-
if (error)
159-
{
160-
LOG_D("%s: init program not found\n"
161-
"Switching to legacy mode...", __func__);
162-
}
163-
return error;
164-
}
165-
INIT_APP_EXPORT(lwp_startup);
166-
16786
void lwp_setcwd(char *buf)
16887
{
16988
struct rt_lwp *lwp = RT_NULL;

components/lwp/lwp_runtime.c

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (c) 2006-2024, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2024-11-11 Shell moved lwp_startup() from lwp.c;
9+
* added lwp_teardown()
10+
*/
11+
12+
#define DBG_TAG "lwp"
13+
#define DBG_LVL DBG_INFO
14+
#include <rtdbg.h>
15+
16+
#include "lwp_internal.h"
17+
18+
#include <rthw.h>
19+
#include <rtthread.h>
20+
21+
#include <dfs_file.h>
22+
#include <dfs_mnt.h>
23+
#include <dfs_fs.h>
24+
25+
/**
26+
* lwp_runtime:
27+
* Runtime environment provide by init process including boot scripts,
28+
* poweroff, shutdown, reboot, service management etc. In the kernel, lwp will
29+
* provide the underlying software bootstrap and cleanup for the init proc.
30+
*
31+
*/
32+
33+
rt_weak int lwp_startup_debug_request(void)
34+
{
35+
return 0;
36+
}
37+
38+
#define LATENCY_TIMES (3)
39+
#define LATENCY_IN_MSEC (128)
40+
#define LWP_CONSOLE_PATH "CONSOLE=/dev/console"
41+
const char *init_search_path[] = {
42+
"/sbin/init",
43+
"/bin/init",
44+
};
45+
46+
/**
47+
* Startup process 1 and do the essential works
48+
*/
49+
static int lwp_startup(void)
50+
{
51+
int error;
52+
53+
const char *init_path;
54+
char *argv[] = {0, "&"};
55+
char *envp[] = {LWP_CONSOLE_PATH, 0};
56+
57+
#ifdef LWP_DEBUG_INIT
58+
int command;
59+
int countdown = LATENCY_TIMES;
60+
while (countdown)
61+
{
62+
command = lwp_startup_debug_request();
63+
if (command)
64+
{
65+
return 0;
66+
}
67+
rt_kprintf("Press any key to stop init process startup ... %d\n", countdown);
68+
countdown -= 1;
69+
rt_thread_mdelay(LATENCY_IN_MSEC);
70+
}
71+
rt_kprintf("Starting init ...\n");
72+
#endif /* LWP_DEBUG_INIT */
73+
74+
for (size_t i = 0; i < sizeof(init_search_path)/sizeof(init_search_path[0]); i++)
75+
{
76+
struct stat s;
77+
init_path = init_search_path[i];
78+
error = stat(init_path, &s);
79+
if (error == 0)
80+
{
81+
argv[0] = (void *)init_path;
82+
error = lwp_execve((void *)init_path, 0, sizeof(argv)/sizeof(argv[0]), argv, envp);
83+
if (error < 0)
84+
{
85+
LOG_W("%s: failed to setup runtime environment\b"
86+
"\tlwp_execve() failed with code %d", __func__, error);
87+
}
88+
else if (error != 1)
89+
{
90+
LOG_W("%s: pid 1 is already allocated", __func__);
91+
error = -EBUSY;
92+
}
93+
else
94+
{
95+
rt_lwp_t p = lwp_from_pid_locked(1);
96+
p->sig_protected = 1;
97+
98+
error = 0;
99+
}
100+
break;
101+
}
102+
}
103+
104+
if (error)
105+
{
106+
LOG_D("%s: failed to setup runtime environment\b"
107+
"\tinit program not found", __func__);
108+
}
109+
return error;
110+
}
111+
INIT_APP_EXPORT(lwp_startup);

0 commit comments

Comments
 (0)