main.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Service.h"
  27. #include <AK/Assertions.h>
  28. #include <AK/ByteBuffer.h>
  29. #include <LibCore/ConfigFile.h>
  30. #include <LibCore/Event.h>
  31. #include <LibCore/EventLoop.h>
  32. #include <LibCore/File.h>
  33. #include <errno.h>
  34. #include <signal.h>
  35. #include <stdio.h>
  36. #include <sys/stat.h>
  37. #include <sys/types.h>
  38. #include <sys/wait.h>
  39. #include <unistd.h>
  40. String g_boot_mode = "graphical";
  41. static void sigchld_handler(int)
  42. {
  43. for (;;) {
  44. int status = 0;
  45. pid_t pid = waitpid(-1, &status, WNOHANG);
  46. if (pid < 0) {
  47. perror("waitpid");
  48. break;
  49. }
  50. if (pid == 0)
  51. break;
  52. #ifdef SYSTEMSERVER_DEBUG
  53. dbg() << "Reaped child with pid " << pid << ", exit status " << status;
  54. #endif
  55. Service* service = Service::find_by_pid(pid);
  56. if (service == nullptr) {
  57. // This can happen for multi-instance services.
  58. continue;
  59. }
  60. service->did_exit(status);
  61. }
  62. }
  63. static void parse_boot_mode()
  64. {
  65. auto f = Core::File::construct("/proc/cmdline");
  66. if (!f->open(Core::IODevice::ReadOnly)) {
  67. dbg() << "Failed to read command line: " << f->error_string();
  68. return;
  69. }
  70. const String cmdline = String::copy(f->read_all(), Chomp);
  71. dbg() << "Read command line: " << cmdline;
  72. for (auto& part : cmdline.split_view(' ')) {
  73. auto pair = part.split_view('=', 2);
  74. if (pair.size() == 2 && pair[0] == "boot_mode")
  75. g_boot_mode = pair[1];
  76. }
  77. dbg() << "Booting in " << g_boot_mode << " mode";
  78. }
  79. static void prepare_devfs()
  80. {
  81. // FIXME: Find a better way to all of this stuff, without hardcoding all of this!
  82. int rc = mount(-1, "/dev", "dev", 0);
  83. if (rc != 0) {
  84. ASSERT_NOT_REACHED();
  85. }
  86. rc = mkdir("/dev/pts", 0755);
  87. if (rc != 0) {
  88. ASSERT_NOT_REACHED();
  89. }
  90. rc = mount(-1, "/dev/pts", "devpts", 0);
  91. if (rc != 0) {
  92. ASSERT_NOT_REACHED();
  93. }
  94. rc = symlink("/dev/random", "/dev/urandom");
  95. if (rc < 0) {
  96. ASSERT_NOT_REACHED();
  97. }
  98. // FIXME: Find a better way to chown without hardcoding the gid!
  99. rc = chown("/dev/fb0", 0, 3);
  100. if (rc < 0) {
  101. ASSERT_NOT_REACHED();
  102. }
  103. // FIXME: Find a better way to chown without hardcoding the gid!
  104. rc = chown("/dev/keyboard", 0, 3);
  105. if (rc < 0) {
  106. ASSERT_NOT_REACHED();
  107. }
  108. // FIXME: Find a better way to chown without hardcoding the gid!
  109. rc = chown("/dev/mouse", 0, 3);
  110. if (rc < 0) {
  111. ASSERT_NOT_REACHED();
  112. }
  113. for (size_t index = 0; index < 4; index++) {
  114. // FIXME: Find a better way to chown without hardcoding the gid!
  115. rc = chown(String::format("/dev/tty%d", index).characters(), 0, 2);
  116. if (rc < 0) {
  117. ASSERT_NOT_REACHED();
  118. }
  119. }
  120. for (size_t index = 0; index < 4; index++) {
  121. // FIXME: Find a better way to chown without hardcoding the gid!
  122. rc = chown(String::format("/dev/ttyS%d", index).characters(), 0, 2);
  123. if (rc < 0) {
  124. ASSERT_NOT_REACHED();
  125. }
  126. }
  127. // FIXME: Find a better way to chown without hardcoding the gid!
  128. rc = chown("/dev/audio", 0, 4);
  129. if (rc < 0) {
  130. ASSERT_NOT_REACHED();
  131. }
  132. rc = symlink("/proc/self/fd/0", "/dev/stdin");
  133. if (rc < 0) {
  134. ASSERT_NOT_REACHED();
  135. }
  136. rc = symlink("/proc/self/fd/1", "/dev/stdout");
  137. if (rc < 0) {
  138. ASSERT_NOT_REACHED();
  139. }
  140. rc = symlink("/proc/self/fd/2", "/dev/stderr");
  141. if (rc < 0) {
  142. ASSERT_NOT_REACHED();
  143. }
  144. }
  145. static void mount_all_filesystems()
  146. {
  147. dbgln("Spawning mount -a to mount all filesystems.");
  148. pid_t pid = fork();
  149. if (pid < 0) {
  150. perror("fork");
  151. ASSERT_NOT_REACHED();
  152. } else if (pid == 0) {
  153. execl("/bin/mount", "mount", "-a", nullptr);
  154. perror("exec");
  155. ASSERT_NOT_REACHED();
  156. } else {
  157. wait(nullptr);
  158. }
  159. }
  160. static void create_tmp_rpc_directory()
  161. {
  162. dbgln("Creating /tmp/rpc directory");
  163. auto old_umask = umask(0);
  164. auto rc = mkdir("/tmp/rpc", 01777);
  165. if (rc < 0) {
  166. perror("mkdir(/tmp/rpc)");
  167. ASSERT_NOT_REACHED();
  168. }
  169. umask(old_umask);
  170. }
  171. static void create_tmp_coredump_directory()
  172. {
  173. dbgln("Creating /tmp/coredump directory");
  174. auto old_umask = umask(0);
  175. auto rc = mkdir("/tmp/coredump", 0755);
  176. if (rc < 0) {
  177. perror("mkdir(/tmp/coredump)");
  178. ASSERT_NOT_REACHED();
  179. }
  180. umask(old_umask);
  181. }
  182. static void create_tmp_profiler_coredumps_directory()
  183. {
  184. dbgln("Creating /tmp/profiler_coredumps directory");
  185. auto old_umask = umask(0);
  186. auto rc = mkdir("/tmp/profiler_coredumps", 0755);
  187. if (rc < 0) {
  188. perror("mkdir(/tmp/profiler_coredumps)");
  189. ASSERT_NOT_REACHED();
  190. }
  191. umask(old_umask);
  192. }
  193. int main(int, char**)
  194. {
  195. prepare_devfs();
  196. if (pledge("stdio proc exec tty accept unix rpath wpath cpath chown fattr id sigaction", nullptr) < 0) {
  197. perror("pledge");
  198. return 1;
  199. }
  200. mount_all_filesystems();
  201. create_tmp_rpc_directory();
  202. create_tmp_coredump_directory();
  203. create_tmp_profiler_coredumps_directory();
  204. parse_boot_mode();
  205. Core::EventLoop event_loop;
  206. event_loop.register_signal(SIGCHLD, sigchld_handler);
  207. // Read our config and instantiate services.
  208. // This takes care of setting up sockets.
  209. NonnullRefPtrVector<Service> services;
  210. auto config = Core::ConfigFile::get_for_system("SystemServer");
  211. for (auto name : config->groups()) {
  212. auto service = Service::construct(*config, name);
  213. if (service->is_enabled())
  214. services.append(service);
  215. }
  216. // After we've set them all up, activate them!
  217. dbg() << "Activating " << services.size() << " services...";
  218. for (auto& service : services)
  219. service.activate();
  220. return event_loop.exec();
  221. }