diff options
| author | namilsk <namilsk@namilsk.tech> | 2026-01-08 18:41:34 +0300 |
|---|---|---|
| committer | namilsk <namilsk@namilsk.tech> | 2026-01-08 18:41:34 +0300 |
| commit | 3a5c327a546ff1838e4dc32b8b67a056c1b95f3d (patch) | |
| tree | 2aac7d8b53dda2104a926b1fa53129db78dd410d /init/src/host | |
| parent | 2cad2077b647770aac103360cbd28b29c513db6c (diff) | |
Implementing main functions of init, check TODO.md
Diffstat (limited to 'init/src/host')
| -rw-r--r-- | init/src/host/mod.rs | 1 | ||||
| -rw-r--r-- | init/src/host/sethn.rs | 37 |
2 files changed, 38 insertions, 0 deletions
diff --git a/init/src/host/mod.rs b/init/src/host/mod.rs new file mode 100644 index 0000000..4a3de53 --- /dev/null +++ b/init/src/host/mod.rs @@ -0,0 +1 @@ +pub mod sethn;
\ No newline at end of file diff --git a/init/src/host/sethn.rs b/init/src/host/sethn.rs new file mode 100644 index 0000000..1857297 --- /dev/null +++ b/init/src/host/sethn.rs @@ -0,0 +1,37 @@ +use std::{ffi::CString, fs, io}; +use libc::{sethostname, c_char, size_t}; +use crate::log::{log_warning}; + +pub fn try_set_hostname() { + let content = fs::read_to_string("/etc/hostname") + .map(|s| s.trim().to_string()) + .unwrap_or_else(|_| "localhost".to_string()); + + if let Err(e) = perform_set_hostname(&content) { + log_warning(&format!("Failed to set hostname to '{}': {}. Falling back to localhost.", content, e)); + let _ = perform_set_hostname("localhost"); + } +} + +fn perform_set_hostname(name: &str) -> io::Result<()> { + if name.len() > 64 { + return Err(io::Error::new(io::ErrorKind::InvalidInput, "Hostname too long")); + } + + let c_str = CString::new(name).map_err(|_| { + io::Error::new(io::ErrorKind::InvalidInput, "Hostname contains null byte") + })?; + + let res = unsafe { + sethostname( + c_str.as_ptr() as *const c_char, + name.len() as size_t, + ) + }; + + if res != 0 { + return Err(io::Error::last_os_error()); + } + + Ok(()) +}
\ No newline at end of file |
