Skip to content

Commit

Permalink
[Agent] Fix the issue of getting the hostname to fail in low-version …
Browse files Browse the repository at this point in the history
…kernels
  • Loading branch information
TomatoMr authored and rvql committed May 14, 2024
1 parent 9868c9c commit e82307b
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions agent/src/utils/command/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,23 @@ fn set_utsns(fp: &File) -> Result<()> {
}

pub fn get_hostname() -> Result<String> {
let origin_fp = File::open(Path::new(ORIGIN_UTS_PATH))?;
let root_fp = File::open(Path::new(ROOT_UTS_PATH))?;
fn hostname() -> Result<String> {
hostname::get()?
.into_string()
.map_err(|_| Error::new(ErrorKind::Other, "get hostname failed"))
}
// If the ORIGIN_UTS_PATH or ROOT_UTS_PATH does not exist, it may be due to a kernel version that is too low
// to support UTS namespaces, therefore, do not switch UTS namespaces and directly return the hostname.
let Ok(origin_fp) = File::open(Path::new(ORIGIN_UTS_PATH)) else {
return hostname();
};
let Ok(root_fp) = File::open(Path::new(ROOT_UTS_PATH)) else {
return hostname();
};
if let Err(e) = set_utsns(&root_fp) {
return Err(Error::new(ErrorKind::Other, e));
}
let name = hostname::get()?
.into_string()
.map_err(|_| Error::new(ErrorKind::Other, "get hostname failed"));
let name = hostname();
if let Err(e) = set_utsns(&origin_fp) {
return Err(Error::new(ErrorKind::Other, e));
}
Expand Down

0 comments on commit e82307b

Please sign in to comment.