Service.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include "Service.h"
  2. #include <AK/HashMap.h>
  3. #include <AK/JsonArray.h>
  4. #include <AK/JsonObject.h>
  5. #include <LibCore/CConfigFile.h>
  6. #include <LibCore/CLocalSocket.h>
  7. #include <fcntl.h>
  8. #include <grp.h>
  9. #include <libgen.h>
  10. #include <pwd.h>
  11. #include <sched.h>
  12. #include <stdio.h>
  13. #include <sys/ioctl.h>
  14. #include <sys/stat.h>
  15. #include <unistd.h>
  16. struct UidAndGids {
  17. uid_t uid;
  18. gid_t gid;
  19. Vector<gid_t> extra_gids;
  20. };
  21. static HashMap<String, UidAndGids>* s_user_map;
  22. static HashMap<pid_t, Service*> s_service_map;
  23. void Service::resolve_user()
  24. {
  25. if (s_user_map == nullptr) {
  26. s_user_map = new HashMap<String, UidAndGids>;
  27. for (struct passwd* passwd = getpwent(); passwd; passwd = getpwent()) {
  28. Vector<gid_t> extra_gids;
  29. for (struct group* group = getgrent(); group; group = getgrent()) {
  30. for (size_t m = 0; group->gr_mem[m]; ++m) {
  31. if (!strcmp(group->gr_mem[m], passwd->pw_name))
  32. extra_gids.append(group->gr_gid);
  33. }
  34. }
  35. endgrent();
  36. s_user_map->set(passwd->pw_name, { passwd->pw_uid, passwd->pw_gid, move(extra_gids) });
  37. }
  38. endpwent();
  39. }
  40. auto user = s_user_map->get(m_user);
  41. if (!user.has_value()) {
  42. dbg() << "Failed to resolve user name " << m_user;
  43. ASSERT_NOT_REACHED();
  44. }
  45. m_uid = user.value().uid;
  46. m_gid = user.value().gid;
  47. m_extra_gids = user.value().extra_gids;
  48. }
  49. Service* Service::find_by_pid(pid_t pid)
  50. {
  51. auto it = s_service_map.find(pid);
  52. if (it == s_service_map.end())
  53. return nullptr;
  54. return (*it).value;
  55. }
  56. static int ensure_parent_directories(const char* path)
  57. {
  58. ASSERT(path[0] == '/');
  59. char* parent_buffer = strdup(path);
  60. const char* parent = dirname(parent_buffer);
  61. int rc = 0;
  62. while (true) {
  63. int rc = mkdir(parent, 0755);
  64. if (rc == 0)
  65. break;
  66. if (errno != ENOENT)
  67. break;
  68. ensure_parent_directories(parent);
  69. };
  70. free(parent_buffer);
  71. return rc;
  72. }
  73. void Service::setup_socket()
  74. {
  75. ASSERT(!m_socket_path.is_null());
  76. ASSERT(m_socket_fd == -1);
  77. ensure_parent_directories(m_socket_path.characters());
  78. // Note: we use SOCK_CLOEXEC here to make sure we don't leak every socket to
  79. // all the clients. We'll make the one we do need to pass down !CLOEXEC later
  80. // after forking off the process.
  81. m_socket_fd = socket(AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  82. if (m_socket_fd < 0) {
  83. perror("socket");
  84. ASSERT_NOT_REACHED();
  85. }
  86. if (fchown(m_socket_fd, m_uid, m_gid) < 0) {
  87. perror("fchown");
  88. ASSERT_NOT_REACHED();
  89. }
  90. if (fchmod(m_socket_fd, 0600) < 0) {
  91. perror("fchmod");
  92. ASSERT_NOT_REACHED();
  93. }
  94. auto socket_address = CSocketAddress::local(m_socket_path);
  95. auto un = socket_address.to_sockaddr_un();
  96. int rc = bind(m_socket_fd, (const sockaddr*)&un, sizeof(un));
  97. if (rc < 0) {
  98. perror("bind");
  99. ASSERT_NOT_REACHED();
  100. }
  101. rc = listen(m_socket_fd, 5);
  102. if (rc < 0) {
  103. perror("listen");
  104. ASSERT_NOT_REACHED();
  105. }
  106. }
  107. void Service::setup_notifier()
  108. {
  109. ASSERT(m_lazy);
  110. ASSERT(m_socket_fd >= 0);
  111. ASSERT(!m_socket_notifier);
  112. m_socket_notifier = CNotifier::construct(m_socket_fd, CNotifier::Event::Read, this);
  113. m_socket_notifier->on_ready_to_read = [this] {
  114. dbg() << "Ready to read on behalf of " << name();
  115. remove_child(*m_socket_notifier);
  116. m_socket_notifier = nullptr;
  117. spawn();
  118. };
  119. }
  120. void Service::activate()
  121. {
  122. ASSERT(m_pid < 0);
  123. if (m_lazy)
  124. setup_notifier();
  125. else
  126. spawn();
  127. }
  128. void Service::spawn()
  129. {
  130. dbg() << "Spawning " << name();
  131. m_pid = fork();
  132. if (m_pid < 0) {
  133. perror("fork");
  134. ASSERT_NOT_REACHED();
  135. } else if (m_pid == 0) {
  136. // We are the child.
  137. struct sched_param p;
  138. p.sched_priority = m_priority;
  139. int rc = sched_setparam(0, &p);
  140. if (rc < 0) {
  141. perror("sched_setparam");
  142. ASSERT_NOT_REACHED();
  143. }
  144. if (!m_stdio_file_path.is_null()) {
  145. close(STDIN_FILENO);
  146. int fd = open_with_path_length(m_stdio_file_path.characters(), m_stdio_file_path.length(), O_RDWR, 0);
  147. ASSERT(fd <= 0);
  148. if (fd < 0) {
  149. perror("open");
  150. ASSERT_NOT_REACHED();
  151. }
  152. dup2(STDIN_FILENO, STDOUT_FILENO);
  153. dup2(STDIN_FILENO, STDERR_FILENO);
  154. if (isatty(STDIN_FILENO)) {
  155. ioctl(STDIN_FILENO, TIOCSCTTY);
  156. }
  157. } else {
  158. if (isatty(STDIN_FILENO)) {
  159. ioctl(STDIN_FILENO, TIOCNOTTY);
  160. }
  161. close(STDIN_FILENO);
  162. close(STDOUT_FILENO);
  163. close(STDERR_FILENO);
  164. }
  165. if (!m_socket_path.is_null()) {
  166. ASSERT(m_socket_fd > 2);
  167. dup2(m_socket_fd, 3);
  168. // The new descriptor is !CLOEXEC here.
  169. // This is true even if m_socket_fd == 3.
  170. setenv("SOCKET_TAKEOVER", "1", true);
  171. }
  172. if (!m_user.is_null()) {
  173. if (setgid(m_gid) < 0 || setgroups(m_extra_gids.size(), m_extra_gids.data()) < 0 || setuid(m_uid) < 0) {
  174. dbgprintf("Failed to drop privileges (GID=%u, UID=%u)\n", m_gid, m_uid);
  175. exit(1);
  176. }
  177. }
  178. char* argv[m_extra_arguments.size() + 2];
  179. argv[0] = const_cast<char*>(m_executable_path.characters());
  180. for (int i = 0; i < m_extra_arguments.size(); i++)
  181. argv[i + 1] = const_cast<char*>(m_extra_arguments[i].characters());
  182. argv[m_extra_arguments.size() + 1] = nullptr;
  183. rc = execv(argv[0], argv);
  184. perror("exec");
  185. ASSERT_NOT_REACHED();
  186. } else {
  187. // We are the parent.
  188. s_service_map.set(m_pid, this);
  189. }
  190. }
  191. void Service::did_exit(int exit_code)
  192. {
  193. ASSERT(m_pid > 0);
  194. (void)exit_code;
  195. dbg() << "Service " << name() << " has exited";
  196. s_service_map.remove(m_pid);
  197. m_pid = -1;
  198. if (m_keep_alive)
  199. activate();
  200. }
  201. Service::Service(const CConfigFile& config, const StringView& name)
  202. : CObject(nullptr)
  203. {
  204. ASSERT(config.has_group(name));
  205. set_name(name);
  206. m_executable_path = config.read_entry(name, "Executable", String::format("/bin/%s", this->name().characters()));
  207. m_extra_arguments = config.read_entry(name, "Arguments", "").split(' ');
  208. m_stdio_file_path = config.read_entry(name, "StdIO");
  209. String prio = config.read_entry(name, "Priority");
  210. if (prio == "low")
  211. m_priority = 10;
  212. else if (prio == "normal" || prio.is_null())
  213. m_priority = 30;
  214. else if (prio == "high")
  215. m_priority = 50;
  216. else
  217. ASSERT_NOT_REACHED();
  218. m_keep_alive = config.read_bool_entry(name, "KeepAlive");
  219. m_lazy = config.read_bool_entry(name, "Lazy");
  220. m_user = config.read_entry(name, "User");
  221. if (!m_user.is_null())
  222. resolve_user();
  223. m_socket_path = config.read_entry(name, "Socket");
  224. if (!m_socket_path.is_null()) {
  225. setup_socket();
  226. }
  227. }
  228. void Service::save_to(JsonObject& json)
  229. {
  230. CObject::save_to(json);
  231. json.set("executable_path", m_executable_path);
  232. // FIXME: This crashes Inspector.
  233. /*
  234. JsonArray extra_args;
  235. for (String& arg : m_extra_arguments)
  236. extra_args.append(arg);
  237. json.set("extra_arguments", move(extra_args));
  238. */
  239. json.set("stdio_file_path", m_stdio_file_path);
  240. json.set("priority", m_priority);
  241. json.set("keep_alive", m_keep_alive);
  242. json.set("socket_path", m_socket_path);
  243. json.set("lazy", m_lazy);
  244. json.set("user", m_user);
  245. json.set("uid", m_uid);
  246. json.set("gid", m_gid);
  247. if (m_pid > 0)
  248. json.set("pid", m_pid);
  249. else
  250. json.set("pid", nullptr);
  251. }