Advertisement
Carsten_Milkau

init.c (original version)

Jun 15th, 2016
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include <errno.h>
  2. #include <error.h>
  3. #include <limits.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <sys/mount.h>
  7. #include <sys/types.h>
  8. #include <sys/wait.h>
  9.  
  10. extern char     **environ;
  11.  
  12. int     run()
  13. {
  14.         const char * const      root    = "/dev/md/Volume0_0p3";
  15.         const char * const      rootfs  = "xfs";
  16.         char                    real_root[PATH_MAX];
  17.         int                     retval  = EXIT_SUCCESS;
  18.         pid_t                   child;
  19.  
  20.         // mount /sys and /proc
  21.         if (mount("proc", "/proc", "proc", MS_MGC_VAL, NULL) == -1)
  22.         { error(0, errno, "mount -t proc proc /proc failed with error %d", errno); return errno; }
  23.         if (mount("sysfs", "/sys", "sysfs", MS_MGC_VAL, NULL) == -1)
  24.         { error(0, errno, "mount -t sysfs sysfs /sys failed with error %d", errno); return errno; }
  25.  
  26.         // run mdadm
  27.         child = vfork();
  28.         if (child == 0) { execl("/sbin/mdadm", "/sbin/mdadm", "--assemble", "--scan", "--auto=mdp", NULL); }
  29.         else
  30.         {
  31.                 siginfo_t       info;
  32.  
  33.                 waitid(P_PID, child, &info, WEXITED);
  34.                 retval = info.si_status;
  35.         }
  36.         if (retval != EXIT_SUCCESS) { error(0, 0, "mdadm --assemble --scan --auto=mdp exited with status %d, aborting.\n", retval); return retval; }
  37.  
  38.         // mount /
  39.         if (realpath(root, real_root) == NULL)
  40.         { error(0, errno, "realpath %s failed with error %d", root, errno); return errno; }
  41.         if (mount(real_root, "/", rootfs, MS_MGC_VAL|MS_RDONLY, NULL) == -1)
  42.         { error(0, errno, "mount -o ro -t %s %s / failed with error %d", rootfs, root, errno); return errno; }
  43.  
  44.         // run init
  45.         retval = execl("/sbin/init", "/sbin/init", NULL);
  46.         if (retval == -1) { error(0, errno, "/sbin/init failed with error %d", errno); return errno; }
  47.         else if (retval != EXIT_SUCCESS) { error(0, 0, "init exited with status %d, aborting.\n", retval); return retval; }
  48.  
  49.         return EXIT_SUCCESS;
  50. }
  51.  
  52. int     main (int argc, const char * const argv[])
  53. {
  54.         int retval = run();
  55.  
  56.         sleep(30);
  57.  
  58.         return retval;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement