sys-tuner to check uid of the proc entry (#7232)

automerge
This commit is contained in:
Pankaj Garg 2019-12-03 16:03:10 -08:00 committed by Grimes
parent d357192025
commit f75c51ff71
1 changed files with 34 additions and 25 deletions

View File

@ -1,19 +1,29 @@
use log::*; use log::*;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
fn tune_system() { fn tune_system(uid: u32) {
fn find_pid<P: AsRef<std::path::Path>, F>(name: &str, path: P, processor: F) -> Option<u64> fn find_pid<P: AsRef<std::path::Path>, F>(
name: &str,
path: P,
uid: u32,
processor: F,
) -> Option<u64>
where where
F: Fn(&std::fs::DirEntry) -> Option<u64>, F: Fn(&std::fs::DirEntry) -> Option<u64>,
{ {
for entry in std::fs::read_dir(path).expect("Failed to read /proc folder") { for entry in std::fs::read_dir(path).expect("Failed to read /proc folder") {
use std::os::unix::fs::MetadataExt;
if let Ok(dir) = entry { if let Ok(dir) = entry {
let mut path = dir.path(); if let Ok(meta) = std::fs::metadata(dir.path()) {
path.push("comm"); if uid == meta.uid() {
if let Ok(comm) = std::fs::read_to_string(path.as_path()) { let mut path = dir.path();
if comm.starts_with(name) { path.push("comm");
if let Some(pid) = processor(&dir) { if let Ok(comm) = std::fs::read_to_string(path.as_path()) {
return Some(pid); if comm.starts_with(name) {
if let Some(pid) = processor(&dir) {
return Some(pid);
}
}
} }
} }
} }
@ -26,10 +36,10 @@ fn tune_system() {
use std::process::Command; use std::process::Command;
use std::str::from_utf8; use std::str::from_utf8;
if let Some(pid) = find_pid("solana-validato", "/proc", |dir| { if let Some(pid) = find_pid("solana-validato", "/proc", uid, |dir| {
let mut path = dir.path(); let mut path = dir.path();
path.push("task"); path.push("task");
find_pid("solana-poh-serv", path, |dir1| { find_pid("solana-poh-serv", path, uid, |dir1| {
if let Ok(pid) = dir1.file_name().into_string() { if let Ok(pid) = dir1.file_name().into_string() {
pid.parse::<u64>().ok() pid.parse::<u64>().ok()
} else { } else {
@ -66,21 +76,20 @@ fn main() {
let listener = unix_socket::UnixListener::bind(solana_sys_tuner::SOLANA_SYS_TUNER_PATH) let listener = unix_socket::UnixListener::bind(solana_sys_tuner::SOLANA_SYS_TUNER_PATH)
.expect("Failed to bind to the socket file"); .expect("Failed to bind to the socket file");
let peer_uid;
// set socket permission // set socket permission
#[cfg(target_os = "linux")] if let Some(user) = users::get_user_by_name("solana") {
{ peer_uid = user.uid();
if let Some(user) = users::get_user_by_name("solana") { info!("UID for solana is {}", peer_uid);
let uid = format!("{}", user.uid()); nix::unistd::chown(
info!("UID for solana is {}", uid); solana_sys_tuner::SOLANA_SYS_TUNER_PATH,
nix::unistd::chown( Some(nix::unistd::Uid::from_raw(peer_uid)),
solana_sys_tuner::SOLANA_SYS_TUNER_PATH, None,
Some(nix::unistd::Uid::from_raw(user.uid())), )
None, .expect("Expected to change UID of the socket file");
) } else {
.expect("Expected to change UID of the socket file"); panic!("Could not find UID for solana user");
} else {
error!("Could not find UID for solana user");
}
} }
info!("Waiting for tuning requests"); info!("Waiting for tuning requests");
@ -88,7 +97,7 @@ fn main() {
if stream.is_ok() { if stream.is_ok() {
info!("Tuning the system now"); info!("Tuning the system now");
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
tune_system(); tune_system(peer_uid);
} }
} }