init: Hostname validation

This commit is contained in:
apio 2023-08-24 15:28:08 +02:00
parent 0f377e7289
commit 3e174337ab
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -3,6 +3,7 @@
#include <luna/PathParser.h> #include <luna/PathParser.h>
#include <luna/Sort.h> #include <luna/Sort.h>
#include <luna/String.h> #include <luna/String.h>
#include <luna/Utf8.h>
#include <luna/Vector.h> #include <luna/Vector.h>
#include <os/ArgumentParser.h> #include <os/ArgumentParser.h>
#include <os/Directory.h> #include <os/Directory.h>
@ -300,6 +301,19 @@ static Result<void> set_hostname()
auto hostname = TRY(file->read_line()); auto hostname = TRY(file->read_line());
hostname.trim("\n"); hostname.trim("\n");
if (hostname.is_empty())
{
do_log("[init] /etc/hostname is empty or invalid, keeping the default hostname\n");
return {};
}
Utf8StringDecoder decoder(hostname.chars());
if (decoder.code_points().has_error())
{
do_log("[init] /etc/hostname is not valid UTF-8, keeping the default hostname\n");
return {};
}
if (sethostname(hostname.chars(), hostname.length()) < 0) return {}; if (sethostname(hostname.chars(), hostname.length()) < 0) return {};
do_log("[init] successfully set system hostname to '%s'\n", hostname.chars()); do_log("[init] successfully set system hostname to '%s'\n", hostname.chars());