Luna/apps/src/init.c

78 lines
1.2 KiB
C

#include <errno.h>
#include <fcntl.h>
#include <luna.h>
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
void show_motd()
{
int fd = open("/etc/motd", O_RDONLY | O_CLOEXEC);
if (fd < 0)
{
if (errno != ENOENT) { perror("open"); }
return;
}
FILE* fp = fdopen(fd, "r");
if (!fp)
{
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 must be started as PID 1\n");
return 1;
}
if (getuid() != 0)
{
fprintf(stderr, "init must be started as root\n");
return 1;
}
show_motd();
pid_t child = fork();
if (child < 0)
{
perror("fork");
return 1;
}
if (child == 0)
{
char* argv[] = {"/bin/session", NULL};
execv(argv[0], argv);
perror("execv");
return 1;
}
pid_t result;
for (;;)
{
result = wait(NULL);
if (result == child) return 0;
}
}