Luna/apps/src/init.c

81 lines
1.3 KiB
C
Raw Normal View History

#include <luna.h>
2022-10-17 21:22:18 +02:00
#include <setjmp.h>
#include <stdio.h>
2022-10-02 17:25:56 +02:00
#include <stdlib.h>
#include <string.h>
2022-10-16 17:37:29 +02:00
#include <sys/stat.h>
#include <sys/wait.h>
2022-10-02 17:25:56 +02:00
#include <unistd.h>
int print_version()
{
char version[4096];
2022-10-17 20:53:09 +02:00
FILE* fp = fopen("/dev/version", "r");
if (!fp)
{
perror("fopen");
return 1;
}
2022-10-17 20:53:09 +02:00
size_t nread = fread(version, 4096, 1, fp);
if (ferror(fp))
{
perror("fread");
return 1;
}
version[nread] = 0;
2022-10-17 20:53:09 +02:00
if (fclose(fp) < 0)
{
perror("fclose");
return 1;
}
printf("Your kernel version is %s\n\n", version);
return 0;
}
2022-10-02 17:25:56 +02:00
int main()
{
if (getpid() != 1)
2022-10-02 17:25:56 +02:00
{
fprintf(stderr, "init should be started as PID 1\n");
return 1;
2022-10-02 17:25:56 +02:00
}
printf("Welcome to Luna!\n");
2022-10-02 17:25:56 +02:00
2022-10-19 19:42:05 +02:00
printf("Running as PID %ld\n", getpid());
2022-10-02 17:25:56 +02:00
if (print_version()) return 1;
2022-10-02 17:25:56 +02:00
2022-10-19 19:42:05 +02:00
sleep(1);
pid_t child = fork();
if (child < 0)
{
perror("fork");
return 1;
}
if (child == 0)
{
2022-10-19 19:42:05 +02:00
execv("/bin/sh", NULL);
perror("execv");
return 1;
}
2022-10-17 21:22:18 +02:00
pid_t result;
for (;;)
{
while ((result = wait(NULL)) == 0) // No child has exited yet
{
msleep(100);
}
if (result == child) { return 0; }
}
2022-10-02 17:25:56 +02:00
}