main.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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_matching_device_nodes(group* group, unsigned major_number)
  77. {
  78. VERIFY(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 (major(cur_file_stat.st_rdev) != major_number)
  89. continue;
  90. chown_wrapper(entry_name.characters(), 0, group->gr_gid);
  91. }
  92. }
  93. static void prepare_devfs()
  94. {
  95. // FIXME: Find a better way to all of this stuff, without hardcoding all of this!
  96. int rc = mount(-1, "/dev", "dev", 0);
  97. if (rc != 0) {
  98. VERIFY_NOT_REACHED();
  99. }
  100. rc = mount(-1, "/sys", "sys", 0);
  101. if (rc != 0) {
  102. VERIFY_NOT_REACHED();
  103. }
  104. rc = mkdir("/dev/pts", 0755);
  105. if (rc != 0) {
  106. VERIFY_NOT_REACHED();
  107. }
  108. rc = mount(-1, "/dev/pts", "devpts", 0);
  109. if (rc != 0) {
  110. VERIFY_NOT_REACHED();
  111. }
  112. rc = symlink("/dev/random", "/dev/urandom");
  113. if (rc < 0) {
  114. VERIFY_NOT_REACHED();
  115. }
  116. auto phys_group = getgrnam("phys");
  117. VERIFY(phys_group);
  118. // FIXME: Try to find a way to not hardcode the major number of framebuffer device nodes.
  119. chown_all_matching_device_nodes(phys_group, 29);
  120. chown_wrapper("/dev/keyboard0", 0, phys_group->gr_gid);
  121. chown_wrapper("/dev/mouse0", 0, phys_group->gr_gid);
  122. auto tty_group = getgrnam("tty");
  123. VERIFY(tty_group);
  124. // FIXME: Try to find a way to not hardcode the major number of tty nodes.
  125. chown_all_matching_device_nodes(tty_group, 4);
  126. auto audio_group = getgrnam("audio");
  127. VERIFY(audio_group);
  128. chown_wrapper("/dev/audio", 0, audio_group->gr_gid);
  129. rc = symlink("/proc/self/fd/0", "/dev/stdin");
  130. if (rc < 0) {
  131. VERIFY_NOT_REACHED();
  132. }
  133. rc = symlink("/proc/self/fd/1", "/dev/stdout");
  134. if (rc < 0) {
  135. VERIFY_NOT_REACHED();
  136. }
  137. rc = symlink("/proc/self/fd/2", "/dev/stderr");
  138. if (rc < 0) {
  139. VERIFY_NOT_REACHED();
  140. }
  141. endgrent();
  142. }
  143. static void mount_all_filesystems()
  144. {
  145. dbgln("Spawning mount -a to mount all filesystems.");
  146. pid_t pid = fork();
  147. if (pid < 0) {
  148. perror("fork");
  149. VERIFY_NOT_REACHED();
  150. } else if (pid == 0) {
  151. execl("/bin/mount", "mount", "-a", nullptr);
  152. perror("exec");
  153. VERIFY_NOT_REACHED();
  154. } else {
  155. wait(nullptr);
  156. }
  157. }
  158. static void create_tmp_coredump_directory()
  159. {
  160. dbgln("Creating /tmp/coredump directory");
  161. auto old_umask = umask(0);
  162. // FIXME: the coredump directory should be made read-only once CrashDaemon is no longer responsible for compressing coredumps
  163. auto rc = mkdir("/tmp/coredump", 0777);
  164. if (rc < 0) {
  165. perror("mkdir(/tmp/coredump)");
  166. VERIFY_NOT_REACHED();
  167. }
  168. umask(old_umask);
  169. }
  170. int main(int, char**)
  171. {
  172. prepare_devfs();
  173. if (pledge("stdio proc exec tty accept unix rpath wpath cpath chown fattr id sigaction", nullptr) < 0) {
  174. perror("pledge");
  175. return 1;
  176. }
  177. mount_all_filesystems();
  178. create_tmp_coredump_directory();
  179. parse_boot_mode();
  180. Core::EventLoop event_loop;
  181. event_loop.register_signal(SIGCHLD, sigchld_handler);
  182. // Read our config and instantiate services.
  183. // This takes care of setting up sockets.
  184. NonnullRefPtrVector<Service> services;
  185. auto config = Core::ConfigFile::open_for_system("SystemServer");
  186. for (auto name : config->groups()) {
  187. auto service = Service::construct(*config, name);
  188. if (service->is_enabled())
  189. services.append(service);
  190. }
  191. // After we've set them all up, activate them!
  192. dbgln("Activating {} services...", services.size());
  193. for (auto& service : services)
  194. service.activate();
  195. return event_loop.exec();
  196. }