2023-03-18 18:23:18 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/stat.h>
|
2023-03-18 20:55:16 +00:00
|
|
|
#include <sys/syscall.h>
|
2023-03-18 18:23:18 +00:00
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define xmknod(path, mode, maj, min) \
|
|
|
|
if (mknod(path, mode, makedev(maj, min)) < 0) exit(255);
|
|
|
|
|
|
|
|
// Too early for console logs (/dev/console is created here!), so we have to resort to exiting with a weird exit code in
|
|
|
|
// case of failure.
|
|
|
|
static void populate_devfs()
|
|
|
|
{
|
|
|
|
if (mkdir("/dev", 0755) < 0 && errno != EEXIST) exit(255);
|
|
|
|
|
|
|
|
xmknod("/dev/console", 0666, 1, 0);
|
|
|
|
xmknod("/dev/null", 0666, 2, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
if (getpid() != 1)
|
|
|
|
{
|
2023-03-19 18:19:20 +00:00
|
|
|
fprintf(stderr, "error: init not running as PID 1.\n");
|
2023-03-18 18:23:18 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
populate_devfs();
|
|
|
|
|
2023-03-19 18:19:20 +00:00
|
|
|
// Before this point, we don't even have an stdin, stdout and stderr. Set it up now so that child processes (and us)
|
|
|
|
// can print stuff.
|
|
|
|
stdin = fopen("/dev/console", "r");
|
2023-03-18 18:23:18 +00:00
|
|
|
stdout = fopen("/dev/console", "w");
|
|
|
|
stderr = fopen("/dev/console", "w");
|
|
|
|
|
2023-03-18 22:58:56 +00:00
|
|
|
pid_t ret = fork();
|
2023-03-18 22:45:48 +00:00
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
{
|
2023-03-23 21:19:54 +00:00
|
|
|
char* argv[] = { "/bin/sh", NULL };
|
|
|
|
execv(argv[0], argv);
|
|
|
|
perror("execv");
|
|
|
|
return 1;
|
2023-03-18 22:45:48 +00:00
|
|
|
}
|
2023-03-23 21:19:54 +00:00
|
|
|
|
|
|
|
while (1)
|
|
|
|
;
|
2023-03-18 18:23:18 +00:00
|
|
|
}
|