Skip to content

Commit

Permalink
For NordicHPC#152 - Remove hostname dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars T Hansen committed Apr 4, 2024
1 parent f2f229a commit db2c700
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 19 deletions.
18 changes: 0 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ edition = "2021"
[dependencies]
subprocess = "0.2"
chrono = "0.4"
hostname = "0.3"
clap = { version = "4.5", features = ["derive"] }
csv = "1.3"
log = "0.4"
Expand Down
69 changes: 69 additions & 0 deletions src/hostname.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Origin of this code: https://github.com/svartalf/hostname.

/*
MIT License
Copyright (c) 2016 fengcen
Copyright (c) 2019 svartalf
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

use std::ffi::OsString;
use std::os::unix::ffi::OsStringExt;
use std::io;
use libc;

pub fn get() -> io::Result<OsString> {
// According to the POSIX specification,
// host names are limited to `HOST_NAME_MAX` bytes
//
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/gethostname.html
let size =
unsafe { libc::sysconf(libc::_SC_HOST_NAME_MAX) as libc::size_t };

// Stack buffer OK: HOST_NAME_MAX is typically very small (64 on Linux).
let mut buffer = vec![0u8; size];

let result = unsafe {
libc::gethostname(buffer.as_mut_ptr() as *mut libc::c_char, size)
};

if result != 0 {
return Err(io::Error::last_os_error());
}

Ok(wrap_buffer(buffer))
}

fn wrap_buffer(mut bytes: Vec<u8>) -> OsString {
// Returned name might be truncated if it does not fit
// and `buffer` will not contain the trailing \0 in that case.
// Manually capping the buffer length here.
let end = bytes
.iter()
.position(|&byte| byte == 0x00)
.unwrap_or_else(|| bytes.len());
bytes.resize(end, 0x00);

OsString::from_vec(bytes)
}

1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod amd;
mod batchless;
mod command;
mod gpu;
mod hostname;
mod jobs;
mod nvidia;
mod procfs;
Expand Down
1 change: 1 addition & 0 deletions src/ps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
extern crate log;

use crate::amd;
use crate::hostname;
use crate::jobs;
use crate::nvidia;
use crate::procfs;
Expand Down
1 change: 1 addition & 0 deletions src/sysinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extern crate log;

use crate::amd;
use crate::gpu;
use crate::hostname;
use crate::nvidia;
use crate::procfs;
use crate::procfsapi;
Expand Down

0 comments on commit db2c700

Please sign in to comment.