Luna/apps/src/init.c

69 lines
1.0 KiB
C
Raw Normal View History

2022-10-20 19:27:37 +02:00
#include <errno.h>
#include <luna.h>
#include <stdio.h>
#include <sys/wait.h>
2022-10-02 17:25:56 +02:00
#include <unistd.h>
2022-10-20 19:27:37 +02:00
void show_motd()
{
FILE* fp = fopen("/etc/motd", "r");
if (!fp)
{
if (errno != ENOENT) { perror("fopen"); }
return;
}
char buf[4096];
size_t nread = fread(buf, sizeof(buf) - 1, 1, fp);
if (ferror(fp))
{
perror("fread");
fclose(fp);
return;
}
buf[nread] = 0;
puts(buf);
fclose(fp);
putchar('\n');
}
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
}
2022-10-20 19:27:37 +02:00
show_motd();
2022-10-02 17:25:56 +02:00
2022-10-19 20:36:27 +02:00
msleep(200);
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
}