forked from Tasssadar/multirom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadb.c
172 lines (140 loc) · 3.71 KB
/
adb.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
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/mount.h>
#include <sys/klog.h>
#include <linux/loop.h>
#include "adb.h"
#include "util.h"
#include "multirom.h"
#include "log.h"
static pthread_t adb_thread;
static volatile int run_thread = 0;
static pid_t adb_pid = -1;
static char * const ENV[] = {
"PATH=/sbin:/bin:/usr/bin:/usr/sbin:/mrom_bin",
"LD_LIBRARY_PATH=.:/sbin",
"ANDROID_ROOT=/system",
"ANDROID_DATA=/data",
"EXTERNAL_STORAGE=/sdcard",
"ANDROID_PROPERTY_WORKSPACE=8,49152",
NULL
};
static void *adb_thread_work(void *c)
{
adb_init_usb();
if(adb_init_busybox() < 0)
return NULL;
adb_init_fs();
while(run_thread)
{
adb_pid = fork();
if(adb_pid == 0) // child
{
setsid();
umask(077);
setpgid(0, getpid());
static char * const cmd[] = { adbd_path, NULL };
execve(cmd[0], cmd, ENV);
exit(0);
}
else
{
int status = 0;
while(waitpid(adb_pid, &status, WNOHANG) == 0)
usleep(300000);
}
usleep(300000);
}
adb_cleanup();
return NULL;
}
void adb_init(void)
{
INFO("Starting adbd\n");
if(run_thread)
return;
run_thread = 1;
pthread_create(&adb_thread, NULL, adb_thread_work, NULL);
}
void adb_quit(void)
{
if(!run_thread)
return;
INFO("Stopping adbd\n");
run_thread = 0;
if(adb_pid != -1)
{
kill(adb_pid, 9);
adb_pid = -1;
}
pthread_join(adb_thread, NULL);
}
void adb_init_usb(void)
{
write_file("/sys/class/android_usb/android0/enable", "0");
char cmdline[1024];
char serial[64] = { 0 };
static const char *tag = "androidboot.serialno=";
if(multirom_get_cmdline(cmdline, sizeof(cmdline)) >= 0)
{
char *start = strstr(cmdline, tag);
if(start)
{
start += strlen(tag);
char *end = strchr(start, ' ');
if(end && end-start < (int)sizeof(serial))
strncpy(serial, start, end-serial);
}
}
write_file("/sys/class/android_usb/android0/idVendor", "18d1");
write_file("/sys/class/android_usb/android0/idProduct", "4e42");
write_file("/sys/class/android_usb/android0/functions", "adb");
write_file("/sys/class/android_usb/android0/iManufacturer", "unknown");
write_file("/sys/class/android_usb/android0/iProduct", "Nexus 7");
write_file("/sys/class/android_usb/android0/iSerial", serial);
write_file("/sys/class/android_usb/android0/enable", "1");
}
int adb_init_busybox(void)
{
mkdir("/mrom_bin", 0777);
copy_file(busybox_path, "/mrom_bin/busybox");
chmod("/mrom_bin/busybox", 0755);
static const char *install_cmd[] = {
"/mrom_bin/busybox", "--install", "/mrom_bin/", NULL
};
if(run_cmd((char**)install_cmd) != 0)
{
ERROR("adb: failed to --install busybox\n");
return -1;
}
mkdir("/dev/pts", 0666);
if(mount("devpts", "/dev/pts", "devpts", 0, NULL) < 0)
{
ERROR("Failed to mount devpts: %d (%s)\n", errno, strerror(errno));
return -1;
}
return 0;
}
void adb_init_fs(void)
{
mkdir("/sdcard", 0777);
if(strstr(adbd_path, "/realdata/media/0/multirom"))
mount("/realdata/media/0/", "/sdcard/", "auto", MS_BIND, "");
else
mount("/realdata/media/", "/sdcard/", "auto", MS_BIND, "");
}
void adb_cleanup(void)
{
if(umount("/sdcard") >= 0)
remove_dir("/sdcard");
remove_dir("/mrom_bin");
umount("/dev/pts");
rmdir("/dev/pts");
}