Add spawn and wait/waitpid functions into libc.

This commit is contained in:
Ali Mashtizadeh 2023-10-02 19:33:47 -04:00
parent 21a30db5d3
commit 63f2ae716e
14 changed files with 122 additions and 13 deletions

View File

@ -177,8 +177,10 @@ SConscript('bin/date/SConscript', variant_dir='build/bin/date')
SConscript('bin/echo/SConscript', variant_dir='build/bin/echo')
SConscript('bin/ethdump/SConscript', variant_dir='build/bin/ethdump')
SConscript('bin/ethinject/SConscript', variant_dir='build/bin/ethinject')
SConscript('bin/false/SConscript', variant_dir='build/bin/false')
SConscript('bin/ls/SConscript', variant_dir='build/bin/ls')
SConscript('bin/shell/SConscript', variant_dir='build/bin/shell')
SConscript('bin/true/SConscript', variant_dir='build/bin/true')
SConscript('sbin/ifconfig/SConscript', variant_dir='build/sbin/ifconfig')
SConscript('sbin/init/SConscript', variant_dir='build/sbin/init')
SConscript('sbin/sysctl/SConscript', variant_dir='build/sbin/sysctl')
@ -207,8 +209,10 @@ if env["BOOTDISK"] == "1":
Depends(bootdisk, "#build/bin/echo/echo")
Depends(bootdisk, "#build/bin/ethdump/ethdump")
Depends(bootdisk, "#build/bin/ethinject/ethinject")
Depends(bootdisk, "#build/bin/false/false")
Depends(bootdisk, "#build/bin/ls/ls")
Depends(bootdisk, "#build/bin/shell/shell")
Depends(bootdisk, "#build/bin/true/true")
Depends(bootdisk, "#build/sbin/ifconfig/ifconfig")
Depends(bootdisk, "#build/sbin/init/init")
Depends(bootdisk, "#build/sbin/sysctl/sysctl")

23
bin/false/SConscript Normal file
View File

@ -0,0 +1,23 @@
import sys
Import('env')
false_env = env.Clone()
src = [ ]
src_common = [
"false.c"
]
src.append(env["CRTBEGIN"])
src.append(src_common)
src.append(env["CRTEND"])
false_env.Append(LINKFLAGS = ['-nostdlib'])
false_env.Append(CPPFLAGS = ['-fno-builtin', '-nostdinc'])
false_env.Append(CPPPATH = ['#build/include'])
false_env.Append(LIBPATH = ['#build/lib/libc'], LIBS = ['c'])
false_env.Program("false", src)

7
bin/false/false.c Normal file
View File

@ -0,0 +1,7 @@
int
main(int argc, const char *argv[])
{
return 1;
}

View File

@ -3,10 +3,11 @@
#include <stdlib.h>
#include <string.h>
// Castor Only
#include <syscall.h>
#include <sys/syscall.h>
#include <sys/dirent.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <syscall.h>
#define SHELL_MAX_ARGS 5
#define SHELL_MAX_LINE 256
@ -66,12 +67,13 @@ Cmd_Run(int argc, const char *argv[])
}
argv[argc] = NULL;
status = OSSpawn(path, &argv[0]);
status = spawn(path, &argv[0]);
if (status > 100000) {
printf("Spawn failed!\n");
}
status = OSWait(status);
printf("Process result: %d\n", status);
pid_t pid = wait(&status);
printf("Process result: %d (pid %d)\n", WEXITSTATUS(status), pid);
return;
}

23
bin/true/SConscript Normal file
View File

@ -0,0 +1,23 @@
import sys
Import('env')
true_env = env.Clone()
src = [ ]
src_common = [
"true.c"
]
src.append(env["CRTBEGIN"])
src.append(src_common)
src.append(env["CRTEND"])
true_env.Append(LINKFLAGS = ['-nostdlib'])
true_env.Append(CPPFLAGS = ['-fno-builtin', '-nostdinc'])
true_env.Append(CPPPATH = ['#build/include'])
true_env.Append(LIBPATH = ['#build/lib/libc'], LIBS = ['c'])
true_env.Program("true", src)

