Skip to content
This repository was archived by the owner on Sep 2, 2021. It is now read-only.

Commit 89b2e39

Browse files
committed
Read config from rirud first
1 parent fb50cf4 commit 89b2e39

File tree

11 files changed

+316
-20
lines changed

11 files changed

+316
-20
lines changed

module.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ ext {
33
moduleName = "Location Report Enabler"
44
moduleAuthor = "Rikka"
55
moduleDescription = "Enable Google Feed & Timeline & Location reprot in unsupported regions by changing system properties in related packages."
6-
moduleVersion = "v10.0"
7-
moduleVersionCode = 13
6+
moduleVersion = "v10.1"
7+
moduleVersionCode = 14
88
moduleMinRiruApiVersion = 9
9-
moduleMinRiruVersionName = "v22.0"
10-
moduleMaxRiruApiVersion = 9
9+
moduleMinRiruVersionName = "v23.0"
10+
moduleMaxRiruApiVersion = 10
1111

1212
moduleProp = [
1313
name : moduleName,

module/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ repositories {
3636
}
3737

3838
dependencies {
39-
implementation 'rikka.ndk:riru:9.1'
39+
implementation 'rikka.ndk:riru:10'
4040
implementation 'rikka.ndk.thirdparty:xhook:1.2.0'
4141
implementation 'rikka.ndk.thirdparty:nativehelper:20201111'
4242
}

module/src/main/cpp/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ find_package(riru REQUIRED CONFIG)
3535
find_package(nativehelper REQUIRED CONFIG)
3636
find_package(xhook REQUIRED CONFIG)
3737
38-
add_library(${MODULE_NAME} SHARED main.cpp hook.cpp android.cpp misc.cpp config.cpp)
38+
add_library(${MODULE_NAME} SHARED main.cpp hook.cpp android.cpp misc.cpp config.cpp socket.cpp rirud.cpp)
3939
target_link_libraries(${MODULE_NAME} log riru::riru nativehelper::nativehelper_header_only xhook::xhook)
4040
set_target_properties(${MODULE_NAME} PROPERTIES LINK_FLAGS_RELEASE -s)

module/src/main/cpp/config.cpp

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "config.h"
88
#include "misc.h"
99
#include "logging.h"
10+
#include "rirud.h"
1011

1112
using namespace Config;
1213

@@ -51,21 +52,50 @@ void Packages::Add(const char *name) {
5152
}
5253

5354
void Config::Load() {
54-
foreach_dir(PROPS_PATH, [](int dirfd, struct dirent *entry) {
55+
if (!rirud::ForeachDir(PROPS_PATH, [](struct dirent *entry, bool *) {
56+
if (entry->d_name[0] == '.') return;
57+
58+
char path[PATH_MAX];
59+
char *buf;
60+
size_t size;
61+
5562
auto name = entry->d_name;
56-
int fd = openat(dirfd, name, O_RDONLY);
57-
if (fd == -1) return;
63+
snprintf(path, PATH_MAX, "%s/%s", PROPS_PATH, name);
5864

59-
char buf[PROP_VALUE_MAX]{0};
60-
if (read(fd, buf, PROP_VALUE_MAX) >= 0) {
65+
if (rirud::ReadFile(path, buf, size)) {
6166
Properties::Put(name, buf);
67+
if (buf) free(buf);
6268
}
69+
})) {
70+
LOGD("read props from rirud failed");
71+
72+
foreach_dir(PROPS_PATH, [](int dirfd, struct dirent *entry, bool *) {
73+
auto name = entry->d_name;
74+
int fd = openat(dirfd, name, O_RDONLY);
75+
if (fd == -1) return;
76+
77+
char buf[PROP_VALUE_MAX]{0};
78+
if (read(fd, buf, PROP_VALUE_MAX) >= 0) {
79+
Properties::Put(name, buf);
80+
}
81+
82+
close(fd);
83+
});
84+
} else {
85+
LOGD("read props from rirud");
86+
}
6387

64-
close(fd);
65-
});
66-
67-
foreach_dir(PACKAGES_PATH, [](int, struct dirent *entry) {
88+
if (!rirud::ForeachDir(PACKAGES_PATH, [](struct dirent *entry, bool *) {
89+
if (entry->d_name[0] == '.') return;
6890
auto name = entry->d_name;
6991
Packages::Add(name);
70-
});
92+
})) {
93+
LOGD("read packages from rirud failed");
94+
foreach_dir(PACKAGES_PATH, [](int, struct dirent *entry, bool *) {
95+
auto name = entry->d_name;
96+
Packages::Add(name);
97+
});
98+
} else {
99+
LOGD("read packages from rirud");
100+
}
71101
}

module/src/main/cpp/main.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ void *init(void *arg) {
119119
}
120120
case 2: {
121121
switch (riru_api_version) {
122+
case 10:
122123
case 9: {
123124
riru_api_v9 = (RiruApiV9 *) arg;
124125

module/src/main/cpp/misc.cpp

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,60 @@
11
#include <dirent.h>
2+
#include <unistd.h>
3+
#include <cerrno>
24

3-
int foreach_dir(const char *path, void(*callback)(int, struct dirent *)) {
5+
bool foreach_dir(const char *path, void(*callback)(int, struct dirent *, bool *)) {
46
DIR *dir;
57
struct dirent *entry;
68
int fd;
9+
bool continue_read = true;
710

811
if ((dir = opendir(path)) == nullptr)
9-
return -1;
12+
return false;
1013

1114
fd = dirfd(dir);
1215

1316
while ((entry = readdir(dir))) {
1417
if (entry->d_name[0] == '.') continue;
15-
callback(fd, entry);
18+
callback(fd, entry, &continue_read);
19+
if (!continue_read) break;
1620
}
1721

1822
closedir(dir);
23+
return true;
24+
}
1925

26+
static ssize_t read_eintr(int fd, void *out, size_t len) {
27+
ssize_t ret;
28+
do {
29+
ret = read(fd, out, len);
30+
} while (ret < 0 && errno == EINTR);
31+
return ret;
32+
}
33+
34+
int read_full(int fd, void *out, size_t len) {
35+
while (len > 0) {
36+
ssize_t ret = read_eintr(fd, out, len);
37+
if (ret <= 0) {
38+
return -1;
39+
}
40+
out = (void *) ((uintptr_t) out + ret);
41+
len -= ret;
42+
}
43+
return 0;
44+
}
45+
46+
int write_full(int fd, const void *buf, size_t count) {
47+
while (count > 0) {
48+
ssize_t size = write(fd, buf, count < SSIZE_MAX ? count : SSIZE_MAX);
49+
if (size == -1) {
50+
if (errno == EINTR)
51+
continue;
52+
else
53+
return -1;
54+
}
55+
56+
buf = (const void *) ((uintptr_t) buf + size);
57+
count -= size;
58+
}
2059
return 0;
2160
}

module/src/main/cpp/misc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
#include <dirent.h>
44

5-
int foreach_dir(const char *path, void(*callback)(int dirfd, struct dirent * entry));
5+
bool foreach_dir(const char *path, void(*callback)(int, struct dirent *, bool *));
6+
int read_full(int fd, void *buf, size_t count);
7+
int write_full(int fd, const void *buf, size_t count);

module/src/main/cpp/rirud.cpp

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
#include <sys/socket.h>
2+
#include <sys/un.h>
3+
#include <unistd.h>
4+
#include <cinttypes>
5+
#include <malloc.h>
6+
#include <dirent.h>
7+
#include "riru.h"
8+
#include "rirud.h"
9+
#include "logging.h"
10+
#include "socket.h"
11+
#include "misc.h"
12+
13+
#define SOCKET_ADDRESS "rirud"
14+
15+
static const uint32_t ACTION_READ_FILE = 4;
16+
static const uint32_t ACTION_READ_DIR = 5;
17+
18+
bool rirud::ReadFile(const char *path, char *&bytes, size_t &bytes_size) {
19+
if (riru_api_version < 10) return false;
20+
21+
struct sockaddr_un addr{};
22+
uint32_t path_size = strlen(path);
23+
int32_t reply;
24+
int32_t file_size;
25+
int fd;
26+
socklen_t socklen;
27+
uint32_t buffer_size = 1024 * 8;
28+
bool res = false;
29+
30+
bytes = nullptr;
31+
bytes_size = 0;
32+
33+
if ((fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0) {
34+
PLOGE("socket");
35+
goto clean;
36+
}
37+
38+
socklen = setup_sockaddr(&addr, SOCKET_ADDRESS);
39+
40+
if (connect(fd, (struct sockaddr *) &addr, socklen) == -1) {
41+
PLOGE("connect %s", SOCKET_ADDRESS);
42+
goto clean;
43+
}
44+
45+
if (write_full(fd, &ACTION_READ_FILE, sizeof(uint32_t)) != 0
46+
|| write_full(fd, &path_size, sizeof(uint32_t)) != 0
47+
|| write_full(fd, path, path_size) != 0) {
48+
PLOGE("write %s", SOCKET_ADDRESS);
49+
goto clean;
50+
}
51+
52+
if (read_full(fd, &reply, sizeof(int32_t)) != 0) {
53+
PLOGE("read %s", SOCKET_ADDRESS);
54+
goto clean;
55+
}
56+
57+
if (reply != 0) {
58+
LOGE("open %s failed with %d from remote: %s", path, reply, strerror(reply));
59+
errno = reply;
60+
goto clean;
61+
}
62+
63+
if (read_full(fd, &file_size, sizeof(uint32_t)) != 0) {
64+
PLOGE("read %s", SOCKET_ADDRESS);
65+
goto clean;
66+
}
67+
68+
LOGD("%s size %d", path, file_size);
69+
70+
if (file_size > 0) {
71+
bytes = (char *) malloc(file_size);
72+
while (file_size > 0) {
73+
LOGD("attempt to read %d bytes", (int) buffer_size);
74+
auto read_size = TEMP_FAILURE_RETRY(read(fd, bytes + bytes_size, buffer_size));
75+
if (read_size == -1) {
76+
PLOGE("read");
77+
goto clean;
78+
}
79+
80+
file_size -= read_size;
81+
bytes_size += read_size;
82+
LOGD("read %d bytes (total %d)", (int) read_size, (int) bytes_size);
83+
}
84+
res = true;
85+
} else if (file_size == 0) {
86+
while (true) {
87+
if (bytes == nullptr) {
88+
bytes = (char *) malloc(buffer_size);
89+
} else {
90+
bytes = (char *) realloc(bytes, bytes_size + buffer_size);
91+
}
92+
93+
LOGD("attempt to read %d bytes", (int) buffer_size);
94+
auto read_size = TEMP_FAILURE_RETRY(read(fd, bytes + bytes_size, buffer_size));
95+
if (read_size == -1) {
96+
PLOGE("read");
97+
goto clean;
98+
}
99+
if (read_size == 0) {
100+
res = true;
101+
goto clean;
102+
}
103+
104+
bytes_size += read_size;
105+
LOGD("read %d bytes (total %d)", (int) read_size, (int) bytes_size);
106+
}
107+
}
108+
109+
110+
clean:
111+
if (fd != -1) close(fd);
112+
return res;
113+
}
114+
115+
bool rirud::ForeachDir(const char *path, void(*callback)(struct dirent *, bool *continue_read)) {
116+
if (riru_api_version < 10) return false;
117+
118+
struct sockaddr_un addr{};
119+
uint32_t path_size = strlen(path);
120+
int32_t reply;
121+
int fd;
122+
socklen_t socklen;
123+
bool res = false;
124+
bool continue_read = true;
125+
126+
dirent dirent{};
127+
128+
if ((fd = socket(PF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)) < 0) {
129+
PLOGE("socket");
130+
goto clean;
131+
}
132+
133+
socklen = setup_sockaddr(&addr, SOCKET_ADDRESS);
134+
135+
if (connect(fd, (struct sockaddr *) &addr, socklen) == -1) {
136+
PLOGE("connect %s", SOCKET_ADDRESS);
137+
goto clean;
138+
}
139+
140+
if (write_full(fd, &ACTION_READ_DIR, sizeof(uint32_t)) != 0
141+
|| write_full(fd, &path_size, sizeof(uint32_t)) != 0
142+
|| write_full(fd, path, path_size) != 0) {
143+
PLOGE("write %s", SOCKET_ADDRESS);
144+
goto clean;
145+
}
146+
147+
if (read_full(fd, &reply, sizeof(int32_t)) != 0) {
148+
PLOGE("read %s", SOCKET_ADDRESS);
149+
goto clean;
150+
}
151+
152+
if (reply != 0) {
153+
LOGE("opendir %s failed with %d from remote: %s", path, reply, strerror(reply));
154+
errno = reply;
155+
goto clean;
156+
}
157+
158+
while (true) {
159+
if (write_full(fd, &continue_read, sizeof(uint8_t)) != 0) {
160+
PLOGE("write %s", SOCKET_ADDRESS);
161+
goto clean;
162+
}
163+
164+
if (read_full(fd, &reply, sizeof(int32_t)) != 0) {
165+
PLOGE("read %s", SOCKET_ADDRESS);
166+
goto clean;
167+
}
168+
169+
if (reply == -1) {
170+
goto clean;
171+
}
172+
173+
if (reply != 0) {
174+
LOGE("opendir %s failed with %d from remote: %s", path, reply, strerror(reply));
175+
continue;
176+
}
177+
178+
if (read_full(fd, &dirent.d_type, sizeof(unsigned char)) != 0
179+
|| read_full(fd, dirent.d_name, 256) != 0) {
180+
PLOGE("read %s", SOCKET_ADDRESS);
181+
goto clean;
182+
}
183+
184+
callback(&dirent, &continue_read);
185+
}
186+
187+
clean:
188+
if (fd != -1) close(fd);
189+
return res;
190+
}

module/src/main/cpp/rirud.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
namespace rirud {
4+
5+
bool ReadFile(const char *path, char *&bytes, size_t &bytes_size);
6+
bool ForeachDir(const char *path, void(*callback)(struct dirent *, bool *continue_read));
7+
}

0 commit comments

Comments
 (0)