Luna/apps/init.c
apio 40f01c825d
All checks were successful
continuous-integration/drone/push Build is passing
libc: Add fork()
2023-03-18 23:58:56 +01:00

49 lines
1.3 KiB
C

#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <unistd.h>
#define xmknod(path, mode, maj, min) \
if (mknod(path, mode, makedev(maj, min)) < 0) exit(255);
// Too early for console logs (/dev/console is created here!), so we have to resort to exiting with a weird exit code in
// case of failure.
static void populate_devfs()
{
if (mkdir("/dev", 0755) < 0 && errno != EEXIST) exit(255);
xmknod("/dev/console", 0666, 1, 0);
xmknod("/dev/null", 0666, 2, 0);
}
int main()
{
if (getpid() != 1)
{
printf("error: init not running as PID 1.\n");
return 1;
}
populate_devfs();
// Before this point, we don't even have an stdout and stderr. Set it up now so that child processes (and us) can
// print stuff.
stdout = fopen("/dev/console", "w");
stderr = fopen("/dev/console", "w");
fprintf(stderr, "init is running as PID %d\n", getpid());
pid_t ret = fork();
if (ret == 0)
{
char* argv[] = { "/bin/hello", "--help", NULL };
execv("/bin/hello", argv);
}
else { printf("my child is PID %d!\n", ret); }
}