-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit.c
More file actions
256 lines (230 loc) · 7.29 KB
/
Copy pathinit.c
File metadata and controls
256 lines (230 loc) · 7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include "runtime_setup.h"
static void xmkdir(const char *p) { mkdir(p, 0755); }
static void write_file(const char *path, const char *data, int mode)
{
int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, mode);
if (fd >= 0) { write(fd, data, strlen(data)); close(fd); }
}
static void append_if_dir(char *buf, size_t cap, const char *path)
{
struct stat st;
if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) {
size_t len = strlen(buf);
snprintf(buf + len, cap - len, "%s%s", len ? ":" : "", path);
}
}
static pid_t shell_pid;
static pid_t desktop_pid;
static pid_t telnet_pid;
static volatile sig_atomic_t got_signal;
static void sig_handler(int sig)
{
(void)sig;
got_signal = 1;
if (shell_pid > 0)
kill(shell_pid, SIGTERM);
if (desktop_pid > 0)
kill(desktop_pid, SIGTERM);
if (telnet_pid > 0)
kill(telnet_pid, SIGTERM);
}
static void setup_console(void)
{
int fd = open("/dev/console", O_RDWR);
if (fd < 0)
return;
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > 2)
close(fd);
}
static void setup_signals(void)
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sig_handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sa.sa_handler = SIG_IGN;
sigaction(SIGHUP, &sa, NULL);
}
static void reap_children(void)
{
while (waitpid(-1, NULL, WNOHANG) > 0)
;
}
static void do_shutdown(void)
{
sync();
printf("[init] shutting down\n");
reboot(RB_POWER_OFF);
for (;;) __asm__ volatile("");
}
int main(void)
{
setsid();
setup_console();
setvbuf(stdout, NULL, _IONBF, 0);
setvbuf(stderr, NULL, _IONBF, 0);
setup_signals();
#if defined(__powerpc64__)
char *argv[] = {"mksh", NULL};
char *envp[] = {"PATH=/bin", "HOME=/", "SHELL=/bin/mksh",
"TERM=vt100", NULL};
execve("/bin/mksh", argv, envp);
perror("execve mksh");
do_shutdown();
#else
int preliminary_contest =
(access("/bin/etc/contest-mode", F_OK) == 0);
int final_contest =
(access("/bin/etc/final-eval-group", F_OK) == 0);
if (preliminary_contest && final_contest) {
printf("[init] conflicting contest entry markers\n");
printf("[init] use contest-mode for preliminary evaluation or "
"final-eval-group for final evaluation, not both\n");
do_shutdown();
}
/*
* Final-round images are complete root filesystems. Their dynamic
* loader and libraries become available after the static chroot helper
* enters /test, so copying those files into the RAMFS only delays entry.
* Keep the original setup for interactive and legacy contest images.
*/
if (!final_contest)
setup_runtime_links();
xmkdir("/tmp");
xmkdir("/tmp/sysinfo");
write_file("/tmp/sysinfo/model", "A20OS Virtual Machine\n", 0644);
char path_val[512] = "/bin";
static const char *path_dirs[] = {
"/usr/bin", "/test", "/test/bin", "/test/glibc", "/test/musl",
#if defined(__loongarch64)
"/testla/glibc", "/testla/musl", "/testrv/glibc", "/testrv/musl",
#else
"/testrv/glibc", "/testrv/musl", "/testla/glibc", "/testla/musl",
#endif
NULL,
};
for (const char **p = path_dirs; *p; p++)
append_if_dir(path_val, sizeof(path_val), *p);
char ld_val[256] = "";
static const char *ld_dirs[] = {
"/test/glibc/lib", "/test/lib",
#if defined(__loongarch64)
"/testla/glibc/lib", "/testrv/glibc/lib",
#else
"/testrv/glibc/lib", "/testla/glibc/lib",
#endif
"/glibc/lib", "/usr/lib", "/lib", "/bin/lib", NULL,
};
for (const char **p = ld_dirs; *p; p++)
append_if_dir(ld_val, sizeof(ld_val), *p);
if (!ld_val[0]) strcpy(ld_val, "/lib:/bin/lib");
char path_env[576], ld_env[320];
snprintf(path_env, sizeof(path_env), "PATH=%s", path_val);
snprintf(ld_env, sizeof(ld_env), "LD_LIBRARY_PATH=%s", ld_val);
char *envp[] = {path_env, ld_env, "HOME=/", "SHELL=/bin/mksh", "TERM=vt100", NULL};
/* The GUI image carries both locations. Keep the root marker as a
* fallback for 32-bit ports whose early VFS path walk cannot yet resolve
* the marker through the /bin mount prefix reliably. */
if (access("/bin/etc/a20-gui", F_OK) == 0 ||
access("/bin/a20-gui", F_OK) == 0) {
printf("[init] starting desktop\n");
desktop_pid = fork();
if (desktop_pid == 0) {
if (access("/bin/wayland-session", X_OK) == 0) {
char *desktop_argv[] = {"wayland-session", NULL};
execve("/bin/wayland-session", desktop_argv, envp);
} else if (access("/bin/run-desktop.sh", F_OK) == 0) {
char *desktop_argv[] = {
"mksh", "/bin/run-desktop.sh", NULL
};
execve("/bin/mksh", desktop_argv, envp);
} else {
char *desktop_argv[] = {"desktop", NULL};
execve("/bin/desktop", desktop_argv, envp);
}
perror("execve desktop");
_exit(127);
}
if (desktop_pid < 0) {
perror("fork desktop");
desktop_pid = 0;
} else {
printf("[init] desktop queued: pid=%d\n", desktop_pid);
}
}
telnet_pid = fork();
if (telnet_pid == 0) {
char *telnet_argv[] = { "telnetd", "2323", NULL };
execve("/bin/telnetd", telnet_argv, envp);
_exit(127);
}
if (telnet_pid < 0) {
perror("fork telnetd");
telnet_pid = 0;
}
char *script = final_contest ? "/bin/final_contest.sh" :
preliminary_contest ? "/bin/contest.sh" : NULL;
char *argv[] = {"mksh", script, NULL};
printf("[init] forking...\n");
shell_pid = fork();
if (shell_pid == 0) {
printf("[init] fork=0, calling execve mksh\n");
execve("/bin/mksh", argv, envp);
perror("execve mksh");
_exit(127);
}
if (shell_pid < 0) {
printf("[init] fork failed with %d, errno %d\n", shell_pid, errno);
if (errno == EINVAL) {
/* NOMMU kernels do not implement fork(). Replace this
* process with the shell so the system still reaches an
* interactive prompt. */
printf("[init] errno=EINVAL, calling execve mksh\n");
execve("/bin/mksh", argv, envp);
perror("execve mksh");
} else {
perror("fork");
}
do_shutdown();
}
for (;;) {
int status;
pid_t w = waitpid(-1, &status, 0);
if (w < 0) {
if (got_signal)
break;
continue;
}
if (w == shell_pid) {
shell_pid = 0;
break;
}
if (w == desktop_pid) {
desktop_pid = 0;
printf("[init] desktop exited\n");
}
if (w == telnet_pid) {
telnet_pid = 0;
printf("[init] telnetd exited\n");
}
}
reap_children();
do_shutdown();
#endif
}