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
+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;
}