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

Add support for displaying process usernames on Windows #1016

Merged
merged 1 commit into from
Feb 10, 2023
Merged
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
4 changes: 4 additions & 0 deletions src/app/data_harvester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ impl DataCollector {
self.sys.refresh_networks_list();
}

if cfg!(target_os = "windows") && self.widgets_to_harvest.use_proc {
self.sys.refresh_users_list();
}

if self.widgets_to_harvest.use_proc || self.widgets_to_harvest.use_cpu {
self.sys.refresh_cpu();
}
Expand Down
3 changes: 1 addition & 2 deletions src/app/data_harvester/processes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ pub struct ProcessHarvest {
#[cfg(target_family = "unix")]
pub uid: Option<libc::uid_t>,

/// This is the process' user. This is only used on Unix platforms.
#[cfg(target_family = "unix")]
/// This is the process' user.
pub user: std::borrow::Cow<'static, str>,
// TODO: Additional fields
// pub rss_kb: u64,
Expand Down
6 changes: 5 additions & 1 deletion src/app/data_harvester/processes/windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Process data collection for Windows. Uses sysinfo.

use sysinfo::{CpuExt, PidExt, ProcessExt, System, SystemExt};
use sysinfo::{CpuExt, PidExt, ProcessExt, System, SystemExt, UserExt};

use super::ProcessHarvest;

Expand Down Expand Up @@ -75,6 +75,10 @@ pub fn get_process_data(
total_read_bytes: disk_usage.total_read_bytes,
total_write_bytes: disk_usage.total_written_bytes,
process_state,
user: process_val
.user_id()
.and_then(|uid| sys.get_user_by_id(uid))
.map_or_else(|| "N/A".into(), |user| user.name().to_owned().into()),
});
}

Expand Down
11 changes: 1 addition & 10 deletions src/app/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,16 +670,7 @@ impl Prefix {
}),
PrefixType::Pid => r.is_match(process.pid.to_string().as_str()),
PrefixType::State => r.is_match(process.process_state.0.as_str()),
PrefixType::User => {
#[cfg(target_family = "unix")]
{
r.is_match(process.user.as_ref())
}
#[cfg(not(target_family = "unix"))]
{
false
}
}
PrefixType::User => r.is_match(process.user.as_ref()),
_ => true,
}
} else {
Expand Down
9 changes: 2 additions & 7 deletions src/widgets/process_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,8 @@ impl ProcWidgetState {
pub const WPS: usize = 5;
pub const T_READ: usize = 6;
pub const T_WRITE: usize = 7;
#[cfg(target_family = "unix")]
pub const USER: usize = 8;
#[cfg(target_family = "unix")]
pub const STATE: usize = 9;
#[cfg(not(target_family = "unix"))]
pub const STATE: usize = 8;

fn new_sort_table(config: &AppConfigFields, colours: &CanvasColours) -> SortTable {
const COLUMNS: [Column<SortTableColumn>; 1] = [Column::hard(SortTableColumn, 7)];
Expand Down Expand Up @@ -162,7 +158,6 @@ impl ProcWidgetState {
wps,
tr,
tw,
#[cfg(target_family = "unix")]
SortColumn::soft(User, Some(0.05)),
state,
]
Expand Down Expand Up @@ -677,7 +672,6 @@ impl ProcWidgetState {
*col = ProcColumn::Count;
sort_col.default_order = SortOrder::Descending;

#[cfg(target_family = "unix")]
self.hide_column(Self::USER);
self.hide_column(Self::STATE);
self.mode = ProcWidgetMode::Grouped;
Expand All @@ -686,7 +680,6 @@ impl ProcWidgetState {
*col = ProcColumn::Pid;
sort_col.default_order = SortOrder::Ascending;

#[cfg(target_family = "unix")]
self.show_column(Self::USER);
self.show_column(Self::STATE);
self.mode = ProcWidgetMode::Normal;
Expand Down Expand Up @@ -821,6 +814,8 @@ mod test {
process_char: '?',
#[cfg(target_family = "unix")]
user: "root".to_string(),
#[cfg(not(target_family = "unix"))]
user: "N/A".to_string(),
num_similar: 0,
disabled: false,
};
Expand Down
11 changes: 4 additions & 7 deletions src/widgets/process_table/proc_widget_column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,10 @@ impl SortsRow for ProcColumn {
}
}
ProcColumn::User => {
#[cfg(target_family = "unix")]
{
if descending {
data.sort_by_cached_key(|pd| Reverse(pd.user.to_lowercase()));
} else {
data.sort_by_cached_key(|pd| pd.user.to_lowercase());
}
if descending {
data.sort_by_cached_key(|pd| Reverse(pd.user.to_lowercase()));
} else {
data.sort_by_cached_key(|pd| pd.user.to_lowercase());
}
}
}
Expand Down
24 changes: 2 additions & 22 deletions src/widgets/process_table/proc_widget_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ pub struct ProcWidgetData {
pub total_write: u64,
pub process_state: String,
pub process_char: char,
#[cfg(target_family = "unix")]
pub user: String,
pub num_similar: u64,
pub disabled: bool,
Expand Down Expand Up @@ -155,7 +154,6 @@ impl ProcWidgetData {
total_write: process.total_write_bytes,
process_state: process.process_state.0.clone(),
process_char: process.process_state.1,
#[cfg(target_family = "unix")]
user: process.user.to_string(),
num_similar: 1,
disabled: false,
Expand Down Expand Up @@ -205,16 +203,7 @@ impl ProcWidgetData {
ProcColumn::TotalRead => dec_bytes_string(self.total_read),
ProcColumn::TotalWrite => dec_bytes_string(self.total_write),
ProcColumn::State => self.process_char.to_string(),
ProcColumn::User => {
#[cfg(target_family = "unix")]
{
self.user.clone()
}
#[cfg(not(target_family = "unix"))]
{
"".to_string()
}
}
ProcColumn::User => self.user.clone(),
}
}
}
Expand Down Expand Up @@ -247,16 +236,7 @@ impl DataToCell<ProcColumn> for ProcWidgetData {
self.process_state.clone()
}
}
ProcColumn::User => {
#[cfg(target_family = "unix")]
{
self.user.clone()
}
#[cfg(not(target_family = "unix"))]
{
"".to_string()
}
}
ProcColumn::User => self.user.clone(),
},
calculated_width,
))
Expand Down