main.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <AK/Assertions.h>
  2. #include <errno.h>
  3. #include <sched.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <LibCore/CFile.h>
  9. void start_process(const char* prog, int prio)
  10. {
  11. pid_t pid = 0;
  12. while (true) {
  13. dbgprintf("Forking for %s...\n", prog);
  14. int pid = fork();
  15. if (pid < 0) {
  16. dbgprintf("Fork %s failed! %s\n", prog, strerror(errno));
  17. continue;
  18. } else if (pid > 0) {
  19. // parent...
  20. dbgprintf("Process %s hopefully started with priority %d...\n", prog, prio);
  21. return;
  22. }
  23. break;
  24. }
  25. while (true) {
  26. if (pid == 0) {
  27. dbgprintf("Executing for %s... at prio %d\n", prog, prio);
  28. struct sched_param p;
  29. p.sched_priority = prio;
  30. int ret = sched_setparam(pid, &p);
  31. ASSERT(ret == 0);
  32. char* progv[256];
  33. progv[0] = const_cast<char*>(prog);
  34. progv[1] = nullptr;
  35. ret = execv(prog, progv);
  36. if (ret < 0) {
  37. dbgprintf("Exec %s failed! %s", prog, strerror(errno));
  38. continue;
  39. }
  40. break;
  41. } else {
  42. break;
  43. }
  44. }
  45. }
  46. static void check_for_test_mode()
  47. {
  48. CFile f("/proc/cmdline");
  49. if (!f.open(CIODevice::ReadOnly)) {
  50. dbgprintf("Failed to read command line: %s\n", f.error_string());
  51. ASSERT(false);
  52. }
  53. const String cmd = String::copy(f.read_all());
  54. dbgprintf("Read command line: %s\n", cmd.characters());
  55. if (cmd.matches("*testmode=1*")) {
  56. // Eventually, we should run a test binary and wait for it to finish
  57. // before shutting down. But this is good enough for now.
  58. dbgprintf("Waiting for testmode shutdown...\n");
  59. sleep(5);
  60. dbgprintf("Shutting down due to testmode...\n");
  61. if (fork() == 0) {
  62. execl("/bin/shutdown", "/bin/shutdown", "-n", nullptr);
  63. ASSERT_NOT_REACHED();
  64. }
  65. } else {
  66. dbgprintf("Continuing normally\n");
  67. }
  68. }
  69. int main(int, char**)
  70. {
  71. int lowest_prio = sched_get_priority_min(SCHED_OTHER);
  72. int highest_prio = sched_get_priority_max(SCHED_OTHER);
  73. start_process("/bin/LookupServer", lowest_prio);
  74. start_process("/bin/WindowServer", highest_prio);
  75. start_process("/bin/AudioServer", highest_prio);
  76. start_process("/bin/Taskbar", highest_prio);
  77. start_process("/bin/Terminal", highest_prio - 1);
  78. start_process("/bin/Launcher", highest_prio);
  79. // This won't return if we're in test mode.
  80. check_for_test_mode();
  81. while (1) {
  82. sleep(1);
  83. }
  84. }