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
+1
View File
@@ -0,0 +1 @@
astral
+5 -1
View File
@@ -62,6 +62,8 @@ extern syscall_sigaltstack
extern syscall_sigprocmask
extern syscall_kill
extern syscall_sigreturn
extern syscall_uname
extern syscall_hostname
syscalltab:
dq syscall_print
dq syscall_mmap
@@ -126,7 +128,9 @@ dq syscall_sigaltstack
dq syscall_sigprocmask
dq syscall_kill
dq syscall_sigreturn
syscallcount equ 63
dq syscall_uname
dq syscall_hostname
syscallcount equ 65
section .text
global arch_syscall_entry
; on entry:
+7 -3
View File
@@ -4,7 +4,7 @@
#ifdef SYSCALL_LOGGING
#define SYSCALL_COUNT 63
#define SYSCALL_COUNT 65
#define LOGSTR(x) arch_e9_puts(x)
static char *name[] = {
@@ -70,7 +70,9 @@ static char *name[] = {
"sigaltstack",
"sigprocmask",
"kill",
"sigreturn"
"sigreturn",
"uname",
"hostname"
};
static char *args[] = {
@@ -136,7 +138,9 @@ static char *args[] = {
"new %p old %p", // sigaltstack
"how %d set %p oldset %p", // sigprocmask
"pid %d signal %d", // kill
"N/A" // sigreturn
"N/A", // sigreturn
"utsname %p", // uname
"new %p newsize %lu old %p oldsize %lu" // hostname
};
#endif
+2
View File
@@ -164,4 +164,6 @@ typedef struct {
unsigned short int rt_irtt;
} abirtentry_t;
#define HOST_NAME_MAX 64
#endif
+61
View File
@@ -0,0 +1,61 @@
#include <kernel/syscalls.h>
#include <kernel/abi.h>
#include <spinlock.h>
#include <util.h>
static spinlock_t lock;
static char buffer[HOST_NAME_MAX];
static size_t bufferlen;
void hostname_get(char *buf) {
bool intstatus = interrupt_set(false);
spinlock_acquire(&lock);
memcpy(buf, buffer, bufferlen);
buf[bufferlen] = '\0';
spinlock_release(&lock);
interrupt_set(intstatus);
}
syscallret_t syscall_hostname(context_t *, char *unew, size_t newsize, char *uold, size_t oldsize) {
syscallret_t ret = {
.ret = -1
};
if ((unew && newsize > HOST_NAME_MAX) || (uold && oldsize == 0)) {
ret.errno = EINVAL;
return ret;
}
char new[HOST_NAME_MAX];
char old[HOST_NAME_MAX + 1];
if (unew)
memcpy(new, unew, newsize);
interrupt_set(false);
spinlock_acquire(&lock);
if (uold) {
size_t copysize = min(oldsize - 1, bufferlen);
memcpy(old, buffer, copysize);
old[copysize] = '\0';
}
if (unew) {
bufferlen = newsize;
strcpy(buffer, new);
}
spinlock_release(&lock);
interrupt_set(true);
if (uold)
strcpy(uold, old);
ret.errno = 0;
ret.ret = 0;
return ret;
}
+29
View File
@@ -0,0 +1,29 @@
#include <kernel/syscalls.h>
#include <build.h>
typedef struct {
char sysname[65];
char nodename[65];
char release[65];
char version[65];
char machine[65];
char domainname[65];
} utsname_t;
void hostname_get(char *buf);
syscallret_t syscall_uname(context_t *, utsname_t *utsname) {
syscallret_t ret = {0};
char hostname[HOST_NAME_MAX + 1];
hostname_get(hostname);
strcpy(utsname->sysname, "Astral");
strcpy(utsname->nodename, hostname);
strcpy(utsname->release, "Astral");
strcpy(utsname->version, BUILD_DATE);
strcpy(utsname->machine, BUILD_TARGET);
strcpy(utsname->domainname, hostname);
return ret;
}
+15 -17
View File
@@ -192,10 +192,10 @@ index 0000000..e5b087a
+
diff --git mlibc-workdir/sysdeps/astral/generic/generic.cpp mlibc-workdir/sysdeps/astral/generic/generic.cpp
new file mode 100644
index 0000000..8ebf840
index 0000000..c409d89
--- /dev/null
+++ mlibc-workdir/sysdeps/astral/generic/generic.cpp
@@ -0,0 +1,694 @@
@@ -0,0 +1,690 @@
+#include <bits/ensure.h>
+#include <mlibc/debug.hpp>
+#include <mlibc/all-sysdeps.hpp>
@@ -300,23 +300,19 @@ index 0000000..8ebf840
+ return 0;
+ }
+
+ #define ASTRALHOSTNAME "astral"
+
+ int sys_gethostname(char *buffer, size_t bufsize) {
+ // TODO unstub
+ memcpy(buffer, ASTRALHOSTNAME, strlen(ASTRALHOSTNAME));
+ return 0;
+ long ret;
+ return syscall(SYSCALL_HOSTNAME, &ret, NULL, 0, (uint64_t)buffer, bufsize);
+ }
+
+ int sys_sethostname(const char *buffer, size_t bufsize) {
+ long ret;
+ return syscall(SYSCALL_HOSTNAME, &ret, (uint64_t)buffer, bufsize, NULL, 0), NULL, 0;
+ }
+
+ int sys_uname(struct utsname *buf) {
+ // TODO unstub
+ memcpy(buf->sysname, "Astral", 7);
+ memcpy(buf->nodename, ASTRALHOSTNAME, strlen(ASTRALHOSTNAME)+1);
+ memcpy(buf->version, "rw-0.0.1", 9);
+ memcpy(buf->release, "Astral", 13);
+ memcpy(buf->machine, "x86_64", 7);
+ memcpy(buf->domainname, ASTRALHOSTNAME, strlen(ASTRALHOSTNAME)+1);
+ return 0;
+ long ret;
+ return syscall(SYSCALL_UNAME, &ret, (uint64_t)buf);
+ }
+
+ int sys_fsync(int fd) {
@@ -909,10 +905,10 @@ index 0000000..990eca0
+#endif
diff --git mlibc-workdir/sysdeps/astral/include/astral/syscall.h mlibc-workdir/sysdeps/astral/include/astral/syscall.h
new file mode 100644
index 0000000..14f3c2b
index 0000000..3a130be
--- /dev/null
+++ mlibc-workdir/sysdeps/astral/include/astral/syscall.h
@@ -0,0 +1,86 @@
@@ -0,0 +1,88 @@
+#ifndef _SYSCALL_H_INCLUDE
+#define _SYSCALL_H_INCLUDE
+
@@ -979,6 +975,8 @@ index 0000000..14f3c2b
+#define SYSCALL_SIGPROCMASK 60
+#define SYSCALL_KILL 61
+#define SYSCALL_SIGRETURN 62
+#define SYSCALL_UNAME 63
+#define SYSCALL_HOSTNAME 64
+
+#include <stddef.h>
+#include <stdint.h>
+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();