1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <signal.h> 20 #include <sys/types.h> 21 22 #include <chrono> 23 #include <memory> 24 #include <optional> 25 #include <set> 26 #include <string> 27 #include <vector> 28 29 #include <android-base/chrono_utils.h> 30 #include <cutils/iosched_policy.h> 31 32 #include "action.h" 33 #include "capabilities.h" 34 #include "keyword_map.h" 35 #include "parser.h" 36 #include "service_utils.h" 37 #include "subcontext.h" 38 39 #define SVC_DISABLED 0x001 // do not autostart with class 40 #define SVC_ONESHOT 0x002 // do not restart on exit 41 #define SVC_RUNNING 0x004 // currently active 42 #define SVC_RESTARTING 0x008 // waiting to restart 43 #define SVC_CONSOLE 0x010 // requires console 44 #define SVC_CRITICAL 0x020 // will reboot into bootloader if keeps crashing 45 #define SVC_RESET 0x040 // Use when stopping a process, 46 // but not disabling so it can be restarted with its class. 47 #define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script. 48 #define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service. 49 #define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time. 50 #define SVC_EXEC 0x400 // This service was started by either 'exec' or 'exec_start' and stops 51 // init from processing more commands until it completes 52 53 #define SVC_SHUTDOWN_CRITICAL 0x800 // This service is critical for shutdown and 54 // should not be killed during shutdown 55 #define SVC_TEMPORARY 0x1000 // This service was started by 'exec' and should be removed from the 56 // service list once it is reaped. 57 58 #define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups 59 60 namespace android { 61 namespace init { 62 63 class Service { 64 friend class ServiceParser; 65 66 public: 67 Service(const std::string& name, Subcontext* subcontext_for_restart_commands, 68 const std::vector<std::string>& args, bool from_apex = false); 69 70 Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid, 71 const std::vector<gid_t>& supp_gids, int namespace_flags, const std::string& seclabel, 72 Subcontext* subcontext_for_restart_commands, const std::vector<std::string>& args, 73 bool from_apex = false); 74 75 static Result<std::unique_ptr<Service>> MakeTemporaryOneshotService( 76 const std::vector<std::string>& args); 77 IsRunning()78 bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; } IsEnabled()79 bool IsEnabled() { return (flags_ & SVC_DISABLED) == 0; } 80 Result<void> ExecStart(); 81 Result<void> Start(); 82 Result<void> StartIfNotDisabled(); 83 Result<void> Enable(); 84 void Reset(); 85 void Stop(); 86 void Terminate(); 87 void Timeout(); 88 void Restart(); 89 void Reap(const siginfo_t& siginfo); 90 void DumpState() const; SetShutdownCritical()91 void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; } IsShutdownCritical()92 bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; } UnSetExec()93 void UnSetExec() { 94 is_exec_service_running_ = false; 95 flags_ &= ~SVC_EXEC; 96 } AddReapCallback(std::function<void (const siginfo_t & siginfo)> callback)97 void AddReapCallback(std::function<void(const siginfo_t& siginfo)> callback) { 98 reap_callbacks_.emplace_back(std::move(callback)); 99 } 100 void SetStartedInFirstStage(pid_t pid); 101 bool MarkSocketPersistent(const std::string& socket_name); CheckAllCommands()102 size_t CheckAllCommands() const { return onrestart_.CheckAllCommands(); } 103 is_exec_service_running()104 static bool is_exec_service_running() { return is_exec_service_running_; } exec_service_pid()105 static pid_t exec_service_pid() { return exec_service_pid_; } exec_service_started()106 static std::chrono::time_point<std::chrono::steady_clock> exec_service_started() { 107 return exec_service_started_; 108 } 109 name()110 const std::string& name() const { return name_; } classnames()111 const std::set<std::string>& classnames() const { return classnames_; } flags()112 unsigned flags() const { return flags_; } pid()113 pid_t pid() const { return pid_; } time_started()114 android::base::boot_clock::time_point time_started() const { return time_started_; } crash_count()115 int crash_count() const { return crash_count_; } uid()116 uid_t uid() const { return proc_attr_.uid; } gid()117 gid_t gid() const { return proc_attr_.gid; } namespace_flags()118 int namespace_flags() const { return namespaces_.flags; } supp_gids()119 const std::vector<gid_t>& supp_gids() const { return proc_attr_.supp_gids; } seclabel()120 const std::string& seclabel() const { return seclabel_; } keycodes()121 const std::vector<int>& keycodes() const { return keycodes_; } ioprio_class()122 IoSchedClass ioprio_class() const { return proc_attr_.ioprio_class; } ioprio_pri()123 int ioprio_pri() const { return proc_attr_.ioprio_pri; } interfaces()124 const std::set<std::string>& interfaces() const { return interfaces_; } priority()125 int priority() const { return proc_attr_.priority; } oom_score_adjust()126 int oom_score_adjust() const { return oom_score_adjust_; } is_override()127 bool is_override() const { return override_; } process_cgroup_empty()128 bool process_cgroup_empty() const { return process_cgroup_empty_; } start_order()129 unsigned long start_order() const { return start_order_; } set_sigstop(bool value)130 void set_sigstop(bool value) { sigstop_ = value; } restart_period()131 std::chrono::seconds restart_period() const { return restart_period_; } timeout_period()132 std::optional<std::chrono::seconds> timeout_period() const { return timeout_period_; } args()133 const std::vector<std::string>& args() const { return args_; } is_updatable()134 bool is_updatable() const { return updatable_; } is_post_data()135 bool is_post_data() const { return post_data_; } is_from_apex()136 bool is_from_apex() const { return from_apex_; } set_oneshot(bool value)137 void set_oneshot(bool value) { 138 if (value) { 139 flags_ |= SVC_ONESHOT; 140 } else { 141 flags_ &= ~SVC_ONESHOT; 142 } 143 } subcontext()144 Subcontext* subcontext() const { return subcontext_; } 145 146 private: 147 void NotifyStateChange(const std::string& new_state) const; 148 void StopOrReset(int how); 149 void KillProcessGroup(int signal, bool report_oneshot = false); 150 void SetProcessAttributesAndCaps(); 151 void ResetFlagsForStart(); 152 Result<void> CheckConsole(); 153 void ConfigureMemcg(); 154 void RunService( 155 const std::optional<MountNamespace>& override_mount_namespace, 156 const std::vector<Descriptor>& descriptors, 157 std::unique_ptr<std::array<int, 2>, void (*)(const std::array<int, 2>* pipe)> pipefd); 158 159 static unsigned long next_start_order_; 160 static bool is_exec_service_running_; 161 static std::chrono::time_point<std::chrono::steady_clock> exec_service_started_; 162 static pid_t exec_service_pid_; 163 164 std::string name_; 165 std::set<std::string> classnames_; 166 167 unsigned flags_; 168 pid_t pid_; 169 android::base::boot_clock::time_point time_started_; // time of last start 170 android::base::boot_clock::time_point time_crashed_; // first crash within inspection window 171 int crash_count_; // number of times crashed within window 172 std::chrono::minutes fatal_crash_window_ = 4min; // fatal() when more than 4 crashes in it 173 std::optional<std::string> fatal_reboot_target_; // reboot target of fatal handler 174 175 std::optional<CapSet> capabilities_; 176 ProcessAttributes proc_attr_; 177 NamespaceInfo namespaces_; 178 179 std::string seclabel_; 180 181 std::vector<SocketDescriptor> sockets_; 182 std::vector<FileDescriptor> files_; 183 std::vector<std::pair<std::string, std::string>> environment_vars_; 184 185 Subcontext* subcontext_; 186 Action onrestart_; // Commands to execute on restart. 187 188 std::vector<std::string> writepid_files_; 189 190 std::vector<std::string> task_profiles_; 191 192 std::set<std::string> interfaces_; // e.g. some.package.foo@1.0::IBaz/instance-name 193 194 // keycodes for triggering this service via /dev/input/input* 195 std::vector<int> keycodes_; 196 197 int oom_score_adjust_; 198 199 int swappiness_ = -1; 200 int soft_limit_in_bytes_ = -1; 201 202 int limit_in_bytes_ = -1; 203 int limit_percent_ = -1; 204 std::string limit_property_; 205 206 bool process_cgroup_empty_ = false; 207 208 bool override_ = false; 209 210 unsigned long start_order_; 211 212 bool sigstop_ = false; 213 214 std::chrono::seconds restart_period_ = 5s; 215 std::optional<std::chrono::seconds> timeout_period_; 216 217 bool updatable_ = false; 218 219 std::vector<std::string> args_; 220 221 std::vector<std::function<void(const siginfo_t& siginfo)>> reap_callbacks_; 222 223 bool use_bootstrap_ns_ = false; 224 225 bool post_data_ = false; 226 227 std::optional<std::string> on_failure_reboot_target_; 228 229 bool from_apex_ = false; 230 }; 231 232 } // namespace init 233 } // namespace android 234