Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use cmdline instead of comm, in terminal detection #130

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ features = ["build","cargo","git","rustc"]
dirs = "4.0"
walkdir = "2.3.2"
os-release = "0.1"
terminfo = "0.7"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you forgot to remove this dependency.


[target.'cfg(target_os = "netbsd")'.dependencies]
nix = { version = "0.24.1", default-features = false, features = ["hostname"] }
Expand Down
54 changes: 39 additions & 15 deletions src/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,15 @@ impl GeneralReadout for LinuxGeneralReadout {
shared::window_manager()
}

// fn terminal(&self) -> Result<String, ReadoutError> {
// use terminfo::Database;
//
// let db = Database::from_env()
// let term = db.name();
//
// Ok(term.to_string())
// }

Comment on lines +382 to +390
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remove this commented function?

fn terminal(&self) -> Result<String, ReadoutError> {
// This function returns the PPID of a given PID:
// - The file used to extract this data: /proc/<pid>/status
Expand All @@ -404,35 +413,50 @@ impl GeneralReadout for LinuxGeneralReadout {
}
}

fn terminal_exec(pid: i32) -> String {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

terminal_name seems more appropriate here.

let data_path = PathBuf::from("/proc").join(pid.to_string()).join("cmdline");
let empty = "".to_string();
let raw_path = match fs::read_to_string(data_path) {
Ok(v) => v,
Err(_) => return empty,
};
let exec_name = match PathBuf::from(raw_path).file_name() {
Some(v) => match v.to_str() {
Some(v) => v.to_string(),
None => return empty,
},
None => return empty,
};
exec_name
}

// This function returns the name associated with a given PPID
fn terminal_name() -> String {
let mut terminal_pid = get_parent(unsafe { libc::getppid() });

let path = PathBuf::from("/proc")
.join(terminal_pid.to_string())
.join("comm");

let mut terminal_name = terminal_exec(terminal_pid);
if terminal_name.is_empty() {
return "".to_string();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use String::new() instead?

};
// The below loop will traverse /proc to find the
// terminal inside of which the user is operating
if let Ok(mut terminal_name) = fs::read_to_string(path) {
while extra::common_shells().contains(&terminal_name.replace('\n', "").as_str()) {
// Any command_name we find that matches
// one of the elements within this table
// is effectively ignored
while extra::common_shells().contains(&terminal_name.replace('\n', "").as_str()) {
let ppid = get_parent(terminal_pid);
terminal_pid = ppid;
let ppid = get_parent(terminal_pid);
terminal_pid = ppid;

let path = PathBuf::from("/proc").join(ppid.to_string()).join("comm");
let exec_name = terminal_exec(terminal_pid);

if let Ok(comm) = fs::read_to_string(path) {
terminal_name = comm;
}
if !exec_name.is_empty() {
terminal_name = exec_name;
} else {
return "".to_string();
}

return terminal_name;
}

String::new()
terminal_name
}

let terminal = terminal_name();
Expand Down