forked from zhaarey/wrapper
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathwrapper.c
More file actions
74 lines (62 loc) · 1.52 KB
/
wrapper.c
File metadata and controls
74 lines (62 loc) · 1.52 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
#define _GNU_SOURCE
#include <errno.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mount.h>
#include <unistd.h>
#include <string.h>
#include "cmdline.h"
pid_t child_proc = -1;
struct gengetopt_args_info args_info;
static void intHan(int signum) {
if (child_proc != -1) {
kill(child_proc, SIGKILL);
}
}
int main(int argc, char *argv[], char *envp[]) {
cmdline_parser(argc, argv, &args_info);
if (signal(SIGINT, intHan) == SIG_ERR) {
perror("signal");
return 1;
}
mkdir("./rootfs/proc", 0755);
if (mount("proc", "./rootfs/proc", "proc", 0, NULL) != 0) {
perror("mount proc");
return 1;
}
mount("/dev", "./rootfs/dev", NULL, MS_BIND, NULL);
if (chdir("./rootfs") != 0) {
perror("chdir");
return 1;
}
if (chroot("./") != 0) {
perror("chroot");
return 1;
}
chmod("/system/bin/linker64", 0755);
chmod("/system/bin/main", 0755);
if (unshare(CLONE_NEWPID)) {
perror("unshare");
return 1;
}
child_proc = fork();
if (child_proc == -1) {
perror("fork");
return 1;
}
if (child_proc > 0) {
wait(NULL);
return 0;
}
// Child process logic
mkdir(args_info.base_dir_arg, 0777);
mkdir(strcat(args_info.base_dir_arg, "/mpl_db"), 0777);
execve("/system/bin/main", argv, envp);
perror("execve");
return 1;
}