Luna/apps/src/init.c

78 lines
1.2 KiB
C

#include <luna.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
int print_version()
{
char version[4096];
FILE* fp = fopen("/dev/version", "r");
if (!fp)
{
perror("fopen");
return 1;
}
size_t nread = fread(version, 4096, 1, fp);
if (ferror(fp))
{
perror("fread");
return 1;
}
version[nread] = 0;
if (fclose(fp) < 0)
{
perror("fclose");
return 1;
}
printf("Your kernel version is %s\n\n", version);
return 0;
}
int main()
{
if (getpid() != 1)
{
fprintf(stderr, "init should be started as PID 1\n");
return 1;
}
printf("Welcome to Luna!\n");
printf("Running as PID %ld\n", getpid());
if (print_version()) return 1;
sleep(1);
pid_t child = fork();
if (child < 0)
{
perror("fork");
return 1;
}
if (child == 0)
{
execv("/bin/sh", NULL);
perror("execv");
return 1;
}
for (;;)
{
while (wait(NULL) == 0) // No child has exited yet
{
msleep(100);
}
}
}