exec.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "exec.h"
  2. #include "entry.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <dirent.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. void umount_all()
  12. {
  13. DIR *d;
  14. struct dirent *dir;
  15. d = opendir("/tmp/mountpoint/");
  16. if (d)
  17. {
  18. while ((dir = readdir(d)) != NULL)
  19. {
  20. struct stat buf;
  21. char path[256];
  22. if(!strcmp(dir->d_name, ".") || !strcmp(dir->d_name, "..")) continue;
  23. sprintf(path, "/tmp/mountpoint/%s", dir->d_name);
  24. if(lstat(path, &buf))
  25. {
  26. fprintf(stderr, "Error can't stat '%s'", path);
  27. perror(" ");
  28. continue;
  29. }
  30. if(S_ISDIR(buf.st_mode))
  31. {
  32. if(umount(path))
  33. {
  34. fprintf(stderr, "Unable to unmount '%s'", path);
  35. perror(" ");
  36. }
  37. }
  38. }
  39. closedir(d);
  40. }
  41. }
  42. int file_exists(const char* file)
  43. {
  44. FILE* f=fopen(file, "r");
  45. if(!f)
  46. {
  47. fprintf(stderr, "Erreur fichier '%s' ", file);
  48. perror(" ");
  49. return -1;
  50. }
  51. fclose(f);
  52. return 0;
  53. }
  54. int os_exec(const char* base, os_entry_t* os)
  55. {
  56. int pid;
  57. char cmd[4096];//--initrd=/initrd.img
  58. char kernel[512], initrd[512]="", root_fs[512], root_path[512], root_type[16], cmdline[2048];
  59. sprintf(cmdline, "--append=\"%s single root_fs=%s root_path=%s \"", os->append, os->rootfs, root_path);
  60. sprintf(kernel, "/tmp/mountpoint/%s/%s/%s", base, os->location, os->kernel);
  61. if(file_exists(kernel)) return -1;
  62. sprintf(initrd, "--initrd=/initrd.img");
  63. sprintf(cmd, "kexec -l %s --initrd=/initrd.img --append=root=/dev/sda1", kernel, cmdline);
  64. printf("%s\n",cmd);
  65. /*if(pid=fork())
  66. {
  67. int ret=1, rpid;
  68. rpid=waitpid(pid, &ret, 0);
  69. if(ret && pid==rpid)
  70. {
  71. fprintf(stderr, "Unable to load kexec : %s\nexitting...\n", strerror(ret));
  72. return -1;
  73. }
  74. }
  75. else
  76. {
  77. if(execl("/usr/sbin/kexec", "/usr/sbin/kexec", "-l", kernel, initrd, cmdline, NULL))
  78. {
  79. fprintf(stderr, "Unable to exec kexec");
  80. perror(" ");
  81. }
  82. }*/
  83. //sleep(10);
  84. system(cmd);
  85. //system("umount -a");
  86. /*system("umount /");
  87. system("umount /tmp/mountpoint/sdb1");
  88. system("umount /proc");
  89. system("umount /sys");
  90. system("umount /dev");*/
  91. //umount_all();
  92. //sleep(10);
  93. //system("umount -a");
  94. /*if(pid=fork())
  95. {
  96. int ret;
  97. waitpid(pid, &ret, 0);
  98. if(ret)
  99. {
  100. fprintf(stderr, "Unable to kexec : %s\nexitting...\n", strerror(ret));
  101. return -1;
  102. }
  103. //unreachable code
  104. }
  105. else
  106. {
  107. if(!execl("/usr/sbin/kexec", "/usr/bin/kexec", "-e", NULL))
  108. {
  109. fprintf(stderr, "Unable to exec kexec");
  110. perror(" ");
  111. }
  112. }*/
  113. system("kexec -e");
  114. }