SHARE
TWEET

Untitled

a guest Oct 9th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use failure::{err_msg, Error};
  2. use std::collections::HashMap;
  3. use std::fs::File;
  4. use std::fs::ReadDir;
  5. use std::io::prelude::*;
  6. use std::path::Path;
  7.  
  8. pub struct Event {
  9.     uuid: i32,
  10.     pid: i32,
  11.     cmd: String,
  12. }
  13.  
  14. pub struct Events {
  15.     cmds: HashMap<i32, Event>,
  16. }
  17.  
  18. pub fn refresh(events: &mut Events) -> Result<&mut Events, Error> {
  19.     let pids = getpids()?;
  20.     for pid in pids {
  21.         if !events.cmds.contains_key(&pid) {
  22.             let _cmd = proc_cmd_read(pid);
  23.             let _uuid = get_uuid(pid);
  24.             if _cmd.is_ok() && _uuid.is_ok() {
  25.                 let event = Event {
  26.                     pid: pid,
  27.                     cmd: _cmd?,
  28.                     uuid: _uuid?,
  29.                 };
  30.                 println!("PID: {},UID:{}\t || CMD:\t{}", event.pid, event.uuid, event.cmd);
  31.                 events.cmds.insert(pid, event);
  32.             }
  33.         }
  34.     }
  35.     Ok(events)
  36. }
  37. pub fn read_proc_dir() -> Result<ReadDir, Error> {
  38.     let dir = Path::new("/proc").read_dir()?;
  39.     Ok(dir)
  40. }
  41.  
  42. pub fn proc_status_read(id: i32) -> Result<String, Error> {
  43.     let status_file = format!("/proc/{}/status", id);
  44.     let mut file = File::open(status_file)?;
  45.     let mut contents = String::new();
  46.     file.read_to_string(&mut contents)?;
  47.     Ok(contents)
  48. }
  49.  
  50. pub fn proc_cmd_read(id: i32) -> Result<String, Error> {
  51.     let status_file = format!("/proc/{}/cmdline", id);
  52.     let mut file = File::open(status_file)?;
  53.     let mut buffer = Vec::new();
  54.     file.read_to_end(&mut buffer)?;
  55.     let contents = String::from_utf8_lossy(&buffer);
  56.     Ok(contents.to_string())
  57. }
  58.  
  59. pub fn f2pid(folder: &Path) -> Result<i32, Error> {
  60.     let get_pid = folder
  61.         .file_stem()
  62.         .ok_or(err_msg("Failed to find file stem"))?
  63.         .to_str()
  64.         .ok_or(err_msg("Failed to create string"))?;
  65.     let pid = get_pid.parse::<i32>()?;
  66.     Ok(pid)
  67. }
  68.  
  69. pub fn getpids() -> Result<Vec<i32>, Error> {
  70.     let mut pids = Vec::new();
  71.     let procc_list = read_proc_dir()?;
  72.     for procc in procc_list {
  73.         let procc = procc?;
  74.         let path = procc.path();
  75.         if path.is_dir() {
  76.             let pid = f2pid(path.as_path());
  77.             if pid.is_ok() {
  78.                 pids.push(pid?);
  79.             }
  80.         }
  81.     }
  82.     Ok(pids)
  83. }
  84.  
  85. pub fn get_uuid(pid: i32) -> Result<i32, Error> {
  86.     let status = proc_status_read(pid)?;
  87.     let lines: Vec<&str> = status.split("\n").collect();
  88.     if lines.len() < 9 {
  89.         err_msg("No uuid found");
  90.     }
  91.     let uuid1: Vec<&str> = lines[8].split("\t").collect();
  92.     if uuid1.len() < 2 {
  93.         err_msg("No uuid found");
  94.     }
  95.     let uuid = uuid1[1].parse::<i32>()?;
  96.  
  97.     Ok(uuid)
  98. }
  99.  
  100. pub fn start() -> Result<(), Error> {
  101.     let pids = getpids()?;
  102.     let mut events = HashMap::new();
  103.     for pid in pids {
  104.         let _cmd = proc_cmd_read(pid)?;
  105.         let _uuid = get_uuid(pid)?;
  106.         let event = Event {
  107.             pid: pid,
  108.             cmd: _cmd,
  109.             uuid: _uuid,
  110.         };
  111.         println!("PID: {},UID:{}\t || CMD:\t{}", event.pid, event.uuid, event.cmd);
  112.         events.insert(pid, event);
  113.     }
  114.     let mut all_events = Events { cmds: events };
  115.     loop {
  116.         refresh(&mut all_events)?;
  117.     }
  118. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top