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> StartIfPostData(); 84 Result<void> Enable(); 85 void Reset(); 86 void ResetIfPostData(); 87 void Stop(); 88 void Terminate(); 89 void Timeout(); 90 void Restart(); 91 void Reap(const siginfo_t& siginfo); 92 void DumpState() const; SetShutdownCritical()93 void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; } IsShutdownCritical()94 bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; } UnSetExec()95 void UnSetExec() { 96 is_exec_service_running_ = false; 97 flags_ &= ~SVC_EXEC; 98 } AddReapCallback(std::function<void (const siginfo_t & siginfo)> callback)99 void AddReapCallback(std::function<void(const siginfo_t& siginfo)> callback) { 100 reap_callbacks_.emplace_back(std::move(callback)); 101 } 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_; } 105 name()106 const std::string& name() const { return name_; } classnames()107 const std::set<std::string>& classnames() const { return classnames_; } flags()108 unsigned flags() const { return flags_; } pid()109 pid_t pid() const { return pid_; } time_started()110 android::base::boot_clock::time_point time_started() const { return time_started_; } crash_count()111 int crash_count() const { return crash_count_; } uid()112 uid_t uid() const { return proc_attr_.uid; } gid()113 gid_t gid() const { return proc_attr_.gid; } namespace_flags()114 int namespace_flags() const { return namespaces_.flags; } supp_gids()115 const std::vector<gid_t>& supp_gids() const { return proc_attr_.supp_gids; } seclabel()116 const std::string& seclabel() const { return seclabel_; } keycodes()117 const std::vector<int>& keycodes() const { return keycodes_; } ioprio_class()118 IoSchedClass ioprio_class() const { return proc_attr_.ioprio_class; } ioprio_pri()119 int ioprio_pri() const { return proc_attr_.ioprio_pri; } interfaces()120 const std::set<std::string>& interfaces() const { return interfaces_; } priority()121 int priority() const { return proc_attr_.priority; } oom_score_adjust()122 int oom_score_adjust() const { return oom_score_adjust_; } is_override()123 bool is_override() const { return override_; } process_cgroup_empty()124 bool process_cgroup_empty() const { return process_cgroup_empty_; } start_order()125 unsigned long start_order() const { return start_order_; } set_sigstop(bool value)126 void set_sigstop(bool value) { sigstop_ = value; } restart_period()127 std::chrono::seconds restart_period() const { return restart_period_; } timeout_period()128 std::optional<std::chrono::seconds> timeout_period() const { return timeout_period_; } args()129 const std::vector<std::string>& args() const { return args_; } is_updatable()130 bool is_updatable() const { return updatable_; } is_post_data()131 bool is_post_data() const { return post_data_; } is_from_apex()132 bool is_from_apex() const { return from_apex_; } set_oneshot(bool value)133 void set_oneshot(bool value) { 134 if (value) { 135 flags_ |= SVC_ONESHOT; 136 } else { 137 flags_ &= ~SVC_ONESHOT; 138 } 139 } subcontext()140 Subcontext* subcontext() const { return subcontext_; } 141 142 private: 143 void NotifyStateChange(const std::string& new_state) const; 144 void StopOrReset(int how); 145 void KillProcessGroup(int signal, bool report_oneshot = false); 146 void SetProcessAttributesAndCaps(); 147 148 static unsigned long next_start_order_; 149 static bool is_exec_service_running_; 150 151 std::string name_; 152 std::set<std::string> classnames_; 153 154 unsigned flags_; 155 pid_t pid_; 156 android::base::boot_clock::time_point time_started_; // time of last start 157 android::base::boot_clock::time_point time_crashed_; // first crash within inspection window 158 int crash_count_; // number of times crashed within window 159 std::chrono::minutes fatal_crash_window_ = 4min; // fatal() when more than 4 crashes in it 160 std::optional<std::string> fatal_reboot_target_; // reboot target of fatal handler 161 162 std::optional<CapSet> capabilities_; 163 ProcessAttributes proc_attr_; 164 NamespaceInfo namespaces_; 165 166 std::string seclabel_; 167 168 std::vector<SocketDescriptor> sockets_; 169 std::vector<FileDescriptor> files_; 170 std::vector<std::pair<std::string, std::string>> environment_vars_; 171 172 Subcontext* subcontext_; 173 Action onrestart_; // Commands to execute on restart. 174 175 std::vector<std::string> writepid_files_; 176 177 std::vector<std::string> task_profiles_; 178 179 std::set<std::string> interfaces_; // e.g. some.package.foo@1.0::IBaz/instance-name 180 181 // keycodes for triggering this service via /dev/input/input* 182 std::vector<int> keycodes_; 183 184 int oom_score_adjust_; 185 186 int swappiness_ = -1; 187 int soft_limit_in_bytes_ = -1; 188 189 int limit_in_bytes_ = -1; 190 int limit_percent_ = -1; 191 std::string limit_property_; 192 193 bool process_cgroup_empty_ = false; 194 195 bool override_ = false; 196 197 unsigned long start_order_; 198 199 bool sigstop_ = false; 200 201 std::chrono::seconds restart_period_ = 5s; 202 std::optional<std::chrono::seconds> timeout_period_; 203 204 bool updatable_ = false; 205 206 std::vector<std::string> args_; 207 208 std::vector<std::function<void(const siginfo_t& siginfo)>> reap_callbacks_; 209 210 bool use_bootstrap_ns_ = false; 211 212 bool post_data_ = false; 213 214 bool running_at_post_data_reset_ = false; 215 216 std::optional<std::string> on_failure_reboot_target_; 217 218 bool from_apex_ = false; 219 }; 220 221 } // namespace init 222 } // namespace android 223