Serenity Operating System
at portability 139 lines 4.6 kB view raw
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 check_for_test_mode() 66{ 67 auto f = Core::File::construct("/proc/cmdline"); 68 if (!f->open(Core::IODevice::ReadOnly)) { 69 dbg() << "Failed to read command line: " << f->error_string(); 70 ASSERT(false); 71 } 72 const String cmd = String::copy(f->read_all()); 73 dbg() << "Read command line: " << cmd; 74 if (cmd.matches("*testmode=1*")) { 75 // Eventually, we should run a test binary and wait for it to finish 76 // before shutting down. But this is good enough for now. 77 dbg() << "Waiting for testmode shutdown..."; 78 sleep(5); 79 dbg() << "Shutting down due to testmode..."; 80 if (fork() == 0) { 81 execl("/bin/shutdown", "/bin/shutdown", "-n", nullptr); 82 ASSERT_NOT_REACHED(); 83 } 84 } else { 85 dbg() << "Continuing normally"; 86 } 87} 88 89static void mount_all_filesystems() 90{ 91 dbg() << "Spawning mount -a to mount all filesystems."; 92 pid_t pid = fork(); 93 94 if (pid < 0) { 95 perror("fork"); 96 ASSERT_NOT_REACHED(); 97 } else if (pid == 0) { 98 execl("/bin/mount", "mount", "-a", nullptr); 99 perror("exec"); 100 ASSERT_NOT_REACHED(); 101 } else { 102 wait(nullptr); 103 } 104} 105 106int main(int, char**) 107{ 108 if (pledge("stdio proc exec tty accept unix rpath wpath cpath chown fattr id", nullptr) < 0) { 109 perror("pledge"); 110 return 1; 111 } 112 113 mount_all_filesystems(); 114 115 struct sigaction sa = { 116 .sa_handler = sigchld_handler, 117 .sa_mask = 0, 118 .sa_flags = SA_RESTART 119 }; 120 sigaction(SIGCHLD, &sa, nullptr); 121 122 Core::EventLoop event_loop; 123 124 // Read our config and instantiate services. 125 // This takes care of setting up sockets. 126 Vector<RefPtr<Service>> services; 127 auto config = Core::ConfigFile::get_for_system("SystemServer"); 128 for (auto name : config->groups()) 129 services.append(Service::construct(*config, name)); 130 131 // After we've set them all up, activate them! 132 for (auto& service : services) 133 service->activate(); 134 135 // This won't return if we're in test mode. 136 check_for_test_mode(); 137 138 return event_loop.exec(); 139}