commit b3f4fe1cc22d410aafb60161cc2690b9654aceb2 Author: 驻魂圣使 Date: Sat May 11 23:06:18 2024 +0800 首次提交 diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..f04ff41 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,15 @@ +[target.aarch64-linux-android] +linker = "aarch64-linux-android21-clang++" +ar = "llvm-ar" + +[target.armv7-linux-androideabi] +linker = "armv7a-linux-androideabi21-clang++" +ar = "llvm-ar" + +[target.i686-linux-android] +linker = "i686-linux-android21-clang++" +ar = "llvm-ar" + +[target.x86_64-linux-android] +linker = "x86_64-linux-android21-clang++" +ar = "llvm-ar" \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2a0038a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +.idea \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..7d03bbe --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,129 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ProcessManager" +version = "0.1.0" +dependencies = [ + "colored", + "nix", +] + +[[package]] +name = "bitflags" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "cfg_aliases" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd16c4719339c4530435d38e511904438d07cce7950afa3718a84ac36c10e89e" + +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys", +] + +[[package]] +name = "lazy_static" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" + +[[package]] +name = "libc" +version = "0.2.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346" + +[[package]] +name = "nix" +version = "0.28.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d0639a1 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ProcessManager" +version = "0.1.0" +edition = "2021" + +[dependencies] +nix = { version = "0.28.0", features = ["user"] } +colored = "2.0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..748c129 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,71 @@ +use std::{env, fs, io}; +use std::fs::{DirEntry, File}; +use std::io::{BufRead, BufReader}; +use colored::Colorize; +use nix::unistd::Uid; + +fn main() { + let args: Vec = env::args().collect(); + if args.len() != 2 { + println!("需要 1 个参数,但是找到找到{}个。", args.len() - 1); + return; + } + + let process_name = &args[1]; + println!("名称:{}", process_name); + + is_root(); + let dir_entries = fs::read_dir("/proc").expect("访问/proc目录出错!"); + for entry in dir_entries { + if entry.is_err() { + println!("{:?}", entry.err()); + continue; + } + match examine_entry(entry.unwrap(), process_name) { + Err(err) => println!("{:?}", err), + _ => {} + } + } +} + +fn examine_entry(file_entry: DirEntry, name: &str) -> Result<(), io::Error> { + //跳过不是目录的条目 + if !file_entry.metadata()?.is_dir() { + return Ok(()); + } + + let file_name = file_entry.file_name().into_string().unwrap_or_else(|os_string| { + os_string.to_string_lossy().to_string() + }); + + //跳过名称不是纯数字的条目 + if !is_numeric_string(&file_name) { + return Ok(()); + } + + let file = File::open(format!("/proc/{}/cmdline", file_name))?; + let reader = BufReader::new(file); + + // 读取文件的第一行 + if let Some(Ok(first_line)) = reader.lines().next() { + if first_line.contains(name) { + println!("PID:{} PackName: {}", file_name, first_line); + } + } + + Ok(()) +} + +/// 检查给定字符串是否由纯数字组成 +fn is_numeric_string(s: &str) -> bool { + s.chars().all(|c| c.is_numeric()) +} + + +fn is_root() { + let uid = Uid::current().as_raw(); + if uid != 0 { + println!("{}", format!("警告:当前用户的UID为 {},不具有root权限!", uid).red()); + println!("{}", "不具备root权限,将无法访问系统内的全部进程。".red()); + } +} \ No newline at end of file