Luna/apps/src/init.c

67 lines
1020 B
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)
{
char* argv[] = {"/bin/sh", NULL};
execv(argv[0], argv);
perror("execv");
return 1;
}
2022-10-17 21:22:18 +02:00
pid_t result;
for (;;)
{
result = wait(NULL);
2022-10-25 18:58:06 +02:00
if (result == child) return 0;
}
2022-10-02 17:25:56 +02:00
}