handle /etc/hostname, sethostname(2), gethostname(2) and uname(2)

This commit is contained in:
Matheus
2024-05-22 21:09:17 -03:00
parent e7c9654d58
commit 78492d4b56
8 changed files with 146 additions and 21 deletions
+26
View File
@@ -5,6 +5,7 @@
#include <pwd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/param.h>
int main(int argc, char *argv[]) {
printf("init: Welcome to Astral!\n");
@@ -27,6 +28,31 @@ int main(int argc, char *argv[]) {
setenv("TERM", "linux", 1);
//setenv("DISPLAY", ":0", 1);
// set hostname
char hostname[HOST_NAME_MAX + 1];
int hostnamefd = open("/etc/hostname", O_RDONLY);
if (hostnamefd == -1) {
// non fatal error
perror("init: failed to open /etc/hostname");
} else {
int readcount = read(hostnamefd, hostname, HOST_NAME_MAX);
if (readcount == -1) {
perror("init: failed to read /etc/hostname");
} else {
hostname[readcount] = '\0';
for (int i = 0; i < readcount; ++i) {
if (hostname[i] == '\n')
hostname[i] = '\0';
}
sethostname(hostname, strlen(hostname));
}
close(hostnamefd);
}
// run rc to initialize environment (mount filesystems, etc)
printf("init: running /etc/rc\n");
pid_t pid = fork();