summaryrefslogtreecommitdiff
path: root/usr.sbin/init/main.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-08-06 23:06:27 -0400
committerIan Moffett <ian@osmora.org>2025-08-06 23:06:27 -0400
commit88713baadfbc4ac78c5eccc546ecab3d145e12fb (patch)
tree3698f6f1d657d388dc918d1bc5296ed2bd168c04 /usr.sbin/init/main.c
parent9de636933536756970fb2da41030dc4c5afd83b9 (diff)
usr.sbin: init: Set hostname to /etc/hostname
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'usr.sbin/init/main.c')
-rw-r--r--usr.sbin/init/main.c40
1 files changed, 40 insertions, 0 deletions
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;