diff options
author | Ian Moffett <ian@osmora.org> | 2025-08-06 23:06:27 -0400 |
---|---|---|
committer | Ian Moffett <ian@osmora.org> | 2025-08-06 23:06:27 -0400 |
commit | 88713baadfbc4ac78c5eccc546ecab3d145e12fb (patch) | |
tree | 3698f6f1d657d388dc918d1bc5296ed2bd168c04 | |
parent | 9de636933536756970fb2da41030dc4c5afd83b9 (diff) |
usr.sbin: init: Set hostname to /etc/hostname
Signed-off-by: Ian Moffett <ian@osmora.org>
-rw-r--r-- | etc/hostname | 1 | ||||
-rw-r--r-- | usr.sbin/init/main.c | 40 |
2 files changed, 41 insertions, 0 deletions
diff --git a/etc/hostname b/etc/hostname new file mode 100644 index 0000000..c90b108 --- /dev/null +++ b/etc/hostname @@ -0,0 +1 @@ +osmora diff --git a/usr.sbin/init/main.c b/usr.sbin/init/main.c index e1ee4d8..5da33c6 100644 --- a/usr.sbin/init/main.c +++ b/usr.sbin/init/main.c @@ -29,11 +29,48 @@ #include <sys/spawn.h> #include <stddef.h> +#include <unistd.h> +#include <stdio.h> +#include <string.h> #define SHELL_PATH "/usr/bin/osh" #define LOGIN_PATH "/usr/bin/login" #define INIT_RC_PATH "/usr/rc/init.rc" +static void +init_hostname(void) +{ + char hostname[128]; + int error; + size_t len; + FILE *fp; + + fp = fopen("/etc/hostname", "r"); + if (fp == NULL) { + printf("[init]: error opening /etc/hostname\n"); + return; + } + + error = fread(hostname, sizeof(char), sizeof(hostname), fp); + if (error <= 0) { + printf("[init]: error reading /etc/hostname\n"); + fclose(fp); + return; + } + + len = strlen(hostname); + hostname[len - 2] = '\0'; + + if (sethostname(hostname, len) < 0) { + printf("[init]: error setting hostname\n"); + printf("[init]: tried to set %s (len=%d)\n", hostname, len); + fclose(fp); + return; + } + + fclose(fp); +} + int main(int argc, char **argv) { @@ -41,6 +78,9 @@ main(int argc, char **argv) char *start_argv[] = { SHELL_PATH, INIT_RC_PATH, NULL }; char *envp[] = { NULL }; + /* Initialize the system hostname */ + init_hostname(); + /* Start the init.rc */ spawn(SHELL_PATH, start_argv, envp, 0); start_argv[1] = NULL; |