Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 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 <cutils/iosched_policy.h> |
| 23 | |
| 24 | #include <memory> |
| 25 | #include <string> |
| 26 | #include <vector> |
| 27 | |
James Hawkins | 7c92e48 | 2017-02-13 15:47:21 -0800 | [diff] [blame^] | 28 | #include <android-base/chrono_utils.h> |
| 29 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 30 | #include "action.h" |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 31 | #include "capabilities.h" |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 32 | #include "descriptors.h" |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 33 | #include "init_parser.h" |
| 34 | #include "keyword_map.h" |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 35 | #include "util.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 36 | |
| 37 | #define SVC_DISABLED 0x001 // do not autostart with class |
| 38 | #define SVC_ONESHOT 0x002 // do not restart on exit |
| 39 | #define SVC_RUNNING 0x004 // currently active |
| 40 | #define SVC_RESTARTING 0x008 // waiting to restart |
| 41 | #define SVC_CONSOLE 0x010 // requires console |
| 42 | #define SVC_CRITICAL 0x020 // will reboot into recovery if keeps crashing |
| 43 | #define SVC_RESET 0x040 // Use when stopping a process, |
| 44 | // but not disabling so it can be restarted with its class. |
| 45 | #define SVC_RC_DISABLED 0x080 // Remember if the disabled flag was set in the rc script. |
| 46 | #define SVC_RESTART 0x100 // Use to safely restart (stop, wait, start) a service. |
| 47 | #define SVC_DISABLED_START 0x200 // A start was requested but it was disabled at the time. |
| 48 | #define SVC_EXEC 0x400 // This synthetic service corresponds to an 'exec'. |
| 49 | |
| 50 | #define NR_SVC_SUPP_GIDS 12 // twelve supplementary groups |
| 51 | |
| 52 | class Action; |
| 53 | class ServiceManager; |
| 54 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 55 | struct ServiceEnvironmentInfo { |
| 56 | ServiceEnvironmentInfo(); |
| 57 | ServiceEnvironmentInfo(const std::string& name, const std::string& value); |
| 58 | std::string name; |
| 59 | std::string value; |
| 60 | }; |
| 61 | |
| 62 | class Service { |
| 63 | public: |
| 64 | Service(const std::string& name, const std::string& classname, |
| 65 | const std::vector<std::string>& args); |
| 66 | |
| 67 | Service(const std::string& name, const std::string& classname, |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 68 | unsigned flags, uid_t uid, gid_t gid, |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 69 | const std::vector<gid_t>& supp_gids, const CapSet& capabilities, |
| 70 | unsigned namespace_flags, const std::string& seclabel, |
| 71 | const std::vector<std::string>& args); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 72 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 73 | bool ParseLine(const std::vector<std::string>& args, std::string* err); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 74 | bool Start(); |
| 75 | bool StartIfNotDisabled(); |
| 76 | bool Enable(); |
| 77 | void Reset(); |
| 78 | void Stop(); |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 79 | void Terminate(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 80 | void Restart(); |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 81 | void RestartIfNeeded(time_t* process_needs_restart_at); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 82 | bool Reap(); |
| 83 | void DumpState() const; |
| 84 | |
| 85 | const std::string& name() const { return name_; } |
| 86 | const std::string& classname() const { return classname_; } |
| 87 | unsigned flags() const { return flags_; } |
| 88 | pid_t pid() const { return pid_; } |
| 89 | uid_t uid() const { return uid_; } |
| 90 | gid_t gid() const { return gid_; } |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 91 | int priority() const { return priority_; } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 92 | const std::vector<gid_t>& supp_gids() const { return supp_gids_; } |
| 93 | const std::string& seclabel() const { return seclabel_; } |
| 94 | const std::vector<int>& keycodes() const { return keycodes_; } |
| 95 | int keychord_id() const { return keychord_id_; } |
| 96 | void set_keychord_id(int keychord_id) { keychord_id_ = keychord_id; } |
| 97 | const std::vector<std::string>& args() const { return args_; } |
| 98 | |
| 99 | private: |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 100 | using OptionParser = bool (Service::*) (const std::vector<std::string>& args, |
| 101 | std::string* err); |
| 102 | class OptionParserMap; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 103 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 104 | void NotifyStateChange(const std::string& new_state) const; |
| 105 | void StopOrReset(int how); |
| 106 | void ZapStdio() const; |
| 107 | void OpenConsole() const; |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 108 | void KillProcessGroup(int signal); |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 109 | void SetProcessAttributes(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 110 | |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 111 | bool ParseCapabilities(const std::vector<std::string>& args, std::string *err); |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 112 | bool ParseClass(const std::vector<std::string>& args, std::string* err); |
| 113 | bool ParseConsole(const std::vector<std::string>& args, std::string* err); |
| 114 | bool ParseCritical(const std::vector<std::string>& args, std::string* err); |
| 115 | bool ParseDisabled(const std::vector<std::string>& args, std::string* err); |
| 116 | bool ParseGroup(const std::vector<std::string>& args, std::string* err); |
| 117 | bool ParsePriority(const std::vector<std::string>& args, std::string* err); |
| 118 | bool ParseIoprio(const std::vector<std::string>& args, std::string* err); |
| 119 | bool ParseKeycodes(const std::vector<std::string>& args, std::string* err); |
| 120 | bool ParseOneshot(const std::vector<std::string>& args, std::string* err); |
| 121 | bool ParseOnrestart(const std::vector<std::string>& args, std::string* err); |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 122 | bool ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err); |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 123 | bool ParseNamespace(const std::vector<std::string>& args, std::string* err); |
| 124 | bool ParseSeclabel(const std::vector<std::string>& args, std::string* err); |
| 125 | bool ParseSetenv(const std::vector<std::string>& args, std::string* err); |
| 126 | bool ParseSocket(const std::vector<std::string>& args, std::string* err); |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 127 | bool ParseFile(const std::vector<std::string>& args, std::string* err); |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 128 | bool ParseUser(const std::vector<std::string>& args, std::string* err); |
| 129 | bool ParseWritepid(const std::vector<std::string>& args, std::string* err); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 130 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 131 | template <typename T> |
| 132 | bool AddDescriptor(const std::vector<std::string>& args, std::string* err); |
| 133 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 134 | std::string name_; |
| 135 | std::string classname_; |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 136 | std::string console_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 137 | |
| 138 | unsigned flags_; |
| 139 | pid_t pid_; |
James Hawkins | 7c92e48 | 2017-02-13 15:47:21 -0800 | [diff] [blame^] | 140 | android::base::boot_clock::time_point time_started_; // time of last start |
| 141 | android::base::boot_clock::time_point time_crashed_; // first crash within inspection window |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 142 | int crash_count_; // number of times crashed within window |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 143 | |
| 144 | uid_t uid_; |
| 145 | gid_t gid_; |
| 146 | std::vector<gid_t> supp_gids_; |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 147 | CapSet capabilities_; |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 148 | unsigned namespace_flags_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 149 | |
| 150 | std::string seclabel_; |
| 151 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 152 | std::vector<std::unique_ptr<DescriptorInfo>> descriptors_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 153 | std::vector<ServiceEnvironmentInfo> envvars_; |
| 154 | |
| 155 | Action onrestart_; // Commands to execute on restart. |
| 156 | |
| 157 | std::vector<std::string> writepid_files_; |
| 158 | |
| 159 | // keycodes for triggering this service via /dev/keychord |
| 160 | std::vector<int> keycodes_; |
| 161 | int keychord_id_; |
| 162 | |
| 163 | IoSchedClass ioprio_class_; |
| 164 | int ioprio_pri_; |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 165 | int priority_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 166 | |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 167 | int oom_score_adjust_; |
| 168 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 169 | std::vector<std::string> args_; |
| 170 | }; |
| 171 | |
| 172 | class ServiceManager { |
| 173 | public: |
| 174 | static ServiceManager& GetInstance(); |
| 175 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 176 | void AddService(std::unique_ptr<Service> service); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 177 | Service* MakeExecOneshotService(const std::vector<std::string>& args); |
| 178 | Service* FindServiceByName(const std::string& name) const; |
| 179 | Service* FindServiceByPid(pid_t pid) const; |
| 180 | Service* FindServiceByKeychord(int keychord_id) const; |
Chih-Hung Hsieh | 8f7b9e3 | 2016-07-27 16:25:51 -0700 | [diff] [blame] | 181 | void ForEachService(const std::function<void(Service*)>& callback) const; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 182 | void ForEachServiceInClass(const std::string& classname, |
| 183 | void (*func)(Service* svc)) const; |
| 184 | void ForEachServiceWithFlags(unsigned matchflags, |
| 185 | void (*func)(Service* svc)) const; |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 186 | void ReapAnyOutstandingChildren(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 187 | void RemoveService(const Service& svc); |
| 188 | void DumpState() const; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 189 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 190 | private: |
| 191 | ServiceManager(); |
| 192 | |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 193 | // Cleans up a child process that exited. |
| 194 | // Returns true iff a children was cleaned up. |
| 195 | bool ReapOneProcess(); |
| 196 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 197 | static int exec_count_; // Every service needs a unique name. |
| 198 | std::vector<std::unique_ptr<Service>> services_; |
| 199 | }; |
| 200 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 201 | class ServiceParser : public SectionParser { |
| 202 | public: |
| 203 | ServiceParser() : service_(nullptr) { |
| 204 | } |
| 205 | bool ParseSection(const std::vector<std::string>& args, |
| 206 | std::string* err) override; |
| 207 | bool ParseLineSection(const std::vector<std::string>& args, |
| 208 | const std::string& filename, int line, |
| 209 | std::string* err) const override; |
| 210 | void EndSection() override; |
| 211 | void EndFile(const std::string&) override { |
| 212 | } |
| 213 | private: |
| 214 | bool IsValidName(const std::string& name) const; |
| 215 | |
| 216 | std::unique_ptr<Service> service_; |
| 217 | }; |
| 218 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 219 | #endif |