main.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Service.h"
  7. #include <AK/Assertions.h>
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/Debug.h>
  10. #include <LibCore/ConfigFile.h>
  11. #include <LibCore/DirIterator.h>
  12. #include <LibCore/Event.h>
  13. #include <LibCore/EventLoop.h>
  14. #include <LibCore/File.h>
  15. #include <errno.h>
  16. #include <grp.h>
  17. #include <signal.h>
  18. #include <stdio.h>
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. #include <sys/wait.h>
  22. #include <unistd.h>
  23. String g_boot_mode = "graphical";
  24. static void sigchld_handler(int)
  25. {
  26. for (;;) {
  27. int status = 0;
  28. pid_t pid = waitpid(-1, &status, WNOHANG);
  29. if (pid < 0) {
  30. perror("waitpid");
  31. break;
  32. }
  33. if (pid == 0)
  34. break;
  35. dbgln_if(SYSTEMSERVER_DEBUG, "Reaped child with pid {}, exit status {}", pid, status);
  36. Service* service = Service::find_by_pid(pid);
  37. if (service == nullptr) {
  38. // This can happen for multi-instance services.
  39. continue;
  40. }
  41. service->did_exit(status);
  42. }
  43. }
  44. static void parse_boot_mode()
  45. {
  46. auto f = Core::File::construct("/proc/cmdline");
  47. if (!f->open(Core::OpenMode::ReadOnly)) {
  48. dbgln("Failed to read command line: {}", f->error_string());
  49. return;
  50. }
  51. const String cmdline = String::copy(f->read_all(), Chomp);
  52. dbgln("Read command line: {}", cmdline);
  53. // FIXME: Support more than one framebuffer detection
  54. struct stat file_state;
  55. int rc = lstat("/dev/fb0", &file_state);
  56. if (rc < 0) {
  57. for (auto& part : cmdline.split_view(' ')) {
  58. auto pair = part.split_view('=', 2);
  59. if (pair.size() == 2 && pair[0] == "boot_mode")
  60. g_boot_mode = pair[1];
  61. }
  62. // We could boot into self-test which is not graphical too.
  63. if (g_boot_mode == "self-test")
  64. return;
  65. g_boot_mode = "text";
  66. }
  67. dbgln("Booting in {} mode", g_boot_mode);
  68. }
  69. static void chown_wrapper(const char* path, uid_t uid, gid_t gid)
  70. {
  71. int rc = chown(path, uid, gid);
  72. if (rc < 0 && errno != ENOENT) {
  73. VERIFY_NOT_REACHED();
  74. }
  75. }
  76. static void chown_all_framebuffer_devices(group* phys_group)
  77. {
  78. VERIFY(phys_group);
  79. struct stat cur_file_stat;
  80. Core::DirIterator di("/dev/", Core::DirIterator::SkipParentAndBaseDir);
  81. if (di.has_error())
  82. VERIFY_NOT_REACHED();
  83. while (di.has_next()) {
  84. auto entry_name = di.next_full_path();
  85. auto rc = stat(entry_name.characters(), &cur_file_stat);
  86. if (rc < 0)
  87. continue;
  88. if (!S_ISBLK(cur_file_stat.st_mode))
  89. continue;
  90. // FIXME: Try to find a way to not hardcode the major number of framebuffer device nodes.
  91. if (major(cur_file_stat.st_rdev) != 29)
  92. continue;
  93. chown_wrapper(entry_name.characters(), 0, phys_group->gr_gid);
  94. }
  95. }
  96. static void prepare_devfs()
  97. {
  98. // FIXME: Find a better way to all of this stuff, without hardcoding all of this!
  99. int rc = mount(-1, "/dev", "dev", 0);
  100. if (rc != 0) {
  101. VERIFY_NOT_REACHED();
  102. }
  103. rc = mount(-1, "/sys", "sys", 0);
  104. if (rc != 0) {
  105. VERIFY_NOT_REACHED();
  106. }
  107. rc = mkdir("/dev/pts", 0755);
  108. if (rc != 0) {
  109. VERIFY_NOT_REACHED();
  110. }
  111. rc = mount(-1, "/dev/pts", "devpts", 0);
  112. if (rc != 0) {
  113. VERIFY_NOT_REACHED();
  114. }
  115. rc = symlink("/dev/random", "/dev/urandom");
  116. if (rc < 0) {
  117. VERIFY_NOT_REACHED();
  118. }
  119. auto phys_group = getgrnam("phys");
  120. VERIFY(phys_group);
  121. chown_all_framebuffer_devices(phys_group);
  122. chown_wrapper("/dev/keyboard0", 0, phys_group->gr_gid);
  123. chown_wrapper("/dev/mouse0", 0, phys_group->gr_gid);
  124. auto tty_group = getgrnam("tty");
  125. VERIFY(tty_group);
  126. // FIXME: Count TTYs instead of using a hardcoded amount
  127. for (size_t index = 0; index < 6; index++) {
  128. chown_wrapper(String::formatted("/dev/tty{}", index).characters(), 0, tty_group->gr_gid);
  129. }
  130. // FIXME: Count serial TTYs instead of using a hardcoded amount
  131. for (size_t index = 0; index < 4; index++) {
  132. chown_wrapper(String::formatted("/dev/ttyS{}", index).characters(), 0, tty_group->gr_gid);
  133. }
  134. auto audio_group = getgrnam("audio");
  135. VERIFY(audio_group);
  136. chown_wrapper("/dev/audio", 0, audio_group->gr_gid);
  137. rc = symlink("/proc/self/fd/0", "/dev/stdin");
  138. if (rc < 0) {
  139. VERIFY_NOT_REACHED();
  140. }
  141. rc = symlink("/proc/self/fd/1", "/dev/stdout");
  142. if (rc < 0) {
  143. VERIFY_NOT_REACHED();
  144. }
  145. rc = symlink("/proc/self/fd/2", "/dev/stderr");
  146. if (rc < 0) {
  147. VERIFY_NOT_REACHED();
  148. }
  149. endgrent();
  150. }
  151. static void mount_all_filesystems()
  152. {
  153. dbgln("Spawning mount -a to mount all filesystems.");
  154. pid_t pid = fork();
  155. if (pid < 0) {
  156. perror("fork");
  157. VERIFY_NOT_REACHED();
  158. } else if (pid == 0) {
  159. execl("/bin/mount", "mount", "-a", nullptr);
  160. perror("exec");
  161. VERIFY_NOT_REACHED();
  162. } else {
  163. wait(nullptr);
  164. }
  165. }
  166. static void create_tmp_coredump_directory()
  167. {
  168. dbgln("Creating /tmp/coredump directory");
  169. auto old_umask = umask(0);
  170. // FIXME: the coredump directory should be made read-only once CrashDaemon is no longer responsible for compressing coredumps
  171. auto rc = mkdir("/tmp/coredump", 0777);
  172. if (rc < 0) {
  173. perror("mkdir(/tmp/coredump)");
  174. VERIFY_NOT_REACHED();
  175. }
  176. umask(old_umask);
  177. }
  178. int main(int, char**)
  179. {
  180. prepare_devfs();
  181. if (pledge("stdio proc exec tty accept unix rpath wpath cpath chown fattr id sigaction", nullptr) < 0) {
  182. perror("pledge");
  183. return 1;
  184. }
  185. mount_all_filesystems();
  186. create_tmp_coredump_directory();
  187. parse_boot_mode();
  188. Core::EventLoop event_loop;
  189. event_loop.register_signal(SIGCHLD, sigchld_handler);
  190. // Read our config and instantiate services.
  191. // This takes care of setting up sockets.
  192. NonnullRefPtrVector<Service> services;
  193. auto config = Core::ConfigFile::get_for_system("SystemServer");
  194. for (auto name : config->groups()) {
  195. auto service = Service::construct(*config, name);
  196. if (service->is_enabled())
  197. services.append(service);
  198. }
  199. // After we've set them all up, activate them!
  200. dbgln("Activating {} services...", services.size());
  201. for (auto& service : services)
  202. service.activate();
  203. return event_loop.exec();
  204. }