Skip to content

Commit 5d0adac

Browse files
committed
improve: code readability; fix: not properly starting up companions
This commit both improves the code readability and also fixes a bug that wouln't start companions properly.
1 parent da68db8 commit 5d0adac

4 files changed

Lines changed: 35 additions & 84 deletions

File tree

zygiskd-new/companion.c

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,9 @@ void *ExecuteNew(void *arg) {
6767

6868

6969
void entry(int fd) {
70-
LOGI("Previous error (2): %s\n", strerror(errno));
71-
7270
LOGI("companion entry fd: |%d|\n", fd);
7371

74-
LOGI("Reading name length\n");
75-
7672
char name[256 + 1];
77-
LOGI("Previous error(6): %s\n", strerror(errno));
7873

7974
/* INFO: Getting stuck here */
8075
ssize_t ret = read_string(fd, name, sizeof(name) - 1);
@@ -84,47 +79,47 @@ void entry(int fd) {
8479

8580
LOGI("Companion process requested for `%s`\n", name);
8681

87-
// int library_fd;
88-
// recv_fd(fd, &library_fd);
82+
int library_fd;
83+
recv_fd(fd, &library_fd);
8984

90-
// LOGI("Library fd: %d\n", library_fd);
85+
LOGI("Library fd: %d\n", library_fd);
9186

92-
// ZygiskCompanionEntryFn entry = load_module(library_fd);
87+
ZygiskCompanionEntryFn entry = load_module(library_fd);
9388

94-
// LOGI("Library loaded\n");
89+
LOGI("Library loaded\n");
9590

96-
// close(library_fd);
91+
close(library_fd);
9792

98-
// LOGI("Library closed\n");
93+
LOGI("Library closed\n");
9994

100-
// if (entry == NULL) {
101-
// LOGI("No companion entry for: %s\n", name);
95+
if (entry == NULL) {
96+
LOGI("No companion entry for: %s\n", name);
10297

103-
// write(fd, (void *)0, 1);
98+
write(fd, (void *)0, 1);
10499

105-
// return;
106-
// }
100+
return;
101+
}
107102

108-
// LOGI("Companion process created for: %s\n", name);
103+
LOGI("Companion process created for: %s\n", name);
109104

110-
// write(fd, (void *)1, 1);
105+
uint8_t response = 1;
106+
write(fd, &response, sizeof(response));
111107

112-
// while (1) {
113-
// int client_fd;
114-
// recv_fd(fd, &client_fd);
108+
while (1) {
109+
int client_fd;
110+
recv_fd(fd, &client_fd);
115111

116-
// LOGI("New companion request from module \"%s\" with fd \"%d\"\n", name, client_fd);
112+
LOGI("New companion request from module \"%s\" with fd \"%d\"\n", name, client_fd);
117113

118-
// int response = 1;
119-
// write(fd, (void *)&response, sizeof(response));
114+
write(fd, &response, sizeof(response));
120115

121-
// int *client_fd_ptr = malloc(sizeof(int));
122-
// *client_fd_ptr = client_fd;
116+
int *client_fd_ptr = malloc(sizeof(int));
117+
*client_fd_ptr = client_fd;
123118

124-
// LOGI("Creating new thread for companion request\n");
119+
LOGI("Creating new thread for companion request\n");
125120

126-
// pthread_t thread;
127-
// pthread_create(&thread, NULL, ExecuteNew, (void *)client_fd_ptr);
128-
// pthread_detach(thread);
129-
// }
121+
pthread_t thread;
122+
pthread_create(&thread, NULL, ExecuteNew, (void *)client_fd_ptr);
123+
pthread_detach(thread);
124+
}
130125
}

zygiskd-new/main.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ int main(int argc, char *argv[]) {
3131
return 1;
3232
}
3333

34-
LOGI("Previous error{3}: %s\n", strerror(errno));
35-
3634
int fd = atoi(argv[2]);
37-
LOGI("Previous error(4): %s\n", strerror(errno));
38-
3935
entry(fd);
4036

4137
return 0;
@@ -82,11 +78,8 @@ int main(int argc, char *argv[]) {
8278
}
8379
}
8480

85-
LOGI("Previous error{27}: %s\n", strerror(errno));
8681
switch_mount_namespace((pid_t)1);
87-
LOGI("Previous error{28}: %s\n", strerror(errno));
8882
root_impls_setup();
89-
LOGI("Previous error{29}: %s\n", strerror(errno));
9083
zygiskd_start();
9184

9285
return 0;

zygiskd-new/utils.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,7 @@ ssize_t write_string(int fd, const char *str) {
269269
ssize_t read_string(int fd, char *str, size_t len) {
270270
size_t str_len_buf[1];
271271

272-
LOGI("Reading string length from fd %d\n", fd);
273-
LOGI("Previous error: %s\n", strerror(errno));
274-
275272
ssize_t read_bytes = read(fd, &str_len_buf, sizeof(str_len_buf));
276-
277-
LOGI("Read %zd bytes\n", read_bytes);
278273
if (read_bytes != (ssize_t)sizeof(str_len_buf)) {
279274
LOGE("Failed to read string length: %s\n", strerror(errno));
280275

zygiskd-new/zygiskd.c

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,13 @@ static void load_modules(enum Architecture arch, struct Context *context) {
148148
context->len = 0;
149149
context->modules = malloc(1);
150150

151-
// LOGI("Previous error{25}: %s\n", strerror(errno));
152-
153151
DIR *dir = opendir(PATH_MODULES_DIR);
154152
if (dir == NULL) {
155153
LOGE("Failed opening modules directory: %s.", PATH_MODULES_DIR);
156154

157155
return;
158156
}
159157

160-
// LOGI("Previous error{27}: %s\n", strerror(errno));
161-
162158
char arch_str[32];
163159
switch (arch) {
164160
case ARM32: {
@@ -184,11 +180,9 @@ static void load_modules(enum Architecture arch, struct Context *context) {
184180
}
185181

186182
LOGI("Loading modules for architecture: %s\n", arch_str);
187-
// LOGI("Previous error{28}: %s\n", strerror(errno));
188183

189184
struct dirent *entry;
190185
while ((entry = readdir(dir)) != NULL) {
191-
// LOGI("Previous error{29}: %s\n", strerror(errno));
192186
if (entry->d_type != DT_DIR) continue; /* INFO: Only directories */
193187
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0 || strcmp(entry->d_name, "zygisksu") == 0) continue;
194188

@@ -244,8 +238,6 @@ static int create_daemon_socket(void) {
244238
}
245239

246240
static int spawn_companion(char *name, int lib_fd) {
247-
// LOGI("Previous error{15}: %s\n", strerror(errno));
248-
249241
int sockets[2];
250242
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) {
251243
LOGE("Failed creating socket pair.\n");
@@ -259,8 +251,6 @@ static int spawn_companion(char *name, int lib_fd) {
259251
LOGI("Companion fd: %d\n", companion_fd);
260252
LOGI("Daemon fd: %d\n", daemon_fd);
261253

262-
// LOGI("Previous error{13}: %s\n", strerror(errno));
263-
264254
pid_t pid = fork();
265255
LOGI("Forked: %d\n", pid);
266256
if (pid < 0) {
@@ -271,20 +261,16 @@ static int spawn_companion(char *name, int lib_fd) {
271261

272262
exit(1);
273263
} else if (pid > 0) {
274-
// LOGI("Previous error{11}: %s\n", strerror(errno));
275-
276264
close(companion_fd);
277265

278266
LOGI("Waiting for companion to start (%d)\n", pid);
279-
// LOGI("Previous error{8}: %s\n", strerror(errno));
280267

281-
int status;
282-
waitpid(pid, &status, 0);
268+
int status = 0;
269+
// waitpid(pid, &status, 0);
283270

284271
LOGI("Companion exited with status %d\n", status);
285-
// LOGI("Previous error{9}: %s\n", strerror(errno));
286272

287-
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
273+
// if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
288274
if (write_string(daemon_fd, name) == -1) return -1;
289275

290276
if (send_fd(daemon_fd, lib_fd) == -1) return -1;
@@ -302,13 +288,13 @@ static int spawn_companion(char *name, int lib_fd) {
302288
if (response == 0) return -2;
303289
else if (response == 1) return daemon_fd;
304290
else return -2;
305-
} else {
306-
LOGE("Exited with status %d\n", status);
291+
// } else {
292+
// LOGE("Exited with status %d\n", status);
307293

308-
close(daemon_fd);
294+
// close(daemon_fd);
309295

310-
return -1;
311-
}
296+
// return -1;
297+
// }
312298
/* INFO: if pid == 0: */
313299
} else {
314300
LOGI("Companion started (%d)\n", pid);
@@ -320,7 +306,6 @@ static int spawn_companion(char *name, int lib_fd) {
320306
snprintf(companion_fd_str, 32, "%d", companion_fd);
321307

322308
LOGI("Executing companion...\n");
323-
// LOGI("Previous error{10}: %s\n", strerror(errno));
324309

325310
char *argv[] = { ZYGISKD_FILE, "companion", companion_fd_str, NULL };
326311
if (execv(ZYGISKD_PATH, argv) == -1) {
@@ -344,20 +329,14 @@ struct __attribute__((__packed__)) MsgHead {
344329
void zygiskd_start(void) {
345330
LOGI("Welcome to ReZygisk %s!", ZKSU_VERSION);
346331

347-
// LOGI("Previous error{26}: %s\n", strerror(errno));
348-
349332
enum Architecture arch = get_arch();
350333

351-
// LOGI("Previous error{25}: %s\n", strerror(errno));
352-
353334
struct Context context;
354335
load_modules(arch, &context);
355336

356337
struct MsgHead *msg = NULL;
357338
size_t msg_sz = 0;
358339

359-
// LOGI("Previous error{24}: %s\n", strerror(errno));
360-
361340
switch (get_impl()) {
362341
case None: {
363342
/* INFO: Stop, compiler. */
@@ -425,14 +404,10 @@ void zygiskd_start(void) {
425404
}
426405
}
427406

428-
// LOGI("Previous error{22}: %s\n", strerror(errno));
429-
430407
unix_datagram_sendto(CONTROLLER_SOCKET, (void *)msg, msg_sz);
431408

432409
free(msg);
433410

434-
// LOGI("Previous error{21}: %s\n", strerror(errno));
435-
436411
int socket_fd = create_daemon_socket();
437412
if (socket_fd == -1) {
438413
LOGE("Failed creating daemon socket\n");
@@ -441,7 +416,6 @@ void zygiskd_start(void) {
441416
}
442417

443418
while (1) {
444-
// LOGI("Previous error{20}: %s\n", strerror(errno));
445419
int client_fd = accept(socket_fd, NULL, NULL);
446420
if (client_fd == -1) {
447421
LOGE("accept: %s\n", strerror(errno));
@@ -451,8 +425,6 @@ void zygiskd_start(void) {
451425

452426
LOGI("Accepted client: %d\n", client_fd);
453427

454-
// LOGI("Previous error{19}: %s\n", strerror(errno));
455-
456428
unsigned char buf[1];
457429
ssize_t len = read(client_fd, buf, sizeof(buf));
458430
if (len == -1) {
@@ -627,15 +599,11 @@ void zygiskd_start(void) {
627599
}
628600
case RequestCompanionSocket: {
629601
LOGI("Requesting companion socket\n");
630-
631-
// LOGI("Previous error{17}: %s\n", strerror(errno));
632602

633603
size_t index_buf[1];
634604
ssize_t ret = read(client_fd, &index_buf, sizeof(index_buf));
635605
ASSURE_SIZE_READ_BREAK("RequestCompanionSocket", "index", ret, sizeof(index_buf));
636606

637-
// LOGI("Previous error{16}: %s\n", strerror(errno));
638-
639607
size_t index = index_buf[0];
640608

641609
struct Module *module = &context.modules[index];

0 commit comments

Comments
 (0)