7
bin/true/true.c Normal file
View File

@ -0,0 +1,7 @@
int
main(int argc, const char *argv[])
{
return 0;
}

View File

@ -10,6 +10,7 @@ struct timeval;
int syscall(int number, ...);
unsigned int sleep(unsigned int seconds);
pid_t spawn(const char *path, const char *argv[]);
#endif /* __UNISTD_H__ */

View File

@ -40,7 +40,7 @@ exit(int status)
_atexit_last = _atexit_last->next;
}
OSExit(status);
OSExit(status & 0x00ff);
__builtin_unreachable();
}

View File

@ -1,7 +1,11 @@
#include <stdint.h>
#include <sys/cdefs.h>
#include <sys/wait.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <errno.h>
#include <syscall.h>
unsigned int
@ -13,3 +17,37 @@ sleep(unsigned int seconds)
return 0;
}
pid_t
spawn(const char *path, const char *argv[])
{
uint64_t status = OSSpawn(path, argv);
if (SYSCALL_ERRCODE(status) != 0) {
errno = SYSCALL_ERRCODE(status);
return -1;
}
return (pid_t)SYSCALL_VALUE(status);
}
pid_t
waitpid(pid_t pid, int *status, UNUSED int options)
{
uint64_t wstatus = OSWait(pid);
if (SYSCALL_ERRCODE(wstatus) != 0) {
errno = SYSCALL_ERRCODE(wstatus);
return -1;
}
*status = SYSCALL_VALUE(wstatus) & 0x0000ffff;
return WGETPID(SYSCALL_VALUE(wstatus));
}
pid_t
wait(int *status)
{
return waitpid(WAIT_ANY, status, 0);
}

View File

@ -16,7 +16,7 @@ OSTime()
void
OSExit(int status)
{
syscall(SYSCALL_EXIT);
syscall(SYSCALL_EXIT, status);
}
uint64_t

View File

@ -10,8 +10,10 @@ DIR /
FILE echo build/bin/echo/echo
FILE ethdump build/bin/ethdump/ethdump
FILE ethinject build/bin/ethinject/ethinject
FILE false build/bin/false/false
FILE ls build/bin/ls/ls
FILE shell build/bin/shell/shell
FILE true build/bin/true/true
END
DIR sbin
FILE ifconfig build/sbin/ifconfig/ifconfig

View File

@ -13,6 +13,8 @@
#define NO_RETURN __attribute__((noreturn))
#define UNREACHABLE __builtin_unreachable
#define UNUSED __attribute__((unused))
#define ROUNDUP(_x, _n) (((_x) + (_n) - 1) & ~((_n) - 1))
#define __MALLOC __attribute__((__malloc__))

View File

@ -25,6 +25,8 @@ typedef uint64_t ino_t;
typedef uint64_t time_t;
typedef uint64_t suseconds_t;
typedef uint16_t pid_t;
#define NULL ((void *)0)
#endif /* __SYS_TYPES_H__ */

View File

@ -209,8 +209,6 @@ Process_Wait(Process *proc, uint64_t pid)
Process *p = NULL;
uint64_t status;
// XXX: Need to verify pid exists!
Mutex_Lock(&proc->zombieProcLock);
if (pid == 0) {
while (1) {
@ -226,7 +224,7 @@ Process_Wait(Process *proc, uint64_t pid)
p = Process_Lookup(pid);
if (p == NULL) {
Mutex_Unlock(&proc->zombieProcLock);
return ENOENT;
return SYSCALL_PACK(ENOENT, 0);
}
while (p->procState != PROC_STATE_ZOMBIE) {
@ -242,7 +240,7 @@ Process_Wait(Process *proc, uint64_t pid)
}
Mutex_Unlock(&proc->zombieProcLock);
status = (p->pid << 16) | p->exitCode;
status = (p->pid << 16) | (p->exitCode & 0xff);
// Release threads
Spinlock_Lock(&proc->lock);
@ -261,7 +259,7 @@ Process_Wait(Process *proc, uint64_t pid)
// Release process
Process_Release(p);
return status;
return SYSCALL_PACK(0, status);
}
/*