1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:37:35 +00:00

Add sys$uname() and a /bin/uname utility.

This commit is contained in:
Andreas Kling 2018-10-26 14:56:21 +02:00
parent 384e2f24d4
commit 1c45b28da6
15 changed files with 129 additions and 10 deletions

View file

@ -112,6 +112,8 @@ DWORD handle(DWORD function, DWORD arg1, DWORD arg2, DWORD arg3)
return current->sys$get_arguments((int*)arg1, (char***)arg2);
case Syscall::PosixChdir:
return current->sys$chdir((const char*)arg1);
case Syscall::PosixUname:
return current->sys$uname((utsname*)arg1);
default:
kprintf("int0x80: Unknown function %x requested {%x, %x, %x}\n", function, arg1, arg2, arg3);
break;

View file

@ -33,6 +33,7 @@ enum Function {
PosixGethostname = 0x2001,
GetArguments = 0x2002,
PosixChdir = 0x2003,
PosixUname = 0x2004,
};
void initialize();

View file

@ -26,12 +26,18 @@ static InlineLinkedList<Task>* s_tasks;
static InlineLinkedList<Task>* s_deadTasks;
static String* s_hostname;
static String& hostname(InterruptDisabler&)
static String& hostnameStorage(InterruptDisabler&)
{
ASSERT(s_hostname);
return *s_hostname;
}
static String getHostname()
{
InterruptDisabler disabler;
return hostnameStorage(disabler).isolatedCopy();
}
static bool contextSwitch(Task*);
static void redoKernelTaskTSS()
@ -177,14 +183,10 @@ int Task::sys$munmap(void* addr, size_t size)
int Task::sys$gethostname(char* buffer, size_t size)
{
String hn;
{
InterruptDisabler disabler;
hn = hostname(disabler).isolatedCopy();
}
if (size < (hn.length() + 1))
auto hostname = getHostname();
if (size < (hostname.length() + 1))
return -ENAMETOOLONG;
memcpy(buffer, hn.characters(), size);
memcpy(buffer, hostname.characters(), size);
return 0;
}
@ -760,6 +762,16 @@ int Task::sys$open(const char* path, size_t pathLength)
return fd;
}
int Task::sys$uname(utsname* buf)
{
strcpy(buf->sysname, "Serenity");
strcpy(buf->release, "1.0-dev");
strcpy(buf->version, "FIXME");
strcpy(buf->machine, "i386");
strcpy(buf->nodename, getHostname().characters());
return 0;
}
int Task::sys$kill(pid_t pid, int sig)
{
(void) sig;

View file

@ -104,6 +104,7 @@ public:
int sys$gettimeofday(timeval*);
int sys$gethostname(char* name, size_t length);
int sys$get_arguments(int* argc, char*** argv);
int sys$uname(utsname*);
static void initialize();

Binary file not shown.

View file

@ -11,5 +11,6 @@ cp ../Userland/true mnt/bin/true
cp ../Userland/false mnt/bin/false
cp ../Userland/hostname mnt/bin/hostname
cp ../Userland/cat mnt/bin/cat
cp ../Userland/uname mnt/bin/uname
umount mnt
sync

View file

@ -31,6 +31,16 @@ struct timeval {
suseconds_t tv_usec;
};
#define UTSNAME_ENTRY_LEN 65
struct utsname {
char sysname[UTSNAME_ENTRY_LEN];
char nodename[UTSNAME_ENTRY_LEN];
char release[UTSNAME_ENTRY_LEN];
char version[UTSNAME_ENTRY_LEN];
char machine[UTSNAME_ENTRY_LEN];
};
struct FarPtr {
DWORD offset { 0 };
WORD selector { 0 };

View file

@ -7,6 +7,7 @@ OBJS = \
dirent.o \
stdlib.o \
time.o \
utsname.o \
entry.o
LIBRARY = LibC.a

View file

@ -1,6 +1,8 @@
#include "stdio.h"
#include "stdarg.h"
#include "types.h"
#include "string.h"
#include "errno.h"
#include <Kernel/Syscall.h>
#define ALWAYS_INLINE __attribute__ ((always_inline))
@ -169,5 +171,10 @@ int sprintf(char* buffer, const char* fmt, ...)
return ret;
}
void perror(const char* s)
{
printf("%s: %s\n", s, strerror(errno));
}
}

View file

@ -5,6 +5,7 @@ extern "C" {
int printf(const char* fmt, ...);
int sprintf(char* buffer, const char* fmt, ...);
int putchar(int ch);
void perror(const char*);
}

14
LibC/utsname.cpp Normal file
View file

@ -0,0 +1,14 @@
#include "utsname.h"
#include "errno.h"
#include <Kernel/Syscall.h>
extern "C" {
int uname(struct utsname* buf)
{
int rc = Syscall::invoke(Syscall::PosixUname, (dword)buf);
__RETURN_WITH_ERRNO(rc, rc, -1);
}
}

17
LibC/utsname.h Normal file
View file

@ -0,0 +1,17 @@
#pragma once
#define UTSNAME_ENTRY_LEN 65
extern "C" {
struct utsname {
char sysname[UTSNAME_ENTRY_LEN];
char nodename[UTSNAME_ENTRY_LEN];
char release[UTSNAME_ENTRY_LEN];
char version[UTSNAME_ENTRY_LEN];
char machine[UTSNAME_ENTRY_LEN];
};
int uname(struct utsname*);
}

1
Userland/.gitignore vendored
View file

@ -10,3 +10,4 @@ false
true
hostname
cat
uname

View file

@ -9,7 +9,8 @@ OBJS = \
true.o \
false.o \
hostname.o \
cat.o
cat.o \
uname.o
APPS = \
id \
@ -22,7 +23,8 @@ APPS = \
true \
false \
hostname \
cat
cat \
uname
ARCH_FLAGS =
STANDARD_FLAGS = -std=c++17 -nostdinc++ -nostdlib
@ -75,6 +77,9 @@ hostname: hostname.o
cat: cat.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
uname: uname.o
$(LD) -o $@ $(LDFLAGS) $< ../LibC/LibC.a
.cpp.o:
@echo "CXX $<"; $(CXX) $(CXXFLAGS) -o $@ -c $<

46
Userland/uname.cpp Normal file
View file

@ -0,0 +1,46 @@
#include <LibC/utsname.h>
#include <LibC/stdio.h>
int main(int argc, char** argv)
{
utsname uts;
int rc = uname(&uts);
if (rc < 0) {
perror("uname() failed");
return 0;
}
bool flag_s = false;
bool flag_n = false;
bool flag_r = false;
bool flag_m = false;
if (argc == 1) {
flag_s = true;
} else {
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
for (const char* o = &argv[i][1]; *o; ++o) {
switch (*o) {
case 's': flag_s = true; break;
case 'n': flag_n = true; break;
case 'r': flag_r = true; break;
case 'm': flag_m = true; break;
case 'a': flag_s = flag_n = flag_r = flag_m = true; break;
}
}
}
}
}
if (!flag_s && !flag_n && !flag_r && !flag_m)
flag_s = true;
if (flag_s)
printf("%s ", uts.sysname);
if (flag_n)
printf("%s ", uts.nodename);
if (flag_r)
printf("%s ", uts.release);
if (flag_m)
printf("%s ", uts.machine);
printf("\n");
return 0;
}