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 <sys/types.h> 21 22 #include <memory> 23 #include <set> 24 #include <string> 25 #include <vector> 26 27 #include <android-base/chrono_utils.h> 28 #include <cutils/iosched_policy.h> 29 30 #include "action.h" 31 #include "capabilities.h" 32 #include "descriptors.h" 33 #include "init_parser.h" 34 #include "keyword_map.h" 35 36 #define SVC_DISABLED 0x001 // do not autostart with class 37 #define SVC_ONESHOT 0x002 // do not restart on exit 38 #define SVC_RUNNING 0x004 // currently active 39 #define SVC_RESTARTING 0x008 // waiting to restart 40 #define SVC_CONSOLE 0x010 // requires console 41 #define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing 42 #define SVC_RESET 0x040 // Use when stopping a process, 43 // but not disabling so it can be restarted with its class. 44 #define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script. 45 #define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service. 46 #define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time. 47 #define SVC_EXEC 0x400 // This service was started by either 'exec' or 'exec_start' and stops 48 // init from processing more commands until it completes 49 50 #define SVC_SHUTDOWN_CRITICAL 0x800 // This service is critical for shutdown and 51 // should not be killed during shutdown 52 #define SVC_TEMPORARY 0x1000 // This service was started by 'exec' and should be removed from the 53 // service list once it is reaped. 54 55 #define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups 56 57 namespace android { 58 namespace init { 59 60 struct ServiceEnvironmentInfo { 61 ServiceEnvironmentInfo(); 62 ServiceEnvironmentInfo(const std::string& name, const std::string& value); 63 std::string name; 64 std::string value; 65 }; 66 67 class Service { 68 public: 69 Service(const std::string& name, const std::vector<std::string>& args); 70 71 Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid, 72 const std::vector<gid_t>& supp_gids, const CapSet& capabilities, 73 unsigned namespace_flags, const std::string& seclabel, 74 const std::vector<std::string>& args); 75 IsRunning()76 bool IsRunning() { return (flags_ & SVC_RUNNING) != 0; } 77 bool ParseLine(const std::vector<std::string>& args, std::string* err); 78 bool ExecStart(std::unique_ptr<android::base::Timer>* exec_waiter); 79 bool Start(); 80 bool StartIfNotDisabled(); 81 bool Enable(); 82 void Reset(); 83 void Stop(); 84 void Terminate(); 85 void Restart(); 86 void RestartIfNeeded(time_t* process_needs_restart_at); 87 void Reap(); 88 void DumpState() const; SetShutdownCritical()89 void SetShutdownCritical() { flags_ |= SVC_SHUTDOWN_CRITICAL; } IsShutdownCritical()90 bool IsShutdownCritical() const { return (flags_ & SVC_SHUTDOWN_CRITICAL) != 0; } UnSetExec()91 void UnSetExec() { flags_ &= ~SVC_EXEC; } 92 name()93 const std::string& name() const { return name_; } classnames()94 const std::set<std::string>& classnames() const { return classnames_; } flags()95 unsigned flags() const { return flags_; } pid()96 pid_t pid() const { return pid_; } crash_count()97 int crash_count() const { return crash_count_; } uid()98 uid_t uid() const { return uid_; } gid()99 gid_t gid() const { return gid_; } namespace_flags()100 unsigned namespace_flags() const { return namespace_flags_; } supp_gids()101 const std::vector<gid_t>& supp_gids() const { return supp_gids_; } seclabel()102 const std::string& seclabel() const { return seclabel_; } keycodes()103 const std::vector<int>& keycodes() const { return keycodes_; } keychord_id()104 int keychord_id() const { return keychord_id_; } set_keychord_id(int keychord_id)105 void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; } ioprio_class()106 IoSchedClass ioprio_class() const { return ioprio_class_; } ioprio_pri()107 int ioprio_pri() const { return ioprio_pri_; } priority()108 int priority() const { return priority_; } oom_score_adjust()109 int oom_score_adjust() const { return oom_score_adjust_; } process_cgroup_empty()110 bool process_cgroup_empty() const { return process_cgroup_empty_; } args()111 const std::vector<std::string>& args() const { return args_; } 112 113 private: 114 using OptionParser = bool (Service::*) (const std::vector<std::string>& args, 115 std::string* err); 116 class OptionParserMap; 117 118 void NotifyStateChange(const std::string& new_state) const; 119 void StopOrReset(int how); 120 void ZapStdio() const; 121 void OpenConsole() const; 122 void KillProcessGroup(int signal); 123 void SetProcessAttributes(); 124 125 bool ParseCapabilities(const std::vector<std::string>& args, std::string *err); 126 bool ParseClass(const std::vector<std::string>& args, std::string* err); 127 bool ParseConsole(const std::vector<std::string>& args, std::string* err); 128 bool ParseCritical(const std::vector<std::string>& args, std::string* err); 129 bool ParseDisabled(const std::vector<std::string>& args, std::string* err); 130 bool ParseGroup(const std::vector<std::string>& args, std::string* err); 131 bool ParsePriority(const std::vector<std::string>& args, std::string* err); 132 bool ParseIoprio(const std::vector<std::string>& args, std::string* err); 133 bool ParseKeycodes(const std::vector<std::string>& args, std::string* err); 134 bool ParseOneshot(const std::vector<std::string>& args, std::string* err); 135 bool ParseOnrestart(const std::vector<std::string>& args, std::string* err); 136 bool ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err); 137 bool ParseMemcgLimitInBytes(const std::vector<std::string>& args, std::string* err); 138 bool ParseMemcgSoftLimitInBytes(const std::vector<std::string>& args, std::string* err); 139 bool ParseMemcgSwappiness(const std::vector<std::string>& args, std::string* err); 140 bool ParseNamespace(const std::vector<std::string>& args, std::string* err); 141 bool ParseSeclabel(const std::vector<std::string>& args, std::string* err); 142 bool ParseSetenv(const std::vector<std::string>& args, std::string* err); 143 bool ParseShutdown(const std::vector<std::string>& args, std::string* err); 144 bool ParseSocket(const std::vector<std::string>& args, std::string* err); 145 bool ParseFile(const std::vector<std::string>& args, std::string* err); 146 bool ParseUser(const std::vector<std::string>& args, std::string* err); 147 bool ParseWritepid(const std::vector<std::string>& args, std::string* err); 148 149 template <typename T> 150 bool AddDescriptor(const std::vector<std::string>& args, std::string* err); 151 152 std::string name_; 153 std::set<std::string> classnames_; 154 std::string console_; 155 156 unsigned flags_; 157 pid_t pid_; 158 android::base::boot_clock::time_point time_started_; // time of last start 159 android::base::boot_clock::time_point time_crashed_; // first crash within inspection window 160 int crash_count_; // number of times crashed within window 161 162 uid_t uid_; 163 gid_t gid_; 164 std::vector<gid_t> supp_gids_; 165 CapSet capabilities_; 166 unsigned namespace_flags_; 167 168 std::string seclabel_; 169 170 std::vector<std::unique_ptr<DescriptorInfo>> descriptors_; 171 std::vector<ServiceEnvironmentInfo> envvars_; 172 173 Action onrestart_; // Commands to execute on restart. 174 175 std::vector<std::string> writepid_files_; 176 177 // keycodes for triggering this service via /dev/keychord 178 std::vector<int> keycodes_; 179 int keychord_id_; 180 181 IoSchedClass ioprio_class_; 182 int ioprio_pri_; 183 int priority_; 184 185 int oom_score_adjust_; 186 187 int swappiness_; 188 int soft_limit_in_bytes_; 189 int limit_in_bytes_; 190 191 bool process_cgroup_empty_ = false; 192 193 std::vector<std::string> args_; 194 }; 195 196 class ServiceManager { 197 public: 198 static ServiceManager& GetInstance(); 199 200 // Exposed for testing 201 ServiceManager(); 202 203 void AddService(std::unique_ptr<Service> service); 204 Service* MakeExecOneshotService(const std::vector<std::string>& args); 205 bool Exec(const std::vector<std::string>& args); 206 bool ExecStart(const std::string& name); 207 bool IsWaitingForExec() const; 208 Service* FindServiceByName(const std::string& name) const; 209 Service* FindServiceByPid(pid_t pid) const; 210 Service* FindServiceByKeychord(int keychord_id) const; 211 void ForEachService(const std::function<void(Service*)>& callback) const; 212 void ForEachServiceInClass(const std::string& classname, 213 void (*func)(Service* svc)) const; 214 void ForEachServiceWithFlags(unsigned matchflags, 215 void (*func)(Service* svc)) const; 216 void ReapAnyOutstandingChildren(); 217 void RemoveService(const Service& svc); 218 void DumpState() const; 219 void ClearExecWait(); 220 221 private: 222 // Cleans up a child process that exited. 223 // Returns true iff a children was cleaned up. 224 bool ReapOneProcess(); 225 226 static int exec_count_; // Every service needs a unique name. 227 std::unique_ptr<android::base::Timer> exec_waiter_; 228 229 std::vector<std::unique_ptr<Service>> services_; 230 }; 231 232 class ServiceParser : public SectionParser { 233 public: ServiceParser(ServiceManager * service_manager)234 ServiceParser(ServiceManager* service_manager) 235 : service_manager_(service_manager), service_(nullptr) {} 236 bool ParseSection(std::vector<std::string>&& args, const std::string& filename, int line, 237 std::string* err) override; 238 bool ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) override; 239 void EndSection() override; 240 241 private: 242 bool IsValidName(const std::string& name) const; 243 244 ServiceManager* service_manager_; 245 std::unique_ptr<Service> service_; 246 }; 247 248 } // namespace init 249 } // namespace android 250 251 #endif 252