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