Skip to content

Commit 2342327

Browse files
author
AXON
committed
Major improvements
1 parent 5e10b98 commit 2342327

File tree

8 files changed

+90
-39
lines changed

8 files changed

+90
-39
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
[package]
22
name = "rusgik"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
authors = ["AXON <[email protected]>"]
55
edition = "2018"
66

77
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
88

99
[dependencies]
10-
nix = "0.19.1"
10+
nix = "0.19.1"
File renamed without changes.

src/asset/magisk.apk

3.01 MB
Binary file not shown.
File renamed without changes.
File renamed without changes.

src/config/su

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
# su daemon
1+
on early-init
2+
export PATH /sbin:/bin:/system/bin:/system/xbin:/system/vendor/bin:/gearlock/bin:/apex/com.android.runtime/bin:/apex/com.android.art/bin
3+
24
service su_daemon /sbin/magisk --daemon
35
seclabel u:r:su:s0
46
oneshot
57

6-
on property:persist.sys.root_access=0
8+
on boot
79
start su_daemon
8-
9-
on property:persist.sys.root_access=2
10-
start su_daemon
11-
12-
on property:persist.sys.root_access=1
13-
start su_daemon
14-
15-
on property:persist.sys.root_access=3
16-
start su_daemon
17-
18-
on post-fs-data
1910
exec u:r:su:s0 -- /sbin/magisk --post-fs-data
2011

2112
on property:sys.boot_completed=1
2213
exec u:r:su:s0 -- /sbin/magisk --service
23-
exec u:r:su:s0 -- /sbin/magisk --boot-complete
14+
exec u:r:su:s0 -- /sbin/magisk --boot-complete

src/main.rs

Lines changed: 82 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ pub fn init_magisk() {
7373
let superuser_config = "/init.superuser.rc";
7474
let magisk_config = "/sbin/.magisk/config";
7575
let magisk_bin = "/sbin/magisk";
76+
let magisk_apk_dir = "/system/priv-app/MagiskSu";
7677

7778
match fs::write(superuser_config, include_bytes!("config/su")) {
7879
Ok(_) => {
@@ -98,8 +99,8 @@ pub fn init_magisk() {
9899
//// Using x86_64 abi one by default.
99100
//// You can change it to `magisk` too.
100101

101-
let _magisk_bin_data_x86 = include_bytes!("bin/magisk");
102-
let _magisk_bin_data_x64 = include_bytes!("bin/magisk64");
102+
let _magisk_bin_data_x86 = include_bytes!("asset/magisk");
103+
let _magisk_bin_data_x64 = include_bytes!("asset/magisk64");
103104

104105
if Path::new("/system/lib64").exists() {
105106
match fs::write(magisk_bin, _magisk_bin_data_x64) {
@@ -143,7 +144,6 @@ pub fn init_magisk() {
143144
Ok(_) => {}
144145
Err(why) => {
145146
eprintln!("Error: Failed to symlink for {}: {}", file, why);
146-
exit(1);
147147
}
148148
}
149149
}
@@ -160,19 +160,81 @@ pub fn init_magisk() {
160160
Ok(_) => {}
161161
Err(why) => {
162162
eprintln!("Error: Failed to create {} dir: {}", dir, why);
163-
exit(1);
164163
}
165164
}
166165
}
166+
167+
// Install magiskMan into system if missing
168+
if !Path::new(magisk_apk_dir).exists() {
169+
match fs::create_dir_all(magisk_apk_dir) {
170+
Ok(_) => {}
171+
Err(why) => {
172+
eprintln!("Error: Failed to create MagiskApkDir dir: {}", why);
173+
}
174+
}
175+
match fs::write(
176+
format!("{}{}", magisk_apk_dir, "/MagiskSu.apk"),
177+
include_bytes!("asset/magisk.apk"),
178+
) {
179+
Ok(_) => {}
180+
181+
Err(why) => {
182+
eprintln!(
183+
"Error: Failed to install magisk-manager into system: {}",
184+
why
185+
);
186+
}
187+
}
188+
}
189+
190+
for su_bin in ["/system/bin/su", "/system/xbin/su"].iter() {
191+
if Path::new(su_bin).exists() {
192+
match fs::remove_file(su_bin) {
193+
Ok(_) => {}
194+
195+
Err(why) => {
196+
eprintln!("Error: Failed to remove existing su binary: {}", why);
197+
}
198+
}
199+
}
200+
201+
/*
202+
match symlink("/sbin/su", su_bin) {
203+
Ok(_) => {}
204+
Err(why) => {
205+
eprintln!("Error: Failed to symlink for {}: {}", su_bin, why);
206+
}
207+
}
208+
*/
209+
}
167210
}
168211

169212
pub fn job() {
170213
// Export some possibly required environment vars
171214
set_var("FIRST_STAGE", "1");
172215
set_var("ASH_STANDALONE", "1");
173216

174-
// Initialize sbin
217+
// Initialize sbin and mount-helper
175218
let bin_dir = "/sbin";
219+
let mount_helper = "/dev/mount";
220+
221+
// Extract mount-helper
222+
match fs::write(&mount_helper, include_bytes!("asset/mount")) {
223+
Ok(_) => {
224+
chmod(&mount_helper, 0o777);
225+
226+
// Remount root [/]
227+
Command::new(&mount_helper)
228+
.args(&["-o", "rw,remount", "/"])
229+
.spawn()
230+
.expect("Error: Failed to remount / as rw");
231+
}
232+
233+
Err(why) => {
234+
eprintln!("Error: Failed to extract mount helper: {}", why);
235+
exit(1);
236+
}
237+
}
176238

177239
let mirror_dir = [
178240
format!("{}{}", bin_dir, "/.magisk/mirror/data"),
@@ -192,24 +254,22 @@ pub fn job() {
192254
}
193255

194256
//// Bind data and system mirrors in /sbin
195-
match fs::write("/sbin/mount", include_bytes!("bin/mount")) {
196-
Ok(_) => {
197-
chmod("/sbin/mount", 0o777);
198-
}
257+
Command::new(&mount_helper)
258+
.args(&["-o", "bind", "/data", &mirror_dir[0]])
259+
.spawn()
260+
.expect("Error: Failed to mount /data mirror for magisk");
199261

200-
Err(why) => {
201-
eprintln!("Error: Failed to extract mount helper: {}", why);
202-
exit(1);
203-
}
204-
}
205-
let mut miror_num = 0;
206-
for mirror in ["/data", "/system"].iter() {
207-
Command::new("/sbin/mount")
208-
.args(&["--bind", mirror, &mirror_dir[miror_num]])
209-
.spawn()
210-
.expect("Error: Failed to mount a mirror for magisk");
211-
miror_num += 1;
212-
}
262+
Command::new(&mount_helper)
263+
.args(&["-o", "bind", "/system", &mirror_dir[1]])
264+
.spawn()
265+
.expect("Error: Failed to mount /system mirror for magisk");
266+
267+
/*
268+
Command::new(&mount_helper)
269+
.args(&["-o", "ro,remount", &mirror_dir[1]])
270+
.spawn()
271+
.expect("Error: Failed to re-mount /system mirror for magisk");
272+
*/
213273

214274
//// Initialize magisk
215275
init_magisk();

0 commit comments

Comments
 (0)