Serenity Operating System
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
42 static Service* find_by_pid(pid_t);
43
44 void save_to(AK::JsonObject&) override;
45
46private:
47 Service(const Core::ConfigFile&, const StringView& name);
48
49 void spawn();
50
51 // Path to the executable. By default this is /bin/{m_name}.
52 String m_executable_path;
53 // Extra arguments, starting from argv[1], to pass when exec'ing.
54 Vector<String> m_extra_arguments;
55 // File path to open as stdio fds.
56 String m_stdio_file_path;
57 int m_priority { 1 };
58 // Whether we should re-launch it if it exits.
59 bool m_keep_alive { false };
60 // Path to the socket to create and listen on on behalf of this service.
61 String m_socket_path;
62 // File system permissions for the socket.
63 mode_t m_socket_permissions { 0 };
64 // Whether we should only spawn this service once somebody connects to the socket.
65 bool m_lazy;
66 // The name of the user we should run this service as.
67 String m_user;
68 uid_t m_uid { 0 };
69 gid_t m_gid { 0 };
70 Vector<gid_t> m_extra_gids;
71
72 // PID of the running instance of this service.
73 pid_t m_pid { -1 };
74 // An open fd to the socket.
75 int m_socket_fd { -1 };
76 RefPtr<Core::Notifier> m_socket_notifier;
77
78 // Timer since we last spawned the service.
79 Core::ElapsedTimer m_run_timer;
80 // How many times we have tried to restart this service, only counting those
81 // times where it has exited unsuccessfully and too quickly.
82 int m_restart_attempts { 0 };
83
84 void resolve_user();
85 void setup_socket();
86 void setup_notifier();
87};