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