Skip to content

Commit 1a3f497

Browse files
committed
improve: port common code to C
This commit ports even more C++ code to C99, now, the codes available in the "common" folder.
1 parent 5b3d9c7 commit 1a3f497

20 files changed

Lines changed: 634 additions & 858 deletions

loader/src/common/daemon.c

Lines changed: 365 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
1+
// #include <unistd.h>
2+
// #include <sys/types.h>
3+
// #include <sys/stat.h>
4+
// #include <dirent.h>
5+
// #include <fcntl.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <stdbool.h>
9+
#include <sys/socket.h>
10+
11+
#include <linux/un.h>
12+
13+
#include "logging.h"
14+
#include "socket_utils.h"
15+
16+
#include "daemon.h"
17+
18+
char daemon_path[PATH_MAX];
19+
20+
void rezygiskd_init(const char *path) {
21+
snprintf(daemon_path, sizeof(daemon_path), "%s/%s", path, SOCKET_FILE_NAME);
22+
}
23+
24+
void rezygiskd_get_path(char *buf, size_t buf_size) {
25+
size_t fileless_daemon_path = strlen(daemon_path) - strlen("/") - strlen(SOCKET_FILE_NAME);
26+
27+
strncpy(buf, daemon_path, buf_size > fileless_daemon_path ? fileless_daemon_path : buf_size);
28+
buf[fileless_daemon_path] = '\0';
29+
}
30+
31+
int rezygiskd_connect(uint8_t retry) {
32+
retry++;
33+
34+
int fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0);
35+
if (fd == -1) {
36+
PLOGE("socket create");
37+
38+
return -1;
39+
}
40+
41+
struct sockaddr_un addr = {
42+
.sun_family = AF_UNIX,
43+
.sun_path = { 0 }
44+
};
45+
46+
/*
47+
INFO: Application must assume that sun_path can hold _POSIX_PATH_MAX characters.
48+
49+
Sources:
50+
- https://pubs.opengroup.org/onlinepubs/009696699/basedefs/sys/un.h.html
51+
*/
52+
strcpy(addr.sun_path, daemon_path);
53+
socklen_t socklen = sizeof(addr);
54+
55+
while (--retry) {
56+
int ret = connect(fd, (struct sockaddr *)&addr, socklen);
57+
if (ret == 0) return fd;
58+
if (retry) {
59+
PLOGE("Retrying to connect to ReZygiskd, sleep 1s");
60+
61+
sleep(1);
62+
}
63+
}
64+
65+
close(fd);
66+
67+
return -1;
68+
}
69+
70+
bool rezygiskd_ping() {
71+
int fd = rezygiskd_connect(5);
72+
if (fd == -1) {
73+
PLOGE("connection to ReZygiskd");
74+
75+
return false;
76+
}
77+
78+
write_uint8_t(fd, (uint8_t)PingHeartbeat);
79+
80+
close(fd);
81+
82+
return true;
83+
}
84+
85+
uint32_t rezygiskd_get_process_flags(uid_t uid) {
86+
int fd = rezygiskd_connect(1);
87+
if (fd == -1) {
88+
PLOGE("connection to ReZygiskd");
89+
90+
return 0;
91+
}
92+
93+
write_uint8_t(fd, (uint8_t)GetProcessFlags);
94+
write_uint32_t(fd, (uint32_t)uid);
95+
96+
uint32_t res = 0;
97+
read_uint32_t(fd, &res);
98+
99+
close(fd);
100+
101+
return res;
102+
}
103+
104+
void rezygiskd_get_info(struct rezygisk_info *info) {
105+
int fd = rezygiskd_connect(1);
106+
if (fd == -1) {
107+
PLOGE("connection to ReZygiskd");
108+
109+
info->running = false;
110+
111+
return;
112+
}
113+
114+
info->running = true;
115+
116+
write_uint8_t(fd, (uint8_t)GetInfo);
117+
118+
uint32_t flags = 0;
119+
read_uint32_t(fd, &flags);
120+
121+
if (flags & (1 << 27)) info->root_impl = ROOT_IMPL_APATCH;
122+
else if (flags & (1 << 29)) info->root_impl = ROOT_IMPL_KERNELSU;
123+
else if (flags & (1 << 30)) info->root_impl = ROOT_IMPL_MAGISK;
124+
else info->root_impl = ROOT_IMPL_NONE;
125+
126+
read_uint32_t(fd, (uint32_t *)&info->pid);
127+
128+
read_size_t(fd, &info->modules->modules_count);
129+
if (info->modules->modules_count == 0) {
130+
info->modules->modules = NULL;
131+
132+
close(fd);
133+
134+
return;
135+
}
136+
137+
info->modules->modules = (char **)malloc(sizeof(char *) * info->modules->modules_count);
138+
if (info->modules->modules == NULL) {
139+
PLOGE("allocating modules name memory");
140+
141+
free(info->modules);
142+
info->modules = NULL;
143+
info->modules->modules_count = 0;
144+
145+
close(fd);
146+
147+
return;
148+
}
149+
150+
for (size_t i = 0; i < info->modules->modules_count; i++) {
151+
char *module_name = read_string(fd);
152+
if (module_name == NULL) {
153+
PLOGE("reading module name");
154+
155+
info->modules->modules_count = i;
156+
157+
free_rezygisk_info(info);
158+
159+
info->modules = NULL;
160+
info->modules->modules_count = 0;
161+
162+
close(fd);
163+
164+
return;
165+
}
166+
167+
char module_path[PATH_MAX];
168+
snprintf(module_path, sizeof(module_path), "/data/adb/modules/%s/module.prop", module_name);
169+
170+
FILE *module_prop = fopen(module_path, "r");
171+
if (!module_prop) {
172+
PLOGE("failed to open module prop file %s", module_path);
173+
174+
info->modules->modules_count = i;
175+
176+
free_rezygisk_info(info);
177+
178+
info->modules = NULL;
179+
info->modules->modules_count = 0;
180+
181+
close(fd);
182+
183+
return;
184+
}
185+
186+
char line[1024];
187+
while (fgets(line, sizeof(line), module_prop) != NULL) {
188+
if (strncmp(line, "name=", strlen("name=")) != 0) continue;
189+
190+
info->modules->modules[i] = strndup(line + 5, strlen(line) - 6);
191+
192+
break;
193+
}
194+
195+
fclose(module_prop);
196+
}
197+
198+
close(fd);
199+
}
200+
201+
void free_rezygisk_info(struct rezygisk_info *info) {
202+
if (info->modules->modules) {
203+
for (size_t i = 0; i < info->modules->modules_count; i++) {
204+
free(info->modules->modules[i]);
205+
}
206+
207+
free(info->modules->modules);
208+
}
209+
210+
free(info->modules);
211+
}
212+
213+
bool rezygiskd_read_modules(struct zygisk_modules *modules) {
214+
int fd = rezygiskd_connect(1);
215+
if (fd == -1) {
216+
PLOGE("connection to ReZygiskd");
217+
218+
return false;
219+
}
220+
221+
write_uint8_t(fd, (uint8_t)ReadModules);
222+
223+
size_t len = 0;
224+
read_size_t(fd, &len);
225+
226+
modules->modules = malloc(len * sizeof(char *));
227+
if (!modules->modules) {
228+
PLOGE("allocating modules name memory");
229+
230+
close(fd);
231+
232+
return false;
233+
}
234+
modules->modules_count = len;
235+
236+
for (size_t i = 0; i < len; i++) {
237+
char *lib_path = read_string(fd);
238+
if (!lib_path) {
239+
PLOGE("reading module lib_path");
240+
241+
close(fd);
242+
243+
return false;
244+
}
245+
246+
modules->modules[i] = lib_path;
247+
}
248+
249+
close(fd);
250+
251+
return true;
252+
}
253+
254+
void free_modules(struct zygisk_modules *modules) {
255+
if (modules->modules) {
256+
for (size_t i = 0; i < modules->modules_count; i++) {
257+
free(modules->modules[i]);
258+
}
259+
260+
free(modules->modules);
261+
}
262+
}
263+
264+
int rezygiskd_connect_companion(size_t index) {
265+
int fd = rezygiskd_connect(1);
266+
if (fd == -1) {
267+
PLOGE("connection to ReZygiskd");
268+
269+
return -1;
270+
}
271+
272+
write_uint8_t(fd, (uint8_t)RequestCompanionSocket);
273+
write_size_t(fd, index);
274+
275+
uint8_t res = 0;
276+
read_uint8_t(fd, &res);
277+
278+
if (res == 1) return fd;
279+
else {
280+
close(fd);
281+
282+
return -1;
283+
}
284+
}
285+
286+
int rezygiskd_get_module_dir(size_t index) {
287+
int fd = rezygiskd_connect(1);
288+
if (fd == -1) {
289+
PLOGE("connection to ReZygiskd");
290+
291+
return -1;
292+
}
293+
294+
write_uint8_t(fd, (uint8_t)GetModuleDir);
295+
write_size_t(fd, index);
296+
297+
int dirfd = read_fd(fd);
298+
299+
close(fd);
300+
301+
return dirfd;
302+
}
303+
304+
void rezygiskd_zygote_restart() {
305+
int fd = rezygiskd_connect(1);
306+
if (fd == -1) {
307+
if (errno == ENOENT) LOGD("Could not notify ZygoteRestart (maybe it hasn't been created)");
308+
else PLOGE("Could not notify ZygoteRestart");
309+
310+
return;
311+
}
312+
313+
if (!write_uint8_t(fd, (uint8_t)ZygoteRestart))
314+
PLOGE("Failed to request ZygoteRestart");
315+
316+
close(fd);
317+
}
318+
319+
void rezygiskd_system_server_started() {
320+
int fd = rezygiskd_connect(1);
321+
if (fd == -1) {
322+
PLOGE("Failed to report system server started");
323+
324+
return;
325+
}
326+
327+
if (!write_uint8_t(fd, (uint8_t)SystemServerStarted))
328+
PLOGE("Failed to request SystemServerStarted");
329+
330+
close(fd);
331+
}
332+
333+
bool rezygiskd_update_mns(enum mount_namespace_state nms_state, char *buf, size_t buf_size) {
334+
int fd = rezygiskd_connect(1);
335+
if (fd == -1) {
336+
PLOGE("connection to ReZygiskd");
337+
338+
return false;
339+
}
340+
341+
write_uint8_t(fd, (uint8_t)UpdateMountNamespace);
342+
write_uint32_t(fd, (uint32_t)getpid());
343+
write_uint8_t(fd, (uint8_t)nms_state);
344+
345+
uint32_t target_pid = 0;
346+
read_uint32_t(fd, &target_pid);
347+
if (target_pid == 0) {
348+
close(fd);
349+
350+
return false;
351+
}
352+
353+
int target_fd = read_fd(fd);
354+
if (target_fd == -1) {
355+
close(fd);
356+
357+
return false;
358+
}
359+
360+
snprintf(buf, buf_size, "/proc/%u/fd/%u", target_pid, target_fd);
361+
362+
close(fd);
363+
364+
return true;
365+
}

0 commit comments

Comments
 (0)