coreutils

custom mini coreutils
git clone git://git.evenfri.pw/coreutils.git
Log | Files | Refs | README

init.c (1052B)


      1 #include <libc.h>
      2 
      3 #define MSG_CLEAR "\033[2J\033[H"
      4 #define MSG_WELCOME "[init] starting...\n"
      5 #define MSG_PROC "[init] mouting /proc...\n"
      6 #define MSG_SYS "[init] mouting /sys...\n"
      7 #define MSG_DEV "[init] mouting /dev...\n"
      8 
      9 int main(void) {
     10   write(stdout, MSG_CLEAR, strlen(MSG_CLEAR));
     11 
     12   write(stdout, MSG_WELCOME, strlen(MSG_WELCOME));
     13 
     14   write(stdout, MSG_PROC, strlen(MSG_PROC));
     15   mount("proc", "/proc", "proc", 0, NULL);
     16 
     17   write(stdout, MSG_SYS, strlen(MSG_SYS));
     18   mount("sysfs", "/sys", "sysfs", 0, NULL);
     19 
     20   write(stdout, MSG_DEV, strlen(MSG_DEV));
     21   mount("devtmpfs", "/dev", "devtmpfs", 0, NULL);
     22 
     23   while (1) {
     24     int pid = fork();
     25     if (pid < 0) {
     26       sleep(1);
     27       continue;
     28     }
     29 
     30     if (pid == 0) {
     31       char *argv[] = { "/bin/sh", NULL };
     32       char *envp[] = { "PATH=/bin:/sbin", NULL };
     33 
     34       execve("/bin/sh", argv, envp);
     35       const char msg[] = "[init] Failed to exec /bin/sh!\n";
     36       write(stderr, msg, sizeof(msg) - 1);
     37       _exit(1);
     38     }
     39 
     40     int status;
     41     waitpid(pid, &status, 0);
     42   }
     43 
     44   return 0;
     45 }