Luna/apps/src/init.c

67 lines
1020 B
C

#include <errno.h>
#include <luna.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
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');
}
int main()
{
if (getpid() != 1)
{
fprintf(stderr, "init should be started as PID 1\n");
return 1;
}
show_motd();
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;
}
pid_t result;
for (;;)
{
result = wait(NULL);
if (result == child) return 0;
}
}