Serenity Operating System
at hosted 94 lines 3.5 kB view raw
1/* 2 * Copyright (c) 2019-2020, Sergey Bugaev <bugaevc@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#pragma once 28 29#include <AK/RefPtr.h> 30#include <AK/String.h> 31#include <LibCore/ElapsedTimer.h> 32#include <LibCore/Notifier.h> 33#include <LibCore/Object.h> 34 35class Service final : public Core::Object { 36 C_OBJECT(Service) 37 38public: 39 void activate(); 40 void did_exit(int exit_code); 41 void kill(int signal); 42 43 static Service* find_by_pid(pid_t); 44 static void for_each(Function<void(Service&)>); 45 46 void save_to(AK::JsonObject&) override; 47 48private: 49 Service(const Core::ConfigFile&, const StringView& name); 50 51 void spawn(); 52 53 // Path to the executable. By default this is /bin/{m_name}. 54 String m_executable_path; 55 // Extra arguments, starting from argv[1], to pass when exec'ing. 56 Vector<String> m_extra_arguments; 57 // File path to open as stdio fds. 58 String m_stdio_file_path; 59 int m_priority { 1 }; 60 // Whether we should re-launch it if it exits. 61 bool m_keep_alive { false }; 62 // Whether we're in the shutdown path. 63 bool m_dying { false }; 64 // Path to the socket to create and listen on on behalf of this service. 65 String m_socket_path; 66 // File system permissions for the socket. 67 mode_t m_socket_permissions { 0 }; 68 // Whether we should only spawn this service once somebody connects to the socket. 69 bool m_lazy; 70 // The name of the user we should run this service as. 71 String m_user; 72 uid_t m_uid { 0 }; 73 gid_t m_gid { 0 }; 74 Vector<gid_t> m_extra_gids; 75 76 // PID of the running instance of this service. 77 pid_t m_pid { -1 }; 78 // An open fd to the socket. 79 int m_socket_fd { -1 }; 80 RefPtr<Core::Notifier> m_socket_notifier; 81 82 // Timer since we last spawned the service. 83 Core::ElapsedTimer m_run_timer; 84 // How many times we have tried to restart this service, only counting those 85 // times where it has exited unsuccessfully and too quickly. 86 int m_restart_attempts { 0 }; 87 88 // The working directory in which to spawn the service 89 String m_working_directory; 90 91 void resolve_user(); 92 void setup_socket(); 93 void setup_notifier(); 94};