Luna/kernel/src/sys/uname.cpp

56 lines
1.5 KiB
C++

#include "arch/CPU.h"
#include "config.h"
#include "memory/MemoryManager.h"
#include "sys/Syscall.h"
#include "thread/Scheduler.h"
#include <bits/struct_utsname.h>
#include <luna/CString.h>
#include <luna/StaticString.h>
StaticString<_UTSNAME_LENGTH - 1> s_hostname;
void set_host_name(StringView hostname)
{
s_hostname.adopt(hostname.chars());
}
Result<u64> sys_uname(Registers*, SyscallArgs args)
{
utsname* buf = (utsname*)args[0];
utsname result;
strncpy(result.sysname, "moon", _UTSNAME_LENGTH);
strncpy(result.nodename, s_hostname.chars(), _UTSNAME_LENGTH);
strncpy(result.release, MOON_VERSION, _UTSNAME_LENGTH);
// FIXME: Hardcode this at build time instead of in code (should be the short commit hash).
strncpy(result.version, "alpha", _UTSNAME_LENGTH);
strncpy(result.machine, CPU::platform_string().chars(), _UTSNAME_LENGTH);
if (!MemoryManager::copy_to_user_typed(buf, &result)) return err(EFAULT);
return 0;
}
Result<u64> sys_sethostname(Registers*, SyscallArgs args)
{
const char* buf = (const char*)args[0];
usize length = (usize)args[1];
Thread* current = Scheduler::current();
if (current->auth.euid != 0) return err(EPERM);
if (length >= _UTSNAME_LENGTH) return err(EINVAL);
char new_hostname[_UTSNAME_LENGTH];
memset(new_hostname, 0, _UTSNAME_LENGTH);
if (!MemoryManager::copy_from_user(buf, new_hostname, length)) return err(EFAULT);
s_hostname.adopt(new_hostname);
return 0;
}