main.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <AK/Assertions.h>
  2. #include <LibCore/CFile.h>
  3. #include <errno.h>
  4. #include <fcntl.h>
  5. #include <sched.h>
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <sys/wait.h>
  11. #include <unistd.h>
  12. void sigchld_handler(int)
  13. {
  14. int status = 0;
  15. pid_t pid = waitpid(-1, &status, WNOHANG);
  16. if (pid)
  17. dbg() << "reaped pid " << pid;
  18. }
  19. void start_process(const String& program, const Vector<String>& arguments, int prio, const char* tty = nullptr)
  20. {
  21. pid_t pid = 0;
  22. while (true) {
  23. dbg() << "Forking for " << program << "...";
  24. int pid = fork();
  25. if (pid < 0) {
  26. dbg() << "Fork " << program << " failed! " << strerror(errno);
  27. continue;
  28. } else if (pid > 0) {
  29. // parent...
  30. dbg() << "Process " << program << " hopefully started with priority " << prio << "...";
  31. return;
  32. }
  33. break;
  34. }
  35. while (true) {
  36. dbg() << "Executing for " << program << "... at prio " << prio;
  37. struct sched_param p;
  38. p.sched_priority = prio;
  39. int ret = sched_setparam(pid, &p);
  40. ASSERT(ret == 0);
  41. if (tty != nullptr) {
  42. close(0);
  43. ASSERT(open(tty, O_RDWR) == 0);
  44. dup2(0, 1);
  45. dup2(0, 2);
  46. }
  47. char* progv[256];
  48. progv[0] = const_cast<char*>(program.characters());
  49. for (int i = 0; i < arguments.size() && i < 254; i++)
  50. progv[i + 1] = const_cast<char*>(arguments[i].characters());
  51. progv[arguments.size() + 1] = nullptr;
  52. ret = execv(progv[0], progv);
  53. if (ret < 0) {
  54. dbg() << "Exec " << progv[0] << " failed! " << strerror(errno);
  55. continue;
  56. }
  57. break;
  58. }
  59. }
  60. static void check_for_test_mode()
  61. {
  62. auto f = CFile::construct("/proc/cmdline");
  63. if (!f->open(CIODevice::ReadOnly)) {
  64. dbg() << "Failed to read command line: " << f->error_string();
  65. ASSERT(false);
  66. }
  67. const String cmd = String::copy(f->read_all());
  68. dbg() << "Read command line: " << cmd;
  69. if (cmd.matches("*testmode=1*")) {
  70. // Eventually, we should run a test binary and wait for it to finish
  71. // before shutting down. But this is good enough for now.
  72. dbg() << "Waiting for testmode shutdown...";
  73. sleep(5);
  74. dbg() << "Shutting down due to testmode...";
  75. if (fork() == 0) {
  76. execl("/bin/shutdown", "/bin/shutdown", "-n", nullptr);
  77. ASSERT_NOT_REACHED();
  78. }
  79. } else {
  80. dbg() << "Continuing normally";
  81. }
  82. }
  83. int main(int, char**)
  84. {
  85. int lowest_prio = sched_get_priority_min(SCHED_OTHER);
  86. int highest_prio = sched_get_priority_max(SCHED_OTHER);
  87. // Mount the filesystems.
  88. start_process("/bin/mount", { "-a" }, highest_prio);
  89. wait(nullptr);
  90. // NOTE: We don't start anything on tty0 since that's the "active" TTY while WindowServer is up.
  91. start_process("/bin/TTYServer", { "tty1" }, highest_prio, "/dev/tty1");
  92. // Drop privileges.
  93. setgid(100);
  94. setuid(100);
  95. signal(SIGCHLD, sigchld_handler);
  96. start_process("/bin/LookupServer", {}, lowest_prio);
  97. start_process("/bin/WindowServer", {}, highest_prio);
  98. start_process("/bin/AudioServer", {}, highest_prio);
  99. start_process("/bin/Taskbar", {}, highest_prio);
  100. start_process("/bin/Terminal", {}, highest_prio - 1);
  101. start_process("/bin/Launcher", {}, highest_prio);
  102. // This won't return if we're in test mode.
  103. check_for_test_mode();
  104. while (1) {
  105. sleep(3600);
  106. }
  107. }