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 #ifndef _INIT_SERVICE_H 18 #define _INIT_SERVICE_H 19 20 #include <signal.h> 21 #include <sys/resource.h> 22 #include <sys/types.h> 23 24 #include <memory> 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 "descriptors.h" 35 #include "keyword_map.h" 36 #include "parser.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 recovery 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 public: 65 Service(const std::string& name, Subcontext* subcontext_for_restart_commands, 66 const std::vector<std::string>& args); 67 68 Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid, 69 const std::vector<gid_t>& supp_gids, const CapSet& capabilities, 70 unsigned namespace_flags, const std::string& seclabel, 71 Subcontext* subcontext_for_restart_commands, const std::vector<std::string>& args); 72 73 static std::unique_ptr<Service> MakeTemporaryOneshotService(const std::vector<std::string>& args); 74 75 bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; } 76 Result<Success> ParseLine(const std::vector<std::string>& args); 77 Result<Success> ExecStart(); 78 Result<Success> Start(); 79 Result<Success> StartIfNotDisabled(); 80 Result<Success> Enable(); 81 void Reset(); 82 void Stop(); 83 void Terminate(); 84 void Restart(); 85 void Reap(const siginfo_t& siginfo); 86 void DumpState() const; 87 void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; } 88 bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; } 89 void UnSetExec() { 90 is_exec_service_running_ = false; 91 flags_ &= ~SVC_EXEC; 92 } 93 void AddReapCallback(std::function<void(const siginfo_t& siginfo)> callback) { 94 reap_callbacks_.emplace_back(std::move(callback)); 95 } 96 97 static bool is_exec_service_running() { return is_exec_service_running_; } 98 99 const std::string& name() const { return name_; } 100 const std::set<std::string>& classnames() const { return classnames_; } 101 unsigned flags() const { return flags_; } 102 pid_t pid() const { return pid_; } 103 android::base::boot_clock::time_point time_started() const { return time_started_; } 104 int crash_count() const { return crash_count_; } 105 uid_t uid() const { return uid_; } 106 gid_t gid() const { return gid_; } 107 unsigned namespace_flags() const { return namespace_flags_; } 108 const std::vector<gid_t>& supp_gids() const { return supp_gids_; } 109 const std::string& seclabel() const { return seclabel_; } 110 const std::vector<int>& keycodes() const { return keycodes_; } 111 int keychord_id() const { return keychord_id_; } 112 void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; } 113 IoSchedClass ioprio_class() const { return ioprio_class_; } 114 int ioprio_pri() const { return ioprio_pri_; } 115 const std::set<std::string>& interfaces() const { return interfaces_; } 116 int priority() const { return priority_; } 117 int oom_score_adjust() const { return oom_score_adjust_; } 118 bool is_override() const { return override_; } 119 bool process_cgroup_empty() const { return process_cgroup_empty_; } 120 unsigned long start_order() const { return start_order_; } 121 const std::vector<std::string>& args() const { return args_; } 122 123 private: 124 using OptionParser = Result<Success> (Service::*)(const std::vector<std::string>& args); 125 class OptionParserMap; 126 127 Result<Success> SetUpMountNamespace() const; 128 Result<Success> SetUpPidNamespace() const; 129 Result<Success> EnterNamespaces() const; 130 void NotifyStateChange(const std::string& new_state) const; 131 void StopOrReset(int how); 132 void ZapStdio() const; 133 void OpenConsole() const; 134 void KillProcessGroup(int signal); 135 void SetProcessAttributes(); 136 137 Result<Success> ParseCapabilities(const std::vector<std::string>& args); 138 Result<Success> ParseClass(const std::vector<std::string>& args); 139 Result<Success> ParseConsole(const std::vector<std::string>& args); 140 Result<Success> ParseCritical(const std::vector<std::string>& args); 141 Result<Success> ParseDisabled(const std::vector<std::string>& args); 142 Result<Success> ParseEnterNamespace(const std::vector<std::string>& args); 143 Result<Success> ParseGroup(const std::vector<std::string>& args); 144 Result<Success> ParsePriority(const std::vector<std::string>& args); 145 Result<Success> ParseInterface(const std::vector<std::string>& args); 146 Result<Success> ParseIoprio(const std::vector<std::string>& args); 147 Result<Success> ParseKeycodes(const std::vector<std::string>& args); 148 Result<Success> ParseOneshot(const std::vector<std::string>& args); 149 Result<Success> ParseOnrestart(const std::vector<std::string>& args); 150 Result<Success> ParseOomScoreAdjust(const std::vector<std::string>& args); 151 Result<Success> ParseOverride(const std::vector<std::string>& args); 152 Result<Success> ParseMemcgLimitInBytes(const std::vector<std::string>& args); 153 Result<Success> ParseMemcgSoftLimitInBytes(const std::vector<std::string>& args); 154 Result<Success> ParseMemcgSwappiness(const std::vector<std::string>& args); 155 Result<Success> ParseNamespace(const std::vector<std::string>& args); 156 Result<Success> ParseProcessRlimit(const std::vector<std::string>& args); 157 Result<Success> ParseSeclabel(const std::vector<std::string>& args); 158 Result<Success> ParseSetenv(const std::vector<std::string>& args); 159 Result<Success> ParseShutdown(const std::vector<std::string>& args); 160 Result<Success> ParseSocket(const std::vector<std::string>& args); 161 Result<Success> ParseFile(const std::vector<std::string>& args); 162 Result<Success> ParseUser(const std::vector<std::string>& args); 163 Result<Success> ParseWritepid(const std::vector<std::string>& args); 164 165 template <typename T> 166 Result<Success> AddDescriptor(const std::vector<std::string>& args); 167 168 static unsigned long next_start_order_; 169 static bool is_exec_service_running_; 170 171 std::string name_; 172 std::set<std::string> classnames_; 173 std::string console_; 174 175 unsigned flags_; 176 pid_t pid_; 177 android::base::boot_clock::time_point time_started_; // time of last start 178 android::base::boot_clock::time_point time_crashed_; // first crash within inspection window 179 int crash_count_; // number of times crashed within window 180 181 uid_t uid_; 182 gid_t gid_; 183 std::vector<gid_t> supp_gids_; 184 CapSet capabilities_; 185 unsigned namespace_flags_; 186 // Pair of namespace type, path to namespace. 187 std::vector<std::pair<int, std::string>> namespaces_to_enter_; 188 189 std::string seclabel_; 190 191 std::vector<std::unique_ptr<DescriptorInfo>> descriptors_; 192 std::vector<std::pair<std::string, std::string>> environment_vars_; 193 194 Action onrestart_; // Commands to execute on restart. 195 196 std::vector<std::string> writepid_files_; 197 198 std::set<std::string> interfaces_; // e.g. some.package.foo (at) 1.0::IBaz/instance-name 199 200 // keycodes for triggering this service via /dev/keychord 201 std::vector<int> keycodes_; 202 int keychord_id_; 203 204 IoSchedClass ioprio_class_; 205 int ioprio_pri_; 206 int priority_; 207 208 int oom_score_adjust_; 209 210 int swappiness_; 211 int soft_limit_in_bytes_; 212 int limit_in_bytes_; 213 214 bool process_cgroup_empty_ = false; 215 216 bool override_ = false; 217 218 unsigned long start_order_; 219 220 std::vector<std::pair<int, rlimit>> rlimits_; 221 222 std::vector<std::string> args_; 223 224 std::vector<std::function<void(const siginfo_t& siginfo)>> reap_callbacks_; 225 }; 226 227 class ServiceList { 228 public: 229 static ServiceList& GetInstance(); 230 231 // Exposed for testing 232 ServiceList(); 233 234 void AddService(std::unique_ptr<Service> service); 235 void RemoveService(const Service& svc); 236 237 template <typename T, typename F = decltype(&Service::name)> 238 Service* FindService(T value, F function = &Service::name) const { 239 auto svc = std::find_if(services_.begin(), services_.end(), 240 [&function, &value](const std::unique_ptr<Service>& s) { 241 return std::invoke(function, s) == value; 242 }); 243 if (svc != services_.end()) { 244 return svc->get(); 245 } 246 return nullptr; 247 } 248 249 void DumpState() const; 250 251 auto begin() const { return services_.begin(); } 252 auto end() const { return services_.end(); } 253 const std::vector<std::unique_ptr<Service>>& services() const { return services_; } 254 const std::vector<Service*> services_in_shutdown_order() const; 255 256 private: 257 std::vector<std::unique_ptr<Service>> services_; 258 }; 259 260 class ServiceParser : public SectionParser { 261 public: 262 ServiceParser(ServiceList* service_list, std::vector<Subcontext>* subcontexts) 263 : service_list_(service_list), subcontexts_(subcontexts), service_(nullptr) {} 264 Result<Success> ParseSection(std::vector<std::string>&& args, const std::string& filename, 265 int line) override; 266 Result<Success> ParseLineSection(std::vector<std::string>&& args, int line) override; 267 Result<Success> EndSection() override; 268 269 private: 270 bool IsValidName(const std::string& name) const; 271 272 ServiceList* service_list_; 273 std::vector<Subcontext>* subcontexts_; 274 std::unique_ptr<Service> service_; 275 }; 276 277 } // namespace init 278 } // namespace android 279 280 #endif 281