-
Notifications
You must be signed in to change notification settings - Fork 1
/
proc.c
334 lines (284 loc) · 8.16 KB
/
proc.c
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// This file is part of initrg project.
// Licensed under the terms of the GNU General Public License v3 or later.
// proc.c: functions for executing process
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <unistd.h>
#include "fs.h"
#include "net.h"
#include "util.h"
volatile int g_addGui = false;
int start_import(const char *dir)
{
int ret = 0, wstatus;
const pid_t childPid = fork();
if (childPid < 0)
{
LOG_ERROR("fork %d", errno);
return childPid;
}
if (!childPid)
{
const int stdinSock = connect_hv_socket(LXSS_SERVER_PORT, STDIN_FILENO, false);
if (stdinSock >= 0)
{
const int stderrSock = connect_hv_socket(LXSS_SERVER_PORT, STDERR_FILENO, false);
if (stderrSock >= 0)
{
ret = execl("/tools/bsdtar", "/tools/bsdtar", "-C", dir, "-x", "-p",
"--xattrs", "-f", "-", NULL);
if ( ret < 0 )
LOG_ERROR("execl %d", errno);
close(stdinSock);
}
close(stderrSock);
}
exit(ret);
}
ret = TEMP_FAILURE_RETRY(waitpid(childPid, &wstatus, 0));
if (ret >= 0)
return -(wstatus != 0);
else
LOG_ERROR("waitpid %d", errno);
return ret;
}
int start_export(const char *dir)
{
int ret = 0, wstatus;
const int stdoutSock = connect_hv_socket(LXSS_SERVER_PORT, -1, true);
if (stdoutSock < 0)
return stdoutSock;
const pid_t childPid = fork();
if (childPid < 0)
{
LOG_ERROR("fork %d", errno);
close(stdoutSock);
return childPid;
}
if (!childPid)
{
if (stdoutSock != STDOUT_FILENO)
{
ret = TEMP_FAILURE_RETRY(dup2(stdoutSock, STDOUT_FILENO));
if (ret < 0)
{
LOG_ERROR("dup2 %d", errno);
exit(ret);
}
close(stdoutSock);
}
const int stderrSock = connect_hv_socket(LXSS_SERVER_PORT, STDERR_FILENO, false);
if (stderrSock >= 0)
{
ret = execl("/tools/bsdtar", "/tools/bsdtar", "-C", dir, "-c",
"--one-file-system", "--xattrs", "-f", "-", ".", NULL);
if (ret < 0)
LOG_ERROR("execl %d", errno);
close(stderrSock);
}
exit(ret);
}
ret = TEMP_FAILURE_RETRY(waitpid(childPid, &wstatus, 0));
if (ret < 0)
{
LOG_ERROR("waitpid %d", errno);
close(stdoutSock);
return ret;
}
ret = shutdown(stdoutSock, SHUT_WR);
if (ret < 0)
LOG_ERROR("shutdown %d", errno);
ret = -(wstatus != 0);
close(stdoutSock);
return ret;
}
int start_gns(const int gnsSock)
{
const pid_t childPid = fork();
if (childPid < 0)
{
LOG_ERROR("fork %d", errno);
return childPid;
}
if (!childPid)
{
char str[10];
memset(str, '\0', sizeof str);
sprintf(str, "%d", gnsSock);
if (execl("gns", "gns", "--socket", str, NULL) < 0)
LOG_ERROR("execl %d", errno);
_exit(0);
}
return 0;
}
int start_localhost(void)
{
const int sock = connect_hv_socket(LXSS_SERVER_PORT, LXSS_SERVER_FD, false);
close(sock);
return 0;
}
int start_telemetry(void)
{
const int sock = connect_hv_socket(LXSS_SERVER_PORT, LXSS_SERVER_FD, false);
close(sock);
return 0;
}
int start_tracker(void)
{
const int sock = connect_hv_socket(LXSS_SERVER_PORT, -1, false);
close(sock);
return 0;
}
int start_init(int sock, char *rootDir, char *initCommand,
char *vmId, char *distroName, char *sharedMem)
{
#define TOTAL_ENVCOUNT 9
int ret = 0, envCount = 0;
char *userDistro = NULL, *initMount = NULL, *systemDistro = NULL;
size_t rootLen = strlen(rootDir);
char *envp[TOTAL_ENVCOUNT];
memset(envp, 0, sizeof envp);
if (initCommand && *initCommand)
{
close(sock);
sock = -1;
}
else
{
initCommand = "/init";
ret = util_mkdtemp(rootDir, &userDistro);
if (ret < 0) return ret;
ret = util_mount("/share", userDistro, NULL, MS_BIND | MS_REC, NULL, 0);
if (ret < 0) return ret;
if (g_addGui)
{
ret = util_mkdtemp(rootDir, &systemDistro);
if (ret < 0) return ret;
ret = util_mount("/wslg", systemDistro, NULL, MS_MOVE, NULL, 0);
if (ret < 0) return ret;
}
if (asprintf(&initMount, "%s%s", rootDir, initCommand) < 0)
{
LOG_ERROR("asprintf(%s)", rootDir);
return -1;
}
ret = mount_init(initMount);
if (ret < 0)
return ret;
free(initMount);
if (sock != LXSS_SERVER_FD)
{
ret = dup2(sock, LXSS_SERVER_FD);
if (ret < 0)
{
LOG_ERROR("dup2 %d", errno);
return ret;
}
close(sock);
sock = LXSS_SERVER_FD;
}
if (asprintf(&envp[envCount], "WSL2_CROSS_DISTRO=%s",
&userDistro[rootLen]) < 0)
{
LOG_ERROR("asprintf(%s)", userDistro);
return -1;
}
envCount++;
if (g_addGui)
{
envp[envCount] = "WSL2_GUI_APPS_ENABLED=1";
envCount++;
if (asprintf(&envp[envCount], "WSL2_SYSTEM_DISTRO_SHARE=%s",
&systemDistro[rootLen]) < 0)
{
LOG_ERROR("asprintf(%s)", systemDistro);
return -1;
}
envCount++;
}
if (vmId && *vmId)
{
if (asprintf(&envp[envCount], "WSL2_VM_ID=%s", vmId) < 0)
{
LOG_ERROR("asprintf(%s)", vmId);
return -1;
}
envCount++;
}
if (distroName && *distroName)
{
if (asprintf(&envp[envCount], "WSL2_DISTRO_NAME=%s", distroName) < 0)
{
LOG_ERROR("asprintf(%s)", distroName);
return -1;
}
envCount++;
}
if (sharedMem && *sharedMem)
{
if (asprintf(&envp[envCount], "WSL2_SHARED_MEMORY_OB_DIRECTORY=%s",
sharedMem) < 0)
{
LOG_ERROR("asprintf(%s)", sharedMem);
return -1;
}
envCount++;
}
if (envp[TOTAL_ENVCOUNT -1])
{
LOG_ERROR("envp[%d] has more member than expected", envCount);
exit(1);
}
ret = chdir(rootDir);
if (ret >= 0)
{
ret = mount(".", "/", NULL, MS_MOVE, NULL);
if (ret >= 0)
{
ret = chroot(".");
if (ret < 0)
LOG_ERROR("chroot %d", errno);
}
else
LOG_ERROR("mount %d", errno);
}
else
LOG_ERROR("chdir %d", errno);
if (g_addGui)
{
util_mount(&systemDistro[strlen(rootDir)], "/mnt/wslg", NULL, MS_MOVE, NULL, 0);
if (rmdir(&systemDistro[strlen(rootDir)]) < 0)
LOG_ERROR("rmdir %d", errno);
if (remove("/tmp/.X11-unix") < 0)
LOG_ERROR("remove %d", errno);
if (symlink("/mnt/wsl/.X11-unix", "/tmp/.X11-unix") < 0)
LOG_ERROR("symlink %d", errno);
}
execle(initCommand, initCommand, NULL, envp);
}
free(userDistro);
close(sock);
exit(ret);
return 0;
}
int start_overlay_init(int sock, char *rootDir, char *initCommand,
char *vmId, char *distroName, char *sharedMem)
{
int ret;
char *lowerDir = NULL, *overlayData = NULL;
ret = mount_overlay(rootDir, &lowerDir, &overlayData);
if (ret < 0) return ret;
ret = util_mount("/systemvhd", lowerDir, NULL, MS_BIND, NULL, 0);
if (ret < 0) return ret;
ret = util_mount(NULL, rootDir, "overlay", 0, overlayData, 0);
if (ret < 0) return ret;
free(lowerDir);
free(overlayData);
ret = start_init(sock, rootDir, initCommand, vmId, distroName, sharedMem);
exit(ret);
}