Compare commits

...

2 Commits

Author SHA1 Message Date
4becb2e427
init: Log requested exits
All checks were successful
continuous-integration/drone/push Build is passing
2023-08-24 15:28:20 +02:00
3e174337ab
init: Hostname validation 2023-08-24 15:28:08 +02:00

View File

@ -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>
@ -24,18 +25,6 @@ static bool g_is_system = false;
FILE* g_init_log = nullptr;
// Request a successful exit from the system (for tests)
void sigterm_handler(int)
{
_exit(0);
}
// Request a failure exit from the system (for tests)
void sigquit_handler(int)
{
_exit(1);
}
struct Service
{
String name;
@ -63,6 +52,20 @@ static void do_log(const char* format, ...)
va_end(ap);
}
// Request a successful exit from the system (for tests)
void sigterm_handler(int)
{
do_log("[init] successful exit requested, complying\n");
exit(0);
}
// Request a failure exit from the system (for tests)
void sigquit_handler(int)
{
do_log("[init] failure exit requested, complying\n");
exit(1);
}
static Result<void> service_child(const Service& service, SharedPtr<os::File> output, SharedPtr<os::File> error,
SharedPtr<os::File> input)
{
@ -300,6 +303,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());