Serenity Operating System
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
27#include "Service.h"
28#include <AK/Assertions.h>
29#include <AK/ByteBuffer.h>
30#include <LibCore/ConfigFile.h>
31#include <LibCore/Event.h>
32#include <LibCore/EventLoop.h>
33#include <LibCore/File.h>
34#include <errno.h>
35#include <signal.h>
36#include <stdio.h>
37#include <sys/types.h>
38#include <sys/wait.h>
39#include <unistd.h>
40
41static void sigchld_handler(int)
42{
43 int status = 0;
44 pid_t pid = waitpid(-1, &status, WNOHANG);
45 if (!pid)
46 return;
47
48 dbg() << "Reaped child with pid " << pid;
49 Service* service = Service::find_by_pid(pid);
50 if (service == nullptr) {
51 dbg() << "There was no service with this pid, what is going on?";
52 return;
53 }
54
55 // Call service->did_exit(status) some time soon.
56 // We wouldn't want to run the complex logic, such
57 // as possibly spawning the service again, from the
58 // signal handler, so defer it.
59 Core::EventLoop::main().post_event(*service, make<Core::DeferredInvocationEvent>([=](auto&) {
60 service->did_exit(status);
61 }));
62 Core::EventLoop::wake();
63}
64
65static void shutdown(int signal)
66{
67 dbg() << "Got signal " << signal << ", shutting down";
68
69 Service::for_each([signal](Service& service) {
70 service.kill(signal);
71 });
72
73 Core::EventLoop::main().quit(1);
74}
75
76static void check_for_test_mode()
77{
78#ifndef __serenity__
79 return;
80#endif
81
82 auto f = Core::File::construct("/proc/cmdline");
83 if (!f->open(Core::IODevice::ReadOnly)) {
84 dbg() << "Failed to read command line: " << f->error_string();
85 ASSERT(false);
86 }
87 const String cmd = String::copy(f->read_all());
88 dbg() << "Read command line: " << cmd;
89 if (cmd.matches("*testmode=1*")) {
90 // Eventually, we should run a test binary and wait for it to finish
91 // before shutting down. But this is good enough for now.
92 dbg() << "Waiting for testmode shutdown...";
93 sleep(5);
94 dbg() << "Shutting down due to testmode...";
95 if (fork() == 0) {
96 execl("/bin/shutdown", "/bin/shutdown", "-n", nullptr);
97 ASSERT_NOT_REACHED();
98 }
99 } else {
100 dbg() << "Continuing normally";
101 }
102}
103
104static void mount_all_filesystems()
105{
106#ifndef __serenity__
107 return;
108#endif
109
110 dbg() << "Spawning mount -a to mount all filesystems.";
111 pid_t pid = fork();
112
113 if (pid < 0) {
114 perror("fork");
115 ASSERT_NOT_REACHED();
116 } else if (pid == 0) {
117 execl("/bin/mount", "mount", "-a", nullptr);
118 perror("exec");
119 ASSERT_NOT_REACHED();
120 } else {
121 wait(nullptr);
122 }
123}
124
125int main(int, char**)
126{
127#ifdef __serenity__
128 if (pledge("stdio proc exec tty accept unix rpath wpath cpath chown fattr id", nullptr) < 0) {
129 perror("pledge");
130 return 1;
131 }
132#endif
133
134 if (getuid() != 0) {
135 printf("not root\n");
136 return 1;
137 }
138
139 if (setsid() == -1) {
140 perror("setsid");
141 return 1;
142 }
143
144 if (setlogin("root") == -1) {
145 perror("setlogin");
146 return 1;
147 }
148
149 mount_all_filesystems();
150
151 struct sigaction sa = {
152 .sa_handler = sigchld_handler,
153 .sa_mask = 0,
154 .sa_flags = SA_RESTART
155 };
156 sigaction(SIGCHLD, &sa, nullptr);
157
158 struct sigaction sdkill = {
159 .sa_handler = shutdown,
160 .sa_mask = 0,
161 .sa_flags = SA_RESTART
162 };
163 sigaction(SIGTERM, &sdkill, nullptr);
164
165 Core::EventLoop event_loop;
166
167 // Read our config and instantiate services.
168 // This takes care of setting up sockets.
169 Vector<RefPtr<Service>> services;
170 auto config = Core::ConfigFile::get_for_system("SystemServer");
171 for (auto name : config->groups())
172 services.append(Service::construct(*config, name));
173
174 // After we've set them all up, activate them!
175 for (auto& service : services)
176 service->activate();
177
178 // This won't return if we're in test mode.
179 check_for_test_mode();
180
181 return event_loop.exec();
182}