Skip to content

Commit

Permalink
apd: Optimize sync uid logic
Browse files Browse the repository at this point in the history
1. Trigger sync uid on boot completed event
2. Remove opts from su, add opt to apd normal
3. Upgrade deps and format

Signed-off-by: GarfieldHan <[email protected]>
  • Loading branch information
pomelohan committed May 30, 2024
1 parent ada571f commit fdcfd65
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 48 deletions.
24 changes: 12 additions & 12 deletions apd/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ zip-extensions = "0.6"
java-properties = "2.0.0"
log = "0.4"
env_logger = "0.11"
serde = { version = "1.0.130", features = ["derive"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
regex = "1"
encoding_rs = "0.8"
Expand Down
4 changes: 2 additions & 2 deletions apd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ fn main() {
}
};
let out_dir = env::var("OUT_DIR").expect("Failed to get $OUT_DIR");
println!("out_dir: ${out_dir}" );
println!("code: ${code}" );
println!("out_dir: ${out_dir}");
println!("code: ${code}");
let out_dir = Path::new(&out_dir);
File::create(Path::new(out_dir).join("VERSION_CODE"))
.expect("Failed to create VERSION_CODE")
Expand Down
11 changes: 2 additions & 9 deletions apd/src/apd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ use std::os::unix::process::CommandExt;
use std::path::PathBuf;
use std::{ffi::CStr, process::Command};

#[cfg(any(target_os = "linux", target_os = "android"))]
use crate::pty::prepare_pty;
use crate::{
defs,
utils::{self, umask},
package::synchronization_package_uid,
};
#[cfg(any(target_os = "linux", target_os = "android"))]
use crate::pty::prepare_pty;

fn print_usage(opts: Options) {
let brief = format!("APatch\n\nUsage: <command> [options] [-] [user [argument...]]");
Expand Down Expand Up @@ -61,7 +60,6 @@ pub fn root_shell() -> Result<()> {
"COMMAND",
);
opts.optflag("h", "help", "display this help message and exit");
opts.optflag("y", "synchronization", "synchronization packagelist id");
opts.optflag("l", "login", "pretend the shell to be a login shell");
opts.optflag(
"p",
Expand Down Expand Up @@ -120,11 +118,6 @@ pub fn root_shell() -> Result<()> {
return Ok(());
}

if matches.opt_present("y") {
synchronization_package_uid();
return Ok(());
}

let shell = matches.opt_str("s").unwrap_or("/system/bin/sh".to_string());
let mut is_login = matches.opt_present("l");
let preserve_env = matches.opt_present("p");
Expand Down
7 changes: 6 additions & 1 deletion apd/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ enum Commands {

/// Trigger `boot-complete` event
BootCompleted,

/// Sync package uid from system's packages.list
SyncPackageUid,
}

#[derive(clap::Subcommand, Debug)]
Expand Down Expand Up @@ -69,7 +72,7 @@ pub fn run() -> Result<()> {
android_logger::init_once(
Config::default()
.with_max_level(LevelFilter::Trace) // limit log level
.with_tag("APatchD"),
.with_tag("APatchD"),
);

#[cfg(not(target_os = "android"))]
Expand Down Expand Up @@ -106,6 +109,8 @@ pub fn run() -> Result<()> {
}

Commands::Services => event::on_services(),

Commands::SyncPackageUid => event::on_sync_uid(),
};

if let Err(e) = &result {
Expand Down
12 changes: 9 additions & 3 deletions apd/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::{collections::HashMap, path::Path};

use crate::module::prune_modules;
use crate::{
assets, defs, mount, restorecon,
assets, defs, mount,
package::synchronize_package_uid,
restorecon,
utils::{self, ensure_clean_dir},
};

Expand Down Expand Up @@ -98,7 +100,6 @@ pub fn mount_systemlessly(module_dir: &str) -> Result<()> {
}

pub fn on_post_data_fs() -> Result<()> {

utils::umask(0);

#[cfg(unix)]
Expand Down Expand Up @@ -195,7 +196,7 @@ pub fn on_post_data_fs() -> Result<()> {
if crate::module::load_sepolicy_rule().is_err() {
warn!("load sepolicy.rule failed");
}

if let Err(e) = mount::mount_tmpfs(utils::get_tmp_path()) {
warn!("do temp dir mount failed: {}", e);
}
Expand Down Expand Up @@ -268,11 +269,16 @@ pub fn on_boot_completed() -> Result<()> {
}
}

synchronize_package_uid();
run_stage("boot-completed", false);

Ok(())
}

pub fn on_sync_uid() -> Result<()> {
synchronize_package_uid();
return Ok(());
}

#[cfg(unix)]
fn catch_bootlog() -> Result<()> {
Expand Down
8 changes: 4 additions & 4 deletions apd/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
mod apd;
mod assets;
mod cli;
mod defs;
mod event;
#[cfg(any(target_os = "linux", target_os = "android"))]
mod pty;
mod apd;
mod module;
mod mount;
mod package;
#[cfg(any(target_os = "linux", target_os = "android"))]
mod pty;
mod restorecon;
mod utils;
mod package;
fn main() -> anyhow::Result<()> {
cli::run()
}
12 changes: 6 additions & 6 deletions apd/src/mount.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
use anyhow::{anyhow, bail, Ok, Result};
use std::fs::create_dir;
#[cfg(any(target_os = "linux", target_os = "android"))]
use anyhow::Context;
use anyhow::{anyhow, bail, Ok, Result};
#[cfg(any(target_os = "linux", target_os = "android"))]
#[allow(unused_imports)]
use retry::delay::NoDelay;
#[cfg(any(target_os = "linux", target_os = "android"))]
//use sys_mount::{unmount, FilesystemType, Mount, MountFlags, Unmount, UnmountFlags};
#[cfg(any(target_os = "linux", target_os = "android"))]
use rustix::{fd::AsFd, fs::CWD, mount::*};
use std::fs::create_dir;

use crate::defs::PTS_NAME;
use crate::defs::AP_OVERLAY_SOURCE;
use crate::defs::PTS_NAME;
use log::{info, warn};
#[cfg(any(target_os = "linux", target_os = "android"))]
use procfs::process::Process;
use std::fs;
use std::path::Path;
use std::path::PathBuf;
use std::fs;

pub struct AutoMountExt4 {
target: String,
Expand All @@ -36,7 +36,7 @@ impl AutoMountExt4 {
if !metadata.permissions().readonly() {
println!("Source path is not read-only");
}

mount_ext4(source, target)?;
Ok(Self {
target: target.to_string(),
Expand Down Expand Up @@ -73,7 +73,7 @@ impl Drop for AutoMountExt4 {
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn mount_image(src: &str, target: &str, _autodrop: bool) -> Result<()> {
mount_ext4(src, target)?;
Ok(())
Ok(())
}

#[cfg(any(target_os = "linux", target_os = "android"))]
Expand Down
17 changes: 8 additions & 9 deletions apd/src/package.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::{self, BufRead};
use std::path::Path;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)] // Add Serialize trait
struct PackageConfig {
Expand All @@ -13,7 +13,7 @@ struct PackageConfig {
sctx: String,
}

fn read_package_config() -> Vec<PackageConfig> {
fn read_ap_package_config() -> Vec<PackageConfig> {
let file = match File::open("/data/adb/ap/package_config") {
Ok(file) => file,
Err(e) => {
Expand All @@ -36,7 +36,7 @@ fn read_package_config() -> Vec<PackageConfig> {
package_configs
}

fn write_package_config(package_configs: &Vec<PackageConfig>) {
fn write_ap_package_config(package_configs: &Vec<PackageConfig>) {
let file = match File::create("/data/adb/ap/package_config") {
Ok(file) => file,
Err(e) => {
Expand All @@ -61,17 +61,16 @@ where
Ok(io::BufReader::new(file).lines())
}


pub fn synchronization_package_uid() {
pub fn synchronize_package_uid() {
if let Ok(lines) = read_lines("/data/system/packages.list") {
let mut package_configs = read_package_config();
let mut package_configs = read_ap_package_config();
for line in lines {
if let Ok(line) = line {
let words: Vec<&str> = line.split_whitespace().collect();
if words.len() >= 2 {
if let Ok(uid) = words[1].parse::<i32>() {
package_configs
.iter_mut() // Use iter_mut() to get mutable references
.iter_mut() // Use iter_mut() to get mutable references
.find(|config| config.pkg == words[0])
.map(|config| {
config.uid = uid;
Expand All @@ -82,6 +81,6 @@ pub fn synchronization_package_uid() {
}
}
}
write_package_config(&package_configs);
write_ap_package_config(&package_configs);
}
}
}
2 changes: 1 addition & 1 deletion apd/src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,4 @@ pub fn prepare_pty() -> Result<()> {
dup2_stderr(&pty_fd)?;
}
Ok(())
}
}

0 comments on commit fdcfd65

Please sign in to comment.