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 | #include "service.h" |
| 18 | |
| 19 | #include <fcntl.h> |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 20 | #include <inttypes.h> |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 21 | #include <linux/securebits.h> |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 22 | #include <sched.h> |
| 23 | #include <sys/mount.h> |
| 24 | #include <sys/prctl.h> |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 25 | #include <sys/resource.h> |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 26 | #include <sys/stat.h> |
Tom Cherry | f57c0bf | 2017-04-07 13:46:21 -0700 | [diff] [blame] | 27 | #include <sys/system_properties.h> |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 28 | #include <sys/time.h> |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 29 | #include <sys/wait.h> |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 30 | #include <termios.h> |
Dan Albert | af9ba4d | 2015-08-11 16:37:04 -0700 | [diff] [blame] | 31 | #include <unistd.h> |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 32 | |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 33 | #include <android-base/file.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 34 | #include <android-base/logging.h> |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 35 | #include <android-base/parseint.h> |
Tom Cherry | ccf2353 | 2017-03-28 16:40:41 -0700 | [diff] [blame] | 36 | #include <android-base/properties.h> |
Tom Cherry | 8d13d80 | 2017-06-29 16:18:08 -0700 | [diff] [blame] | 37 | #include <android-base/scopeguard.h> |
Elliott Hughes | 4f71319 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 38 | #include <android-base/stringprintf.h> |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 39 | #include <android-base/strings.h> |
Tom Cherry | 3f5eaae5 | 2017-04-06 16:30:22 -0700 | [diff] [blame] | 40 | #include <processgroup/processgroup.h> |
| 41 | #include <selinux/selinux.h> |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 42 | #include <system/thread_defs.h> |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 43 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 44 | #include "init.h" |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 45 | #include "property_service.h" |
| 46 | #include "util.h" |
| 47 | |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 48 | using android::base::boot_clock; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 49 | using android::base::GetProperty; |
| 50 | using android::base::Join; |
Tom Cherry | 8d13d80 | 2017-06-29 16:18:08 -0700 | [diff] [blame] | 51 | using android::base::make_scope_guard; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 52 | using android::base::ParseInt; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 53 | using android::base::StartsWith; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 54 | using android::base::StringPrintf; |
| 55 | using android::base::WriteStringToFile; |
| 56 | |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 57 | namespace android { |
| 58 | namespace init { |
| 59 | |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 60 | static std::string ComputeContextFromExecutable(std::string& service_name, |
| 61 | const std::string& service_path) { |
| 62 | std::string computed_context; |
| 63 | |
| 64 | char* raw_con = nullptr; |
| 65 | char* raw_filecon = nullptr; |
| 66 | |
| 67 | if (getcon(&raw_con) == -1) { |
| 68 | LOG(ERROR) << "could not get context while starting '" << service_name << "'"; |
| 69 | return ""; |
| 70 | } |
| 71 | std::unique_ptr<char> mycon(raw_con); |
| 72 | |
| 73 | if (getfilecon(service_path.c_str(), &raw_filecon) == -1) { |
| 74 | LOG(ERROR) << "could not get file context while starting '" << service_name << "'"; |
| 75 | return ""; |
| 76 | } |
| 77 | std::unique_ptr<char> filecon(raw_filecon); |
| 78 | |
| 79 | char* new_con = nullptr; |
| 80 | int rc = security_compute_create(mycon.get(), filecon.get(), |
| 81 | string_to_security_class("process"), &new_con); |
| 82 | if (rc == 0) { |
| 83 | computed_context = new_con; |
| 84 | free(new_con); |
| 85 | } |
| 86 | if (rc == 0 && computed_context == mycon.get()) { |
| 87 | LOG(ERROR) << "service " << service_name << " does not have a SELinux domain defined"; |
| 88 | return ""; |
| 89 | } |
| 90 | if (rc < 0) { |
| 91 | LOG(ERROR) << "could not get context while starting '" << service_name << "'"; |
| 92 | return ""; |
| 93 | } |
| 94 | return computed_context; |
| 95 | } |
| 96 | |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 97 | static void SetUpPidNamespace(const std::string& service_name) { |
| 98 | constexpr unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID; |
| 99 | |
| 100 | // It's OK to LOG(FATAL) in this function since it's running in the first |
| 101 | // child process. |
| 102 | if (mount("", "/proc", "proc", kSafeFlags | MS_REMOUNT, "") == -1) { |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 103 | PLOG(FATAL) << "couldn't remount(/proc) for " << service_name; |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (prctl(PR_SET_NAME, service_name.c_str()) == -1) { |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 107 | PLOG(FATAL) << "couldn't set name for " << service_name; |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | pid_t child_pid = fork(); |
| 111 | if (child_pid == -1) { |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 112 | PLOG(FATAL) << "couldn't fork init inside the PID namespace for " << service_name; |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | if (child_pid > 0) { |
| 116 | // So that we exit with the right status. |
| 117 | static int init_exitstatus = 0; |
| 118 | signal(SIGTERM, [](int) { _exit(init_exitstatus); }); |
| 119 | |
| 120 | pid_t waited_pid; |
| 121 | int status; |
| 122 | while ((waited_pid = wait(&status)) > 0) { |
| 123 | // This loop will end when there are no processes left inside the |
| 124 | // PID namespace or when the init process inside the PID namespace |
| 125 | // gets a signal. |
| 126 | if (waited_pid == child_pid) { |
| 127 | init_exitstatus = status; |
| 128 | } |
| 129 | } |
| 130 | if (!WIFEXITED(init_exitstatus)) { |
| 131 | _exit(EXIT_FAILURE); |
| 132 | } |
| 133 | _exit(WEXITSTATUS(init_exitstatus)); |
| 134 | } |
| 135 | } |
| 136 | |
Tom Cherry | 48db85c | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 137 | static bool ExpandArgsAndExecve(const std::vector<std::string>& args) { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 138 | std::vector<std::string> expanded_args; |
Tom Cherry | 48db85c | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 139 | std::vector<char*> c_strings; |
| 140 | |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 141 | expanded_args.resize(args.size()); |
Tom Cherry | 48db85c | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 142 | c_strings.push_back(const_cast<char*>(args[0].data())); |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 143 | for (std::size_t i = 1; i < args.size(); ++i) { |
| 144 | if (!expand_props(args[i], &expanded_args[i])) { |
| 145 | LOG(FATAL) << args[0] << ": cannot expand '" << args[i] << "'"; |
| 146 | } |
Tom Cherry | 48db85c | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 147 | c_strings.push_back(expanded_args[i].data()); |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 148 | } |
Tom Cherry | 48db85c | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 149 | c_strings.push_back(nullptr); |
| 150 | |
| 151 | return execve(c_strings[0], c_strings.data(), (char**)ENV) == 0; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 154 | ServiceEnvironmentInfo::ServiceEnvironmentInfo() { |
| 155 | } |
| 156 | |
| 157 | ServiceEnvironmentInfo::ServiceEnvironmentInfo(const std::string& name, |
| 158 | const std::string& value) |
| 159 | : name(name), value(value) { |
| 160 | } |
| 161 | |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 162 | Service::Service(const std::string& name, const std::vector<std::string>& args) |
| 163 | : name_(name), |
| 164 | classnames_({"default"}), |
| 165 | flags_(0), |
| 166 | pid_(0), |
| 167 | crash_count_(0), |
| 168 | uid_(0), |
| 169 | gid_(0), |
| 170 | namespace_flags_(0), |
| 171 | seclabel_(""), |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 172 | onrestart_(false, "<Service '" + name + "' onrestart>", 0), |
Tom Cherry | 7da5485 | 2017-05-01 14:16:41 -0700 | [diff] [blame] | 173 | keychord_id_(0), |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 174 | ioprio_class_(IoSchedClass_NONE), |
| 175 | ioprio_pri_(0), |
| 176 | priority_(0), |
| 177 | oom_score_adjust_(-1000), |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 178 | swappiness_(-1), |
| 179 | soft_limit_in_bytes_(-1), |
| 180 | limit_in_bytes_(-1), |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 181 | args_(args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 182 | onrestart_.InitSingleTrigger("onrestart"); |
| 183 | } |
| 184 | |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 185 | Service::Service(const std::string& name, unsigned flags, uid_t uid, gid_t gid, |
| 186 | const std::vector<gid_t>& supp_gids, const CapSet& capabilities, |
| 187 | unsigned namespace_flags, const std::string& seclabel, |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 188 | const std::vector<std::string>& args) |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 189 | : name_(name), |
| 190 | classnames_({"default"}), |
| 191 | flags_(flags), |
| 192 | pid_(0), |
| 193 | crash_count_(0), |
| 194 | uid_(uid), |
| 195 | gid_(gid), |
| 196 | supp_gids_(supp_gids), |
| 197 | capabilities_(capabilities), |
| 198 | namespace_flags_(namespace_flags), |
| 199 | seclabel_(seclabel), |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 200 | onrestart_(false, "<Service '" + name + "' onrestart>", 0), |
Tom Cherry | 7da5485 | 2017-05-01 14:16:41 -0700 | [diff] [blame] | 201 | keychord_id_(0), |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 202 | ioprio_class_(IoSchedClass_NONE), |
| 203 | ioprio_pri_(0), |
| 204 | priority_(0), |
| 205 | oom_score_adjust_(-1000), |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 206 | swappiness_(-1), |
| 207 | soft_limit_in_bytes_(-1), |
| 208 | limit_in_bytes_(-1), |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 209 | args_(args) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 210 | onrestart_.InitSingleTrigger("onrestart"); |
| 211 | } |
| 212 | |
| 213 | void Service::NotifyStateChange(const std::string& new_state) const { |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 214 | if ((flags_ & SVC_TEMPORARY) != 0) { |
| 215 | // Services created by 'exec' are temporary and don't have properties tracking their state. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 216 | return; |
| 217 | } |
| 218 | |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 219 | std::string prop_name = "init.svc." + name_; |
| 220 | property_set(prop_name, new_state); |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 221 | |
| 222 | if (new_state == "running") { |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 223 | uint64_t start_ns = time_started_.time_since_epoch().count(); |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 224 | property_set("ro.boottime." + name_, std::to_string(start_ns)); |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 225 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 228 | void Service::KillProcessGroup(int signal) { |
Tom Cherry | 33838b1 | 2017-05-04 11:32:36 -0700 | [diff] [blame] | 229 | // If we've already seen a successful result from killProcessGroup*(), then we have removed |
| 230 | // the cgroup already and calling these functions a second time will simply result in an error. |
| 231 | // This is true regardless of which signal was sent. |
| 232 | // These functions handle their own logging, so no additional logging is needed. |
| 233 | if (!process_cgroup_empty_) { |
| 234 | LOG(INFO) << "Sending signal " << signal << " to service '" << name_ << "' (pid " << pid_ |
| 235 | << ") process group..."; |
| 236 | int r; |
| 237 | if (signal == SIGTERM) { |
| 238 | r = killProcessGroupOnce(uid_, pid_, signal); |
| 239 | } else { |
| 240 | r = killProcessGroup(uid_, pid_, signal); |
| 241 | } |
| 242 | |
| 243 | if (r == 0) process_cgroup_empty_ = true; |
| 244 | } |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 247 | void Service::SetProcessAttributes() { |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 248 | // Keep capabilites on uid change. |
| 249 | if (capabilities_.any() && uid_) { |
Luis Hector Chavez | f596551 | 2017-06-30 14:04:20 -0700 | [diff] [blame] | 250 | // If Android is running in a container, some securebits might already |
| 251 | // be locked, so don't change those. |
Ben Fennema | 7d7e7cd | 2017-07-25 14:37:21 -0700 | [diff] [blame] | 252 | unsigned long securebits = prctl(PR_GET_SECUREBITS); |
| 253 | if (securebits == -1UL) { |
Luis Hector Chavez | f596551 | 2017-06-30 14:04:20 -0700 | [diff] [blame] | 254 | PLOG(FATAL) << "prctl(PR_GET_SECUREBITS) failed for " << name_; |
| 255 | } |
| 256 | securebits |= SECBIT_KEEP_CAPS | SECBIT_KEEP_CAPS_LOCKED; |
| 257 | if (prctl(PR_SET_SECUREBITS, securebits) != 0) { |
| 258 | PLOG(FATAL) << "prctl(PR_SET_SECUREBITS) failed for " << name_; |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 262 | // TODO: work out why this fails for `console` then upgrade to FATAL. |
| 263 | if (setpgid(0, getpid()) == -1) PLOG(ERROR) << "setpgid failed for " << name_; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 264 | |
| 265 | if (gid_) { |
| 266 | if (setgid(gid_) != 0) { |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 267 | PLOG(FATAL) << "setgid failed for " << name_; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 268 | } |
| 269 | } |
Nick Kralevich | 80960d2 | 2016-10-29 12:20:00 -0700 | [diff] [blame] | 270 | if (setgroups(supp_gids_.size(), &supp_gids_[0]) != 0) { |
| 271 | PLOG(FATAL) << "setgroups failed for " << name_; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 272 | } |
| 273 | if (uid_) { |
| 274 | if (setuid(uid_) != 0) { |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 275 | PLOG(FATAL) << "setuid failed for " << name_; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | if (!seclabel_.empty()) { |
| 279 | if (setexeccon(seclabel_.c_str()) < 0) { |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 280 | PLOG(FATAL) << "cannot setexeccon('" << seclabel_ << "') for " << name_; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | if (priority_ != 0) { |
| 284 | if (setpriority(PRIO_PROCESS, 0, priority_) != 0) { |
Elliott Hughes | e18e7e5 | 2016-07-25 18:18:16 -0700 | [diff] [blame] | 285 | PLOG(FATAL) << "setpriority failed for " << name_; |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 286 | } |
| 287 | } |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 288 | if (capabilities_.any()) { |
| 289 | if (!SetCapsForExec(capabilities_)) { |
| 290 | LOG(FATAL) << "cannot set capabilities for " << name_; |
| 291 | } |
| 292 | } |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 293 | } |
| 294 | |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 295 | void Service::Reap() { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 296 | if (!(flags_ & SVC_ONESHOT) || (flags_ & SVC_RESTART)) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 297 | KillProcessGroup(SIGKILL); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 300 | // Remove any descriptor resources we may have created. |
| 301 | std::for_each(descriptors_.begin(), descriptors_.end(), |
| 302 | std::bind(&DescriptorInfo::Clean, std::placeholders::_1)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 303 | |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 304 | if (flags_ & SVC_TEMPORARY) { |
| 305 | return; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | pid_ = 0; |
| 309 | flags_ &= (~SVC_RUNNING); |
| 310 | |
| 311 | // Oneshot processes go into the disabled state on exit, |
| 312 | // except when manually restarted. |
| 313 | if ((flags_ & SVC_ONESHOT) && !(flags_ & SVC_RESTART)) { |
| 314 | flags_ |= SVC_DISABLED; |
| 315 | } |
| 316 | |
| 317 | // Disabled and reset processes do not get restarted automatically. |
| 318 | if (flags_ & (SVC_DISABLED | SVC_RESET)) { |
| 319 | NotifyStateChange("stopped"); |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 320 | return; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 323 | // If we crash > 4 times in 4 minutes, reboot into recovery. |
| 324 | boot_clock::time_point now = boot_clock::now(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 325 | if ((flags_ & SVC_CRITICAL) && !(flags_ & SVC_RESTART)) { |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 326 | if (now < time_crashed_ + 4min) { |
| 327 | if (++crash_count_ > 4) { |
Elliott Hughes | 331cf2f | 2016-11-29 19:20:58 +0000 | [diff] [blame] | 328 | LOG(ERROR) << "critical process '" << name_ << "' exited 4 times in 4 minutes"; |
| 329 | panic(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 330 | } |
| 331 | } else { |
| 332 | time_crashed_ = now; |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 333 | crash_count_ = 1; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | flags_ &= (~SVC_RESTART); |
| 338 | flags_ |= SVC_RESTARTING; |
| 339 | |
| 340 | // Execute all onrestart commands for this service. |
| 341 | onrestart_.ExecuteAllCommands(); |
| 342 | |
| 343 | NotifyStateChange("restarting"); |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 344 | return; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | void Service::DumpState() const { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 348 | LOG(INFO) << "service " << name_; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 349 | LOG(INFO) << " class '" << Join(classnames_, " ") << "'"; |
| 350 | LOG(INFO) << " exec " << Join(args_, " "); |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 351 | std::for_each(descriptors_.begin(), descriptors_.end(), |
| 352 | [] (const auto& info) { LOG(INFO) << *info; }); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 355 | bool Service::ParseCapabilities(const std::vector<std::string>& args, std::string* err) { |
| 356 | capabilities_ = 0; |
| 357 | |
Jorge Lucangeli Obes | f3f824e | 2016-12-15 12:13:38 -0500 | [diff] [blame] | 358 | if (!CapAmbientSupported()) { |
| 359 | *err = "capabilities requested but the kernel does not support ambient capabilities"; |
| 360 | return false; |
| 361 | } |
| 362 | |
| 363 | unsigned int last_valid_cap = GetLastValidCap(); |
| 364 | if (last_valid_cap >= capabilities_.size()) { |
| 365 | LOG(WARNING) << "last valid run-time capability is larger than CAP_LAST_CAP"; |
| 366 | } |
| 367 | |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 368 | for (size_t i = 1; i < args.size(); i++) { |
| 369 | const std::string& arg = args[i]; |
Jorge Lucangeli Obes | f3f824e | 2016-12-15 12:13:38 -0500 | [diff] [blame] | 370 | int res = LookupCap(arg); |
| 371 | if (res < 0) { |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 372 | *err = StringPrintf("invalid capability '%s'", arg.c_str()); |
| 373 | return false; |
| 374 | } |
Jorge Lucangeli Obes | f3f824e | 2016-12-15 12:13:38 -0500 | [diff] [blame] | 375 | unsigned int cap = static_cast<unsigned int>(res); // |res| is >= 0. |
| 376 | if (cap > last_valid_cap) { |
| 377 | *err = StringPrintf("capability '%s' not supported by the kernel", arg.c_str()); |
| 378 | return false; |
| 379 | } |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 380 | capabilities_[cap] = true; |
| 381 | } |
| 382 | return true; |
| 383 | } |
| 384 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 385 | bool Service::ParseClass(const std::vector<std::string>& args, std::string* err) { |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 386 | classnames_ = std::set<std::string>(args.begin() + 1, args.end()); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 387 | return true; |
| 388 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 389 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 390 | bool Service::ParseConsole(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 391 | flags_ |= SVC_CONSOLE; |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 392 | console_ = args.size() > 1 ? "/dev/" + args[1] : ""; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 393 | return true; |
| 394 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 395 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 396 | bool Service::ParseCritical(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 397 | flags_ |= SVC_CRITICAL; |
| 398 | return true; |
| 399 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 400 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 401 | bool Service::ParseDisabled(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 402 | flags_ |= SVC_DISABLED; |
| 403 | flags_ |= SVC_RC_DISABLED; |
| 404 | return true; |
| 405 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 406 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 407 | bool Service::ParseGroup(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 408 | std::string decode_uid_err; |
| 409 | if (!DecodeUid(args[1], &gid_, &decode_uid_err)) { |
| 410 | *err = "Unable to find GID for '" + args[1] + "': " + decode_uid_err; |
| 411 | return false; |
| 412 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 413 | for (std::size_t n = 2; n < args.size(); n++) { |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 414 | gid_t gid; |
| 415 | if (!DecodeUid(args[n], &gid, &decode_uid_err)) { |
| 416 | *err = "Unable to find GID for '" + args[n] + "': " + decode_uid_err; |
| 417 | return false; |
| 418 | } |
| 419 | supp_gids_.emplace_back(gid); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 420 | } |
| 421 | return true; |
| 422 | } |
| 423 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 424 | bool Service::ParsePriority(const std::vector<std::string>& args, std::string* err) { |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 425 | priority_ = 0; |
| 426 | if (!ParseInt(args[1], &priority_, |
Keun-young Park | dd34ca4 | 2016-11-11 18:06:31 -0800 | [diff] [blame] | 427 | static_cast<int>(ANDROID_PRIORITY_HIGHEST), // highest is negative |
| 428 | static_cast<int>(ANDROID_PRIORITY_LOWEST))) { |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 429 | *err = StringPrintf("process priority value must be range %d - %d", |
| 430 | ANDROID_PRIORITY_HIGHEST, ANDROID_PRIORITY_LOWEST); |
| 431 | return false; |
| 432 | } |
Vitalii Tomkiv | 081705c | 2016-05-18 17:36:30 -0700 | [diff] [blame] | 433 | return true; |
| 434 | } |
| 435 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 436 | bool Service::ParseIoprio(const std::vector<std::string>& args, std::string* err) { |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 437 | if (!ParseInt(args[2], &ioprio_pri_, 0, 7)) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 438 | *err = "priority value must be range 0 - 7"; |
| 439 | return false; |
| 440 | } |
| 441 | |
| 442 | if (args[1] == "rt") { |
| 443 | ioprio_class_ = IoSchedClass_RT; |
| 444 | } else if (args[1] == "be") { |
| 445 | ioprio_class_ = IoSchedClass_BE; |
| 446 | } else if (args[1] == "idle") { |
| 447 | ioprio_class_ = IoSchedClass_IDLE; |
| 448 | } else { |
| 449 | *err = "ioprio option usage: ioprio <rt|be|idle> <0-7>"; |
| 450 | return false; |
| 451 | } |
| 452 | |
| 453 | return true; |
| 454 | } |
| 455 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 456 | bool Service::ParseKeycodes(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 457 | for (std::size_t i = 1; i < args.size(); i++) { |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 458 | int code; |
| 459 | if (ParseInt(args[i], &code)) { |
| 460 | keycodes_.emplace_back(code); |
| 461 | } else { |
| 462 | LOG(WARNING) << "ignoring invalid keycode: " << args[i]; |
| 463 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 464 | } |
| 465 | return true; |
| 466 | } |
| 467 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 468 | bool Service::ParseOneshot(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 469 | flags_ |= SVC_ONESHOT; |
| 470 | return true; |
| 471 | } |
| 472 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 473 | bool Service::ParseOnrestart(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 474 | std::vector<std::string> str_args(args.begin() + 1, args.end()); |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 475 | int line = onrestart_.NumCommands() + 1; |
| 476 | onrestart_.AddCommand(str_args, line, err); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 477 | return true; |
| 478 | } |
| 479 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 480 | bool Service::ParseNamespace(const std::vector<std::string>& args, std::string* err) { |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 481 | for (size_t i = 1; i < args.size(); i++) { |
| 482 | if (args[i] == "pid") { |
| 483 | namespace_flags_ |= CLONE_NEWPID; |
| 484 | // PID namespaces require mount namespaces. |
| 485 | namespace_flags_ |= CLONE_NEWNS; |
| 486 | } else if (args[i] == "mnt") { |
| 487 | namespace_flags_ |= CLONE_NEWNS; |
| 488 | } else { |
| 489 | *err = "namespace must be 'pid' or 'mnt'"; |
| 490 | return false; |
| 491 | } |
| 492 | } |
| 493 | return true; |
| 494 | } |
| 495 | |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 496 | bool Service::ParseOomScoreAdjust(const std::vector<std::string>& args, std::string* err) { |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 497 | if (!ParseInt(args[1], &oom_score_adjust_, -1000, 1000)) { |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 498 | *err = "oom_score_adjust value must be in range -1000 - +1000"; |
| 499 | return false; |
| 500 | } |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 501 | return true; |
| 502 | } |
| 503 | |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 504 | bool Service::ParseMemcgSwappiness(const std::vector<std::string>& args, std::string* err) { |
| 505 | if (!ParseInt(args[1], &swappiness_, 0)) { |
| 506 | *err = "swappiness value must be equal or greater than 0"; |
| 507 | return false; |
| 508 | } |
| 509 | return true; |
| 510 | } |
| 511 | |
| 512 | bool Service::ParseMemcgLimitInBytes(const std::vector<std::string>& args, std::string* err) { |
| 513 | if (!ParseInt(args[1], &limit_in_bytes_, 0)) { |
| 514 | *err = "limit_in_bytes value must be equal or greater than 0"; |
| 515 | return false; |
| 516 | } |
| 517 | return true; |
| 518 | } |
| 519 | |
| 520 | bool Service::ParseMemcgSoftLimitInBytes(const std::vector<std::string>& args, std::string* err) { |
| 521 | if (!ParseInt(args[1], &soft_limit_in_bytes_, 0)) { |
| 522 | *err = "soft_limit_in_bytes value must be equal or greater than 0"; |
| 523 | return false; |
| 524 | } |
| 525 | return true; |
| 526 | } |
| 527 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 528 | bool Service::ParseSeclabel(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 529 | seclabel_ = args[1]; |
| 530 | return true; |
| 531 | } |
| 532 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 533 | bool Service::ParseSetenv(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 534 | envvars_.emplace_back(args[1], args[2]); |
| 535 | return true; |
| 536 | } |
| 537 | |
Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 538 | bool Service::ParseShutdown(const std::vector<std::string>& args, std::string* err) { |
| 539 | if (args[1] == "critical") { |
| 540 | flags_ |= SVC_SHUTDOWN_CRITICAL; |
| 541 | return true; |
| 542 | } |
| 543 | return false; |
| 544 | } |
| 545 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 546 | template <typename T> |
| 547 | bool Service::AddDescriptor(const std::vector<std::string>& args, std::string* err) { |
| 548 | int perm = args.size() > 3 ? std::strtoul(args[3].c_str(), 0, 8) : -1; |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 549 | uid_t uid = 0; |
| 550 | gid_t gid = 0; |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 551 | std::string context = args.size() > 6 ? args[6] : ""; |
| 552 | |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 553 | std::string decode_uid_err; |
| 554 | if (args.size() > 4) { |
| 555 | if (!DecodeUid(args[4], &uid, &decode_uid_err)) { |
| 556 | *err = "Unable to find UID for '" + args[4] + "': " + decode_uid_err; |
| 557 | return false; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | if (args.size() > 5) { |
| 562 | if (!DecodeUid(args[5], &gid, &decode_uid_err)) { |
| 563 | *err = "Unable to find GID for '" + args[5] + "': " + decode_uid_err; |
| 564 | return false; |
| 565 | } |
| 566 | } |
| 567 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 568 | auto descriptor = std::make_unique<T>(args[1], args[2], uid, gid, perm, context); |
| 569 | |
| 570 | auto old = |
| 571 | std::find_if(descriptors_.begin(), descriptors_.end(), |
| 572 | [&descriptor] (const auto& other) { return descriptor.get() == other.get(); }); |
| 573 | |
| 574 | if (old != descriptors_.end()) { |
| 575 | *err = "duplicate descriptor " + args[1] + " " + args[2]; |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | descriptors_.emplace_back(std::move(descriptor)); |
| 580 | return true; |
| 581 | } |
| 582 | |
| 583 | // name type perm [ uid gid context ] |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 584 | bool Service::ParseSocket(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 585 | if (!StartsWith(args[2], "dgram") && !StartsWith(args[2], "stream") && |
| 586 | !StartsWith(args[2], "seqpacket")) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 587 | *err = "socket type must be 'dgram', 'stream' or 'seqpacket'"; |
| 588 | return false; |
| 589 | } |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 590 | return AddDescriptor<SocketInfo>(args, err); |
| 591 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 592 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 593 | // name type perm [ uid gid context ] |
| 594 | bool Service::ParseFile(const std::vector<std::string>& args, std::string* err) { |
| 595 | if (args[2] != "r" && args[2] != "w" && args[2] != "rw") { |
| 596 | *err = "file type must be 'r', 'w' or 'rw'"; |
| 597 | return false; |
| 598 | } |
| 599 | if ((args[1][0] != '/') || (args[1].find("../") != std::string::npos)) { |
| 600 | *err = "file name must not be relative"; |
| 601 | return false; |
| 602 | } |
| 603 | return AddDescriptor<FileInfo>(args, err); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 604 | } |
| 605 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 606 | bool Service::ParseUser(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 607 | std::string decode_uid_err; |
| 608 | if (!DecodeUid(args[1], &uid_, &decode_uid_err)) { |
| 609 | *err = "Unable to find UID for '" + args[1] + "': " + decode_uid_err; |
| 610 | return false; |
| 611 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 612 | return true; |
| 613 | } |
| 614 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 615 | bool Service::ParseWritepid(const std::vector<std::string>& args, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 616 | writepid_files_.assign(args.begin() + 1, args.end()); |
| 617 | return true; |
| 618 | } |
| 619 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 620 | class Service::OptionParserMap : public KeywordMap<OptionParser> { |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 621 | public: |
| 622 | OptionParserMap() {} |
| 623 | |
| 624 | private: |
| 625 | const Map& map() const override; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 626 | }; |
| 627 | |
Tom Cherry | ad54d09 | 2017-04-19 16:18:50 -0700 | [diff] [blame] | 628 | const Service::OptionParserMap::Map& Service::OptionParserMap::map() const { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 629 | constexpr std::size_t kMax = std::numeric_limits<std::size_t>::max(); |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 630 | // clang-format off |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 631 | static const Map option_parsers = { |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 632 | {"capabilities", |
| 633 | {1, kMax, &Service::ParseCapabilities}}, |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 634 | {"class", {1, kMax, &Service::ParseClass}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 635 | {"console", {0, 1, &Service::ParseConsole}}, |
| 636 | {"critical", {0, 0, &Service::ParseCritical}}, |
| 637 | {"disabled", {0, 0, &Service::ParseDisabled}}, |
| 638 | {"group", {1, NR_SVC_SUPP_GIDS + 1, &Service::ParseGroup}}, |
| 639 | {"ioprio", {2, 2, &Service::ParseIoprio}}, |
| 640 | {"priority", {1, 1, &Service::ParsePriority}}, |
| 641 | {"keycodes", {1, kMax, &Service::ParseKeycodes}}, |
| 642 | {"oneshot", {0, 0, &Service::ParseOneshot}}, |
| 643 | {"onrestart", {1, kMax, &Service::ParseOnrestart}}, |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 644 | {"oom_score_adjust", |
| 645 | {1, 1, &Service::ParseOomScoreAdjust}}, |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 646 | {"memcg.swappiness", |
| 647 | {1, 1, &Service::ParseMemcgSwappiness}}, |
| 648 | {"memcg.soft_limit_in_bytes", |
| 649 | {1, 1, &Service::ParseMemcgSoftLimitInBytes}}, |
| 650 | {"memcg.limit_in_bytes", |
| 651 | {1, 1, &Service::ParseMemcgLimitInBytes}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 652 | {"namespace", {1, 2, &Service::ParseNamespace}}, |
| 653 | {"seclabel", {1, 1, &Service::ParseSeclabel}}, |
| 654 | {"setenv", {2, 2, &Service::ParseSetenv}}, |
Keun-young Park | cccb34f | 2017-07-05 11:38:44 -0700 | [diff] [blame] | 655 | {"shutdown", {1, 1, &Service::ParseShutdown}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 656 | {"socket", {3, 6, &Service::ParseSocket}}, |
Mark Salyzyn | 978fd0e | 2016-12-02 08:05:22 -0800 | [diff] [blame] | 657 | {"file", {2, 2, &Service::ParseFile}}, |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 658 | {"user", {1, 1, &Service::ParseUser}}, |
| 659 | {"writepid", {1, kMax, &Service::ParseWritepid}}, |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 660 | }; |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 661 | // clang-format on |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 662 | return option_parsers; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 665 | bool Service::ParseLine(const std::vector<std::string>& args, std::string* err) { |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 666 | static const OptionParserMap parser_map; |
Tom Cherry | fe06205 | 2017-04-24 16:59:05 -0700 | [diff] [blame] | 667 | auto parser = parser_map.FindFunction(args, err); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 668 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 669 | if (!parser) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 670 | return false; |
| 671 | } |
| 672 | |
Jorge Lucangeli Obes | 177b27d | 2016-06-29 14:32:49 -0400 | [diff] [blame] | 673 | return (this->*parser)(args, err); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 676 | bool Service::ExecStart(std::unique_ptr<android::base::Timer>* exec_waiter) { |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 677 | flags_ |= SVC_EXEC | SVC_ONESHOT; |
| 678 | |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 679 | exec_waiter->reset(new android::base::Timer); |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 680 | |
| 681 | if (!Start()) { |
| 682 | exec_waiter->reset(); |
| 683 | return false; |
| 684 | } |
| 685 | return true; |
| 686 | } |
| 687 | |
Elliott Hughes | bdeac39 | 2016-04-12 15:38:27 -0700 | [diff] [blame] | 688 | bool Service::Start() { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 689 | // Starting a service removes it from the disabled or reset state and |
| 690 | // immediately takes it out of the restarting state if it was in there. |
| 691 | flags_ &= (~(SVC_DISABLED|SVC_RESTARTING|SVC_RESET|SVC_RESTART|SVC_DISABLED_START)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 692 | |
| 693 | // Running processes require no additional work --- if they're in the |
| 694 | // process of exiting, we've ensured that they will immediately restart |
| 695 | // on exit, unless they are ONESHOT. |
| 696 | if (flags_ & SVC_RUNNING) { |
| 697 | return false; |
| 698 | } |
| 699 | |
| 700 | bool needs_console = (flags_ & SVC_CONSOLE); |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 701 | if (needs_console) { |
| 702 | if (console_.empty()) { |
| 703 | console_ = default_console; |
| 704 | } |
| 705 | |
Adrian Salido | 24ef860 | 2016-12-20 15:52:15 -0800 | [diff] [blame] | 706 | // Make sure that open call succeeds to ensure a console driver is |
| 707 | // properly registered for the device node |
| 708 | int console_fd = open(console_.c_str(), O_RDWR | O_CLOEXEC); |
| 709 | if (console_fd < 0) { |
| 710 | PLOG(ERROR) << "service '" << name_ << "' couldn't open console '" << console_ << "'"; |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 711 | flags_ |= SVC_DISABLED; |
| 712 | return false; |
| 713 | } |
Adrian Salido | 24ef860 | 2016-12-20 15:52:15 -0800 | [diff] [blame] | 714 | close(console_fd); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | struct stat sb; |
| 718 | if (stat(args_[0].c_str(), &sb) == -1) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 719 | PLOG(ERROR) << "cannot find '" << args_[0] << "', disabling '" << name_ << "'"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 720 | flags_ |= SVC_DISABLED; |
| 721 | return false; |
| 722 | } |
| 723 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 724 | std::string scon; |
| 725 | if (!seclabel_.empty()) { |
| 726 | scon = seclabel_; |
| 727 | } else { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 728 | scon = ComputeContextFromExecutable(name_, args_[0]); |
| 729 | if (scon == "") { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 730 | return false; |
| 731 | } |
| 732 | } |
| 733 | |
Wei Wang | a285dac | 2016-10-04 14:05:39 -0700 | [diff] [blame] | 734 | LOG(INFO) << "starting service '" << name_ << "'..."; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 735 | |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 736 | pid_t pid = -1; |
| 737 | if (namespace_flags_) { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 738 | pid = clone(nullptr, nullptr, namespace_flags_ | SIGCHLD, nullptr); |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 739 | } else { |
| 740 | pid = fork(); |
| 741 | } |
| 742 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 743 | if (pid == 0) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 744 | umask(077); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 745 | |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 746 | if (namespace_flags_ & CLONE_NEWPID) { |
| 747 | // This will fork again to run an init process inside the PID |
| 748 | // namespace. |
| 749 | SetUpPidNamespace(name_); |
| 750 | } |
| 751 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 752 | for (const auto& ei : envvars_) { |
| 753 | add_environment(ei.name.c_str(), ei.value.c_str()); |
| 754 | } |
| 755 | |
Mark Salyzyn | 62767fe | 2016-10-27 07:45:34 -0700 | [diff] [blame] | 756 | std::for_each(descriptors_.begin(), descriptors_.end(), |
| 757 | std::bind(&DescriptorInfo::CreateAndPublish, std::placeholders::_1, scon)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 758 | |
Alex Vakulenko | 0828676 | 2016-05-03 12:00:00 -0700 | [diff] [blame] | 759 | // See if there were "writepid" instructions to write to files under /dev/cpuset/. |
| 760 | auto cpuset_predicate = [](const std::string& path) { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 761 | return StartsWith(path, "/dev/cpuset/"); |
Alex Vakulenko | 0828676 | 2016-05-03 12:00:00 -0700 | [diff] [blame] | 762 | }; |
| 763 | auto iter = std::find_if(writepid_files_.begin(), writepid_files_.end(), cpuset_predicate); |
| 764 | if (iter == writepid_files_.end()) { |
| 765 | // There were no "writepid" instructions for cpusets, check if the system default |
| 766 | // cpuset is specified to be used for the process. |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 767 | std::string default_cpuset = GetProperty("ro.cpuset.default", ""); |
Alex Vakulenko | 0828676 | 2016-05-03 12:00:00 -0700 | [diff] [blame] | 768 | if (!default_cpuset.empty()) { |
| 769 | // Make sure the cpuset name starts and ends with '/'. |
| 770 | // A single '/' means the 'root' cpuset. |
| 771 | if (default_cpuset.front() != '/') { |
| 772 | default_cpuset.insert(0, 1, '/'); |
| 773 | } |
| 774 | if (default_cpuset.back() != '/') { |
| 775 | default_cpuset.push_back('/'); |
| 776 | } |
| 777 | writepid_files_.push_back( |
| 778 | StringPrintf("/dev/cpuset%stasks", default_cpuset.c_str())); |
| 779 | } |
| 780 | } |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 781 | std::string pid_str = std::to_string(getpid()); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 782 | for (const auto& file : writepid_files_) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 783 | if (!WriteStringToFile(pid_str, file)) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 784 | PLOG(ERROR) << "couldn't write " << pid_str << " to " << file; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 785 | } |
| 786 | } |
| 787 | |
| 788 | if (ioprio_class_ != IoSchedClass_NONE) { |
| 789 | if (android_set_ioprio(getpid(), ioprio_class_, ioprio_pri_)) { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 790 | PLOG(ERROR) << "failed to set pid " << getpid() |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 791 | << " ioprio=" << ioprio_class_ << "," << ioprio_pri_; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 792 | } |
| 793 | } |
| 794 | |
| 795 | if (needs_console) { |
| 796 | setsid(); |
| 797 | OpenConsole(); |
| 798 | } else { |
| 799 | ZapStdio(); |
| 800 | } |
| 801 | |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 802 | // As requested, set our gid, supplemental gids, uid, context, and |
| 803 | // priority. Aborts on failure. |
| 804 | SetProcessAttributes(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 805 | |
Tom Cherry | 48db85c | 2017-09-11 16:08:54 -0700 | [diff] [blame] | 806 | if (!ExpandArgsAndExecve(args_)) { |
| 807 | PLOG(ERROR) << "cannot execve('" << args_[0] << "')"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | _exit(127); |
| 811 | } |
| 812 | |
| 813 | if (pid < 0) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 814 | PLOG(ERROR) << "failed to fork for '" << name_ << "'"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 815 | pid_ = 0; |
| 816 | return false; |
| 817 | } |
| 818 | |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 819 | if (oom_score_adjust_ != -1000) { |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 820 | std::string oom_str = std::to_string(oom_score_adjust_); |
Marco Nelissen | 310f670 | 2016-07-22 12:07:06 -0700 | [diff] [blame] | 821 | std::string oom_file = StringPrintf("/proc/%d/oom_score_adj", pid); |
| 822 | if (!WriteStringToFile(oom_str, oom_file)) { |
| 823 | PLOG(ERROR) << "couldn't write oom_score_adj: " << strerror(errno); |
| 824 | } |
| 825 | } |
| 826 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 827 | time_started_ = boot_clock::now(); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 828 | pid_ = pid; |
| 829 | flags_ |= SVC_RUNNING; |
Tom Cherry | 33838b1 | 2017-05-04 11:32:36 -0700 | [diff] [blame] | 830 | process_cgroup_empty_ = false; |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 831 | |
| 832 | errno = -createProcessGroup(uid_, pid_); |
| 833 | if (errno != 0) { |
Jorge Lucangeli Obes | 344d01f | 2016-07-08 13:32:26 -0400 | [diff] [blame] | 834 | PLOG(ERROR) << "createProcessGroup(" << uid_ << ", " << pid_ << ") failed for service '" |
| 835 | << name_ << "'"; |
Robert Benea | d485226 | 2017-07-16 19:38:11 -0700 | [diff] [blame] | 836 | } else { |
| 837 | if (swappiness_ != -1) { |
| 838 | if (!setProcessGroupSwappiness(uid_, pid_, swappiness_)) { |
| 839 | PLOG(ERROR) << "setProcessGroupSwappiness failed"; |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | if (soft_limit_in_bytes_ != -1) { |
| 844 | if (!setProcessGroupSoftLimit(uid_, pid_, soft_limit_in_bytes_)) { |
| 845 | PLOG(ERROR) << "setProcessGroupSoftLimit failed"; |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | if (limit_in_bytes_ != -1) { |
| 850 | if (!setProcessGroupLimit(uid_, pid_, limit_in_bytes_)) { |
| 851 | PLOG(ERROR) << "setProcessGroupLimit failed"; |
| 852 | } |
| 853 | } |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 854 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 855 | |
| 856 | if ((flags_ & SVC_EXEC) != 0) { |
Tom Cherry | 1c3a53f | 2017-06-22 16:50:31 -0700 | [diff] [blame] | 857 | LOG(INFO) << "SVC_EXEC pid " << pid_ << " (uid " << uid_ << " gid " << gid_ << "+" |
| 858 | << supp_gids_.size() << " context " |
| 859 | << (!seclabel_.empty() ? seclabel_ : "default") << ") started; waiting..."; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | NotifyStateChange("running"); |
| 863 | return true; |
| 864 | } |
| 865 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 866 | bool Service::StartIfNotDisabled() { |
| 867 | if (!(flags_ & SVC_DISABLED)) { |
| 868 | return Start(); |
| 869 | } else { |
| 870 | flags_ |= SVC_DISABLED_START; |
| 871 | } |
| 872 | return true; |
| 873 | } |
| 874 | |
| 875 | bool Service::Enable() { |
| 876 | flags_ &= ~(SVC_DISABLED | SVC_RC_DISABLED); |
| 877 | if (flags_ & SVC_DISABLED_START) { |
| 878 | return Start(); |
| 879 | } |
| 880 | return true; |
| 881 | } |
| 882 | |
| 883 | void Service::Reset() { |
| 884 | StopOrReset(SVC_RESET); |
| 885 | } |
| 886 | |
| 887 | void Service::Stop() { |
| 888 | StopOrReset(SVC_DISABLED); |
| 889 | } |
| 890 | |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 891 | void Service::Terminate() { |
| 892 | flags_ &= ~(SVC_RESTARTING | SVC_DISABLED_START); |
| 893 | flags_ |= SVC_DISABLED; |
| 894 | if (pid_) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 895 | KillProcessGroup(SIGTERM); |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 896 | NotifyStateChange("stopping"); |
| 897 | } |
| 898 | } |
| 899 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 900 | void Service::Restart() { |
| 901 | if (flags_ & SVC_RUNNING) { |
| 902 | /* Stop, wait, then start the service. */ |
| 903 | StopOrReset(SVC_RESTART); |
| 904 | } else if (!(flags_ & SVC_RESTARTING)) { |
| 905 | /* Just start the service since it's not running. */ |
| 906 | Start(); |
| 907 | } /* else: Service is restarting anyways. */ |
| 908 | } |
| 909 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 910 | void Service::RestartIfNeeded(time_t* process_needs_restart_at) { |
| 911 | boot_clock::time_point now = boot_clock::now(); |
| 912 | boot_clock::time_point next_start = time_started_ + 5s; |
| 913 | if (now > next_start) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 914 | flags_ &= (~SVC_RESTARTING); |
| 915 | Start(); |
| 916 | return; |
| 917 | } |
| 918 | |
Elliott Hughes | 9605a94 | 2016-11-10 17:43:47 -0800 | [diff] [blame] | 919 | time_t next_start_time_t = time(nullptr) + |
| 920 | time_t(std::chrono::duration_cast<std::chrono::seconds>(next_start - now).count()); |
| 921 | if (next_start_time_t < *process_needs_restart_at || *process_needs_restart_at == 0) { |
| 922 | *process_needs_restart_at = next_start_time_t; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 923 | } |
| 924 | } |
| 925 | |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 926 | // The how field should be either SVC_DISABLED, SVC_RESET, or SVC_RESTART. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 927 | void Service::StopOrReset(int how) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 928 | // The service is still SVC_RUNNING until its process exits, but if it has |
| 929 | // already exited it shoudn't attempt a restart yet. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 930 | flags_ &= ~(SVC_RESTARTING | SVC_DISABLED_START); |
| 931 | |
| 932 | if ((how != SVC_DISABLED) && (how != SVC_RESET) && (how != SVC_RESTART)) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 933 | // An illegal flag: default to SVC_DISABLED. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 934 | how = SVC_DISABLED; |
| 935 | } |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 936 | |
| 937 | // If the service has not yet started, prevent it from auto-starting with its class. |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 938 | if (how == SVC_RESET) { |
| 939 | flags_ |= (flags_ & SVC_RC_DISABLED) ? SVC_DISABLED : SVC_RESET; |
| 940 | } else { |
| 941 | flags_ |= how; |
| 942 | } |
| 943 | |
| 944 | if (pid_) { |
Elliott Hughes | ad8e94e | 2016-06-15 14:49:57 -0700 | [diff] [blame] | 945 | KillProcessGroup(SIGKILL); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 946 | NotifyStateChange("stopping"); |
| 947 | } else { |
| 948 | NotifyStateChange("stopped"); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | void Service::ZapStdio() const { |
| 953 | int fd; |
| 954 | fd = open("/dev/null", O_RDWR); |
| 955 | dup2(fd, 0); |
| 956 | dup2(fd, 1); |
| 957 | dup2(fd, 2); |
| 958 | close(fd); |
| 959 | } |
| 960 | |
| 961 | void Service::OpenConsole() const { |
Viorel Suman | 70daa67 | 2016-03-21 10:08:07 +0200 | [diff] [blame] | 962 | int fd = open(console_.c_str(), O_RDWR); |
| 963 | if (fd == -1) fd = open("/dev/null", O_RDWR); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 964 | ioctl(fd, TIOCSCTTY, 0); |
| 965 | dup2(fd, 0); |
| 966 | dup2(fd, 1); |
| 967 | dup2(fd, 2); |
| 968 | close(fd); |
| 969 | } |
| 970 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 971 | int ServiceManager::exec_count_ = 0; |
| 972 | |
| 973 | ServiceManager::ServiceManager() { |
| 974 | } |
| 975 | |
| 976 | ServiceManager& ServiceManager::GetInstance() { |
| 977 | static ServiceManager instance; |
| 978 | return instance; |
| 979 | } |
| 980 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 981 | void ServiceManager::AddService(std::unique_ptr<Service> service) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 982 | services_.emplace_back(std::move(service)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 983 | } |
| 984 | |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 985 | bool ServiceManager::Exec(const std::vector<std::string>& args) { |
| 986 | Service* svc = MakeExecOneshotService(args); |
| 987 | if (!svc) { |
| 988 | LOG(ERROR) << "Could not create exec service"; |
| 989 | return false; |
| 990 | } |
| 991 | if (!svc->ExecStart(&exec_waiter_)) { |
| 992 | LOG(ERROR) << "Could not start exec service"; |
| 993 | ServiceManager::GetInstance().RemoveService(*svc); |
| 994 | return false; |
| 995 | } |
| 996 | return true; |
| 997 | } |
| 998 | |
| 999 | bool ServiceManager::ExecStart(const std::string& name) { |
| 1000 | Service* svc = FindServiceByName(name); |
| 1001 | if (!svc) { |
| 1002 | LOG(ERROR) << "ExecStart(" << name << "): Service not found"; |
| 1003 | return false; |
| 1004 | } |
| 1005 | if (!svc->ExecStart(&exec_waiter_)) { |
| 1006 | LOG(ERROR) << "ExecStart(" << name << "): Could not start Service"; |
| 1007 | return false; |
| 1008 | } |
| 1009 | return true; |
| 1010 | } |
| 1011 | |
| 1012 | bool ServiceManager::IsWaitingForExec() const { return exec_waiter_ != nullptr; } |
| 1013 | |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1014 | Service* ServiceManager::MakeExecOneshotService(const std::vector<std::string>& args) { |
| 1015 | // Parse the arguments: exec [SECLABEL [UID [GID]*] --] COMMAND ARGS... |
| 1016 | // SECLABEL can be a - to denote default |
| 1017 | std::size_t command_arg = 1; |
| 1018 | for (std::size_t i = 1; i < args.size(); ++i) { |
| 1019 | if (args[i] == "--") { |
| 1020 | command_arg = i + 1; |
| 1021 | break; |
| 1022 | } |
| 1023 | } |
| 1024 | if (command_arg > 4 + NR_SVC_SUPP_GIDS) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 1025 | LOG(ERROR) << "exec called with too many supplementary group ids"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1026 | return nullptr; |
| 1027 | } |
| 1028 | |
| 1029 | if (command_arg >= args.size()) { |
Elliott Hughes | f86b5a6 | 2016-06-24 15:12:21 -0700 | [diff] [blame] | 1030 | LOG(ERROR) << "exec called without command"; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1031 | return nullptr; |
| 1032 | } |
| 1033 | std::vector<std::string> str_args(args.begin() + command_arg, args.end()); |
| 1034 | |
| 1035 | exec_count_++; |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 1036 | std::string name = "exec " + std::to_string(exec_count_) + " (" + Join(str_args, " ") + ")"; |
Tom Cherry | 86e31a8 | 2017-04-25 17:31:06 -0700 | [diff] [blame] | 1037 | |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 1038 | unsigned flags = SVC_EXEC | SVC_ONESHOT | SVC_TEMPORARY; |
Jorge Lucangeli Obes | 24b2913 | 2016-10-27 10:33:03 -0400 | [diff] [blame] | 1039 | CapSet no_capabilities; |
Jorge Lucangeli Obes | 1b3fa3d | 2016-04-21 15:35:09 -0700 | [diff] [blame] | 1040 | unsigned namespace_flags = 0; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1041 | |
| 1042 | std::string seclabel = ""; |
| 1043 | if (command_arg > 2 && args[1] != "-") { |
| 1044 | seclabel = args[1]; |
| 1045 | } |
| 1046 | uid_t uid = 0; |
| 1047 | if (command_arg > 3) { |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1048 | std::string decode_uid_err; |
| 1049 | if (!DecodeUid(args[2], &uid, &decode_uid_err)) { |
| 1050 | LOG(ERROR) << "Unable to find UID for '" << args[2] << "': " << decode_uid_err; |
| 1051 | return nullptr; |
| 1052 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1053 | } |
| 1054 | gid_t gid = 0; |
| 1055 | std::vector<gid_t> supp_gids; |
| 1056 | if (command_arg > 4) { |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1057 | std::string decode_uid_err; |
| 1058 | if (!DecodeUid(args[3], &gid, &decode_uid_err)) { |
| 1059 | LOG(ERROR) << "Unable to find GID for '" << args[3] << "': " << decode_uid_err; |
| 1060 | return nullptr; |
| 1061 | } |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1062 | std::size_t nr_supp_gids = command_arg - 1 /* -- */ - 4 /* exec SECLABEL UID GID */; |
| 1063 | for (size_t i = 0; i < nr_supp_gids; ++i) { |
Tom Cherry | 517e1f1 | 2017-05-04 17:40:33 -0700 | [diff] [blame] | 1064 | gid_t supp_gid; |
| 1065 | if (!DecodeUid(args[4 + i], &supp_gid, &decode_uid_err)) { |
| 1066 | LOG(ERROR) << "Unable to find UID for '" << args[4 + i] << "': " << decode_uid_err; |
| 1067 | return nullptr; |
| 1068 | } |
| 1069 | supp_gids.push_back(supp_gid); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1070 | } |
| 1071 | } |
| 1072 | |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 1073 | auto svc_p = std::make_unique<Service>(name, flags, uid, gid, supp_gids, no_capabilities, |
| 1074 | namespace_flags, seclabel, str_args); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1075 | Service* svc = svc_p.get(); |
Tom Cherry | 9bdddd7 | 2017-03-23 16:07:51 -0700 | [diff] [blame] | 1076 | services_.emplace_back(std::move(svc_p)); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1077 | |
| 1078 | return svc; |
| 1079 | } |
| 1080 | |
| 1081 | Service* ServiceManager::FindServiceByName(const std::string& name) const { |
| 1082 | auto svc = std::find_if(services_.begin(), services_.end(), |
| 1083 | [&name] (const std::unique_ptr<Service>& s) { |
| 1084 | return name == s->name(); |
| 1085 | }); |
| 1086 | if (svc != services_.end()) { |
| 1087 | return svc->get(); |
| 1088 | } |
| 1089 | return nullptr; |
| 1090 | } |
| 1091 | |
| 1092 | Service* ServiceManager::FindServiceByPid(pid_t pid) const { |
| 1093 | auto svc = std::find_if(services_.begin(), services_.end(), |
| 1094 | [&pid] (const std::unique_ptr<Service>& s) { |
| 1095 | return s->pid() == pid; |
| 1096 | }); |
| 1097 | if (svc != services_.end()) { |
| 1098 | return svc->get(); |
| 1099 | } |
| 1100 | return nullptr; |
| 1101 | } |
| 1102 | |
| 1103 | Service* ServiceManager::FindServiceByKeychord(int keychord_id) const { |
| 1104 | auto svc = std::find_if(services_.begin(), services_.end(), |
| 1105 | [&keychord_id] (const std::unique_ptr<Service>& s) { |
| 1106 | return s->keychord_id() == keychord_id; |
| 1107 | }); |
| 1108 | |
| 1109 | if (svc != services_.end()) { |
| 1110 | return svc->get(); |
| 1111 | } |
| 1112 | return nullptr; |
| 1113 | } |
| 1114 | |
Chih-Hung Hsieh | 8f7b9e3 | 2016-07-27 16:25:51 -0700 | [diff] [blame] | 1115 | void ServiceManager::ForEachService(const std::function<void(Service*)>& callback) const { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1116 | for (const auto& s : services_) { |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1117 | callback(s.get()); |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | void ServiceManager::ForEachServiceInClass(const std::string& classname, |
| 1122 | void (*func)(Service* svc)) const { |
| 1123 | for (const auto& s : services_) { |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 1124 | if (s->classnames().find(classname) != s->classnames().end()) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1125 | func(s.get()); |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | void ServiceManager::ForEachServiceWithFlags(unsigned matchflags, |
| 1131 | void (*func)(Service* svc)) const { |
| 1132 | for (const auto& s : services_) { |
| 1133 | if (s->flags() & matchflags) { |
| 1134 | func(s.get()); |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1139 | void ServiceManager::RemoveService(const Service& svc) { |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1140 | auto svc_it = std::find_if(services_.begin(), services_.end(), |
| 1141 | [&svc] (const std::unique_ptr<Service>& s) { |
| 1142 | return svc.name() == s->name(); |
| 1143 | }); |
| 1144 | if (svc_it == services_.end()) { |
| 1145 | return; |
| 1146 | } |
| 1147 | |
| 1148 | services_.erase(svc_it); |
| 1149 | } |
| 1150 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1151 | void ServiceManager::DumpState() const { |
| 1152 | for (const auto& s : services_) { |
| 1153 | s->DumpState(); |
| 1154 | } |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1157 | bool ServiceManager::ReapOneProcess() { |
Tom Cherry | 8d13d80 | 2017-06-29 16:18:08 -0700 | [diff] [blame] | 1158 | siginfo_t siginfo = {}; |
| 1159 | // This returns a zombie pid or informs us that there are no zombies left to be reaped. |
| 1160 | // It does NOT reap the pid; that is done below. |
| 1161 | if (TEMP_FAILURE_RETRY(waitid(P_ALL, 0, &siginfo, WEXITED | WNOHANG | WNOWAIT)) != 0) { |
| 1162 | PLOG(ERROR) << "waitid failed"; |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1163 | return false; |
Tom Cherry | 8d13d80 | 2017-06-29 16:18:08 -0700 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | auto pid = siginfo.si_pid; |
| 1167 | if (pid == 0) return false; |
| 1168 | |
| 1169 | // At this point we know we have a zombie pid, so we use this scopeguard to reap the pid |
| 1170 | // whenever the function returns from this point forward. |
| 1171 | // We do NOT want to reap the zombie earlier as in Service::Reap(), we kill(-pid, ...) and we |
| 1172 | // want the pid to remain valid throughout that (and potentially future) usages. |
| 1173 | auto reaper = make_scope_guard([pid] { TEMP_FAILURE_RETRY(waitpid(pid, nullptr, WNOHANG)); }); |
| 1174 | |
| 1175 | if (PropertyChildReap(pid)) { |
Tom Marshall | 6269690 | 2017-06-09 18:29:23 +0000 | [diff] [blame] | 1176 | return true; |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | Service* svc = FindServiceByPid(pid); |
| 1180 | |
| 1181 | std::string name; |
Keun-young Park | 4de31e1 | 2017-04-27 16:25:06 -0700 | [diff] [blame] | 1182 | std::string wait_string; |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1183 | if (svc) { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 1184 | name = StringPrintf("Service '%s' (pid %d)", svc->name().c_str(), pid); |
Keun-young Park | 4de31e1 | 2017-04-27 16:25:06 -0700 | [diff] [blame] | 1185 | if (svc->flags() & SVC_EXEC) { |
Tom Cherry | ede0d53 | 2017-07-06 14:20:11 -0700 | [diff] [blame] | 1186 | wait_string = StringPrintf(" waiting took %f seconds", |
| 1187 | exec_waiter_->duration().count() / 1000.0f); |
Keun-young Park | 4de31e1 | 2017-04-27 16:25:06 -0700 | [diff] [blame] | 1188 | } |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1189 | } else { |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 1190 | name = StringPrintf("Untracked pid %d", pid); |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1191 | } |
| 1192 | |
Tom Cherry | 8d13d80 | 2017-06-29 16:18:08 -0700 | [diff] [blame] | 1193 | auto status = siginfo.si_status; |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1194 | if (WIFEXITED(status)) { |
Keun-young Park | 4de31e1 | 2017-04-27 16:25:06 -0700 | [diff] [blame] | 1195 | LOG(INFO) << name << " exited with status " << WEXITSTATUS(status) << wait_string; |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1196 | } else if (WIFSIGNALED(status)) { |
Keun-young Park | 4de31e1 | 2017-04-27 16:25:06 -0700 | [diff] [blame] | 1197 | LOG(INFO) << name << " killed by signal " << WTERMSIG(status) << wait_string; |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
| 1200 | if (!svc) { |
| 1201 | return true; |
| 1202 | } |
| 1203 | |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 1204 | svc->Reap(); |
| 1205 | |
| 1206 | if (svc->flags() & SVC_EXEC) { |
Tom Cherry | b27004a | 2017-03-27 16:27:30 -0700 | [diff] [blame] | 1207 | exec_waiter_.reset(); |
| 1208 | } |
| 1209 | if (svc->flags() & SVC_TEMPORARY) { |
Bertrand SIMONNET | b7e03e8 | 2015-12-18 11:39:59 -0800 | [diff] [blame] | 1210 | RemoveService(*svc); |
| 1211 | } |
| 1212 | |
| 1213 | return true; |
| 1214 | } |
| 1215 | |
| 1216 | void ServiceManager::ReapAnyOutstandingChildren() { |
| 1217 | while (ReapOneProcess()) { |
| 1218 | } |
| 1219 | } |
| 1220 | |
Wei Wang | eeab491 | 2017-06-27 22:08:45 -0700 | [diff] [blame] | 1221 | void ServiceManager::ClearExecWait() { |
| 1222 | // Clear EXEC flag if there is one pending |
| 1223 | // And clear the wait flag |
| 1224 | for (const auto& s : services_) { |
| 1225 | s->UnSetExec(); |
| 1226 | } |
| 1227 | exec_waiter_.reset(); |
| 1228 | } |
| 1229 | |
Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 1230 | bool ServiceParser::ParseSection(std::vector<std::string>&& args, const std::string& filename, |
Tom Cherry | 012c573 | 2017-04-18 13:21:54 -0700 | [diff] [blame] | 1231 | int line, std::string* err) { |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1232 | if (args.size() < 3) { |
| 1233 | *err = "services must have a name and a program"; |
| 1234 | return false; |
| 1235 | } |
| 1236 | |
| 1237 | const std::string& name = args[1]; |
| 1238 | if (!IsValidName(name)) { |
| 1239 | *err = StringPrintf("invalid service name '%s'", name.c_str()); |
| 1240 | return false; |
| 1241 | } |
| 1242 | |
Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 1243 | Service* old_service = service_manager_->FindServiceByName(name); |
| 1244 | if (old_service) { |
| 1245 | *err = "ignored duplicate definition of service '" + name + "'"; |
| 1246 | return false; |
| 1247 | } |
| 1248 | |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1249 | std::vector<std::string> str_args(args.begin() + 2, args.end()); |
Wei Wang | 641ff0a | 2017-03-27 10:59:11 -0700 | [diff] [blame] | 1250 | service_ = std::make_unique<Service>(name, str_args); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1251 | return true; |
| 1252 | } |
| 1253 | |
Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 1254 | bool ServiceParser::ParseLineSection(std::vector<std::string>&& args, int line, std::string* err) { |
| 1255 | return service_ ? service_->ParseLine(std::move(args), err) : false; |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | void ServiceParser::EndSection() { |
| 1259 | if (service_) { |
Tom Cherry | 30a6f27 | 2017-04-19 15:31:58 -0700 | [diff] [blame] | 1260 | service_manager_->AddService(std::move(service_)); |
Tom Cherry | b734990 | 2015-08-26 11:43:36 -0700 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | bool ServiceParser::IsValidName(const std::string& name) const { |
Elliott Hughes | b7788fd | 2017-02-28 09:54:36 -0800 | [diff] [blame] | 1265 | // Property names can be any length, but may only contain certain characters. |
| 1266 | // Property values can contain any characters, but may only be a certain length. |
| 1267 | // (The latter restriction is needed because `start` and `stop` work by writing |
| 1268 | // the service name to the "ctl.start" and "ctl.stop" properties.) |
| 1269 | return is_legal_property_name("init.svc." + name) && name.size() <= PROP_VALUE_MAX; |
Tom Cherry | bac3299 | 2015-07-31 12:45:25 -0700 | [diff] [blame] | 1270 | } |
Tom Cherry | 81f5d3e | 2017-06-22 12:53:17 -0700 | [diff] [blame] | 1271 | |
| 1272 | } // namespace init |
| 1273 | } // namespace android |