init: Hostname validation
This commit is contained in:
parent
0f377e7289
commit
3e174337ab
@ -3,6 +3,7 @@
|
||||
#include <luna/PathParser.h>
|
||||
#include <luna/Sort.h>
|
||||
#include <luna/String.h>
|
||||
#include <luna/Utf8.h>
|
||||
#include <luna/Vector.h>
|
||||
#include <os/ArgumentParser.h>
|
||||
#include <os/Directory.h>
|
||||
@ -300,6 +301,19 @@ static Result<void> set_hostname()
|
||||
auto hostname = TRY(file->read_line());
|
||||
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 {};
|
||||
|
||||
do_log("[init] successfully set system hostname to '%s'\n", hostname.chars());
|
||||
|
Loading…
Reference in New Issue
Block a user