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